__( 'Google Search Console top queries / pages', 'ai-eshop-optimizer' ), 'description' => __( 'READ-ONLY, no AI credits. Returns the site’s real Google organic performance from Search Console: rows of query (or page) with clicks, impressions, CTR and average position, over the last N days. Params: dimension (query|page), days (default 90), limit (default 100). Use it to see what actually brings traffic, spot high-impression low-CTR queries (a title/description fix opportunity), and striking-distance pages. If Search Console is not connected it returns connected=false with a hint to connect it on SEO → Search Queries.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => array( 'default' => array(), 'type' => 'object', 'properties' => array( 'dimension' => array( 'type' => 'string', 'enum' => array( 'query', 'page' ), 'default' => 'query' ), 'days' => array( 'type' => 'integer', 'default' => 90, 'minimum' => 1, 'maximum' => 480 ), 'limit' => array( 'type' => 'integer', 'default' => 100, 'minimum' => 1, 'maximum' => 1000 ), ), ), '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_GSC' ) && defined( 'AIEO_DIR' ) ) { require_once AIEO_DIR . 'includes/seo/class-aieo-gsc.php'; } if ( ! class_exists( 'AIEO_GSC' ) ) { return new \WP_Error( 'aieo_unavailable', __( 'The Search Console connector is unavailable on this site.', 'ai-eshop-optimizer' ) ); } if ( ! \AIEO_GSC::is_connected() || '' === \AIEO_GSC::property() ) { return array( 'connected' => false, 'note' => __( 'Google Search Console is not connected yet. Connect it on SEO → Search Queries (a one-time Google OAuth), then this returns real query/page traffic.', 'ai-eshop-optimizer' ), ); } $dim = ( 'page' === ( $input['dimension'] ?? 'query' ) ) ? 'page' : 'query'; $days = min( 480, max( 1, (int) ( $input['days'] ?? 90 ) ) ); $limit = min( 1000, max( 1, (int) ( $input['limit'] ?? 100 ) ) ); $res = \AIEO_GSC::top_queries( $days, $limit, array( $dim ) ); if ( is_wp_error( $res ) ) { return $res; } $rows = array(); foreach ( (array) ( $res['rows'] ?? array() ) as $r ) { $rows[] = array( $dim => (string) ( $r['keys'][0] ?? '' ), 'clicks' => (int) $r['clicks'], 'impressions' => (int) $r['impressions'], 'ctr' => (float) $r['ctr'], 'position' => (float) $r['position'], ); } return array( 'connected' => true, 'property' => \AIEO_GSC::property(), 'dimension' => $dim, 'range' => $res['range'] ?? array(), 'returned' => count( $rows ), 'rows' => $rows, ); } }