-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeeds.php
More file actions
170 lines (145 loc) · 6.01 KB
/
feeds.php
File metadata and controls
170 lines (145 loc) · 6.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
* Functionality related to feeds
*
* @package PluginPizza\Uncomment
*/
namespace PluginPizza\Uncomment\Feeds;
if ( ! defined( 'ABSPATH' ) ) exit;
// Set the content of <wfw:commentRss> to an empty string.
add_filter( 'post_comments_feed_link', '__return_empty_string' );
// Set the content of <slash:comments> to zero.
add_filter( 'get_comments_number', '__return_zero' );
// Remove comment feed pingback HTTP headers.
add_filter( 'wp_headers', __NAMESPACE__ . '\filter_wp_headers' );
// Return an empty string for post comment link, which takes care of <comments>.
add_filter( 'get_comments_link', '__return_empty_string' );
// Remove comments feed link.
add_filter( 'feed_links_show_comments_feed', '__return_false' );
// Remove or replace extra feed links, removing the post comment feeds.
add_action(
'init',
function () {
/*
* WordPress 6.1.0 introduces filters that allows us to specify whether
* to display the post comments feed link.
*
* @see https://core.trac.wordpress.org/changeset/54161
*
* For versions lower than 6.1.0 we'll replace the core feed_links_extra
* function with our own near-identical one.
*/
if ( version_compare( get_bloginfo( 'version' ), '6.1.0 ', '>=' ) ) {
add_filter( 'feed_links_extra_show_post_comments_feed', '__return_false' );
add_filter( 'feed_links_show_comments_feed', '__return_false' );
} else {
remove_action( 'wp_head', 'feed_links_extra', 3 );
add_action( 'wp_head', __NAMESPACE__ . '\feed_links_extra', 3 );
}
}
);
// Redirect requests for a comment feed.
add_action( 'template_redirect', __NAMESPACE__ . '\filter_query', 9 );
/**
* Remove comment feed pingback HTTP headers.
*
* @param array $headers The list of headers to be sent.
* @return array $headers
*/
function filter_wp_headers( $headers ) {
if ( array_key_exists( 'X-Pingback', $headers ) ) {
unset( $headers['X-Pingback'] );
}
return $headers;
}
/**
* Display the links to the extra feeds such as category feeds.
*
* Note that this is a near-clone of the core feed_links_extra() function from
* WordPress core version 6.0.6, except that we remove the is_singular()
* conditional, add the 'default' namespace to i18n functions and add additional
* escaping.
*
* The core function will add the comment feed link if a post has existing
* comments, which we cannot seem to circumvent without actually deleting
* existing comments.
*
* @param array $args Optional arguments.
*/
function feed_links_extra( $args = array() ) {
$defaults = array(
// phpcs:disable WordPress.WP.I18n.TextDomainMismatch -- We're using the 'default' domain to leverage existing WordPress core translations.
/* translators: Separator between blog name and feed type in feed links. */
'separator' => _x( '»', 'feed link', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Post title. */
'singletitle' => __( '%1$s %2$s %3$s Comments Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Category name. */
'cattitle' => __( '%1$s %2$s %3$s Category Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Tag name. */
'tagtitle' => __( '%1$s %2$s %3$s Tag Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Term name, 4: Taxonomy singular name. */
'taxtitle' => __( '%1$s %2$s %3$s %4$s Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Author name. */
'authortitle' => __( '%1$s %2$s Posts by %3$s Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Search query. */
'searchtitle' => __( '%1$s %2$s Search Results for “%3$s” Feed', 'default' ),
/* translators: 1: Blog name, 2: Separator (raquo), 3: Post type name. */
'posttypetitle' => __( '%1$s %2$s %3$s Feed', 'default' ),
// phpcs:enable WordPress.WP.I18n.TextDomainMismatch
);
$args = wp_parse_args( $args, $defaults );
if ( is_post_type_archive() ) {
$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 );
} elseif ( is_category() ) {
$term = get_queried_object();
if ( $term ) {
$title = sprintf( $args['cattitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
$href = get_category_feed_link( $term->term_id );
}
} elseif ( is_tag() ) {
$term = get_queried_object();
if ( $term ) {
$title = sprintf( $args['tagtitle'], get_bloginfo( 'name' ), $args['separator'], $term->name );
$href = get_tag_feed_link( $term->term_id );
}
} elseif ( is_tax() ) {
$term = get_queried_object();
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() ) {
$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() ) {
$title = sprintf( $args['searchtitle'], get_bloginfo( 'name' ), $args['separator'], get_search_query( false ) );
$href = get_search_feed_link();
}
if ( isset( $title ) && isset( $href ) ) {
echo '<link rel="alternate" type="' . esc_attr( feed_content_type() ) . '" title="' . esc_attr( $title ) . '" href="' . esc_url( $href ) . '" />' . "\n";
}
}
/**
* Redirect requests for a comment feed.
*
* @return void
*/
function filter_query() {
if ( ! is_comment_feed() ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['feed'] ) ) {
wp_safe_redirect( remove_query_arg( 'feed' ), 301 );
exit();
}
set_query_var( 'feed', '' );
}