Make WordPress Core

Ticket #13867: 13867.5.diff

File 13867.5.diff, 7.2 KB (added by audrasjb, 4 years ago)

Feeds: Add a hook to filter RSS2 and ATOM Comments feeds page title

  • src/wp-includes/feed-atom-comments.php

    diff --git a/src/wp-includes/feed-atom-comments.php b/src/wp-includes/feed-atom-comments.php
    index b43f8d4eb1..b9d4f533bb 100644
    a b do_action( 'rss_tag_pre', 'atom-comments' ); 
    2727                do_action( 'atom_comments_ns' );
    2828        ?>
    2929>
    30         <title type="text">
    3130        <?php
     31        $feed_title = '';
    3232        if ( is_singular() ) {
    3333                /* translators: Comments feed title. %s: Post title. */
    34                 printf( ent2ncr( __( 'Comments on %s' ) ), get_the_title_rss() );
     34                $feed_title = sprintf( ent2ncr( __( 'Comments on: %s' ) ), get_the_title_rss() );
    3535        } elseif ( is_search() ) {
    3636                /* translators: Comments feed title. 1: Site title, 2: Search query. */
    37                 printf( ent2ncr( __( 'Comments for %1$s searching on %2$s' ) ), get_bloginfo_rss( 'name' ), get_search_query() );
     37                $feed_title = sprintf( ent2ncr( __( 'Comments for %1$s searching on %2$s' ) ), get_bloginfo_rss( 'name' ), get_search_query() );
    3838        } else {
    3939                /* translators: Comments feed title. %s: Site title. */
    40                 printf( ent2ncr( __( 'Comments for %s' ) ), get_wp_title_rss() );
     40                $feed_title = sprintf( ent2ncr( __( 'Comments for %s' ) ), get_bloginfo_rss( 'name' ) . get_wp_title_rss() );
    4141        }
     42
     43        /**
     44         * Filters the page title for the comments feed.
     45         *
     46         * @since 5.9.0
     47         *
     48         * @param string $feed_title Comments feed page title.
     49         */
     50        $feed_title = apply_filters( 'comments_feed_title', $feed_title, get_the_title_rss() );
    4251        ?>
    43         </title>
     52
     53        <title type="text"><?php echo $feed_title; ?></title>
    4454        <subtitle type="text"><?php bloginfo_rss( 'description' ); ?></subtitle>
    4555
    4656        <updated><?php echo get_feed_build_date( 'Y-m-d\TH:i:s\Z' ); ?></updated>
  • src/wp-includes/feed-rss2-comments.php

    diff --git a/src/wp-includes/feed-rss2-comments.php b/src/wp-includes/feed-rss2-comments.php
    index bf69a4d3ae..d8658eeef9 100644
    a b do_action( 'rss_tag_pre', 'rss2-comments' ); 
    3232        ?>
    3333>
    3434<channel>
    35         <title>
     35
    3636        <?php
     37        $feed_title = '';
    3738        if ( is_singular() ) {
    3839                /* translators: Comments feed title. %s: Post title. */
    39                 printf( ent2ncr( __( 'Comments on: %s' ) ), get_the_title_rss() );
     40                $feed_title = sprintf( ent2ncr( __( 'Comments on: %s' ) ), get_the_title_rss() );
    4041        } elseif ( is_search() ) {
    4142                /* translators: Comments feed title. 1: Site title, 2: Search query. */
    42                 printf( ent2ncr( __( 'Comments for %1$s searching on %2$s' ) ), get_bloginfo_rss( 'name' ), get_search_query() );
     43                $feed_title = sprintf( ent2ncr( __( 'Comments for %1$s searching on %2$s' ) ), get_bloginfo_rss( 'name' ), get_search_query() );
    4344        } else {
    4445                /* translators: Comments feed title. %s: Site title. */
    45                 printf( ent2ncr( __( 'Comments for %s' ) ), get_wp_title_rss() );
     46                $feed_title = sprintf( ent2ncr( __( 'Comments for %s' ) ), get_bloginfo_rss( 'name' ) . get_wp_title_rss() );
    4647        }
     48
     49        /**
     50         * Filters the page title for the comments feed.
     51         *
     52         * @since 5.9.0
     53         *
     54         * @param string $feed_title Comments feed page title.
     55         */
     56        $feed_title = apply_filters( 'comments_feed_title', $feed_title, get_the_title_rss() );
    4757        ?>
    48         </title>
     58
     59        <title><?php echo $feed_title; ?></title>
     60        <!-- JBA -->
    4961        <atom:link href="<?php self_link(); ?>" rel="self" type="application/rss+xml" />
    5062        <link><?php ( is_single() ) ? the_permalink_rss() : bloginfo_rss( 'url' ); ?></link>
    5163        <description><?php bloginfo_rss( 'description' ); ?></description>
  • tests/phpunit/tests/feed/atom.php

    diff --git a/tests/phpunit/tests/feed/atom.php b/tests/phpunit/tests/feed/atom.php
    index 3b2c827749..41f10b2543 100644
    a b class Tests_Feed_Atom extends WP_UnitTestCase { 
    1212        public static $user_id;
    1313        public static $posts;
    1414        public static $category;
     15        public static $post_id;
     16        public static $comment_ids = array();
    1517
    1618        /**
    1719         * Setup a new user and attribute some posts.
    class Tests_Feed_Atom extends WP_UnitTestCase { 
    5153                        wp_set_object_terms( $post, self::$category->slug, 'category' );
    5254                }
    5355
     56                self::$post_id = $factory->post->create();
     57
     58                // Create a comment
     59                self::$comment_ids[] = $factory->comment->create(
     60                        array(
     61                                'comment_author'   => 1,
     62                                'comment_date'     => '2014-05-06 12:00:00',
     63                                'comment_date_gmt' => '2014-05-06 07:00:00',
     64                                'comment_post_ID'  => self::$post_id,
     65                        )
     66                );
    5467        }
    5568
    5669        /**
    class Tests_Feed_Atom extends WP_UnitTestCase { 
    126139                $this->assertSame( home_url( '/?feed=atom' ), $link[1]['attributes']['href'] );
    127140        }
    128141
     142        /**
     143         * Test the <feed> element to make sure its present and populated
     144         * with the modified version of the title.
     145         *
     146         * @ticket 13867
     147         */
     148        function test_feed_title() {
     149                add_filter( 'comments_feed_title', array( $this, 'apply_comments_feed_title' ), 10, 2 );
     150                $this->go_to( '?feed=comments-atom' );
     151                $feed = $this->do_rss2_comments();
     152                $xml  = xml_to_array( $feed );
     153
     154                // Get the <feed> child element of <xml>.
     155                $rss = xml_find( $xml, 'feed' );
     156                error_log( print_r( $rss, true) );
     157                $this->assertEquals( 'Filtered Title', $rss[0]['child'][0]['child'][0]['content'] );
     158        }
     159
     160        /**
     161         * Apply the comments feed title filter.
     162         *
     163         * @ticket 13867
     164         */
     165        function apply_comments_feed_title( $item_title, $the_title_rss ) {
     166                $item_title = 'Filtered Title';
     167                return $item_title;
     168        }
     169
    129170        /**
    130171         * Validate <entry> child elements.
    131172         */
  • tests/phpunit/tests/feed/rss2.php

    diff --git a/tests/phpunit/tests/feed/rss2.php b/tests/phpunit/tests/feed/rss2.php
    index 50138cf255..7640e00f52 100644
    a b class Tests_Feed_RSS2 extends WP_UnitTestCase { 
    1313        public static $posts;
    1414        public static $category;
    1515        public static $post_date;
     16        public static $post_id;
     17        public static $comment_ids = array();
    1618
    1719        /**
    1820         * Setup a new user and attribute some posts.
    class Tests_Feed_RSS2 extends WP_UnitTestCase { 
    5860                foreach ( self::$posts as $post ) {
    5961                        wp_set_object_terms( $post, self::$category->slug, 'category' );
    6062                }
     63
     64                self::$post_id = $factory->post->create();
     65
     66                // Create a comment
     67                self::$comment_ids[] = $factory->comment->create(
     68                        array(
     69                                'comment_author'   => 1,
     70                                'comment_date'     => '2014-05-06 12:00:00',
     71                                'comment_date_gmt' => '2014-05-06 07:00:00',
     72                                'comment_post_ID'  => self::$post_id,
     73                        )
     74                );
    6175        }
    6276
    6377        /**
    class Tests_Feed_RSS2 extends WP_UnitTestCase { 
    117131                $this->assertCount( 1, $rss[0]['child'] );
    118132        }
    119133
     134        /**
     135         * Test the <rss> element to make sure its present and populated
     136         * with the modified version of the title.
     137         *
     138         * @ticket 13867
     139         */
     140        function test_feed_title() {
     141                add_filter( 'comments_feed_title', array( $this, 'apply_comments_feed_title' ), 10, 2 );
     142                $this->go_to( '/comments/feed/' );
     143                $feed = $this->do_rss2_comments();
     144                $xml  = xml_to_array( $feed );
     145
     146                // Get the <rss> child element of <xml>.
     147                $rss = xml_find( $xml, 'rss' );
     148                error_log( print_r( $rss, true) );
     149                $this->assertEquals( 'Filtered Title', $rss[0]['child'][0]['child'][0]['content'] );
     150        }
     151
     152        /**
     153         * Apply the comments feed title filter.
     154         *
     155         * @ticket 13867
     156         */
     157        function apply_comments_feed_title( $item_title, $the_title_rss ) {
     158                $item_title = 'Filtered Title';
     159                return $item_title;
     160        }
     161
    120162        /**
    121163         * [test_channel_element description]
    122164         *