*/ public static function types(): array { $types = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $type ) { if ( in_array( $type->name, self::ignored(), true ) ) { continue; } $types[ $type->name ] = $type; } uasort( $types, static function ( $a, $b ): int { $rank = static fn( string $name ): int => match ( $name ) { 'page' => 0, 'post' => 1, 'product' => 2, default => 3, }; $ra = $rank( $a->name ); $rb = $rank( $b->name ); return $ra === $rb ? strcasecmp( (string) $a->label, (string) $b->label ) : $ra <=> $rb; } ); return array_values( $types ); } /** * Types that are public but are not content anybody edits for SEO. * * WooCommerce order-received pages, Elementor library templates and the * like are public post types by technicality. Listing them as a tab next * to "Products" makes the screen look broken. * * @return array */ private static function ignored(): array { /** * Filter the post types the content library hides. * * @param array $ignored Post type names. */ return (array) apply_filters( 'airanksuite/library/ignored_types', // The one list every counter shares. \AiRankSuite\Support\Inventory::technical_types() ); } /** * Counts and health per post type, in one query. * * @param bool $fresh Skip the cache. * @return array> */ public static function overview( bool $fresh = false ): array { if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } global $wpdb; $types = self::types(); if ( array() === $types ) { return array(); } $names = array_map( static fn( $t ) => $t->name, $types ); $statuses = array( 'publish', 'draft', 'pending', 'future', 'private' ); $type_ph = implode( ', ', array_fill( 0, count( $names ), '%s' ) ); $status_ph = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); /* * A description written by the SEO plugin you are migrating from is * still a description. * * This counted `_airanksuite_seo_desc` alone, so a site whose 160 pages all * carry a Rank Math description was told 160 pages have none — and * every headline, tile and recommendation on the dashboard was built * on that number. It is not a rounding error; it is the difference * between "your site is empty" and "your site is fine, the text lives * in the other plugin". The same is true of the focus keyword. * * The keys come from the import adapters, which already know them, so * a new adapter teaches the counter without touching this query. */ $desc_keys = self::foreign_keys( 'desc' ); $kw_keys = self::foreign_keys( 'keyword' ); $desc_ph = implode( ', ', array_fill( 0, count( $desc_keys ), '%s' ) ); $kw_ph = implode( ', ', array_fill( 0, count( $kw_keys ), '%s' ) ); // One pass. Every LEFT JOIN is on (post_id, meta_key), which is the // index postmeta already has, so this stays a few hundred milliseconds // even at six figures of posts. /* * Two description counts, deliberately. * * no_desc counts every status; no_desc_published counts only what the * bulk writer will actually touch. On a site whose three gaps were all * drafts, the hero offered to write three descriptions and the run it * started reported nothing to do, because that run skips drafts. The * button reads the second figure; the filter chip still reads the first, * because a draft without a description is still a gap worth seeing. */ // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( " SELECT p.post_type AS type, COUNT(*) AS total, SUM( CASE WHEN p.post_status = 'publish' THEN 1 ELSE 0 END ) AS published, SUM( CASE WHEN p.post_status = 'draft' THEN 1 ELSE 0 END ) AS drafts, SUM( CASE WHEN hd.meta_value IS NOT NULL THEN ( CASE WHEN hd.meta_value = '1' THEN 0 ELSE 1 END ) ELSE ( CASE WHEN COALESCE( d.has_value, 0 ) = 0 THEN 1 ELSE 0 END ) END ) AS no_desc, -- The same gap, but only where the bulk writer can act: it -- touches published posts only. SUM( CASE WHEN p.post_status = 'publish' THEN ( CASE WHEN hd.meta_value IS NOT NULL THEN ( CASE WHEN hd.meta_value = '1' THEN 0 ELSE 1 END ) ELSE ( CASE WHEN COALESCE( d.has_value, 0 ) = 0 THEN 1 ELSE 0 END ) END ) ELSE 0 END ) AS no_desc_published, SUM( CASE WHEN COALESCE( k.has_value, 0 ) = 0 THEN 1 ELSE 0 END ) AS no_keyword, SUM( CASE WHEN g.meta_value IS NULL THEN 1 ELSE 0 END ) AS unanalyzed, SUM( CASE WHEN g.meta_value IS NOT NULL AND g.meta_value + 0 < 60 THEN 1 ELSE 0 END ) AS weak_geo, SUM( CASE WHEN r.meta_value LIKE %s THEN 1 ELSE 0 END ) AS noindex, AVG( g.meta_value + 0 ) AS avg_geo, AVG( s.meta_value + 0 ) AS avg_seo, MAX( p.post_modified_gmt ) AS latest FROM {$wpdb->posts} p LEFT JOIN ( SELECT post_id, MAX( CASE WHEN meta_value <> '' THEN 1 ELSE 0 END ) AS has_value FROM {$wpdb->postmeta} WHERE meta_key IN ( {$desc_ph} ) GROUP BY post_id ) d ON d.post_id = p.ID LEFT JOIN ( SELECT post_id, MAX( CASE WHEN meta_value <> '' THEN 1 ELSE 0 END ) AS has_value FROM {$wpdb->postmeta} WHERE meta_key IN ( {$kw_ph} ) GROUP BY post_id ) k ON k.post_id = p.ID /* * The analyser's own verdict, when it has one. * * A description can come from our field, from the SEO plugin you * migrated from, or from a description template — and only the * analyser resolves all three. Where it has run, its answer wins; * where it has not, the meta-key union above is the best guess. */ LEFT JOIN {$wpdb->postmeta} hd ON hd.post_id = p.ID AND hd.meta_key = '_airanksuite_has_desc' LEFT JOIN {$wpdb->postmeta} g ON g.post_id = p.ID AND g.meta_key = '_airanksuite_geo_score' LEFT JOIN {$wpdb->postmeta} s ON s.post_id = p.ID AND s.meta_key = '_airanksuite_seo_score' LEFT JOIN {$wpdb->postmeta} r ON r.post_id = p.ID AND r.meta_key = '_airanksuite_robots' WHERE p.post_type IN ( {$type_ph} ) AND p.post_status IN ( {$status_ph} ) GROUP BY p.post_type ", // Order matters: the noindex pattern is in the SELECT list, and // the two subqueries appear before the WHERE. array_merge( array( '%noindex%' ), $desc_keys, $kw_keys, $names, $statuses ) ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared $by = array(); foreach ( (array) $rows as $row ) { $by[ (string) $row['type'] ] = $row; } $out = array(); foreach ( $types as $type ) { $row = $by[ $type->name ] ?? array(); // A type with nothing in it is not a tab. It is still returned so // the caller can say "you have no products yet" if it wants to, // with `total` at zero rather than the row missing. $total = (int) ( $row['total'] ?? 0 ); $out[] = array( 'name' => $type->name, 'label' => \AiRankSuite\Support\Inventory::type_label( (string) $type->name ), 'singular' => (string) ( $type->labels->singular_name ?? $type->name ), 'hierarchical' => (bool) $type->hierarchical, 'total' => $total, 'published' => (int) ( $row['published'] ?? 0 ), 'drafts' => (int) ( $row['drafts'] ?? 0 ), 'no_desc' => (int) ( $row['no_desc'] ?? 0 ), 'no_desc_published' => (int) ( $row['no_desc_published'] ?? 0 ), 'no_keyword' => (int) ( $row['no_keyword'] ?? 0 ), 'unanalyzed' => (int) ( $row['unanalyzed'] ?? 0 ), 'weak_geo' => (int) ( $row['weak_geo'] ?? 0 ), 'noindex' => (int) ( $row['noindex'] ?? 0 ), // null, not 0, when nothing has been analyzed: an average of // nothing is not zero, and a big red 0 would be a lie. 'avg_geo' => isset( $row['avg_geo'] ) && null !== $row['avg_geo'] ? (int) round( (float) $row['avg_geo'] ) : null, 'avg_seo' => isset( $row['avg_seo'] ) && null !== $row['avg_seo'] ? (int) round( (float) $row['avg_seo'] ) : null, 'latest' => (string) ( $row['latest'] ?? '' ), 'taxonomies' => self::taxonomies( $type->name ), ); } set_transient( self::CACHE, $out, self::TTL ); return $out; } /** * Filterable taxonomies for one type, so a 40,000-post magazine can be * narrowed to one section before anybody starts scrolling. * * @return array> */ private static function taxonomies( string $post_type ): array { $out = array(); foreach ( get_object_taxonomies( $post_type, 'objects' ) as $tax ) { if ( ! $tax->public || ! $tax->show_ui ) { continue; } $count = (int) wp_count_terms( array( 'taxonomy' => $tax->name, 'hide_empty' => true ) ); if ( $count < 2 || $count > 400 ) { // One term is not a filter; four hundred is a search box. continue; } $terms = get_terms( array( 'taxonomy' => $tax->name, 'hide_empty' => true, 'number' => 200, 'orderby' => 'count', 'order' => 'DESC', ) ); if ( is_wp_error( $terms ) ) { continue; } $out[] = array( 'name' => $tax->name, 'label' => (string) ( $tax->labels->name ?? $tax->name ), 'terms' => array_map( static fn( $t ) => array( 'slug' => $t->slug, 'name' => $t->name, 'count' => (int) $t->count, ), $terms ), ); } return $out; } /** Counts change on every save; the screen should not show yesterday's. */ public static function flush(): void { delete_transient( self::CACHE ); } /** * Every meta key that can hold this field, ours first. * * Built from the import adapters so one list serves the importer and the * counters. A key is included whether or not its plugin is active: a site * that deactivated Yoast last week still has the text in the database, and * a page with a description is a page with a description. * * @param string $field 'desc' or 'keyword'. * @return array */ private static function foreign_keys( string $field ): array { $map = array( 'desc' => array( '_airanksuite_seo_desc', 'rank_math_description', '_yoast_wpseo_metadesc', '_seopress_titles_desc', '_aioseo_description', '_genesis_description', '_su_description', '_wpm_description', ), 'keyword' => array( '_airanksuite_focus_keyword', 'rank_math_focus_keyword', '_yoast_wpseo_focuskw', '_seopress_analysis_target_kw', '_aioseo_keywords', ), ); /** * Filter the meta keys a description or keyword may live in. * * @param array $keys Meta keys. * @param string $field 'desc' or 'keyword'. */ return array_values( array_unique( (array) apply_filters( 'airanksuite/library/meta_keys', $map[ $field ] ?? array(), $field ) ) ); } }