Skip to content

Canonical redirects disallow tag named comments. Refresh for 39535.diff #8981

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/wp-includes/canonical.php
Original file line number Diff line number Diff line change
Expand Up @@ -445,10 +445,15 @@
) {
// Strip off any existing paging.
$redirect['path'] = preg_replace( "#/$wp_rewrite->pagination_base/?[0-9]+?(/+)?$#", '/', $redirect['path'] );
// Strip off feed endings.
$redirect['path'] = preg_replace( '#/(comments/?)?(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] );
// Strip off any existing comment paging.
$redirect['path'] = preg_replace( "#/{$wp_rewrite->comments_pagination_base}-[0-9]+?(/+)?$#", '/', $redirect['path'] );
// Only strip comments/ from comment feed on site
if ( $redirect['path'] == preg_replace( '#(https?:)?//[^/]+#', '', get_feed_link( 'comments_rss2' ) ) ) {

Check failure on line 451 in src/wp-includes/canonical.php

View workflow job for this annotation

GitHub Actions / PHP coding standards / Run coding standards checks

Use Yoda Condition checks, you must.
$redirect['path'] = preg_replace( '#/(comments/)(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] );
} else {
// Strip off feed endings
$redirect['path'] = preg_replace( '#/(feed|rss2?|rdf|atom)(/+|$)#', '/', $redirect['path'] );
}
}

$addl_path = '';
Expand Down
88 changes: 88 additions & 0 deletions tests/phpunit/tests/canonical/feeds.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php

/**
* @group canonical
*/
class Tests_Canonical_Feeds extends WP_Canonical_UnitTestCase {
public $structure = '/%postname%/';

public static $posts = array();

public static function wpSetUpBeforeClass( $factory ) {

self::$posts[0] = $factory->post->create( array( 'post_name' => 'post0' ) );

$c1 = self::factory()->tag->create_and_get(
array(
'name' => 'Comments',
'slug' => 'comments',
)
);

$c2 = self::factory()->tag->create_and_get(
array(
'name' => 'Test Tag',
'slug' => 'test',
)
);

wp_set_object_terms( self::$posts[0], array( $c1->slug, $c2->slug ), 'post_tag' );
}

/**
* @ticket 39535
*
* @dataProvider data_canonical_feed
*/
public function test_canonical_feed( $test_url, $expected, $ticket = 0, $expected_doing_it_wrong = array() ) {
global $wp;
if ( $ticket ) {
$this->knownWPBug( $ticket );
}

$this->go_to( $test_url );

$query_vars = array_diff( $wp->query_vars, $wp->extra_query_vars );

$this->assertEquals( $expected, $query_vars );
}

public function data_canonical_feed() {
/* Data format:
* [0]: Test URL.
* [1]: array( expected query vars to be set )
* [2]: (optional) The ticket the test refers to, Can be skipped if unknown.
*/

return array(
// posts feed for tag 'comments'
array(
'/tag/comments/feed/',
array(
'tag' => 'comments',
'feed' => 'feed',
),
39535,
),

// posts feed for tag 'test'
array(
'/tag/test/feed/',
array(
'tag' => 'test',
'feed' => 'feed',
),
),

// comments feed entire site
array(
'/comments/feed/',
array(
'withcomments' => '1',
'feed' => 'feed',
),
),

);
}
}
Loading