Feeds: `<comments>` is optional in RSS2, so don't include it when comments aren't present or open. Same for `<wfw:commentRss>` and `<slash:comments>`

Adds unit test.

Props swissspidy, realloc, hakre, sivel.
Fixes #9134.


git-svn-id: https://develop.svn.wordpress.org/trunk@35506 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Scott Taylor 2015-11-04 17:46:41 +00:00
parent 00ee9a62d1
commit ee98fd84bb
3 changed files with 40 additions and 2 deletions

View File

@ -78,10 +78,13 @@ do_action( 'rss_tag_pre', 'atom' );
* @since 2.0.0
*/
do_action( 'atom_entry' );
if ( get_comments_number() || comments_open() ) :
?>
<link rel="replies" type="<?php bloginfo_rss('html_type'); ?>" href="<?php the_permalink_rss() ?>#comments" thr:count="<?php echo get_comments_number()?>"/>
<link rel="replies" type="application/atom+xml" href="<?php echo esc_url( get_post_comments_feed_link(0, 'atom') ); ?>" thr:count="<?php echo get_comments_number()?>"/>
<thr:total><?php echo get_comments_number()?></thr:total>
<?php endif; ?>
</entry>
<?php endwhile ; ?>
</feed>

View File

@ -83,7 +83,9 @@ do_action( 'rss_tag_pre', 'rss2' );
<item>
<title><?php the_title_rss() ?></title>
<link><?php the_permalink_rss() ?></link>
<?php if ( get_comments_number() || comments_open() ) : ?>
<comments><?php comments_link_feed(); ?></comments>
<?php endif; ?>
<pubDate><?php echo mysql2date('D, d M Y H:i:s +0000', get_post_time('Y-m-d H:i:s', true), false); ?></pubDate>
<dc:creator><![CDATA[<?php the_author() ?>]]></dc:creator>
<?php the_category_rss('rss2') ?>
@ -100,8 +102,10 @@ do_action( 'rss_tag_pre', 'rss2' );
<content:encoded><![CDATA[<?php the_excerpt_rss(); ?>]]></content:encoded>
<?php endif; ?>
<?php endif; ?>
<?php if ( get_comments_number() || comments_open() ) : ?>
<wfw:commentRss><?php echo esc_url( get_post_comments_feed_link(null, 'rss2') ); ?></wfw:commentRss>
<slash:comments><?php echo get_comments_number(); ?></slash:comments>
<?php endif; ?>
<?php rss_enclosure(); ?>
<?php
/**

View File

@ -102,8 +102,7 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
$xml = xml_to_array($feed);
// get all the rss -> channel -> item elements
$items = xml_find($xml, 'rss', 'channel', 'item');
$posts = get_posts('numberposts='.$this->post_count);
$items = xml_find( $xml, 'rss', 'channel', 'item' );
// check each of the items against the known post data
foreach ( $items as $key => $item ) {
@ -178,4 +177,36 @@ class Tests_Feed_RSS2 extends WP_UnitTestCase {
$this->assertEquals( html_entity_decode( get_post_comments_feed_link( $post->ID) ), $comment_rss[0]['content'] );
}
}
/**
* @ticket 9134
*/
function test_items_comments_closed() {
add_filter( 'comments_open', '__return_false' );
$this->go_to( '/?feed=rss2' );
$feed = $this->do_rss2();
$xml = xml_to_array($feed);
// get all the rss -> channel -> item elements
$items = xml_find( $xml, 'rss', 'channel', 'item' );
// check each of the items against the known post data
foreach ( $items as $key => $item ) {
// Get post for comparison
$guid = xml_find( $items[$key]['child'], 'guid' );
preg_match( '/\?p=(\d+)/', $guid[0]['content'], $matches );
$post = get_post( $matches[1] );
// comment link
$comments_link = xml_find( $items[ $key ]['child'], 'comments' );
$this->assertEmpty( $comments_link );
// comment rss
$comment_rss = xml_find( $items[ $key ]['child'], 'wfw:commentRss' );
$this->assertEmpty( $comment_rss );
}
remove_filter( 'comments_open', '__return_false' );
}
}