From 22618cef9affc032537930d3f1144759e5d90893 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Tue, 16 Feb 2021 17:32:58 +0000 Subject: [PATCH] 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 --- src/wp-includes/link-template.php | 2 +- tests/phpunit/tests/link/getFeedLink.php | 67 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 tests/phpunit/tests/link/getFeedLink.php diff --git a/src/wp-includes/link-template.php b/src/wp-includes/link-template.php index 85b61e3a5a..9554de3d2f 100644 --- a/src/wp-includes/link-template.php +++ b/src/wp-includes/link-template.php @@ -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(); diff --git a/tests/phpunit/tests/link/getFeedLink.php b/tests/phpunit/tests/link/getFeedLink.php new file mode 100644 index 0000000000..41d6d922d2 --- /dev/null +++ b/tests/phpunit/tests/link/getFeedLink.php @@ -0,0 +1,67 @@ +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' ), + ); + } +}