Make WordPress Core

Changeset 53125


Ignore:
Timestamp:
04/11/2022 05:01:04 AM (2 years ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/general-template.php

    r53087 r53125  
    31543154        $post = get_post( $id );
    31553155
    3156         if ( comments_open() || pings_open() || $post->comment_count > 0 ) {
    3157             $title = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
    3158             $href  = get_post_comments_feed_link( $post->ID );
     3156        /** This filter is documented in wp-includes/general-template.php */
     3157        $show_comments_feed = apply_filters( 'feed_links_show_comments_feed', true );
     3158
     3159        if ( $show_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
     3160            $title     = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
     3161            $feed_link = get_post_comments_feed_link( $post->ID );
     3162
     3163            if ( $feed_link ) {
     3164                $href = $feed_link;
     3165            }
    31593166        }
    31603167    } elseif ( is_post_type_archive() ) {
  • trunk/tests/phpunit/tests/general/feedLinksExtra.php

    r53033 r53125  
    511511        $this->assertSame( $expected, get_echo( 'feed_links_extra' ) );
    512512    }
     513
     514    /**
     515     * @ticket 54703
     516     */
     517    public function test_feed_links_extra_should_output_nothing_when_show_comments_feed_filter_returns_false() {
     518        add_filter( 'feed_links_show_comments_feed', '__return_false' );
     519
     520        $this->go_to( get_the_permalink( self::$post_with_comment_id ) );
     521        $this->assertEmpty( get_echo( 'feed_links_extra' ) );
     522    }
     523
     524    /**
     525     * @dataProvider data_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy
     526     *
     527     * @ticket 54703
     528     *
     529     * @param string $callback The callback to use for the 'post_comments_feed_link' filter.
     530     */
     531    public function test_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy( $callback ) {
     532        add_filter( 'post_comments_feed_link', $callback );
     533
     534        $this->go_to( get_the_permalink( self::$post_with_comment_id ) );
     535        $this->assertEmpty( get_echo( 'feed_links_extra' ) );
     536    }
     537
     538    /**
     539     * Data provider.
     540     *
     541     * @return array
     542     */
     543    public function data_feed_links_extra_should_output_nothing_when_post_comments_feed_link_is_falsy() {
     544        return array(
     545            'empty string' => array( 'callback' => '__return_empty_string' ),
     546            'empty array'  => array( 'callback' => '__return_empty_array' ),
     547            'zero int'     => array( 'callback' => '__return_zero' ),
     548            'zero float'   => array( 'callback' => array( $this, 'cb_return_zero_float' ) ),
     549            'zero string'  => array( 'callback' => array( $this, 'cb_return_zero_string' ) ),
     550            'null'         => array( 'callback' => '__return_null' ),
     551            'false'        => array( 'callback' => '__return_false' ),
     552        );
     553    }
     554
     555    /**
     556     * Callback that returns 0.0.
     557     *
     558     * @return float 0.0.
     559     */
     560    public function cb_return_zero_float() {
     561        return 0.0;
     562    }
     563
     564    /**
     565     * Callback that returns '0'.
     566     *
     567     * @return string '0'.
     568     */
     569    public function cb_return_zero_string() {
     570        return '0';
     571    }
     572
     573    /**
     574     * @ticket 54703
     575     */
     576    public function test_feed_links_extra_should_output_the_comments_feed_link_when_show_comments_feed_filter_returns_true() {
     577        add_filter( 'feed_links_show_comments_feed', '__return_true' );
     578
     579        $this->go_to( get_the_permalink( self::$post_with_comment_id ) );
     580        $this->assertNotEmpty( get_echo( 'feed_links_extra' ) );
     581    }
    513582}
Note: See TracChangeset for help on using the changeset viewer.