'submit', 'aieo_product_request_add_item' => 'add_item', 'aieo_product_request_remove_item' => 'remove_item', 'aieo_product_request_state' => 'state', 'aieo_product_request_view' => 'record_view', 'aieo_product_request_finish' => 'finish', ); } public static function register() { static $done = false; if ( $done ) { return; } $done = true; foreach ( self::actions() as $action => $method ) { add_action( 'wp_ajax_' . $action, array( __CLASS__, $method ) ); add_action( 'wp_ajax_nopriv_' . $action, array( __CLASS__, $method ) ); } add_action( self::NOTIFY_HOOK, array( __CLASS__, 'run_notification' ), 10, 1 ); } public static function submit() { self::guard( 'submit', 8, 10 * MINUTE_IN_SECONDS, false ); if ( ! AIEO_Product_Request_Settings::frontend_is_active() ) { self::fail( __( 'Requests are not available right now.', 'ai-eshop-optimizer' ) ); } if ( ! empty( $_POST['aieo_hp'] ) ) { self::ok( array( 'message' => (string) AIEO_Product_Request_Settings::get()['form_success_message'] ) ); } $elapsed = isset( $_POST['elapsed'] ) ? (int) $_POST['elapsed'] : 999; if ( $elapsed < 2 ) { self::ok( array( 'message' => (string) AIEO_Product_Request_Settings::get()['form_success_message'] ) ); } $captcha = AIEO_Product_Request_Form::captcha_check(); if ( is_wp_error( $captcha ) ) { self::fail( $captcha->get_error_message() ); } $s = AIEO_Product_Request_Settings::get(); if ( 'yes' === (string) $s['field_consent'] && empty( $_POST['consent'] ) ) { self::fail( __( 'Please accept the privacy policy to continue.', 'ai-eshop-optimizer' ) ); } $open = AIEO_Product_Request_Store::open_request_for_token( AIEO_Product_Request_Store::resolve_token() ); $pid = isset( $_POST['product_id'] ) ? (int) $_POST['product_id'] : 0; $vids = self::posted_variations( $pid ); $qtys = self::posted_quantities( $pid ); $qty = isset( $_POST['qty'] ) ? (int) $_POST['qty'] : 1; if ( $open ) { if ( $pid > 0 ) { foreach ( $vids ? $vids : array( isset( $_POST['variation_id'] ) ? (int) $_POST['variation_id'] : 0 ) as $vid ) { AIEO_Product_Request_Store::add_item( $open, $pid, $vid, $qtys[ $vid ] ?? $qty ); } } AIEO_Product_Request_Store::mirror_cookie( (string) $open->token ); self::ok( array( 'message' => (string) $s['form_success_message'], 'token' => (string) $open->token, 'items' => AIEO_Product_Request_Store::state_for_token( $open ), ) ); } $result = AIEO_Product_Request_Store::create_request( array( 'email' => isset( $_POST['email'] ) ? wp_unslash( (string) $_POST['email'] ) : '', 'request_type' => isset( $_POST['request_type'] ) ? wp_unslash( (string) $_POST['request_type'] ) : '', 'product_id' => isset( $_POST['product_id'] ) ? (int) $_POST['product_id'] : 0, 'variation_id' => $vids ? $vids[0] : ( isset( $_POST['variation_id'] ) ? (int) $_POST['variation_id'] : 0 ), 'qty' => $vids ? ( $qtys[ $vids[0] ] ?? $qty ) : $qty, 'name' => isset( $_POST['name'] ) ? wp_unslash( (string) $_POST['name'] ) : '', 'phone' => isset( $_POST['phone'] ) ? wp_unslash( (string) $_POST['phone'] ) : '', 'message' => isset( $_POST['message'] ) ? wp_unslash( (string) $_POST['message'] ) : '', 'consent' => ! empty( $_POST['consent'] ), 'page_url' => self::referer(), 'seen_before' => isset( $_POST['seen_before'] ) ? substr( wp_unslash( (string) $_POST['seen_before'] ), 0, 4096 ) : '', 'seen_now' => isset( $_POST['seen_now'] ) ? (int) $_POST['seen_now'] : 0, ) ); if ( is_wp_error( $result ) ) { $fields = array( 'aieo_pr_phone' => 'phone', 'aieo_pr_email' => 'email' ); $code = (string) $result->get_error_code(); self::fail( $result->get_error_message(), isset( $fields[ $code ] ) ? array( 'field' => $fields[ $code ] ) : array() ); } if ( count( $vids ) > 1 ) { $created = AIEO_Product_Request_Store::open_request_for_token( $result['token'] ); foreach ( $created ? array_slice( $vids, 1 ) : array() as $vid ) { AIEO_Product_Request_Store::add_item( $created, $pid, $vid, $qtys[ $vid ] ?? $qty ); } } self::queue_notification( (int) $result['id'], $s ); AIEO_Product_Request_Store::mirror_cookie( $result['token'] ); $request = AIEO_Product_Request_Store::open_request_for_token( $result['token'] ); self::ok( array( 'message' => (string) $s['form_success_message'], 'token' => $result['token'], 'items' => $request ? AIEO_Product_Request_Store::state_for_token( $request ) : array(), ) ); } const MAX_POSTED = 200; public static function posted_variations( $product_id ) { return array_keys( self::posted_picks( $product_id ) ); } public static function posted_quantities( $product_id ) { return array_filter( self::posted_picks( $product_id ) ); } private static function posted_picks( $product_id ) { $raw = isset( $_POST['variations'] ) && is_scalar( $_POST['variations'] ) ? (string) wp_unslash( $_POST['variations'] ) : ''; if ( '' === $raw || (int) $product_id <= 0 ) { return array(); } $asked = array(); foreach ( array_slice( explode( ',', substr( $raw, 0, 8192 ) ), 0, 4 * self::MAX_POSTED ) as $entry ) { $parts = explode( ':', $entry, 2 ); $vid = absint( $parts[0] ); if ( $vid > 0 && ! isset( $asked[ $vid ] ) ) { $asked[ $vid ] = isset( $parts[1] ) ? max( 1, min( 100000, absint( $parts[1] ) ) ) : 0; } } if ( $asked && function_exists( '_prime_post_caches' ) ) { _prime_post_caches( array_keys( $asked ), false, false ); } $out = array(); foreach ( $asked as $vid => $quantity ) { if ( 'product_variation' === get_post_type( $vid ) && (int) wp_get_post_parent_id( $vid ) === (int) $product_id ) { $out[ (int) $vid ] = (int) $quantity; } } return array_slice( $out, 0, self::MAX_POSTED, true ); } public static function add_item() { self::guard( 'add', 40, 10 * MINUTE_IN_SECONDS ); $request = self::require_open_request(); $pid = isset( $_POST['product_id'] ) ? (int) $_POST['product_id'] : 0; $vids = self::posted_variations( $pid ); $qtys = self::posted_quantities( $pid ); $general = true; if ( ! empty( $_POST['sync'] ) ) { $had = array(); foreach ( AIEO_Product_Request_Store::items( (int) $request->id ) as $row ) { if ( (int) $row->product_id === $pid && (int) $row->variation_id > 0 ) { $had[ (int) $row->variation_id ] = (int) $row->qty; } } $plan = self::sync_plan( array_keys( $had ), $vids, $had, $qtys ); foreach ( $plan['remove'] as $vid ) { AIEO_Product_Request_Store::remove_item( $request, $pid, $vid ); } $vids = $plan['add']; $general = $plan['general']; } foreach ( $vids ? $vids : ( $general ? array( isset( $_POST['variation_id'] ) ? (int) $_POST['variation_id'] : 0 ) : array() ) as $vid ) { $res = AIEO_Product_Request_Store::add_item( $request, $pid, $vid, $qtys[ $vid ] ?? ( isset( $_POST['qty'] ) ? (int) $_POST['qty'] : 1 ) ); if ( is_wp_error( $res ) ) { self::fail( $res->get_error_message() ); } } AIEO_Product_Request_Store::mirror_cookie( (string) $request->token ); self::ok( array( 'items' => AIEO_Product_Request_Store::state_for_token( $request ) ) ); } public static function sync_plan( array $had, array $ticked, array $in = array(), array $asked = array() ) { $add = array_values( array_diff( $ticked, $had ) ); foreach ( array_intersect( $ticked, $had ) as $shade ) { if ( isset( $asked[ $shade ], $in[ $shade ] ) && (int) $asked[ $shade ] !== (int) $in[ $shade ] ) { $add[] = (int) $shade; } } return array( 'remove' => array_values( array_diff( $had, $ticked ) ), 'add' => $add, 'general' => ! $had, ); } public static function remove_item() { self::guard( 'add', 40, 10 * MINUTE_IN_SECONDS ); $request = self::require_open_request(); AIEO_Product_Request_Store::remove_item( $request, isset( $_POST['product_id'] ) ? (int) $_POST['product_id'] : 0, isset( $_POST['variation_id'] ) ? (int) $_POST['variation_id'] : 0 ); self::ok( array( 'items' => AIEO_Product_Request_Store::state_for_token( $request ) ) ); } public static function state() { self::guard( 'state', 60, 10 * MINUTE_IN_SECONDS ); $request = AIEO_Product_Request_Store::open_request_for_token( AIEO_Product_Request_Store::resolve_token() ); if ( ! $request ) { self::ok( array( 'open' => false, 'items' => array() ) ); } self::ok( array( 'open' => true, 'items' => AIEO_Product_Request_Store::state_for_token( $request ) ) ); } public static function record_view() { self::guard( 'view', 80, 10 * MINUTE_IN_SECONDS ); $request = AIEO_Product_Request_Store::open_request_for_token( AIEO_Product_Request_Store::resolve_token() ); if ( ! $request ) { self::ok( array( 'open' => false ) ); } AIEO_Product_Request_Store::record_view( $request, isset( $_POST['product_id'] ) ? (int) $_POST['product_id'] : 0 ); self::ok( array( 'open' => true ) ); } public static function finish() { self::guard( 'finish', 5, 10 * MINUTE_IN_SECONDS ); $s = AIEO_Product_Request_Settings::get(); if ( 'yes' !== (string) $s['finish_enabled'] ) { self::fail( __( 'That option is not available.', 'ai-eshop-optimizer' ) ); } $request = self::require_open_request(); if ( ! AIEO_Product_Request_Store::claim_summary( (int) $request->id ) ) { self::ok( array( 'message' => (string) $s['form_success_message'] ) ); } AIEO_Product_Request_Store::update_admin_fields( (int) $request->id, 'in_progress', (string) $request->admin_note ); do_action( 'aieo_product_request_sent', (int) $request->id, (string) ( $request->page_url ?? '' ), self::referer() ); if ( class_exists( 'AIEO_Product_Request_Mailer' ) ) { AIEO_Product_Request_Mailer::send( (int) $request->id, 'summary' ); } self::ok( array( 'message' => (string) $s['form_success_message'] ) ); } public static function queue_notification( $request_id, array $s ) { $delay = max( 0, min( 1440, (int) $s['notify_delay_minutes'] ) ) * MINUTE_IN_SECONDS; if ( function_exists( 'as_schedule_single_action' ) && $delay > 0 ) { as_schedule_single_action( time() + $delay, self::NOTIFY_HOOK, array( 'request_id' => (int) $request_id ), 'aieo' ); return 'as_scheduled'; } if ( function_exists( 'as_enqueue_async_action' ) ) { as_enqueue_async_action( self::NOTIFY_HOOK, array( 'request_id' => (int) $request_id ), 'aieo' ); return 'as_async'; } $when = time() + max( 1, $delay ); if ( false !== wp_schedule_single_event( $when, self::NOTIFY_HOOK, array( (int) $request_id ) ) ) { return 'cron'; } add_action( 'shutdown', static function () use ( $request_id ) { self::flush_response(); self::run_notification( (int) $request_id ); }, 99 ); return 'shutdown'; } public static function flush_response() { if ( function_exists( 'fastcgi_finish_request' ) ) { fastcgi_finish_request(); } elseif ( function_exists( 'litespeed_finish_request' ) ) { litespeed_finish_request(); } } public static function run_notification( $request_id = 0 ) { $request_id = (int) $request_id; if ( $request_id <= 0 ) { return; } if ( ! class_exists( 'AIEO_Product_Request_Mailer' ) ) { return; } AIEO_Product_Request_Mailer::send( $request_id, 'staff' ); AIEO_Product_Request_Mailer::send( $request_id, 'ack' ); } private static function guard( $bucket, $limit, $window, $token_scoped = true ) { if ( AIEO_Product_Request_Store::is_bot() ) { self::fail( __( 'Request refused.', 'ai-eshop-optimizer' ) ); } $key = ''; if ( $token_scoped ) { $request = AIEO_Product_Request_Store::open_request_for_token( AIEO_Product_Request_Store::resolve_token() ); $key = $request ? (string) $request->token : ''; } if ( ! AIEO_Product_Request_Store::rate_ok( $bucket, $limit, $window, $key ) ) { self::fail( __( 'Too many attempts. Please try again in a few minutes.', 'ai-eshop-optimizer' ) ); } } private static function require_open_request() { $request = AIEO_Product_Request_Store::open_request_for_token( AIEO_Product_Request_Store::resolve_token() ); if ( ! $request ) { self::fail( __( 'Your quote is no longer open. Please send a new request.', 'ai-eshop-optimizer' ), array( 'open' => false ) ); } return $request; } public static function verify_soft_nonce() { $n = isset( $_POST['aieo_pr_nonce'] ) ? sanitize_text_field( wp_unslash( (string) $_POST['aieo_pr_nonce'] ) ) : ''; return '' !== $n && (bool) wp_verify_nonce( $n, AIEO_Product_Request_Form::NONCE ); } private static function referer() { $r = (string) wp_get_referer(); if ( '' === $r && ! empty( $_SERVER['HTTP_REFERER'] ) ) { $r = esc_url_raw( wp_unslash( (string) $_SERVER['HTTP_REFERER'] ) ); } return substr( $r, 0, 512 ); } private static function ok( array $data = array() ) { wp_send_json_success( $data ); } private static function fail( $message, array $extra = array() ) { wp_send_json_error( array_merge( array( 'message' => (string) $message ), $extra ) ); } } }