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.
Built from https://develop.svn.wordpress.org/trunk@54161


git-svn-id: http://core.svn.wordpress.org/trunk@53720 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2022-09-14 17:14:10 +00:00
parent d5c8952218
commit 2c79eaf031
2 changed files with 181 additions and 33 deletions

View File

@ -3106,7 +3106,12 @@ function feed_links( $args = array() ) {
* @param bool $show Whether to display the posts feed link. Default true. * @param bool $show Whether to display the posts feed link. Default true.
*/ */
if ( apply_filters( 'feed_links_show_posts_feed', true ) ) { if ( apply_filters( 'feed_links_show_posts_feed', true ) ) {
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"; printf(
'<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
feed_content_type(),
esc_attr( sprintf( $args['feedtitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link() )
);
} }
/** /**
@ -3117,7 +3122,12 @@ function feed_links( $args = array() ) {
* @param bool $show Whether to display the comments feed link. Default true. * @param bool $show Whether to display the comments feed link. Default true.
*/ */
if ( apply_filters( 'feed_links_show_comments_feed', true ) ) { if ( apply_filters( 'feed_links_show_comments_feed', true ) ) {
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"; printf(
'<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
feed_content_type(),
esc_attr( sprintf( $args['comstitle'], get_bloginfo( 'name' ), $args['separator'] ) ),
esc_url( get_feed_link( 'comments_' . get_default_feed() ) )
);
} }
} }
@ -3157,8 +3167,29 @@ function feed_links_extra( $args = array() ) {
/** This filter is documented in wp-includes/general-template.php */ /** This filter is documented in wp-includes/general-template.php */
$show_comments_feed = apply_filters( 'feed_links_show_comments_feed', true ); $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 ) ) ); * Filters whether to display the post comments feed link.
*
* This filter allows to enable or disable the feed link for a singular post
* in a way that is independent of {@see 'feed_links_show_comments_feed'}
* (which controls the global comments feed). The result of that filter
* is accepted as a parameter.
*
* @since 6.1.0
*
* @param bool $show_comments_feed Whether to display the post comments feed link. Defaults to
* the {@see 'feed_links_show_comments_feed'} filter result.
*/
$show_post_comments_feed = apply_filters( 'feed_links_extra_show_post_comments_feed', $show_comments_feed );
if ( $show_post_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 ); $feed_link = get_post_comments_feed_link( $post->ID );
if ( $feed_link ) { if ( $feed_link ) {
@ -3166,48 +3197,159 @@ function feed_links_extra( $args = array() ) {
} }
} }
} elseif ( is_post_type_archive() ) { } elseif ( is_post_type_archive() ) {
$post_type = get_query_var( 'post_type' ); /**
if ( is_array( $post_type ) ) { * Filters whether to display the post type archive feed link.
$post_type = reset( $post_type ); *
* @since 6.1.0
*
* @param bool $show Whether to display the post type archive feed link. Default true.
*/
$show_post_type_archive_feed = apply_filters( 'feed_links_extra_show_post_type_archive_feed', true );
if ( $show_post_type_archive_feed ) {
$post_type = get_query_var( 'post_type' );
if ( is_array( $post_type ) ) {
$post_type = reset( $post_type );
}
$post_type_obj = get_post_type_object( $post_type );
$title = sprintf(
$args['posttypetitle'],
get_bloginfo( 'name' ),
$args['separator'],
$post_type_obj->labels->name
);
$href = get_post_type_archive_feed_link( $post_type_obj->name );
} }
$post_type_obj = get_post_type_object( $post_type );
$title = sprintf( $args['posttypetitle'], get_bloginfo( 'name' ), $args['separator'], $post_type_obj->labels->name );
$href = get_post_type_archive_feed_link( $post_type_obj->name );
} elseif ( is_category() ) { } elseif ( is_category() ) {
$term = get_queried_object(); /**
* Filters whether to display the category feed link.
*
* @since 6.1.0
*
* @param bool $show Whether to display the category feed link. Default true.
*/
$show_category_feed = apply_filters( 'feed_links_extra_show_category_feed', true );
if ( $term ) { if ( $show_category_feed ) {
$title = sprintf( $args['cattitle'], get_bloginfo( 'name' ), $args['separator'], $term->name ); $term = get_queried_object();
$href = get_category_feed_link( $term->term_id );
if ( $term ) {
$title = sprintf(
$args['cattitle'],
get_bloginfo( 'name' ),
$args['separator'],
$term->name
);
$href = get_category_feed_link( $term->term_id );
}
} }
} elseif ( is_tag() ) { } elseif ( is_tag() ) {
$term = get_queried_object(); /**
* Filters whether to display the tag feed link.
*
* @since 6.1.0
*
* @param bool $show Whether to display the tag feed link. Default true.
*/
$show_tag_feed = apply_filters( 'feed_links_extra_show_tag_feed', true );
if ( $term ) { if ( $show_tag_feed ) {
$title = sprintf( $args['tagtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name ); $term = get_queried_object();
$href = get_tag_feed_link( $term->term_id );
if ( $term ) {
$title = sprintf(
$args['tagtitle'],
get_bloginfo( 'name' ),
$args['separator'],
$term->name
);
$href = get_tag_feed_link( $term->term_id );
}
} }
} elseif ( is_tax() ) { } elseif ( is_tax() ) {
$term = get_queried_object(); /**
* Filters whether to display the custom taxonomy feed link.
*
* @since 6.1.0
*
* @param bool $show Whether to display the custom taxonomy feed link. Default true.
*/
$show_tax_feed = apply_filters( 'feed_links_extra_show_tax_feed', true );
if ( $term ) { if ( $show_tax_feed ) {
$tax = get_taxonomy( $term->taxonomy ); $term = get_queried_object();
$title = sprintf( $args['taxtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name, $tax->labels->singular_name );
$href = get_term_feed_link( $term->term_id, $term->taxonomy ); if ( $term ) {
$tax = get_taxonomy( $term->taxonomy );
$title = sprintf(
$args['taxtitle'],
get_bloginfo( 'name' ),
$args['separator'],
$term->name,
$tax->labels->singular_name
);
$href = get_term_feed_link( $term->term_id, $term->taxonomy );
}
} }
} elseif ( is_author() ) { } elseif ( is_author() ) {
$author_id = (int) get_query_var( 'author' ); /**
* Filters whether to display the author feed link.
*
* @since 6.1.0
*
* @param bool $show Whether to display the author feed link. Default true.
*/
$show_author_feed = apply_filters( 'feed_links_extra_show_author_feed', true );
$title = sprintf( $args['authortitle'], get_bloginfo( 'name' ), $args['separator'], get_the_author_meta( 'display_name', $author_id ) ); if ( $show_author_feed ) {
$href = get_author_feed_link( $author_id ); $author_id = (int) get_query_var( 'author' );
$title = sprintf(
$args['authortitle'],
get_bloginfo( 'name' ),
$args['separator'],
get_the_author_meta( 'display_name', $author_id )
);
$href = get_author_feed_link( $author_id );
}
} elseif ( is_search() ) { } elseif ( is_search() ) {
$title = sprintf( $args['searchtitle'], get_bloginfo( 'name' ), $args['separator'], get_search_query( false ) ); /**
$href = get_search_feed_link(); * Filters whether to display the search results feed link.
*
* @since 6.1.0
*
* @param bool $show Whether to display the search results feed link. Default true.
*/
$show_search_feed = apply_filters( 'feed_links_extra_show_search_feed', true );
if ( $show_search_feed ) {
$title = sprintf(
$args['searchtitle'],
get_bloginfo( 'name' ),
$args['separator'],
get_search_query( false )
);
$href = get_search_feed_link();
}
} }
if ( isset( $title ) && isset( $href ) ) { if ( isset( $title ) && isset( $href ) ) {
echo '<link rel="alternate" type="' . feed_content_type() . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n"; printf(
'<link rel="alternate" type="%s" title="%s" href="%s" />' . "\n",
feed_content_type(),
esc_attr( $title ),
esc_url( $href )
);
} }
} }
@ -3218,7 +3360,10 @@ function feed_links_extra( $args = array() ) {
* @since 2.0.0 * @since 2.0.0
*/ */
function rsd_link() { function rsd_link() {
echo '<link rel="EditURI" type="application/rsd+xml" title="RSD" href="' . esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) ) . '" />' . "\n"; printf(
'<link rel="EditURI" type="application/rsd+xml" title="RSD" href="%s" />' . "\n",
esc_url( site_url( 'xmlrpc.php?rsd', 'rpc' ) )
);
} }
/** /**
@ -3228,7 +3373,10 @@ function rsd_link() {
* @since 2.3.1 * @since 2.3.1
*/ */
function wlwmanifest_link() { function wlwmanifest_link() {
echo '<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="' . includes_url( 'wlwmanifest.xml' ) . '" /> ' . "\n"; printf(
'<link rel="wlwmanifest" type="application/wlwmanifest+xml" href="%s" />' . "\n",
includes_url( 'wlwmanifest.xml' )
);
} }
/** /**

View File

@ -16,7 +16,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '6.1-alpha-54160'; $wp_version = '6.1-alpha-54161';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.