settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); $mode = $this->settings['disable_feeds'] ?? ''; if( 'all' !== $mode && 'comments' !== $mode ) { return; } add_action( 'template_redirect', [ $this, 'maybe_redirect' ], 1 ); if( 'all' === $mode ) { Utils::remove_feed_links(); } else { add_filter( 'feed_links_show_comments_feed', '__return_false' ); } } /** * Adds the feeds choice to the Posts tab. * * @param array $fields The settings fields keyed by tab. * * @return array The modified settings fields. */ public function adminease_settings_fields( array $fields ): array { $fields['posts']['fields'][] = [ 'type' => 'select', 'id' => 'disable-feeds', 'name' => 'adminease[posts][disable_feeds]', 'value' => $this->settings['disable_feeds'] ?? '', 'options' => [ '' => __( 'Keep all feeds', 'adminease' ), 'comments' => __( 'Turn off comment feeds only', 'adminease' ), 'all' => __( 'Turn off all feeds', 'adminease' ), ], 'label_class' => 'pwp-field-label', 'input_class' => 'pwp-field-input', 'label' => __( 'RSS feeds', 'adminease' ), 'description' => __( 'Comment feeds are a known target for spam and scraping, so they can go while your post feed stays. Turning off all feeds sends visitors to the matching page instead and removes the feed links from your pages. Podcasts, shop product feeds and email newsletters that read your RSS feed will stop working.', 'adminease' ), ]; return $fields; } /** * Sends a request for a switched off feed to the page it belongs to. * * @return void */ public function maybe_redirect() { $mode = $this->settings['disable_feeds'] ?? ''; if( ( 'all' === $mode && is_feed() ) || ( 'comments' === $mode && is_comment_feed() ) ) { nocache_headers(); wp_safe_redirect( $this->get_redirect_url(), 302 ); exit; } } /** * Works out the page a feed request should go to, from the current query. * A post goes to its permalink, a term to its archive, an author to their page, * a post type archive to its link and everything else to the home page. * * @return string */ public function get_redirect_url(): string { $url = ''; if( is_singular() ) { $url = get_permalink( get_queried_object_id() ); } elseif( is_category() || is_tag() || is_tax() ) { $term = get_queried_object(); $link = $term instanceof \WP_Term ? get_term_link( $term ) : ''; $url = is_wp_error( $link ) ? '' : $link; } elseif( is_author() ) { $url = get_author_posts_url( get_queried_object_id() ); } elseif( is_post_type_archive() ) { $post_type = get_query_var( 'post_type' ); $post_type = is_array( $post_type ) ? reset( $post_type ) : $post_type; $url = get_post_type_archive_link( (string) $post_type ); } return $url ? (string) $url : home_url( '/' ); } }