Feeds: Remove comment feed HTML headers when empty.

Remove the `link[rel=alternate]` element from the HTML header when the comment feeds are disabled. Previously the HTML element was output with an empty `href` attribute.

The element is removed if `get_post_comments_feed_link()` returns an empty string or the feed is disabled via the `feed_links_show_comments_feed` filter.

Props barryceelen, audrasjb, costdev, rachelbaker, Boniu91.
Fixes #54703.



git-svn-id: https://develop.svn.wordpress.org/trunk@53125 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2022-04-11 05:01:04 +00:00
parent 63d1383a59
commit 3c804bbead
2 changed files with 79 additions and 3 deletions

View File

@ -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' );

View File

@ -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' ) );
}
}