__( 'List content gaps from customer chat', 'ai-eshop-optimizer' ), 'description' => __( 'READ-ONLY, no AI credits. Returns the demand-weighted content gaps mined from the customer-facing chat: questions where the assistant found NO products (catalogue gaps) or the shopper rated the answer poorly (answer gaps). Each item has gap_key, a sample_question, occurrences, zero_product_turns, unhappy_turns, avg_rating, demand_weight, status, and (if an operator mapped it to a page) the mapped object + its live seo_score/open_fixes. Use it to find what content to CREATE or improve; then act with aieo/seo-apply-free-fixes (free) and the metered aieo/seo-fix-page / aieo/seo-fix-body. Params: limit, min_occurrences, only_unmapped, status (open|mapped|dismissed). Signals refresh on the AI Chat Analytics cadence.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'properties' => array( 'limit' => array( 'type' => 'integer', 'default' => 20, 'minimum' => 1, 'maximum' => 100 ), 'min_occurrences' => array( 'type' => 'integer', 'default' => 2, 'minimum' => 1 ), 'only_unmapped' => array( 'type' => 'boolean', 'default' => false ), 'status' => array( 'type' => 'string', 'enum' => array( 'open', 'mapped', 'dismissed' ), 'description' => 'Filter by triage status; omit for open+mapped.' ), ), ), 'output_schema' => array( 'type' => 'object' ), 'execute_callback' => array( __CLASS__, 'execute' ), 'permission_callback' => array( __CLASS__, 'permission_check' ), 'meta' => array( 'annotations' => array( 'readonly' => true, 'idempotent' => true ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( 'manage_woocommerce' ); } public static function execute( $input ) { $input = is_array( $input ) ? $input : array(); if ( ! class_exists( 'AIEO_SEO_Content_Gaps' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-seo-content-gaps.php'; } if ( ! class_exists( 'AIEO_SEO_Content_Gaps' ) ) { return new \WP_Error( 'aieo_unavailable', __( 'The content-gaps signal store is unavailable on this site.', 'ai-eshop-optimizer' ) ); } $rows = \AIEO_SEO_Content_Gaps::top( (int) ( $input['limit'] ?? 20 ), array( 'min_occurrences' => (int) ( $input['min_occurrences'] ?? 2 ), 'only_unmapped' => ! empty( $input['only_unmapped'] ), 'status' => isset( $input['status'] ) ? (string) $input['status'] : '', ) ); global $wpdb; $sc = $wpdb->prefix . 'aieo_seo_scores'; $items = array(); foreach ( $rows as $r ) { $item = array( 'gap_key' => (string) $r['gap_key'], 'sample_question' => (string) $r['sample_question'], 'occurrences' => (int) $r['occurrences'], 'zero_product_turns' => (int) $r['zero_product_turns'], 'unhappy_turns' => (int) $r['unhappy_turns'], 'avg_rating' => null === $r['avg_rating'] ? null : (float) $r['avg_rating'], 'demand_weight' => (float) $r['demand_weight'], 'status' => (string) $r['status'], 'first_seen' => (string) $r['first_seen'], 'last_seen' => (string) $r['last_seen'], ); if ( ! empty( $r['mapped_object_id'] ) && ! empty( $r['mapped_object_type'] ) ) { $oid = (int) $r['mapped_object_id']; $otype = (string) $r['mapped_object_type']; $item['mapped'] = array( 'object_type' => $otype, 'object_id' => $oid, 'title' => get_the_title( $oid ), 'url' => (string) get_permalink( $oid ), ); $score = $wpdb->get_row( $wpdb->prepare( "SELECT score, fix_count FROM `{$sc}` WHERE object_type = %s AND object_id = %d", $otype, $oid ), ARRAY_A ); if ( $score ) { $item['mapped']['seo_score'] = (int) $score['score']; $item['mapped']['open_fixes'] = (int) $score['fix_count']; } } $items[] = $item; } return array( 'returned' => count( $items ), 'items' => $items, 'note' => __( 'These are customer-driven content gaps (signal only). To act: for an unmapped gap, create or improve the best-matching page, then run aieo/seo-apply-free-fixes (free) and, if needed, the metered aieo/seo-fix-page / aieo/seo-fix-body. The front-end agent never edits the site itself.', 'ai-eshop-optimizer' ), ); } }