Make WordPress Core

Changeset 54161


Ignore:
Timestamp:
09/14/2022 05:12:20 PM (19 months ago)
Author:
SergeyBiryukov
Message:

Feeds: Add a set of fine-grained filters to disable the different types of feed links separately.

The previously available set of filters in the feed_links() function to enable or disable display of various feed links was quite limited:

  • feed_links_show_posts_feed to control the main feed
  • feed_links_show_comments_feed to control both the global comments feed and the comment feed for singular posts.

In order to disable the other feeds (post type archive, category, tag, custom taxonomy, author archive, search results), one would have to unhook feed_links_extra() from wp_head, but that would completely remove all of those feeds, as well as the single post comments feed.

To allow for more flexibility, this commit introduces a full set of filters in the feed_links_extra() function to control each one of the feeds independently, including a way to enable/disable the single post comments feed when the global comments feed is disabled/enabled:

  • feed_links_extra_show_post_comments_feed
  • feed_links_extra_show_post_type_archive_feed
  • feed_links_extra_show_category_feed
  • feed_links_extra_show_tag_feed
  • feed_links_extra_show_tax_feed
  • feed_links_extra_show_author_feed
  • feed_links_extra_show_search_feed

All of them default to true, except for feed_links_extra_show_post_comments_feed which defaults to the result of feed_links_show_comments_feed to ensure backward compatibility.

Follow-up to [33838], [33839], [53125].

Props lopo, mukesh27, audrasjb, SergeyBiryukov.
Fixes #55904.

Location:
trunk
Files:
2 edited

