$score, 'grade' => self::grade( $score ), 'catalog' => $catalog, 'store' => $store, 'fixes' => self::build_fixes( $catalog, $store ), 'scanned_at' => time(), ); update_option( self::CACHE_KEY, $result, false ); return $result; } public static function last() { $r = get_option( self::CACHE_KEY ); return is_array( $r ) ? $r : null; } public static function grade( $score ) { if ( $score >= 85 ) { return 'A'; } if ( $score >= 70 ) { return 'B'; } if ( $score >= 50 ) { return 'C'; } return 'D'; } /* ------------------------------------------------------------------ */ /* Catalog */ /* ------------------------------------------------------------------ */ /** Per-product checks: key => [label, weight]. */ public static function product_checks() { return array( 'description' => array( __( 'Detailed description (150+ characters)', 'agentrue' ), 3 ), 'short_desc' => array( __( 'Short description / summary', 'agentrue' ), 1 ), 'image' => array( __( 'Main product image', 'agentrue' ), 3 ), 'gallery' => array( __( 'At least one extra gallery image', 'agentrue' ), 1 ), 'price' => array( __( 'Price set', 'agentrue' ), 3 ), 'sku' => array( __( 'SKU', 'agentrue' ), 1 ), 'gtin' => array( __( 'GTIN / UPC / EAN / ISBN', 'agentrue' ), 2 ), 'category' => array( __( 'Real category (not "Uncategorized")', 'agentrue' ), 1 ), 'stock' => array( __( 'Stock status known and in stock', 'agentrue' ), 2 ), 'weight' => array( __( 'Weight (needed for shipping quotes)', 'agentrue' ), 1 ), 'attributes' => array( __( 'Attributes (size, color, material…)', 'agentrue' ), 1 ), ); } /** * Evaluate a single product. Returns check_key => bool. * * @param WC_Product $product Product. */ public static function check_product( $product ) { $desc = trim( wp_strip_all_tags( (string) $product->get_description() ) ); $short = trim( wp_strip_all_tags( (string) $product->get_short_description() ) ); $gtin = ''; if ( method_exists( $product, 'get_global_unique_id' ) ) { $gtin = (string) $product->get_global_unique_id(); } if ( '' === $gtin ) { foreach ( array( '_gtin', '_ean', '_upc', '_isbn', 'gtin', '_wpm_gtin_code', 'hwp_product_gtin' ) as $meta_key ) { $v = $product->get_meta( $meta_key ); if ( ! empty( $v ) ) { $gtin = (string) $v; break; } } } $uncat = (int) get_option( 'default_product_cat' ); $cats = $product->get_category_ids(); $real_cat = ! empty( array_diff( $cats, array( $uncat ) ) ); $is_virtual = $product->is_virtual() || $product->is_downloadable(); return array( 'description' => mb_strlen( $desc ) >= 150, 'short_desc' => mb_strlen( $short ) >= 20, 'image' => (bool) $product->get_image_id(), 'gallery' => count( $product->get_gallery_image_ids() ) >= 1, 'price' => '' !== (string) $product->get_price(), 'sku' => '' !== (string) $product->get_sku(), 'gtin' => '' !== $gtin, 'category' => $real_cat, 'stock' => $product->is_in_stock(), 'weight' => $is_virtual || '' !== (string) $product->get_weight(), 'attributes' => count( $product->get_attributes() ) >= 1, ); } public static function scan_catalog() { $ids = wc_get_products( array( 'status' => 'publish', 'limit' => self::MAX_PRODUCTS, 'return' => 'ids', 'type' => array( 'simple', 'variable', 'external', 'grouped' ), ) ); $checks = self::product_checks(); $passed = array_fill_keys( array_keys( $checks ), 0 ); $failing = array_fill_keys( array_keys( $checks ), array() ); $worst = array(); $total = count( $ids ); foreach ( $ids as $id ) { $product = wc_get_product( $id ); if ( ! $product ) { continue; } $res = self::check_product( $product ); $got = 0; $possible = 0; foreach ( $checks as $key => $def ) { $possible += $def[1]; if ( $res[ $key ] ) { $passed[ $key ]++; $got += $def[1]; } elseif ( count( $failing[ $key ] ) < 25 ) { $failing[ $key ][] = $id; } } $worst[ $id ] = $possible ? (int) round( 100 * $got / $possible ) : 0; } asort( $worst ); $worst = array_slice( $worst, 0, 10, true ); $score = 0; if ( $total > 0 ) { $weighted = 0; $max = 0; foreach ( $checks as $key => $def ) { $weighted += $def[1] * ( $passed[ $key ] / $total ); $max += $def[1]; } $score = (int) round( 100 * $weighted / $max ); } return array( 'score' => $score, 'product_count' => $total, 'passed' => $passed, 'failing' => $failing, 'worst' => $worst, ); } /* ------------------------------------------------------------------ */ /* Store setup */ /* ------------------------------------------------------------------ */ public static function store_checks() { return array( 'store_live' => array( __( 'Store is live (not in "Coming soon" mode)', 'agentrue' ), 3 ), 'https' => array( __( 'Store served over HTTPS', 'agentrue' ), 3 ), 'guest_checkout' => array( __( 'Guest checkout enabled (agents cannot create accounts)', 'agentrue' ), 3 ), 'no_captcha' => array( __( 'No CAPTCHA plugin blocking automated checkout', 'agentrue' ), 2 ), 'bots_allowed' => array( __( 'AI shopping bots allowed in robots.txt', 'agentrue' ), 3 ), 'feed' => array( __( 'Agent-ready product feed published', 'agentrue' ), 3 ), 'llms_txt' => array( __( 'llms.txt published', 'agentrue' ), 1 ), 'returns_page' => array( __( 'Refund & returns policy page', 'agentrue' ), 2 ), 'shipping' => array( __( 'Shipping zones configured', 'agentrue' ), 2 ), 'store_address' => array( __( 'Store address set', 'agentrue' ), 1 ), ); } public static function captcha_plugins() { $active = (array) get_option( 'active_plugins', array() ); $found = array(); foreach ( $active as $plugin ) { if ( preg_match( '/captcha|turnstile|recaptcha|hcaptcha/i', $plugin ) ) { $found[] = $plugin; } } return $found; } /** * Returns list of AI bots that are blocked by the effective robots.txt. */ public static function blocked_bots() { $robots = ''; $file = ABSPATH . 'robots.txt'; if ( file_exists( $file ) ) { $robots = (string) file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctionsfile_get_contents_file_get_contents } else { $public = (bool) get_option( 'blog_public' ); $robots = apply_filters( 'robots_txt', "User-agent: *\nDisallow: /wp-admin/\n", $public ); if ( ! $public ) { $robots = "User-agent: *\nDisallow: /\n"; } } return self::parse_blocked( $robots, self::ai_bots() ); } /** * Minimal robots.txt parser: a bot is "blocked" if the group that applies to it * (its own group, or * if none) contains "Disallow: /" without an "Allow: /". * * @param string $robots robots.txt content. * @param array $bots Bot names. */ public static function parse_blocked( $robots, array $bots ) { $groups = array(); // Each: array( 'agents' => [], 'rules' => [] ). $idx = -1; $in_rules = true; // Forces a new group on first user-agent line. foreach ( preg_split( '/\r?\n/', (string) $robots ) as $line ) { $line = trim( preg_replace( '/#.*$/', '', $line ) ); if ( '' === $line || false === strpos( $line, ':' ) ) { continue; } list( $field, $value ) = array_map( 'trim', explode( ':', $line, 2 ) ); $field = strtolower( $field ); if ( 'user-agent' === $field ) { if ( $in_rules ) { $groups[] = array( 'agents' => array(), 'rules' => array() ); $idx = count( $groups ) - 1; $in_rules = false; } $groups[ $idx ]['agents'][] = strtolower( $value ); } elseif ( ( 'allow' === $field || 'disallow' === $field ) && $idx >= 0 ) { $in_rules = true; $groups[ $idx ]['rules'][] = array( $field, $value ); } } $rules_by_agent = array(); foreach ( $groups as $g ) { foreach ( $g['agents'] as $agent ) { $rules_by_agent[ $agent ] = array_merge( isset( $rules_by_agent[ $agent ] ) ? $rules_by_agent[ $agent ] : array(), $g['rules'] ); } } $blocked = array(); foreach ( $bots as $bot ) { $key = strtolower( $bot ); if ( isset( $rules_by_agent[ $key ] ) ) { $rules = $rules_by_agent[ $key ]; } elseif ( isset( $rules_by_agent['*'] ) ) { $rules = $rules_by_agent['*']; } else { $rules = array(); } $dis = false; $allow = false; foreach ( $rules as $r ) { if ( '/' === $r[1] ) { if ( 'disallow' === $r[0] ) { $dis = true; } else { $allow = true; } } } if ( $dis && ! $allow ) { $blocked[] = $bot; } } return $blocked; } public static function scan_store() { $checks = self::store_checks(); $zones = class_exists( 'WC_Shipping_Zones' ) ? WC_Shipping_Zones::get_zones() : array(); $rr = (int) get_option( 'woocommerce_refund_returns_page_id' ); $res = array( 'store_live' => 'yes' !== get_option( 'woocommerce_coming_soon' ), 'https' => 0 === strpos( home_url(), 'https://' ), 'guest_checkout' => 'yes' === get_option( 'woocommerce_enable_guest_checkout' ), 'no_captcha' => empty( self::captcha_plugins() ), 'bots_allowed' => empty( self::blocked_bots() ), 'feed' => Agentrue_Settings::on( 'feed_enabled' ), 'llms_txt' => Agentrue_Settings::on( 'llms_txt_enabled' ), 'returns_page' => $rr > 0 && 'publish' === get_post_status( $rr ), 'shipping' => ! empty( $zones ), 'store_address' => '' !== (string) get_option( 'woocommerce_store_address' ), ); $got = 0; $max = 0; foreach ( $checks as $key => $def ) { $max += $def[1]; if ( $res[ $key ] ) { $got += $def[1]; } } return array( 'score' => $max ? (int) round( 100 * $got / $max ) : 0, 'results' => $res, 'blocked_bots' => self::blocked_bots(), 'captcha' => self::captcha_plugins(), ); } /* ------------------------------------------------------------------ */ /* Fix list */ /* ------------------------------------------------------------------ */ public static function build_fixes( $catalog, $store ) { $fixes = array(); $total = max( 1, (int) $catalog['product_count'] ); $help = array( 'store_live' => __( 'Your store is in "Coming soon" mode, so agents only see a placeholder page. Launch it in WooCommerce → Settings → Site visibility.', 'agentrue' ), 'https' => __( 'Install an SSL certificate (most hosts offer free Let\'s Encrypt) and update your site URL to https.', 'agentrue' ), 'guest_checkout' => __( 'WooCommerce → Settings → Accounts & Privacy → enable "Allow customers to place orders without an account".', 'agentrue' ), 'no_captcha' => __( 'CAPTCHA blocks agent checkout. Keep it on login/registration but remove it from checkout, or exclude Agentrue endpoints.', 'agentrue' ), 'bots_allowed' => __( 'Your robots.txt blocks AI shopping bots. Turn on "Allow AI shopping bots" in Agentrue settings, or edit your physical robots.txt file.', 'agentrue' ), 'feed' => __( 'Enable the agent product feed in Agentrue settings.', 'agentrue' ), 'llms_txt' => __( 'Enable llms.txt in Agentrue settings.', 'agentrue' ), 'returns_page' => __( 'Publish a refund & returns policy page and set it in WooCommerce → Settings → Advanced. Agents check return policies before buying.', 'agentrue' ), 'shipping' => __( 'Create at least one shipping zone with a method so agents can quote delivery cost.', 'agentrue' ), 'store_address' => __( 'Set your store address in WooCommerce → Settings → General.', 'agentrue' ), ); $store_checks = self::store_checks(); foreach ( $store['results'] as $key => $ok ) { if ( ! $ok ) { $fixes[] = array( 'area' => 'store', 'key' => $key, 'title' => $store_checks[ $key ][0], 'impact' => $store_checks[ $key ][1] >= 3 ? 'high' : ( $store_checks[ $key ][1] >= 2 ? 'medium' : 'low' ), 'help' => $help[ $key ], 'products' => array(), ); } } $product_checks = self::product_checks(); foreach ( $product_checks as $key => $def ) { $missing = $total - (int) $catalog['passed'][ $key ]; if ( $missing > 0 && $catalog['product_count'] > 0 ) { $fixes[] = array( 'area' => 'catalog', 'key' => $key, /* translators: 1: check label, 2: number of products. */ 'title' => sprintf( __( '%1$s — missing on %2$d product(s)', 'agentrue' ), $def[0], $missing ), 'impact' => ( $def[1] >= 3 && $missing / $total > 0.2 ) ? 'high' : ( $def[1] >= 2 ? 'medium' : 'low' ), 'help' => __( 'AI agents compare products using structured data. Missing fields mean your product is less likely to be picked.', 'agentrue' ), 'products' => $catalog['failing'][ $key ], ); } } $order = array( 'high' => 0, 'medium' => 1, 'low' => 2 ); usort( $fixes, function ( $a, $b ) use ( $order ) { return $order[ $a['impact'] ] - $order[ $b['impact'] ]; } ); return $fixes; } }