the cascade's site-wide override makes every * public object noindexed regardless of Type/Object layers. Short- * circuits to a single COUNT(*). * 2. Otherwise, classify each public post type by its OWN cascade * baseline (Type's explicit index setting, or the Global default when * the Type has none) into "baseline indexed" vs "baseline noindexed": * - baseline-noindexed types: every post of that type is noindexed * (object meta can only ever force noindex further, never force * index back on — same rule resolve_from_raw() encodes). * - baseline-indexed types: only posts additionally carrying the * `_abds_seo_noindex` object-meta flag are noindexed. * * Scoped to posts only, not taxonomy terms or the archive/search/404 * pseudo-contexts — terms carry no per-object override today (so a * Type-noindexed taxonomy's term count could be added later without * changing this method's shape), and archive pages aren't "objects" in the * sense this card's "N noindexed pages" label promises. Cached the same way * ABDS_SEO_Author_Count caches its own site-wide aggregate: tolerant of an * hour's staleness, never actively invalidated on save. * * @package ABDS_SEO */ if ( ! defined( 'ABSPATH' ) ) { exit; } /** * Live count of posts the Policy Engine currently resolves to noindex. */ class ABDS_SEO_Noindex_Count { const TRANSIENT_KEY = 'abds_seo_noindexed_post_count'; const TRANSIENT_TTL = HOUR_IN_SECONDS; /** * The live noindexed-post count, cached for TRANSIENT_TTL. * * @return int */ public static function noindexed_post_count(): int { $cached = get_transient( self::TRANSIENT_KEY ); if ( false !== $cached ) { return (int) $cached; } $count = self::compute(); set_transient( self::TRANSIENT_KEY, $count, self::TRANSIENT_TTL ); return $count; } /** * The actual aggregate computation, see class docblock for the approach. * * @return int */ protected static function compute(): int { global $wpdb; $post_types = array_keys( ABDS_SEO_Taxonomy_Helper::public_post_types() ); if ( array() === $post_types ) { return 0; } if ( '0' === (string) get_option( 'blog_public' ) ) { return self::count_posts_in_types( $post_types ); } $settings = get_option( 'abds_seo_settings', array() ); $type_settings = (array) ( $settings['type_settings'] ?? array() ); $global_default_robots = (string) ( $settings['global']['default_robots'] ?? 'index,follow' ); $global_index_default = ! str_contains( $global_default_robots, 'noindex' ); $baseline_noindexed_types = array(); $baseline_indexed_types = array(); foreach ( $post_types as $post_type ) { $type_index = array_key_exists( 'index', (array) ( $type_settings[ $post_type ] ?? array() ) ) ? $type_settings[ $post_type ]['index'] : null; $baseline = $type_index ?? $global_index_default; if ( $baseline ) { $baseline_indexed_types[] = $post_type; } else { $baseline_noindexed_types[] = $post_type; } } $count = self::count_posts_in_types( $baseline_noindexed_types ); $count += self::count_noindex_meta_in_types( $baseline_indexed_types ); return $count; } /** * Published-post count for a set of post types, no meta filter — used * for both the blog_public=false short-circuit and baseline-noindexed * types (every post of the type counts, unconditionally). * * @param string[] $post_types Post-type slugs. * @return int */ protected static function count_posts_in_types( array $post_types ): int { if ( array() === $post_types ) { return 0; } global $wpdb; $placeholders = implode( ',', array_fill( 0, count( $post_types ), '%s' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $wpdb->posts is core, not user input; {$placeholders} is a dynamically-built but fixed-count "%s,%s,..." list, real args supplied via $post_types just below. $sql = "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_status = 'publish' AND post_type IN ({$placeholders})"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql is prepared just below; result is cached by the public transient-backed caller, not re-run per request. return (int) $wpdb->get_var( $wpdb->prepare( $sql, $post_types ) ); } /** * Count of posts within the given post types carrying an explicit * `_abds_seo_noindex` = '1' object-meta override — used for * baseline-indexed types, where only an explicit per-object flag flips * a post to noindexed. * * @param string[] $post_types Post-type slugs. * @return int */ protected static function count_noindex_meta_in_types( array $post_types ): int { if ( array() === $post_types ) { return 0; } global $wpdb; $placeholders = implode( ',', array_fill( 0, count( $post_types ), '%s' ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare -- $wpdb->posts/$wpdb->postmeta are core, not user input; {$placeholders} is a dynamically-built but fixed-count "%s,%s,..." list, real args supplied via $post_types just below. $sql = "SELECT COUNT(DISTINCT p.ID) FROM {$wpdb->posts} p" . " INNER JOIN {$wpdb->postmeta} pm ON pm.post_id = p.ID" . " WHERE p.post_status = 'publish' AND p.post_type IN ({$placeholders})" . " AND pm.meta_key = '_abds_seo_noindex' AND pm.meta_value = '1'"; // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- $sql is prepared just below; result is cached by the public transient-backed caller, not re-run per request. return (int) $wpdb->get_var( $wpdb->prepare( $sql, $post_types ) ); } }