` output uses. * 2. The explicit "Wyklucz z mapy witryny" toggle (`MetaFields::SITEMAP_EXCLUDE`) * — deliberately SEPARATE from noindex: a page can stay indexable (linked * elsewhere) while being intentionally omitted from the submitted sitemap. * * Taxonomy archives (M56) have no per-term robots override in this plugin * (only a per-TAXONOMY default, `ArchiveSettingsRepositoryInterface`), so * that case is handled at the whole-taxonomy level via `wp_sitemaps_taxonomies` * — there's no finer granularity to preserve. */ final class SitemapExclusion { private const CACHE_GROUP = 'acez_seo'; private const CACHE_TTL = HOUR_IN_SECONDS; private MetaResolver $resolver; private ArchiveSettingsRepositoryInterface $archive_settings; public function __construct( MetaResolver $resolver, ArchiveSettingsRepositoryInterface $archive_settings ) { $this->resolver = $resolver; $this->archive_settings = $archive_settings; } public function register(): void { add_filter( 'wp_sitemaps_posts_query_args', array( $this, 'exclude_noindexed' ), 10, 2 ); add_filter( 'wp_sitemaps_taxonomies', array( $this, 'exclude_noindexed_taxonomies' ) ); } /** * @param array $args * * @return array */ public function exclude_noindexed( array $args, string $post_type ): array { if ( ! in_array( $post_type, PostTypes::supported(), true ) ) { return $args; } $excluded_ids = $this->excluded_post_ids( $post_type ); if ( array() === $excluded_ids ) { return $args; } $existing = isset( $args['post__not_in'] ) ? (array) $args['post__not_in'] : array(); $args['post__not_in'] = array_merge( $existing, $excluded_ids ); // phpcs:ignore WordPressVIPMinimum.Performance.WPQueryParams.PostNotIn_post__not_in -- VIP-hosting-scale advisory, not applicable to a self-hosted plugin; this is WP core's own wp_sitemaps_posts_query_args filter, the only exclusion mechanism it exposes. return $args; } /** * @param array $taxonomies * * @return array */ public function exclude_noindexed_taxonomies( array $taxonomies ): array { $supported = Taxonomies::supported(); foreach ( $taxonomies as $name => $taxonomy ) { if ( ! in_array( $name, $supported, true ) ) { continue; } if ( ! $this->archive_settings->get( $name )['robots_index'] ) { unset( $taxonomies[ $name ] ); } } return $taxonomies; } /** * @return int[] */ private function excluded_post_ids( string $post_type ): array { $cache_key = sprintf( 'sitemap_excluded_%s_v%d', $post_type, CacheVersion::get() ); $cached = wp_cache_get( $cache_key, self::CACHE_GROUP ); if ( is_array( $cached ) ) { return $cached; } $excluded = array(); // Deliberately NOT 'fields' => 'ids' — that mode skips WP core's own // post/meta cache priming (`update_post_caches()`), which would turn // the loop below into one `get_post()` SQL query per post instead of // one batched query for the whole set. Fetching full WP_Post objects // (default 'fields') lets WordPress prime the meta cache for all of // them together, so `get_post_meta()` inside the loop is a cache hit, // not a fresh query — same result, far fewer queries on large catalogs. $query = new \WP_Query( array( 'post_type' => $post_type, 'post_status' => 'publish', 'posts_per_page' => -1, 'no_found_rows' => true, 'update_post_term_cache' => false, ) ); foreach ( $query->posts as $post ) { if ( ! $post instanceof \WP_Post ) { continue; } if ( ! $this->resolver->robots( $post )['index'] || (bool) get_post_meta( $post->ID, MetaFields::SITEMAP_EXCLUDE, true ) ) { $excluded[] = (int) $post->ID; } } wp_cache_set( $cache_key, $excluded, self::CACHE_GROUP, self::CACHE_TTL ); return $excluded; } }