array( 'aieo_dmm_brand_discounts', 'brand_discounts' ), 'gift' => array( 'aieo_dmm_brand_gift_offers', 'brand_gifts' ), 'free_shipping' => array( '', 'free_shipping' ), ); const BY = 'generator'; const SOURCE_KIND = 'ability'; const REASON = 'the ability aieo/draft-offer'; public static function register() { wp_register_ability( self::ABILITY_NAME, array( 'label' => __( 'Draft an offer', 'ai-eshop-optimizer' ), 'description' => __( 'WRITES A DRAFT. Saves ONE new offer (a discount on brands, categories or needs; a gift offer; or a free-shipping rule) as a draft, switched off, and returns its id, its state (always draft) and the statement of what it would do. Nothing changes in the shop: a person activates the draft on its screen (the returned screen). A state, status or enabled other than a draft is refused, and so is an id: it never activates an offer and never changes an existing one. When it answers needs_confirmation, tell the person its message and call again with confirmed true only if they agree. 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' => false, 'idempotent' => false, 'destructive' => false ), 'show_in_rest' => true, ), ) ); } public static function permission_check( $input = null ) { unset( $input ); return current_user_can( defined( 'AIEO_CAPABILITY' ) ? AIEO_CAPABILITY : 'manage_options' ); } public static function input_schema() { $ids = static function ( $description ) { return array( 'type' => 'array', 'items' => array( 'type' => 'integer', 'minimum' => 1, ), 'description' => $description, ); }; return array( 'default' => array(), 'type' => 'object', 'required' => array( 'kind', 'label' ), 'properties' => array( 'kind' => array( 'type' => 'string', 'enum' => array_keys( self::KINDS ), 'description' => 'What the offer is: discount (a percentage off the matching products), gift (a gift product for a cart that qualifies) or free_shipping.', ), 'label' => array( 'type' => 'string', 'description' => 'The name the offer is listed under on its screen (191 characters at most).', ), 'percent' => array( 'type' => 'number', 'description' => 'discount only, required there: the percentage off, above 0 and under 100.', ), 'gift_products' => $ids( 'gift only, required there: the ids of the products the customer chooses the gift from.' ), 'brands' => $ids( 'Brand term ids (a discount: one at most).' ), 'categories' => $ids( 'Product category term ids.' ), 'needs' => $ids( 'Need term ids.' ), 'products' => $ids( 'gift and free_shipping: product ids (a variation counts as its product). Beside brands, categories or needs, the cart must also hold one of them.' ), 'min_subtotal' => array( 'type' => 'number', 'description' => 'gift and free_shipping: the subtotal the cart must reach (a gift counts the matching products, free shipping the whole cart).', ), 'start_date' => array( 'type' => 'string', 'description' => 'The first day, Y-m-d in the shop\'s time zone; left out: from when a person activates it.', ), 'end_date' => array( 'type' => 'string', 'description' => 'The last day, Y-m-d in the shop\'s time zone, included; left out: no end.', ), 'confirmed' => array( 'type' => 'boolean', 'description' => 'The person\'s yes to the question a previous call answered with needs_confirmation (for example: an equal offer already runs). Only after asking them: the answer\'s question says what was asked.', ), ), ); } public static function output_schema() { return array( 'type' => 'object', 'required' => array( 'id', 'state', 'enabled', 'statement' ), 'properties' => array( 'id' => array( 'type' => 'integer', 'description' => 'The offer\'s id.' ), 'state' => array( 'type' => 'string', 'description' => 'Its state as the offers table holds it: draft.' ), 'enabled' => array( 'type' => 'boolean', 'description' => 'Whether it is switched on: false.' ), 'kind' => array( 'type' => 'string' ), 'label' => array( 'type' => 'string' ), 'revision' => array( 'type' => 'integer' ), 'statement' => array( 'type' => 'string', 'description' => 'What the offer would do, in words (the writer\'s preview); empty when there is nothing to say.' ), 'question' => array( 'type' => 'string', 'description' => 'The question the preview asked and confirmed answered; empty when it asked none.' ), 'screen' => array( 'type' => 'string', 'description' => 'The admin screen where a person activates it.' ), 'note' => array( 'type' => 'string' ), ), ); } public static function execute( $input = null ) { global $wpdb; $input = is_array( $input ) ? $input : array(); foreach ( array( 'state', 'status' ) as $key ) { if ( array_key_exists( $key, $input ) && 'draft' !== strtolower( trim( is_scalar( $input[ $key ] ) ? (string) $input[ $key ] : '' ) ) ) { return self::refusal( 'aieo_draft_only', __( 'This ability saves an offer as a draft, switched off, and nothing else. A person activates it on its screen.', 'ai-eshop-optimizer' ), $key ); } } if ( ! empty( $input['enabled'] ) ) { return self::refusal( 'aieo_draft_only', __( 'This ability saves an offer as a draft, switched off, and nothing else. A person activates it on its screen.', 'ai-eshop-optimizer' ), 'enabled' ); } if ( ! empty( $input['id'] ) ) { return self::refusal( 'aieo_new_offer_only', __( 'This ability makes a new draft. It never changes an existing offer: a person edits that on its screen.', 'ai-eshop-optimizer' ), 'id' ); } $kind = isset( $input['kind'] ) && is_string( $input['kind'] ) ? $input['kind'] : ''; if ( ! isset( self::KINDS[ $kind ] ) ) { return self::refusal( 'invalid_offer', 'The offer is of a kind this ability does not draft.', 'kind' ); } if ( ! self::load() ) { return new \WP_Error( 'aieo_unavailable', __( 'The Rooster One offers are not loaded.', 'ai-eshop-optimizer' ) ); } $named = array(); foreach ( array( 'brands' => 'brand', 'categories' => 'category', 'needs' => 'need', 'products' => 'product', 'gift_products' => 'product' ) as $field => $what ) { $named[ $field ] = self::ids( $input, $field, $what ); if ( is_wp_error( $named[ $field ] ) ) { return $named[ $field ]; } } list( $brands, $categories, $needs, $products, $gifts ) = array_values( $named ); if ( $products && class_exists( 'AIEO_DMM_Product_Picker' ) ) { $products = \AIEO_DMM_Product_Picker::roll_up_to_parents( $products ); } $minimum = isset( $input['min_subtotal'] ) && is_numeric( $input['min_subtotal'] ) ? max( 0.0, (float) $input['min_subtotal'] ) : 0.0; $label = isset( $input['label'] ) && is_scalar( $input['label'] ) ? sanitize_text_field( (string) $input['label'] ) : ''; $from = self::day( $input['start_date'] ?? '' ); $to = self::day( $input['end_date'] ?? '' ); if ( 'discount' !== $kind && array_key_exists( 'percent', $input ) ) { return self::refusal( 'aieo_bad_input', __( 'Only a discount takes a percentage.', 'ai-eshop-optimizer' ), 'percent' ); } if ( 'gift' !== $kind && array_key_exists( 'gift_products', $input ) ) { return self::refusal( 'aieo_bad_input', __( 'Only a gift offer takes gift products.', 'ai-eshop-optimizer' ), 'gift_products' ); } if ( 'discount' === $kind ) { if ( $products || $minimum > 0 ) { return self::refusal( 'aieo_bad_input', __( 'A discount does not read the cart yet: it takes no products and no minimum subtotal. Name brands, categories or needs.', 'ai-eshop-optimizer' ), $products ? 'products' : 'min_subtotal' ); } if ( count( $brands ) > 1 ) { return self::refusal( 'aieo_bad_input', __( 'A discount names one brand at most: draft one discount for each brand.', 'ai-eshop-optimizer' ), 'brands' ); } } $lists = array( 'category_term_ids' => (string) wp_json_encode( $categories ), 'need_term_ids' => (string) wp_json_encode( $needs ), 'product_ids' => (string) wp_json_encode( $products ), 'excluded_brand_ids' => '[]', 'excluded_category_ids' => '[]', 'excluded_need_ids' => '[]', 'excluded_product_ids' => '[]', 'code_patterns' => '[]', 'excluded_code_patterns' => '[]', ); if ( 'discount' === $kind ) { $offer = \AIEO_DMM_Offer_Store::row_from_legacy( self::KINDS['discount'][0], array( 'brand_term_id' => $brands ? $brands[0] : 0, 'rule_operator' => 'AND', 'percent' => isset( $input['percent'] ) && is_numeric( $input['percent'] ) ? (float) $input['percent'] : 0.0, 'date_from' => $from, 'date_to' => $to, 'enabled' => 0, 'notes' => '', 'label' => $label, 'min_subtotal' => 0.0, 'min_subtotal_scope' => 'cart_total', 'application_scope' => 'targeted', ) + $lists ); } elseif ( 'gift' === $kind ) { $offer = \AIEO_DMM_Offer_Store::row_from_legacy( self::KINDS['gift'][0], array( 'enabled' => 0, 'label' => $label, 'brand_term_ids' => (string) wp_json_encode( $brands ), 'rule_operator' => 'AND', 'min_subtotal' => $minimum, 'date_from' => $from, 'date_to' => $to, 'gift_product_ids' => (string) wp_json_encode( $gifts ), 'gift_kind' => 'product', 'presentation_mode' => 'auto', 'show_when_pending' => 1, ) + $lists ); } else { $offer = array( 'type' => 'free_shipping', 'enabled' => 0, 'label' => $label, 'priority' => 100, 'rule_operator' => 'AND', 'brand_term_ids' => (string) wp_json_encode( $brands ), 'min_subtotal' => $minimum, 'min_subtotal_scope' => 'cart_total', 'application_scope' => 'whole_cart', 'payload' => (string) wp_json_encode( array( 'action' => 'force_free' ) ), 'display' => '{}', 'start_date' => $from, 'end_date' => $to, ) + $lists; } $offer['source_kind'] = self::SOURCE_KIND; $preview = \AIEO_DMM_Offer_Writer::preview( $offer ); if ( ! empty( $preview['asks'] ) && empty( $input['confirmed'] ) ) { return new \WP_Error( 'needs_confirmation', (string) $preview['question'], array( 'statement' => (string) $preview['question'], 'question' => (string) $preview['question'], 'preview' => $preview, ) ); } $saved = \AIEO_DMM_Offer_Writer::save( $offer, array( 'state' => 'draft', 'by' => self::BY, 'reason' => self::REASON, 'confirmed' => true, ) ); if ( is_wp_error( $saved ) ) { return $saved; } $row = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->prefix}aieo_dmm_offers WHERE id = %d", (int) $saved['id'] ), ARRAY_A ); $row = is_array( $row ) ? $row : array(); $state = class_exists( 'AIEO_DMM_Schedule' ) && $row ? \AIEO_DMM_Schedule::state_of( $row ) : (string) ( $row['state'] ?? '' ); return array( 'id' => (int) $saved['id'], 'state' => $state, 'enabled' => ! empty( $row['enabled'] ), 'kind' => $kind, 'label' => (string) ( $row['label'] ?? $label ), 'revision' => (int) ( $row['revision'] ?? $saved['revision'] ), 'statement' => (string) $preview['statement'], 'question' => ! empty( $preview['asks'] ) ? (string) $preview['question'] : '', 'screen' => admin_url( 'admin.php?page=' . ( class_exists( 'AIEO_DMM' ) ? \AIEO_DMM::MENU_SLUG : 'ai-eshop-dmm' ) . '&dmm_tab=' . self::KINDS[ $kind ][1] ), 'note' => __( 'Saved as a draft, switched off: nothing changes in the shop until a person activates it on its screen.', 'ai-eshop-optimizer' ), ); } private static function refusal( $code, $message, $field ) { return new \WP_Error( (string) $code, (string) $message, array( 'field' => (string) $field ) ); } private static function ids( array $input, $field, $what ) { $list = array_key_exists( $field, $input ) ? $input[ $field ] : array(); if ( ! is_array( $list ) ) { $list = array( $list ); } $out = array(); foreach ( $list as $value ) { $id = is_int( $value ) || ( is_string( $value ) && ctype_digit( $value ) ) ? (int) $value : 0; if ( $id <= 0 || ! self::names_a( $what, $id ) ) { return self::refusal( 'aieo_bad_input', sprintf( __( 'The id %2$s in %1$s names nothing of that kind in this shop.', 'ai-eshop-optimizer' ), $field, is_scalar( $value ) ? (string) $value : gettype( $value ) ), $field ); } $out[ $id ] = $id; } return array_values( $out ); } private static function names_a( $what, $id ) { if ( 'product' === $what ) { $post = get_post( $id ); if ( ! $post || 'publish' !== $post->post_status ) { return false; } if ( 'product_variation' === $post->post_type ) { $parent = get_post( (int) $post->post_parent ); return $parent && 'product' === $parent->post_type && 'publish' === $parent->post_status; } return 'product' === $post->post_type; } $taxonomies = array( 'brand' => class_exists( 'AIEO_DMM_Brand_Discounts' ) && \AIEO_DMM_Brand_Discounts::get_brand_taxonomy() ? (string) \AIEO_DMM_Brand_Discounts::get_brand_taxonomy() : 'product_brand', 'category' => 'product_cat', 'need' => 'pa_need', ); $term = isset( $taxonomies[ $what ] ) && taxonomy_exists( $taxonomies[ $what ] ) ? get_term( $id, $taxonomies[ $what ] ) : null; return $term instanceof \WP_Term; } private static function day( $raw ) { $raw = is_scalar( $raw ) ? trim( (string) $raw ) : ''; return '' === $raw ? null : $raw; } private static function load() { $dir = dirname( __DIR__, 2 ) . '/dmm/services/'; foreach ( array( 'AIEO_DMM_Offer_Matcher' => 'matcher', 'AIEO_DMM_Offer_Registry' => 'registry', 'AIEO_DMM_Offer_Conditions' => 'conditions', 'AIEO_DMM_Offer_Definition' => 'definition', 'AIEO_DMM_Offer_Store' => 'store', 'AIEO_DMM_Offer_Targets' => 'targets', 'AIEO_DMM_Offer_Writer' => 'writer' ) as $class => $file ) { if ( ! class_exists( $class, false ) ) { if ( ! is_readable( $dir . 'class-aieo-dmm-offer-' . $file . '.php' ) ) { return false; } require_once $dir . 'class-aieo-dmm-offer-' . $file . '.php'; } } return true; } }