diff --git a/src/wp-includes/general-template.php b/src/wp-includes/general-template.php index edccff2807..f09c54ee96 100644 --- a/src/wp-includes/general-template.php +++ b/src/wp-includes/general-template.php @@ -3153,9 +3153,16 @@ function feed_links_extra( $args = array() ) { $id = 0; $post = get_post( $id ); - if ( comments_open() || pings_open() || $post->comment_count > 0 ) { - $title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) ); - $href = get_post_comments_feed_link( $post->ID ); + /** This filter is documented in wp-includes/general-template.php */ + $show_comments_feed = apply_filters( 'feed_links_show_comments_feed', true ); + + if ( $show_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) { + $title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) ); + $feed_link = get_post_comments_feed_link( $post->ID ); + + if ( $feed_link ) { + $href = $feed_link; + } } } elseif ( is_post_type_archive() ) { $post_type = get_query_var( 'post_type' ); diff --git a/tests/phpunit/tests/general/feedLinksExtra.php b/tests/phpunit/tests/general/feedLinksExtra.php index c885cac88c..bf7e8de43e 100644 --- a/tests/phpunit/tests/general/feedLinksExtra.php +++ b/tests/phpunit/tests/general/feedLinksExtra.php @@ -510,4 +510,73 @@ class Tests_General_FeedLinksExtra extends WP_UnitTestCase { $expected .= ' href="http://example.org/?feed=foo&p=' . self::$post_with_comment_id . '" />' . "\n"; $this->assertSame( $expected, get_echo( 'feed_links_extra' ) ); } + + /** + * @ticket 54703 + */ + public function test_feed_links_extra_should_output_nothing_when_show_comments_feed_filter_returns_false() { + add_filter( 'feed_links_show_comments_feed', '__return_false' ); + + $this->go_to( get_the_permalink( self::$post_with_comment_id ) ); + $this->assertEmpty( get_echo( 'feed_links_extra' ) ); + } + + /** + * @dataProvider data_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy + * + * @ticket 54703 + * + * @param string $callback The callback to use for the 'post_comments_feed_link' filter. + */ + public function test_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy( $callback ) { + add_filter( 'post_comments_feed_link', $callback ); + + $this->go_to( get_the_permalink( self::$post_with_comment_id ) ); + $this->assertEmpty( get_echo( 'feed_links_extra' ) ); + } + + /** + * Data provider. + * + * @return array + */ + public function data_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy() { + return array( + 'empty string' => array( 'callback' => '__return_empty_string' ), + 'empty array' => array( 'callback' => '__return_empty_array' ), + 'zero int' => array( 'callback' => '__return_zero' ), + 'zero float' => array( 'callback' => array( $this, 'cb_return_zero_float' ) ), + 'zero string' => array( 'callback' => array( $this, 'cb_return_zero_string' ) ), + 'null' => array( 'callback' => '__return_null' ), + 'false' => array( 'callback' => '__return_false' ), + ); + } + + /** + * Callback that returns 0.0. + * + * @return float 0.0. + */ + public function cb_return_zero_float() { + return 0.0; + } + + /** + * Callback that returns '0'. + * + * @return string '0'. + */ + public function cb_return_zero_string() { + return '0'; + } + + /** + * @ticket 54703 + */ + public function test_feed_links_extra_should_output_the_comments_feed_link_when_show_comments_feed_filter_returns_true() { + add_filter( 'feed_links_show_comments_feed', '__return_true' ); + + $this->go_to( get_the_permalink( self::$post_with_comment_id ) ); + $this->assertNotEmpty( get_echo( 'feed_links_extra' ) ); + } }