'string', 'pattern' => '^[0-9]{4}-[0-9]{2}-[0-9]{2}$', 'description' => $description, ); } private static function days( array $input, $defaults ) { $days = array(); foreach ( array( 'from_day', 'to_day' ) as $key ) { $days[ $key ] = ''; if ( ! array_key_exists( $key, $input ) ) { continue; } $raw = is_string( $input[ $key ] ) ? trim( $input[ $key ] ) : ''; if ( ! preg_match( '/^(\d{4})-(\d{2})-(\d{2})$/', $raw, $m ) || ! checkdate( (int) $m[2], (int) $m[3], (int) $m[1] ) ) { return new \WP_Error( 'aieo_bad_input', sprintf( __( '%s is not a day: give it as Y-m-d, for example 2026-11-15.', 'ai-eshop-optimizer' ), $key ), array( 'field' => $key ) ); } $days[ $key ] = $raw; } if ( $defaults ) { if ( '' === $days['to_day'] ) { $days['to_day'] = (string) wp_date( 'Y-m-d' ); } if ( '' === $days['from_day'] ) { $days['from_day'] = gmdate( 'Y-m-d', (int) strtotime( $days['to_day'] . ' 12:00:00 UTC' ) - 29 * DAY_IN_SECONDS ); } } if ( '' !== $days['from_day'] && '' !== $days['to_day'] && $days['from_day'] > $days['to_day'] ) { return new \WP_Error( 'aieo_bad_input', __( 'The first day (from_day) comes after the last (to_day).', 'ai-eshop-optimizer' ), array( 'field' => 'from_day' ) ); } return array( $days['from_day'], $days['to_day'] ); } private static function refusal( $message, $field ) { return new \WP_Error( 'aieo_bad_input', (string) $message, array( 'field' => (string) $field ) ); } private static function reports() { return class_exists( 'AIEO_DMM_Offer_Reports' ) ? true : new \WP_Error( 'aieo_unavailable', __( 'The Rooster One offers\' measurement is not loaded.', 'ai-eshop-optimizer' ) ); } } class ReadOfferResultsAbility { use OfferReadShared; const ABILITY_NAME = 'aieo/read-offer-results'; const MIN = 30; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Read an offer\'s results', 'ai-eshop-optimizer' ), 'description' => __( 'Read-only. The results of one offer (offer_id) or one campaign (campaign_id) over a period of days, Y-m-d in the shop\'s time zone, both included (left out: the last 30 days through today), from the daily rollups of the offers\' measurement: exposures, the people by state with each rate and the count it is of, the order lines and reductions, the offer\'s own revenue, the orders it touched and the shop\'s orders of the same days. For an offer also its rates alone and in company with each other offer met by at least min people (30 unless given), by place where the shop has it (else null), and its holdout\'s two groups. Each answer says whether the measurement is on (on) and when the rollups last ran (as_of, GMT). Requires the Rooster One plugin.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => self::input_schema(), 'output_schema' => self::output_schema(), '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 input_schema() { return array( 'default' => array(), 'type' => 'object', 'additionalProperties' => false, 'properties' => array( 'offer_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'The offer whose results to read. Name it or campaign_id, not both.', ), 'campaign_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'The campaign whose results to read (its offers together, each order once). Name it or offer_id, not both.', ), 'from_day' => self::day_schema( 'The first day, Y-m-d in the shop\'s time zone, included. Left out: 29 days before to_day (30 days in all).' ), 'to_day' => self::day_schema( 'The last day, Y-m-d in the shop\'s time zone, included. Left out: today in the shop\'s time zone.' ), 'min' => array( 'type' => 'integer', 'minimum' => 1, 'default' => self::MIN, 'description' => 'offer_id only (a campaign has no rates in company): the fewest people another offer must have met with this one before its rate in company is given; fewer is "not enough yet". Default 30.', ), ), ); } public static function output_schema() { return array( 'type' => 'object', 'required' => array( 'results' ), 'properties' => array( 'results' => array( 'type' => 'object', 'description' => 'on, as_of, offer_id or campaign_id, period, exposure, people (by state, and all), rates (each with rate, of, n), ledger, own, touched, shop.', ), 'in_company' => array( 'type' => 'object', 'description' => 'Offers only: on, as_of, offer_id, period, alone, company, with (by the other offer, each with rate, of, n, or enough false), min.', ), 'by_place' => array( 'type' => array( 'object', 'null' ), 'description' => 'Offers only: the results by place; null where the shop does not have them yet.', ), 'holdout' => array( 'type' => 'object', 'description' => 'Offers only: on, as_of, offer_id, ran, treated and held (people, bought, share), difference, too_few.', ), ), ); } public static function execute( $input = null ) { $input = is_array( $input ) ? $input : array(); $offer = (int) ( $input['offer_id'] ?? 0 ); $campaign = (int) ( $input['campaign_id'] ?? 0 ); if ( ( $offer > 0 ) === ( $campaign > 0 ) ) { return self::refusal( __( 'Name one offer (offer_id) or one campaign (campaign_id), and not both.', 'ai-eshop-optimizer' ), $offer > 0 ? 'campaign_id' : 'offer_id' ); } $days = self::days( $input, true ); if ( is_wp_error( $days ) ) { return $days; } $ready = self::reports(); if ( is_wp_error( $ready ) ) { return $ready; } list( $from, $to ) = $days; if ( $campaign > 0 ) { return array( 'results' => \AIEO_DMM_Offer_Reports::for_campaign( $campaign, $from, $to ) ); } $min = isset( $input['min'] ) ? max( 1, (int) $input['min'] ) : self::MIN; return array( 'results' => \AIEO_DMM_Offer_Reports::for_offer( $offer, $from, $to ), 'in_company' => \AIEO_DMM_Offer_Reports::in_company( $offer, $from, $to, $min ), 'by_place' => method_exists( 'AIEO_DMM_Offer_Reports', 'by_place' ) ? \AIEO_DMM_Offer_Reports::by_place( $offer, $from, $to ) : null, 'holdout' => \AIEO_DMM_Offer_Reports::holdout( $offer ), ); } } class ReadResponsesAbility { use OfferReadShared; const ABILITY_NAME = 'aieo/read-responses'; const AT_ONCE = 500; const PER_PAGE = 100; const MAX_PER_PAGE = 500; const COLUMNS = array( 'id' => 'integer', 'user_id' => 'integer', 'offer_id' => 'integer', 'seq' => 'integer', 'campaign_id' => 'integer', 'state' => 'string', 'progress' => 'number', 'opened_by' => 'string', 'opened_gmt' => array( 'string', 'null' ), 'seen_gmt' => array( 'string', 'null' ), 'closed_gmt' => array( 'string', 'null' ), 'closed_by' => 'string', 'order_id' => 'integer', 'visits' => 'integer', 'holdout' => 'integer', 'came_through' => 'string', ); const PEOPLE = array( 'user_id', 'user_ids', 'audience_id' ); const OFFERS = array( 'offer_id', 'campaign_id' ); public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Read the responses to offers', 'ai-eshop-optimizer' ), 'description' => __( 'Read-only. The responses to offers, one row per person and run of an offer (its state, how it opened and closed, the order, visits, holdout, the place it came through), a page at a time, with the total and the count of each state over the whole filter. Name an offer (offer_id) or a campaign (campaign_id); or a customer (user_id), customers (user_ids, at most 500) or a saved audience (audience_id: its members with an account, 500 at a time; pass audience.next_after back as audience_after for the next 500); or one of these three with an offer or a campaign. Narrow by state and by the days a response opened. A row names its customer by user_id only (0 for a visitor without an account): never an e-mail or a visitor\'s key. Requires the Rooster One plugin.', 'ai-eshop-optimizer' ), 'category' => \AIEO\Claude\Bootstrap::CATEGORY, 'input_schema' => self::input_schema(), 'output_schema' => self::output_schema(), '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 states() { return class_exists( 'AIEO_DMM_Offer_Responses' ) ? array_keys( \AIEO_DMM_Offer_Responses::RANKS ) : array( 'exposed', 'engaged', 'progressed', 'declined', 'bought_anyway', 'converted' ); } public static function input_schema() { return array( 'default' => array(), 'type' => 'object', 'additionalProperties' => false, 'properties' => array( 'offer_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'The responses to this offer. Not with campaign_id.', ), 'campaign_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'The responses to this campaign\'s offers. Not with offer_id.', ), 'user_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'One customer\'s responses, by their account\'s user id.', ), 'user_ids' => array( 'type' => 'array', 'items' => array( 'type' => 'integer', 'minimum' => 1, ), 'description' => 'Several customers\' responses, by user id: at most 500 different ids, else the call answers too_many.', ), 'audience_id' => array( 'type' => 'integer', 'minimum' => 1, 'description' => 'The responses of a saved audience\'s members, 500 members at a time: those with an account by user id, guest members by their address\'s hash (never the address itself).', ), 'audience_after' => array( 'type' => 'integer', 'minimum' => 0, 'description' => 'audience_id only: the next 500 members after this member, as the previous answer\'s audience.next_after gives it. Left out or 0: the first 500.', ), 'state' => array( 'type' => 'string', 'enum' => self::states(), 'description' => 'Only the responses in this state.', ), 'from_day' => self::day_schema( 'Only the responses opened on or after this day, Y-m-d in the shop\'s time zone. Left out: from the first.' ), 'to_day' => self::day_schema( 'Only the responses opened on or before this day, Y-m-d in the shop\'s time zone. Left out: through the last.' ), 'page' => array( 'type' => 'integer', 'minimum' => 1, 'default' => 1, 'description' => 'The page of rows, from 1.', ), 'per_page' => array( 'type' => 'integer', 'minimum' => 1, 'maximum' => self::MAX_PER_PAGE, 'default' => self::PER_PAGE, 'description' => 'Rows a page, at most 500. Default 100.', ), ), ); } public static function output_schema() { return array( 'type' => 'object', 'required' => array( 'on', 'as_of', 'total', 'by_state', 'rows' ), 'properties' => array( 'on' => array( 'type' => 'boolean', 'description' => 'Whether the offers\' measurement is on; off, every count is 0.' ), 'as_of' => array( 'type' => 'string', 'description' => 'When the rollups last ran (GMT); empty before the first.' ), 'total' => array( 'type' => 'integer', 'description' => 'The responses of the whole filter, every page.' ), 'by_state' => array( 'type' => 'object', 'description' => 'The responses of the whole filter by state: all six states, zeros included.' ), 'rows' => array( 'type' => 'array', 'items' => array( 'type' => 'object', 'additionalProperties' => false, 'properties' => array_map( static function ( $type ) { return array( 'type' => $type ); }, self::COLUMNS ), ), 'description' => 'This page: id, user_id (0 for a visitor), offer_id, seq, campaign_id, order_id, visits and holdout (integers), state, opened_by, closed_by and came_through (strings), progress (a number, 0 to 1), opened_gmt, seen_gmt and closed_gmt (GMT, or null). Never a person key or an e-mail.', ), 'page' => array( 'type' => 'integer' ), 'per_page' => array( 'type' => 'integer' ), 'audience' => array( 'type' => 'object', 'description' => 'audience_id only: id, name, members (the members read in this call), with_account and guests (of those), unreadable (members with neither an account nor an address), after, next_after (the audience_after of the next 500; 0 when these are the last).', ), 'note' => array( 'type' => 'string' ), ), ); } public static function execute( $input = null ) { $input = is_array( $input ) ? $input : array(); $offers = array_values( array_intersect( self::OFFERS, array_keys( $input ) ) ); $people = array_values( array_intersect( self::PEOPLE, array_keys( $input ) ) ); if ( count( $offers ) > 1 ) { return self::refusal( __( 'Name an offer (offer_id) or a campaign (campaign_id), not both.', 'ai-eshop-optimizer' ), 'campaign_id' ); } if ( count( $people ) > 1 ) { return self::refusal( __( 'Name one customer (user_id), a list of customers (user_ids) or one saved audience (audience_id), not two of them.', 'ai-eshop-optimizer' ), $people[1] ); } if ( ! $offers && ! $people ) { return self::refusal( __( 'Name whose responses to read: an offer (offer_id), a campaign (campaign_id), a customer (user_id), customers (user_ids) or a saved audience (audience_id).', 'ai-eshop-optimizer' ), 'offer_id' ); } if ( ! empty( $input['audience_after'] ) && ! array_key_exists( 'audience_id', $input ) ) { return self::refusal( __( 'audience_after reads a saved audience\'s next members: name the audience (audience_id).', 'ai-eshop-optimizer' ), 'audience_after' ); } $days = self::days( $input, false ); if ( is_wp_error( $days ) ) { return $days; } $ready = self::reports(); if ( is_wp_error( $ready ) ) { return $ready; } $filter = array(); foreach ( array( 'offer_id', 'campaign_id', 'user_id' ) as $key ) { if ( isset( $input[ $key ] ) ) { $filter[ $key ] = (int) $input[ $key ]; } } if ( isset( $input['user_ids'] ) ) { $filter['user_ids'] = array_map( 'intval', (array) $input['user_ids'] ); } $audience = null; if ( isset( $input['audience_id'] ) ) { $audience = self::audience_part( (int) $input['audience_id'], (int) ( $input['audience_after'] ?? 0 ) ); if ( is_wp_error( $audience ) ) { return $audience; } $filter['user_ids'] = $audience['user_ids']; $filter['email_hashes'] = $audience['email_hashes']; } if ( isset( $input['state'] ) ) { $filter['state'] = (string) $input['state']; } if ( '' !== $days[0] ) { $filter['from_day'] = $days[0]; } if ( '' !== $days[1] ) { $filter['to_day'] = $days[1]; } $filter['page'] = max( 1, (int) ( $input['page'] ?? 1 ) ); $filter['per_page'] = max( 1, min( self::MAX_PER_PAGE, (int) ( $input['per_page'] ?? self::PER_PAGE ) ) ); $answer = \AIEO_DMM_Offer_Reports::responses( $filter ); if ( ! empty( $answer['error'] ) ) { if ( 'too_many' === $answer['error'] ) { return new \WP_Error( 'too_many', sprintf( __( 'Responses can be read for at most %d customers at once: split the list.', 'ai-eshop-optimizer' ), self::AT_ONCE ), array( 'field' => 'user_ids', 'limit' => self::AT_ONCE ) ); } return new \WP_Error( 'aieo_' . sanitize_key( (string) $answer['error'] ), (string) $answer['error'] ); } $rows = array_map( array( __CLASS__, 'row' ), (array) ( $answer['rows'] ?? array() ) ); $out = array( 'on' => (bool) ( $answer['on'] ?? false ), 'as_of' => (string) ( $answer['as_of'] ?? '' ), 'total' => (int) ( $answer['total'] ?? 0 ), 'by_state' => (array) ( $answer['by_state'] ?? array() ), 'rows' => $rows, 'page' => $filter['page'], 'per_page' => $filter['per_page'], ); if ( null !== $audience ) { $out['audience'] = $audience['audience']; if ( $audience['audience']['unreadable'] > 0 ) { $out['note'] = __( 'A member with neither an account nor an address cannot be read: no response names them.', 'ai-eshop-optimizer' ); } } return $out; } private static function row( $row ) { $row = (array) $row; $out = array(); foreach ( self::COLUMNS as $column => $type ) { if ( ! array_key_exists( $column, $row ) ) { continue; } $value = $row[ $column ]; if ( 'integer' === $type ) { $value = (int) $value; } elseif ( 'number' === $type ) { $value = (float) $value; } elseif ( null !== $value ) { $value = (string) $value; } $out[ $column ] = $value; } return $out; } private static function audience_part( $id, $after ) { if ( ! class_exists( 'AIEO_Audience_Rules' ) ) { $file = dirname( __DIR__, 2 ) . '/class-aieo-audience-rules.php'; if ( is_readable( $file ) ) { require_once $file; } } if ( ! class_exists( 'AIEO_Audience_Rules' ) || ! function_exists( 'aieo_audiences_table' ) ) { return new \WP_Error( 'aieo_unavailable', __( 'The saved audiences are not loaded.', 'ai-eshop-optimizer' ) ); } $found = self::saved_audience( (int) $id ); if ( null === $found || ! is_array( $found['rule'] ?? null ) ) { return self::refusal( sprintf( __( 'No saved audience has the id %d.', 'ai-eshop-optimizer' ), (int) $id ), 'audience_id' ); } $people = (array) \AIEO_Audience_Rules::members( $found['rule'], max( 0, (int) $after ), self::AT_ONCE + 1 ); $more = count( $people ) > self::AT_ONCE; $people = array_slice( $people, 0, self::AT_ONCE ); $last = $people ? end( $people ) : array(); $next = $more ? (int) ( $last['eponymous_id'] ?? 0 ) : 0; $ids = array(); $hashes = array(); $none = 0; foreach ( $people as $person ) { $user = (int) ( $person['wp_user_id'] ?? 0 ); $email = strtolower( trim( (string) ( $person['email'] ?? '' ) ) ); if ( $user > 0 ) { $ids[ $user ] = $user; } elseif ( '' !== $email ) { $hashes[ hash( 'sha256', $email ) ] = true; } else { ++$none; } } return array( 'user_ids' => array_values( $ids ), 'email_hashes' => array_keys( $hashes ), 'audience' => array( 'id' => (int) $id, 'name' => (string) ( $found['name'] ?? '' ), 'members' => count( $people ), 'with_account' => count( $ids ), 'guests' => count( $hashes ), 'unreadable' => $none, 'after' => max( 0, (int) $after ), 'next_after' => $next, ), ); } private static function saved_audience( $id ) { global $wpdb; $table = aieo_audiences_table(); if ( (string) $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ) !== $table ) { return null; } $row = $wpdb->get_row( $wpdb->prepare( "SELECT id, name, rule_json, count_cached FROM `{$table}` WHERE id = %d", (int) $id ), ARRAY_A ); if ( ! is_array( $row ) ) { return null; } return array( 'id' => (int) $row['id'], 'name' => (string) $row['name'], 'rule' => json_decode( (string) $row['rule_json'], true ), 'count_cached' => (int) $row['count_cached'], ); } }