is_enabled() ) { return; } remove_action( 'wp_head', 'feed_links', 2 ); remove_action( 'wp_head', 'feed_links_extra', 3 ); } /** * Redirect or 404 an actual feed request. * * @return void */ public function maybe_block_feed(): void { if ( ! $this->is_enabled() ) { return; } // A feed URL for something that doesn't exist (e.g. a deleted // post's comment feed) is already a 404 -- nothing to change. if ( ! is_feed() || is_404() ) { return; } if ( $this->allow_main() && ! $this->is_specific_feed() ) { return; } $this->block_or_redirect(); } /** * Whether the current feed belongs to something more specific than * the site's global post/comment feed (a single post's comments, an * archive, a search, etc.) -- what "keep the main site feeds" keeps * disabling even when it's on. * * @return bool */ private function is_specific_feed(): bool { return is_singular() || is_archive() || is_date() || is_author() || is_category() || is_tag() || is_tax() || is_search(); } /** * Either redirect to the matching HTML page or force a 404, per the * configured mode. * * @return void */ private function block_or_redirect(): void { if ( 'redirect' !== $this->mode() ) { $this->force_404(); return; } if ( isset( $_GET['feed'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only; only used to decide which redirect strategy fits this request's URL shape. wp_safe_redirect( remove_query_arg( 'feed' ), 301 ); exit; } // WP redirects a feed=old request on its own, and removing the // query var here would only confuse that; leave it alone. if ( 'old' !== get_query_var( 'feed' ) ) { set_query_var( 'feed', '' ); } redirect_canonical(); // Exits itself on success. // Still here: redirect_canonical() didn't redirect (most likely // a filter blocked it) -- strip the feed segment from the raw // request URL directly instead. $this->fallback_redirect(); } /** * Force the current request to behave like a 404, undoing the feed * `Content-Type` header WordPress already sent during its own * `send_headers` (which runs before `template_redirect`). * * @return void */ private function force_404(): void { global $wp_query; $wp_query->is_feed = false; $wp_query->set_404(); status_header( 404 ); // Core's canonical redirect would otherwise "fix" this 404: for a // post's comment feed it guesses the post from the `name` query var // and redirects to a rebuilt feed link (/hello-world/feed/feed/). remove_action( 'template_redirect', 'redirect_canonical' ); header( 'Content-Type: ' . get_option( 'html_type' ) . '; charset=' . get_option( 'blog_charset' ) ); } /** * Strip the feed segment from the raw request URL and redirect * there, for the cases `redirect_canonical()` doesn't handle itself. * * @return void */ private function fallback_redirect(): void { global $wp_rewrite; $struct = ( ! is_singular() && is_comment_feed() ) ? $wp_rewrite->get_comment_feed_permastruct() : $wp_rewrite->get_feed_permastruct(); if ( ! $struct ) { return; } $pattern = preg_replace( '#/+#', '/', str_replace( '%feed%', '(\w+)?', preg_quote( $struct, '#' ) ) ); $host = isset( $_SERVER['HTTP_HOST'] ) ? sanitize_text_field( wp_unslash( $_SERVER['HTTP_HOST'] ) ) : ''; $request_uri = isset( $_SERVER['REQUEST_URI'] ) ? sanitize_text_field( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $requested_url = ( is_ssl() ? 'https://' : 'http://' ) . $host . $request_uri; $new_url = preg_replace( '#' . $pattern . '/?$#', '', $requested_url ); if ( $new_url && $new_url !== $requested_url ) { wp_safe_redirect( $new_url, 301 ); exit; } } /** * Whether the feature is on at all. * * @return bool */ private function is_enabled(): bool { return (bool) aiou_get_setting( 'performance', 'feeds', 'enabled', 0 ); } /** * 'redirect' or '404'. * * @return string */ private function mode(): string { return (string) aiou_get_setting( 'performance', 'feeds', 'mode', 'redirect' ); } /** * Whether the global post/comment feeds stay enabled. * * @return bool */ private function allow_main(): bool { return (bool) aiou_get_setting( 'performance', 'feeds', 'allow_main', 0 ); } }