array( 'Content-Type' => 'application/json', ), 'body' => json_encode(array( 'activationKey' => $activation_key, )) )); if (is_wp_error($response)) { wp_send_json_error(['message' => esc_html__('Unexpected error. Please contact support.', 'add-to-cart-ai')]); } $response_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $data = json_decode($body); if ($response_code == 201) { update_option('a2cai_store_id', $data->storeId); update_option('a2cai_widget_token', $data->widgetToken); update_option('a2cai_activation_key', wp_hash_password($activation_key)); wp_send_json_success(['message' => esc_html($data->message)]); } else { wp_send_json_error(['message' => esc_html($data->message)]); } } public function ajax_start_product_load() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce is verified, not stored. if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'a2cai_nonce')) { wp_send_json_error(esc_html__('Nonce verification failed', 'add-to-cart-ai')); return; } $store_id = intval(get_option('a2cai_store_id', '')); $product_batch_endpoint = get_rest_url(null, 'a2cai/v1/products/batch'); $start_product_load_endpoint = ACAI_API_BASE_URL . '/jobs/extraction'; $activation_key = isset($_POST['activation_key']) ? sanitize_text_field(wp_unslash($_POST['activation_key'])) : ''; if (empty($activation_key)) { wp_send_json_error(['message' => esc_html__('Missing activation key', 'add-to-cart-ai')]); return; } $domain = isset($_POST['domain']) ? sanitize_text_field(wp_unslash($_POST['domain'])) : ''; $wp_internal_url = defined('ACAI_WORDPRESS_INTERNAL_URL') ? ACAI_WORDPRESS_INTERNAL_URL : content_url(); $product_batch_endpoint = defined('ACAI_WORDPRESS_INTERNAL_URL') ? ACAI_WORDPRESS_INTERNAL_URL . '/wp-json/a2cai/v1/products/batch' : get_rest_url(null, 'a2cai/v1/products/batch'); $current_user_endpoint = defined('ACAI_WORDPRESS_INTERNAL_URL') ? ACAI_WORDPRESS_INTERNAL_URL . '/wp-json/a2cai/v1/current-user' : get_rest_url(null, 'a2cai/v1/current-user'); $response = wp_remote_post($start_product_load_endpoint, array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $activation_key ), 'body' => json_encode(array( 'domain' => $domain, 'cartUrl' => wc_get_cart_url(), 'loginUrl' => get_permalink(get_option('woocommerce_myaccount_page_id')), 'productBatchRetrievalUrl' => $product_batch_endpoint, 'productBatchEndpoint' => $product_batch_endpoint, 'currentUserEndpoint' => $current_user_endpoint, )), 'timeout' => 30 )); if (is_wp_error($response)) { wp_send_json_error(['message' => esc_html__('Unexpected error. Please contact support.', 'add-to-cart-ai')]); } $response_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if ($response_code == 201) { update_option('a2cai_setup_completed', true); $job_started_at = $data['jobStartedAt']; update_option('a2cai_sync_complete', false); update_option('a2cai_sync_job_started_at', $job_started_at); wp_send_json_success(); } else { wp_send_json_error(['message' => esc_html($data['message'] ?? __('An error occurred.', 'add-to-cart-ai'))]); } } public function ajax_reset_setup() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce is verified, not stored. if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'a2cai_nonce')) { wp_send_json_error(esc_html__('Nonce verification failed', 'add-to-cart-ai')); return; } update_option('a2cai_setup_completed', false); update_option('a2cai_store_id', ''); update_option('a2cai_activation_key', ''); update_option('a2cai_resume_at_step_if_interrupted', 0); wp_send_json_success(); } public function ajax_fetch_product_load_status() { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Nonce is verified, not stored. if (!isset($_POST['nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['nonce'])), 'a2cai_nonce')) { wp_send_json_error(esc_html__('Nonce verification failed', 'add-to-cart-ai')); return; } $store_id = get_option('a2cai_store_id', ''); $activation_key = get_option('a2cai_activation_key', ''); $status_endpoint = ACAI_API_BASE_URL . '/stores/' . $store_id . '/product-status-from-plugin'; $response = wp_remote_get($status_endpoint, array( 'headers' => array( 'Content-Type' => 'application/json', 'Authorization' => 'Bearer ' . $activation_key ), 'timeout' => 30 )); if (is_wp_error($response)) { wp_send_json_error(['message' => esc_html__('Unexpected error. Please contact support.', 'add-to-cart-ai')]); } $response_code = wp_remote_retrieve_response_code($response); $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if ($response_code == 200) { if ($data['status'] === 'completed') { update_option('a2cai_sync_complete', true); } wp_send_json_success(['status' => esc_html($data['status'])]); } else { wp_send_json_error([ 'message' => esc_html($data['message'] ?? __('An error occurred.', 'add-to-cart-ai')), 'store_id' => esc_html($store_id), 'endpoint' => esc_url($status_endpoint) ]); } } } new ACAI_AdminAjax();