Feeds: Fix the URL returned by get_feed_link() when pretty permalinks are not in use.

Props hauvong, peterwilsoncc, SergeyBiryukov

Fixes #51839


git-svn-id: https://develop.svn.wordpress.org/trunk@50354 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
John Blackbourn
2021-02-16 17:32:58 +00:00
parent 8bd8ead1da
commit 22618cef9a
2 changed files with 68 additions and 1 deletions

View File

@@ -690,7 +690,7 @@ function get_feed_link( $feed = '' ) {
$permalink = $wp_rewrite->get_feed_permastruct();
if ( '' !== $permalink ) {
if ( $permalink ) {
if ( false !== strpos( $feed, 'comments_' ) ) {
$feed = str_replace( 'comments_', '', $feed );
$permalink = $wp_rewrite->get_comment_feed_permastruct();

View File

@@ -0,0 +1,67 @@
<?php
/**
* @group link
* @covers ::get_feed_link
*/
class Tests_Link_GetFeedLink extends WP_UnitTestCase {
/**
* @ticket 51839
* @dataProvider data_plain_permastruct
*
* @param string $expected Expected suffix to home_url().
* @param string $type Feed type to request.
*/
public function tests_plain_permastruct( $expected, $type ) {
$this->set_permalink_structure( '' );
$this->assertSame( home_url( $expected ), get_feed_link( $type ) );
}
public function data_plain_permastruct() {
return array(
array( '?feed=rss2', '' ),
array( '?feed=atom', 'atom' ),
array( '?feed=get-feed-link', 'get-feed-link' ),
array( '?feed=comments-rss2', 'comments_rss2' ),
array( '?feed=comments-atom', 'comments_atom' ),
);
}
/**
* @ticket 51839
* @dataProvider data_pretty_permastruct
*
* @param string $expected Expected suffix to home_url().
* @param string $type Feed type to request.
*/
public function tests_pretty_permastruct( $expected, $type ) {
$this->set_permalink_structure( '/%year%/%monthnum%/%day%/%postname%/' );
$this->assertSame( home_url( $expected ), get_feed_link( $type ) );
}
/**
* @ticket 51839
* @dataProvider data_pretty_permastruct
*
* @param string $expected Expected suffix to home_url().
* @param string $type Feed type to request.
*/
public function tests_pretty_permastruct_with_prefix( $expected, $type ) {
$this->set_permalink_structure( '/archives/%post_id%/%postname%/' );
$this->assertSame( home_url( $expected ), get_feed_link( $type ) );
}
public function data_pretty_permastruct() {
return array(
array( '/feed/', '' ),
array( '/feed/atom/', 'atom' ),
array( '/feed/get-feed-link/', 'get-feed-link' ),
array( '/comments/feed/', 'comments_rss2' ),
array( '/comments/feed/atom/', 'comments_atom' ),
);
}
}