*/ public static function run( bool $fresh = false ): array { if ( ! class_exists( 'WooCommerce' ) ) { return array( 'active' => false ); } if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } $audit = StoreAudit::run( $fresh ); $products = (int) ( $audit['products'] ?? 0 ); $counts = (array) ( $audit['counts'] ?? array() ); $settings = self::settings(); $pillars = self::pillars( $products, $counts, $settings ); $score = 0; $weight = 0; foreach ( $pillars as $pillar ) { $score += $pillar['score'] * $pillar['weight']; $weight += $pillar['weight']; } $payload = array( 'active' => true, 'products' => $products, 'readiness' => $weight > 0 ? (int) round( $score / $weight ) : 0, 'pillars' => array_values( $pillars ), 'counts' => $counts, 'issues' => (array) ( $audit['issues'] ?? array() ), 'schema' => array( 'product_schema' => ! empty( $settings['product_schema'] ), 'identifiers' => ! empty( $settings['identifiers'] ), 'shipping_details' => ! empty( $settings['shipping_details'] ), 'return_policy' => ! empty( $settings['return_policy'] ), 'noindex_cart' => ! empty( $settings['noindex_cart'] ), ), 'feeds' => self::feeds(), 'weakest' => self::weakest(), 'unscored' => self::unscored( $products ), 'links' => array( 'products' => admin_url( 'edit.php?post_type=product' ), 'no_image' => admin_url( 'edit.php?post_type=product&airanksuite_gap=image' ), ), ); set_transient( self::CACHE, $payload, 10 * MINUTE_IN_SECONDS ); return $payload; } /** Forget the cached overview, for the moment a product or a setting changes. */ public static function forget(): void { delete_transient( self::CACHE ); } /** * @return array */ private static function settings(): array { try { return (array) \AiRankSuite\Kernel::service( Options::class )->get( 'woocommerce', WooCommerceModule::defaults() ); } catch ( \Throwable $e ) { return WooCommerceModule::defaults(); } } /** * Four things a shop can be short of, each scored out of 100. * * Product data is what a merchant listing is made of, text is what an AI * answer is made of, stock is whether the page can sell at all, and schema * is whether Google is told any of it. Data and text weigh more because * the other two are one setting away from fixed. * * @param array $counts * @param array $settings * @return array> */ private static function pillars( int $products, array $counts, array $settings ): array { $share = static fn( string $key ): float => $products > 0 ? max( 0.0, 1 - ( (int) ( $counts[ $key ] ?? 0 ) ) / $products ) : 0.0; $data = (int) round( 100 * ( 0.4 * $share( 'no_price' ) + 0.35 * $share( 'no_image' ) + 0.25 * $share( 'no_gtin' ) ) ); $text = (int) round( 100 * ( 0.4 * $share( 'thin' ) + 0.3 * $share( 'no_short_desc' ) + 0.3 * $share( 'no_meta_desc' ) ) ); $stock = (int) round( 100 * $share( 'stranded' ) ); $on = 0; $on += ! empty( $settings['product_schema'] ) ? 40 : 0; $on += ! empty( $settings['identifiers'] ) ? 20 : 0; $on += ! empty( $settings['shipping_details'] ) ? 20 : 0; $on += ! empty( $settings['return_policy'] ) ? 20 : 0; $schema = $on; return array( 'data' => array( 'id' => 'data', 'label' => __( 'Product data', 'ai-rank-suite' ), 'score' => $data, 'weight' => 3, 'hint' => __( 'Price, image and GTIN on every product. A listing without them is not shown as a product anywhere.', 'ai-rank-suite' ) ), 'text' => array( 'id' => 'text', 'label' => __( 'Product text', 'ai-rank-suite' ), 'score' => $text, 'weight' => 3, 'hint' => __( 'Description, short description and meta description. An AI answer quotes text; a product with none is not quoted.', 'ai-rank-suite' ) ), 'stock' => array( 'id' => 'stock', 'label' => __( 'Sellable pages', 'ai-rank-suite' ), 'score' => $stock, 'weight' => 2, 'hint' => __( 'Indexable products that are out of stock send a crawler to a page nobody can buy from.', 'ai-rank-suite' ) ), 'schema' => array( 'id' => 'schema', 'label' => __( 'Product schema', 'ai-rank-suite' ), 'score' => $schema, 'weight' => 2, 'hint' => __( 'Product, identifiers, shipping and returns as structured data, so Google shows price and availability in the result.', 'ai-rank-suite' ) ), ); } /** * @return array */ private static function feeds(): array { $google = get_transient( 'airanksuite_feed_google' ); $meta = get_transient( 'airanksuite_feed_meta' ); return array( 'google' => array( 'items' => is_string( $google ) ? substr_count( $google, '' ) : null, 'url' => home_url( '/feed/google-merchant.xml' ), ), 'meta' => array( 'items' => is_string( $meta ) ? substr_count( $meta, '' ) : null, 'url' => home_url( '/feed/meta-catalog.xml' ), ), ); } /** * The ten products with the lowest AI-search score, the work queue. * * @return array> */ private static function weakest(): array { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $rows = $wpdb->get_results( $wpdb->prepare( "SELECT p.ID, p.post_title, CAST(m.meta_value AS SIGNED) AS score FROM %i p INNER JOIN %i m ON m.post_id = p.ID AND m.meta_key = '_airanksuite_geo_score' WHERE p.post_type = 'product' AND p.post_status = 'publish' ORDER BY score ASC, p.ID ASC LIMIT 10", $wpdb->posts, $wpdb->postmeta ), ARRAY_A ); $out = array(); foreach ( (array) $rows as $row ) { $out[] = array( 'id' => (int) $row['ID'], 'title' => (string) $row['post_title'], 'score' => (int) $row['score'], 'edit' => (string) get_edit_post_link( (int) $row['ID'], 'raw' ), 'url' => (string) get_permalink( (int) $row['ID'] ), ); } return $out; } private static function unscored( int $products ): int { global $wpdb; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery $scored = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i p INNER JOIN %i m ON m.post_id = p.ID AND m.meta_key = '_airanksuite_geo_score' WHERE p.post_type = 'product' AND p.post_status = 'publish'", $wpdb->posts, $wpdb->postmeta ) ); return max( 0, $products - $scored ); } }