'GET', 'callback' => fn() => new WP_REST_Response( $this->calculator->latest() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/score/recalculate', array( 'methods' => 'POST', 'callback' => fn() => new WP_REST_Response( $this->calculator->calculate() ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/score/content', array( 'methods' => 'GET', 'callback' => array( $this, 'content_summary' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); register_rest_route( self::NAMESPACE, '/score/area/(?P[a-z_]+)', array( 'methods' => 'GET', 'callback' => array( $this, 'area_detail' ), 'permission_callback' => static fn(): bool => current_user_can( 'manage_options' ), ) ); } /** * Site-wide content KPIs, the area roll-up, and the pages that need * attention most. * * This used to load two hundred posts and run the analyzer on each one. * That was slow on a large site, wrong (the "site average" was the average * of the two hundred most recently modified pages), and unhelpful — a * health map with one row per page is not a map at a thousand pages. * * Everything here is now aggregated in SQL over the scores stored at * analysis time, so every page counts and the cost does not grow with the * size of the site. */ public function content_summary(): WP_REST_Response { global $wpdb; $types = \AiRankSuite\Support\Inventory::eligible_types(); if ( array() === $types ) { $types = array( 'post' ); } $type_ph = implode( ', ', array_fill( 0, count( $types ), '%s' ) ); // Published only: "pages" on the dashboard means addressable pages, the // same set the content library, the analyzer and the audit count. Drafts // are reported as their own number, never mixed in. $statuses = array( 'publish' ); $stat_ph = implode( ', ', array_fill( 0, count( $statuses ), '%s' ) ); // One row: how many pages exist, how many have been analyzed, and the // two averages — over the whole site rather than over a sample. // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery $totals = $wpdb->get_row( $wpdb->prepare( "SELECT COUNT(*) AS pages, SUM( g.meta_value IS NOT NULL ) AS analyzed, AVG( g.meta_value + 0 ) AS avg_geo, AVG( s.meta_value + 0 ) AS avg_seo, SUM( d.meta_value IS NULL OR d.meta_value = '' ) AS no_desc, SUM( w.meta_value IS NOT NULL AND w.meta_value + 0 < 300 ) AS thin FROM {$wpdb->posts} p 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} d ON d.post_id = p.ID AND d.meta_key = '_airanksuite_seo_desc' LEFT JOIN {$wpdb->postmeta} w ON w.post_id = p.ID AND w.meta_key = '_airanksuite_words' WHERE p.post_type IN ( {$type_ph} ) AND p.post_status IN ( {$stat_ph} )", array_merge( $types, $statuses ) ), ARRAY_A ); // The weakest pages, ranked in SQL. A page that has never been analyzed // has no score and is reported separately rather than ranked as a zero. $weakest = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, p.post_type, g.meta_value + 0 AS geo, s.meta_value + 0 AS seo FROM {$wpdb->posts} p INNER 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' WHERE p.post_type IN ( {$type_ph} ) AND p.post_status IN ( {$stat_ph} ) ORDER BY ( g.meta_value + 0 ) + COALESCE( s.meta_value + 0, 0 ) ASC LIMIT 12", array_merge( $types, $statuses ) ), ARRAY_A ); // phpcs:enable // Page and analyzed counts come from the one inventory (Support\Inventory), // never from this query — otherwise this screen and the dashboard can // disagree by whatever the two SQL statements happen to include. $inventory = \AiRankSuite\Support\Inventory::counts(); $pages = (int) $inventory['published']; $analyzed = (int) $inventory['analyzed']; $needs = array(); foreach ( (array) $weakest as $row ) { $needs[] = array( 'id' => (int) $row['ID'], 'title' => (string) $row['post_title'], 'type' => (string) $row['post_type'], 'geo_score' => (int) $row['geo'], 'seo_score' => (int) $row['seo'], ); } return new WP_REST_Response( array( 'pages' => $pages, 'analyzed' => $analyzed, 'unanalyzed' => max( 0, $pages - $analyzed ), 'drafts' => (int) $inventory['drafts'], 'technical' => (int) $inventory['technical'], 'indexable' => (int) $inventory['indexable'], 'inventory' => $inventory, 'avg_geo' => isset( $totals['avg_geo'] ) && null !== $totals['avg_geo'] ? (int) round( (float) $totals['avg_geo'] ) : 0, 'avg_seo' => isset( $totals['avg_seo'] ) && null !== $totals['avg_seo'] ? (int) round( (float) $totals['avg_seo'] ) : 0, 'missing_desc' => (int) ( $totals['no_desc'] ?? 0 ), 'thin_content' => (int) ( $totals['thin'] ?? 0 ), 'needs_attention' => array_slice( $needs, 0, 5 ), // The map is now the seven areas across the whole site, not a // sample of pages. A cold column is a site-wide habit and says // how many pages it affects. 'areas' => \AiRankSuite\Modules\GeoScore\Services\Dimensions::board(), ) ); } /** * The worst pages in one area — the drill-down from a cold column. */ public function area_detail( WP_REST_Request $request ): WP_REST_Response { return new WP_REST_Response( array( 'rows' => \AiRankSuite\Modules\GeoScore\Services\Dimensions::worst( (string) $request['area'], 20 ), ) ); } }