Legend:

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

    r54137 r54161  
    31073107     */
    31083108    if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
    3109         echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link() ) . "\" />\n";
     3109        printf(
     3110            '<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
     3111            feed_content_type(),
     3112            esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
     3113            esc_url( get_feed_link() )
     3114        );
    31103115    }
    31113116
     
    31183123     */
    31193124    if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
    3120         echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ) . '" href="' . esc_url( get_feed_link( 'comments_' . get_default_feed() ) ) . "\" />\n";
     3125        printf(
     3126            '<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
     3127            feed_content_type(),
     3128            esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
     3129            esc_url( get_feed_link( 'comments_' . get_default_feed() ) )
     3130        );
    31213131    }
    31223132}
     
    31583168        $show_comments_feed = apply_filters( 'feed_links_show_comments_feed', true );
    31593169
    3160         if ( $show_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
    3161             $title     = sprintf( $args['singletitle'], get_bloginfo( 'name' ), $args['separator'], the_title_attribute( array( 'echo' => false ) ) );
     3170        /**
     3171         * Filters whether to display the post comments feed link.
     3172         *
     3173         * This filter allows to enable or disable the feed link for a singular post
     3174         * in a way that is independent of {@see 'feed_links_show_comments_feed'}
     3175         * (which controls the global comments feed). The result of that filter
     3176         * is accepted as a parameter.
     3177         *
     3178         * @since 6.1.0
     3179         *
     3180         * @param bool $show_comments_feed Whether to display the post comments feed link. Defaults to
     3181         *                                 the {@see 'feed_links_show_comments_feed'} filter result.
     3182         */
     3183        $show_post_comments_feed = apply_filters( 'feed_links_extra_show_post_comments_feed', $show_comments_feed );
     3184
     3185        if ( $show_post_comments_feed && ( comments_open() || pings_open() || $post->comment_count > 0 ) ) {
     3186            $title = sprintf(
     3187                $args['singletitle'],
     3188                get_bloginfo( 'name' ),
     3189                $args['separator'],
     3190                the_title_attribute( array( 'echo' => false ) )
     3191            );
     3192
    31623193            $feed_link = get_post_comments_feed_link( $post->ID );
    31633194
     
    31673198        }
    31683199    } elseif ( is_post_type_archive() ) {
    3169         $post_type = get_query_var( 'post_type' );
    3170         if ( is_array( $post_type ) ) {
    3171             $post_type = reset( $post_type );
    3172         }
    3173 
    3174         $post_type_obj = get_post_type_object( $post_type );
    3175         $title         = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name );
    3176         $href          = get_post_type_archive_feed_link( $post_type_obj->name );
     3200        /**
     3201         * Filters whether to display the post type archive feed link.
     3202         *
     3203         * @since 6.1.0
     3204         *
     3205         * @param bool $show Whether to display the post type archive feed link. Default true.
     3206         */
     3207        $show_post_type_archive_feed = apply_filters( 'feed_links_extra_show_post_type_archive_feed', true );
     3208
     3209        if ( $show_post_type_archive_feed ) {
     3210            $post_type = get_query_var( 'post_type' );
     3211
     3212            if ( is_array( $post_type ) ) {
     3213                $post_type = reset( $post_type );
     3214            }
     3215
     3216            $post_type_obj = get_post_type_object( $post_type );
     3217
     3218            $title = sprintf(
     3219                $args['posttypetitle'],
     3220                get_bloginfo( 'name' ),
     3221                $args['separator'],
     3222                $post_type_obj->labels->name
     3223            );
     3224
     3225            $href = get_post_type_archive_feed_link( $post_type_obj->name );
     3226        }
    31773227    } elseif ( is_category() ) {
    3178         $term = get_queried_object();
    3179 
    3180         if ( $term ) {
    3181             $title = sprintf( $args['cattitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
    3182             $href  = get_category_feed_link( $term->term_id );
     3228        /**
     3229         * Filters whether to display the category feed link.
     3230         *
     3231         * @since 6.1.0
     3232         *
     3233         * @param bool $show Whether to display the category feed link. Default true.
     3234         */
     3235        $show_category_feed = apply_filters( 'feed_links_extra_show_category_feed', true );
     3236
     3237        if ( $show_category_feed ) {
     3238            $term = get_queried_object();
     3239
     3240            if ( $term ) {
     3241                $title = sprintf(
     3242                    $args['cattitle'],
     3243                    get_bloginfo( 'name' ),
     3244                    $args['separator'],
     3245                    $term->name
     3246                );
     3247
     3248                $href = get_category_feed_link( $term->term_id );
     3249            }
    31833250        }
    31843251    } elseif ( is_tag() ) {
    3185         $term = get_queried_object();
    3186 
    3187         if ( $term ) {
    3188             $title = sprintf( $args['tagtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
    3189             $href  = get_tag_feed_link( $term->term_id );
     3252        /**
     3253         * Filters whether to display the tag feed link.
     3254         *
     3255         * @since 6.1.0
     3256         *
     3257         * @param bool $show Whether to display the tag feed link. Default true.
     3258         */
     3259        $show_tag_feed = apply_filters( 'feed_links_extra_show_tag_feed', true );
     3260
     3261        if ( $show_tag_feed ) {
     3262            $term = get_queried_object();
     3263
     3264            if ( $term ) {
     3265                $title = sprintf(
     3266                    $args['tagtitle'],
     3267                    get_bloginfo( 'name' ),
     3268                    $args['separator'],
     3269                    $term->name
     3270                );
     3271
     3272                $href = get_tag_feed_link( $term->term_id );
     3273            }
    31903274        }
    31913275    } elseif ( is_tax() ) {
    3192         $term = get_queried_object();
    3193 
    3194         if ( $term ) {
    3195             $tax   = get_taxonomy( $term->taxonomy );
    3196             $title = sprintf( $args['taxtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name, $tax->labels->singular_name );
    3197             $href  = get_term_feed_link( $term->term_id, $term->taxonomy );
     3276        /**
     3277         * Filters whether to display the custom taxonomy feed link.
     3278         *
     3279         * @since 6.1.0
     3280         *
     3281         * @param bool $show Whether to display the custom taxonomy feed link. Default true.
     3282         */
     3283        $show_tax_feed = apply_filters( 'feed_links_extra_show_tax_feed', true );
     3284
     3285        if ( $show_tax_feed ) {
     3286            $term = get_queried_object();
     3287
     3288            if ( $term ) {
     3289                $tax = get_taxonomy( $term->taxonomy );
     3290
     3291                $title = sprintf(
     3292                    $args['taxtitle'],
     3293                    get_bloginfo( 'name' ),
     3294                    $args['separator'],
     3295                    $term->name,
     3296                    $tax->labels->singular_name
     3297                );
     3298
     3299                $href = get_term_feed_link( $term->term_id, $term->taxonomy );
     3300            }
    31983301        }
    31993302    } elseif ( is_author() ) {
    3200         $author_id = (int) get_query_var( 'author' );
    3201 
    3202         $title = sprintf( $args['authortitle'], get_bloginfo( 'name' ), $args['separator'], get_the_author_meta( 'display_name', $author_id ) );
    3203         $href  = get_author_feed_link( $author_id );
     3303        /**
     3304         * Filters whether to display the author feed link.
     3305         *
     3306         * @since 6.1.0
     3307         *
     3308         * @param bool $show Whether to display the author feed link. Default true.
     3309         */
     3310        $show_author_feed = apply_filters( 'feed_links_extra_show_author_feed', true );
     3311
     3312        if ( $show_author_feed ) {
     3313            $author_id = (int) get_query_var( 'author' );
     3314
     3315            $title = sprintf(
     3316                $args['authortitle'],
     3317                get_bloginfo( 'name' ),
     3318                $args['separator'],
     3319                get_the_author_meta( 'display_name', $author_id )
     3320            );
     3321
     3322            $href = get_author_feed_link( $author_id );
     3323        }
    32043324    } elseif ( is_search() ) {
    3205         $title = sprintf( $args['searchtitle'], get_bloginfo( 'name' ), $args['separator'], get_search_query( false ) );
    3206         $href  = get_search_feed_link();
     3325        /**
     3326         * Filters whether to display the search results feed link.
     3327         *
     3328         * @since 6.1.0
     3329         *
     3330         * @param bool $show Whether to display the search results feed link. Default true.
     3331         */
     3332        $show_search_feed = apply_filters( 'feed_links_extra_show_search_feed', true );
     3333
     3334        if ( $show_search_feed ) {
     3335            $title = sprintf(
     3336                $args['searchtitle'],
     3337                get_bloginfo( 'name' ),
     3338                $args['separator'],
     3339                get_search_query( false )
     3340            );
     3341
     3342            $href = get_search_feed_link();
     3343        }
    32073344    }
    32083345
    32093346    if ( isset( $title ) && isset( $href ) ) {
    3210         echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
     3347        printf(
     3348            '<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
     3349            feed_content_type(),
     3350            esc_attr( $title ),
     3351            esc_url( $href )
     3352        );
    32113353    }
    32123354}
     
    32193361 */
    32203362function rsd_link() {
    3221     echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) . '" />' . "\n";
     3363    printf(
     3364        '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="%s" />' . "\n",
     3365        esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) )
     3366    );
    32223367}
    32233368
     
    32293374 */
    32303375function wlwmanifest_link() {
    3231     echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="' . includes_url( 'wlwmanifest.xml' ) . '" /> ' . "\n";
     3376    printf(
     3377        '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="%s" />' . "\n",
     3378        includes_url( 'wlwmanifest.xml' )
     3379    );
    32323380}
    32333381
  • trunk/tests/phpunit/tests/general/feedLinksExtra.php

    r53125 r54161  
    580580        $this->assertNotEmpty( get_echo( 'feed_links_extra' ) );
    581581    }
     582
     583    /**
     584     * @dataProvider data_feed_links_extra_should_output_nothing_when_filters_return_false
     585     *
     586     * @ticket 55904
     587     *
     588     * @param string $type   The name of the test class property containing the object ID.
     589     * @param string $filter The name of the filter to set to false.
     590     */
     591    public function test_feed_links_extra_should_output_nothing_when_filters_return_false( $type, $filter ) {
     592        $permalink = $this->helper_get_the_permalink( $type );
     593        $this->go_to( $permalink );
     594
     595        add_filter( $filter, '__return_false' );
     596
     597        $this->assertEmpty( get_echo( 'feed_links_extra' ) );
     598    }
     599
     600    /**
     601     * Data provider.
     602     *
     603     * @return array
     604     */
     605    public function data_feed_links_extra_should_output_nothing_when_filters_return_false() {
     606        return array(
     607            'a post with a comment' => array(
     608                'type'   => 'post_with_comment',
     609                'filter' => 'feed_links_extra_show_post_comments_feed',
     610            ),
     611            'a custom post type'    => array(
     612                'type'   => 'post_type',
     613                'filter' => 'feed_links_extra_show_post_type_archive_feed',
     614            ),
     615            'a category'            => array(
     616                'type'   => 'category',
     617                'filter' => 'feed_links_extra_show_category_feed',
     618            ),
     619            'a tag'                 => array(
     620                'type'   => 'tag',
     621                'filter' => 'feed_links_extra_show_tag_feed',
     622            ),
     623            'a taxonomy'            => array(
     624                'type'   => 'tax',
     625                'filter' => 'feed_links_extra_show_tax_feed',
     626            ),
     627            'an author'             => array(
     628                'type'   => 'author',
     629                'filter' => 'feed_links_extra_show_author_feed',
     630            ),
     631            'search results'        => array(
     632                'type'   => 'search',
     633                'filter' => 'feed_links_extra_show_search_feed',
     634            ),
     635        );
     636    }
    582637}
Note: See TracChangeset for help on using the changeset viewer.