__( 'List Google Ads campaign plans', 'ai-eshop-optimizer' ), 'description' => __( 'READ-ONLY, no AI credits. Lists the store\'s Google Ads campaign plans: name, status with a human label, objective (sales or leads), daily budget and currency, what the plan was built from, when it was created/pushed/activated, and a summary of its ad groups (headline and keyword counts, landing page). IMPORTANT for how you answer: a plan is created in Google Ads PAUSED and is NEVER auto-enabled — statuses are draft (nothing in Google yet), created (in Google, paused, spending nothing), activated (a person turned it on there), failed or archived. This ability only reports; it cannot compose, push, activate or archive a plan, and nothing it does can spend money. Params: status (optional filter), limit (default 50). Point the merchant to e-shop Manager → Ads to act.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => self::input_schema(), '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' ); } private static function input_schema() { return array( 'default' => array(), 'type' => 'object', 'properties' => array( 'status' => array( 'type' => 'string', 'enum' => array( 'draft', 'created', 'activated', 'failed', 'archived' ), 'description' => 'Optional status filter. Omit to get every plan except archived ones.', ), 'limit' => array( 'type' => 'integer', 'default' => 50, 'minimum' => 1, 'maximum' => 200, 'description' => 'How many plans to return, newest first. Defaults to 50.', ), ), ); } public static function execute( $input = null ) { global $wpdb; if ( ! class_exists( 'AIEO_Ads_Plan' ) ) { return array( 'returned' => 0, 'plans' => array(), 'note' => __( 'The Google Ads feature is not available on this site.', 'ai-eshop-optimizer' ), ); } $table = \AIEO_Ads_Plan::table(); if ( ! $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $table ) ) ) { return array( 'returned' => 0, 'plans' => array(), 'note' => __( 'No Google Ads campaign plans have been composed on this site yet.', 'ai-eshop-optimizer' ), ); } $input = is_array( $input ) ? $input : array(); $limit = isset( $input['limit'] ) ? (int) $input['limit'] : 50; $limit = max( 1, min( 200, $limit ) ); $status = isset( $input['status'] ) ? sanitize_key( (string) $input['status'] ) : ''; if ( ! in_array( $status, array( 'draft', 'created', 'activated', 'failed', 'archived' ), true ) ) { $status = ''; } $rows = (array) \AIEO_Ads_Plan::all( $status, $limit ); $plans = array(); foreach ( $rows as $row ) { if ( ! is_array( $row ) ) { continue; } $row_status = isset( $row['status'] ) ? (string) $row['status'] : ''; $plan = ( isset( $row['plan'] ) && is_array( $row['plan'] ) ) ? $row['plan'] : array(); $plans[] = array( 'id' => isset( $row['id'] ) ? (int) $row['id'] : 0, 'name' => self::shorten( isset( $row['name'] ) ? (string) $row['name'] : '' ), 'status' => $row_status, 'status_label' => \AIEO_Ads_Plan::status_label( $row_status ), 'in_google' => in_array( $row_status, array( \AIEO_Ads_Plan::STATUS_CREATED, \AIEO_Ads_Plan::STATUS_ACTIVATED ), true ), 'spending' => ( \AIEO_Ads_Plan::STATUS_ACTIVATED === $row_status ), 'objective' => isset( $row['objective'] ) ? (string) $row['objective'] : '', 'platform' => isset( $row['platform'] ) ? (string) $row['platform'] : '', 'daily_budget' => isset( $row['daily_budget'] ) ? (float) $row['daily_budget'] : 0.0, 'currency' => isset( $row['currency'] ) ? (string) $row['currency'] : '', 'source_type' => isset( $row['source_type'] ) ? (string) $row['source_type'] : '', 'source_id' => isset( $row['source_id'] ) ? (int) $row['source_id'] : 0, 'external_campaign_id' => isset( $row['external_campaign_id'] ) ? (string) $row['external_campaign_id'] : '', 'created_at' => isset( $row['created_at'] ) ? (string) $row['created_at'] : '', 'pushed_at' => isset( $row['pushed_at'] ) ? (string) $row['pushed_at'] : '', 'activated_at' => isset( $row['activated_at'] ) ? (string) $row['activated_at'] : '', 'last_error' => self::shorten( isset( $row['last_error'] ) ? (string) $row['last_error'] : '', 300 ), 'final_url' => isset( $plan['final_url'] ) ? (string) $plan['final_url'] : '', 'ad_groups' => self::ad_group_summary( $plan ), ); } return array( 'returned' => count( $plans ), 'plans' => $plans, 'note' => __( 'Plans are created in Google Ads PAUSED and are never enabled automatically — only a person can turn one on, in Google. A plan in status "created" exists there and is spending nothing. This report changes nothing; composing, pushing and archiving happen in e-shop Manager → Ads.', 'ai-eshop-optimizer' ), ); } private static function ad_group_summary( array $plan ) { if ( empty( $plan['ad_groups'] ) || ! is_array( $plan['ad_groups'] ) ) { return array(); } $out = array(); foreach ( $plan['ad_groups'] as $group ) { if ( ! is_array( $group ) ) { continue; } $out[] = array( 'name' => self::shorten( isset( $group['name'] ) ? (string) $group['name'] : '' ), 'final_url' => isset( $group['final_url'] ) ? (string) $group['final_url'] : '', 'headlines' => ( isset( $group['headlines'] ) && is_array( $group['headlines'] ) ) ? count( $group['headlines'] ) : 0, 'descriptions' => ( isset( $group['descriptions'] ) && is_array( $group['descriptions'] ) ) ? count( $group['descriptions'] ) : 0, 'keywords' => ( isset( $group['keywords'] ) && is_array( $group['keywords'] ) ) ? count( $group['keywords'] ) : 0, ); } return $out; } private static function shorten( $text, $max = 120 ) { $text = trim( (string) $text ); if ( '' === $text ) { return ''; } if ( function_exists( 'mb_strlen' ) && mb_strlen( $text ) > $max ) { return mb_substr( $text, 0, $max ) . '…'; } return $text; } }