* * @updated 2.1.0 * Added the adscout_save_order_status_sync_settings method * */ class AdScout_Ajax { /** * Stops the request unless it carries a valid settings nonce AND comes from * a user allowed to open the settings page (manage_options). * * @return void * @since 2.5.2 */ private static function authorize(): void { if (!current_user_can('manage_options')) { wp_send_json_error([ 'message' => __('You are not allowed to change these settings', 'adscout') . '.', ], 403); } if (!array_key_exists('_wpnonce', $_POST) or !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['_wpnonce'])), 'adscout_settings')) { wp_send_json_error([ 'message' => __('Nonce is wrong or expired. Please, try again', 'adscout') . '.', ], 403); } } /** * Saves integration settings for AdScout. * Verifies nonce and required fields before saving the settings. * Retrieves and sets the API token and partner domain hex values. * Checks the validity of the token and updates the options accordingly. * Masks the API token and partner domain hex before sending a success response. * * @return void */ public static function adscout_save_integration_settings(): void { self::authorize(); if (!array_key_exists('api_token', $_POST) or !array_key_exists('adscout_partner_domain_hex', $_POST)) { wp_send_json_error([ 'message' => __('Missing required fields', 'adscout'), ], 400); } $data = [ 'api_token' => sanitize_text_field(wp_unslash($_POST['api_token'])), 'adscout_partner_domain_hex' => sanitize_text_field(wp_unslash($_POST['adscout_partner_domain_hex'])), ]; $options = (new AdScout_Options()); $all_options = $options::all_options(); $api_token = $data['api_token'] === '0' ? $all_options['adscout_api_token'] : sanitize_text_field($data['api_token']); $adscout_partner_domain_hex = $data['adscout_partner_domain_hex'] === '0' ? $all_options['adscout_partner_domain_hex'] : sanitize_text_field($data['adscout_partner_domain_hex']); $request = (new AdScout_Request())::get('partner_promocode_ref', array(), $api_token); if ($request and wp_remote_retrieve_response_code($request) == 401) { wp_send_json_error([ 'message' => __('Token is wrong or expired. Please, try again', 'adscout') . '.', ], 401); } $options::set_option('adscout_api_token', $api_token); $options::set_option('adscout_partner_domain_hex', $adscout_partner_domain_hex); $all_options['adscout_api_token'] = $api_token; $all_options['adscout_partner_domain_hex'] = $adscout_partner_domain_hex; $string = new AdScout_Settings(); wp_send_json_success([ 'message' => 'Settings saved', 'adscout_api_token' => $string->mask($all_options['adscout_api_token']), 'adscout_partner_domain_hex' => $string->mask($all_options['adscout_partner_domain_hex']), ]); wp_die(); } public static function adscout_save_base_url(): void { self::authorize(); if (!array_key_exists('base_url', $_POST)) { wp_send_json_error([ 'message' => __('Missing required fields', 'adscout'), ], 400); } $data = [ 'base_url' => isset($_POST['base_url']) ? esc_url_raw(wp_unslash($_POST['base_url'])) : '', ]; // The endpoint receives the API token and serves the frontend script, so only AdScout's own locations are allowed. if (!in_array($data['base_url'], array_column(AdScout_Settings::locations(), 'url'), true)) { wp_send_json_error([ 'message' => __('Unknown AdScout location', 'adscout'), ], 400); } $options = (new AdScout_Options()); $options::set_option('adscout_api_endpoint', $data['base_url']); wp_send_json_success([ 'message' => __('API Base URL saved', 'adscout'), ]); wp_die(); } public static function adscout_share_customer_data(): void { self::authorize(); if (!array_key_exists('adscout_share_order_client_data_toggle', $_POST)) { wp_send_json_error([ 'message' => __('Missing required fields', 'adscout'), ], 400); } $data = [ 'adscout_share_order_client_data' => isset($_POST['adscout_share_order_client_data_toggle']) ? sanitize_text_field(wp_unslash($_POST['adscout_share_order_client_data_toggle'])) : '', ]; $options = (new AdScout_Options()); $options::set_option('sync_user_data', boolval($data['adscout_share_order_client_data'])); wp_send_json_success([ 'message' => __('Order customer data settings saved successfully', 'adscout'), ]); wp_die(); } public static function adscout_generate_feed(): void { self::authorize(); $options = (new AdScout_Options())::all_options(); $feed = (new AdScout_WooCommerce())::generate_product_feed(); if ($feed) { wp_send_json_success([ 'message' => 'Feed generated', 'as_feed_last_updated' => $feed['as_feed_last_updated'], 'as_feed_url' => $feed['as_feed_url'], ]); } else { wp_send_json_error([ 'message' => 'Error generating feed', ]); } wp_die(); } /** * Which order statuses to sync with AdScout * * @return void * * @since 2.1.0 * * @updated 2.2.0 * Added a method to sync all orders statuses with AdScout when the settings are saved * * @updated 2.2.1 * Modified the error responses to include warnings and detailed feedback */ public static function adscout_save_order_status_sync_settings(): void { self::authorize(); if (!array_key_exists('as_order_statuses', $_POST) or !is_array($_POST['as_order_statuses']) or count($_POST['as_order_statuses']) < 1) { wp_send_json_error([ 'message' => 'No statuses selected', ], 400); } $data = [ 'as_order_statuses' => array_map('sanitize_text_field', wp_unslash($_POST['as_order_statuses'])), ]; $statuses_to_sync = ''; $statuses_not_synced = ''; foreach ($data['as_order_statuses'] as $status) { $statuses_to_sync .= $status . ','; if (!array_key_exists($status, wc_get_order_statuses())) { $statuses_not_synced .= $status . ','; } } $statuses_to_sync = rtrim($statuses_to_sync, ','); $options = (new AdScout_Options()); $options::set_option('adscout_order_statuses_to_sync', $statuses_to_sync); $last_synced_statuses = $options::get_option('order_status_synced_to_adscout'); $request = true; if ($last_synced_statuses !== wc_get_order_statuses()) { $request = (new AdScout_Request())::post('sync_order_statuses', wp_json_encode(array( 'statuses' => wc_get_order_statuses(), ))); if ($request and !is_wp_error($request) and wp_remote_retrieve_response_code($request) == 200) { $options::set_option('order_status_synced_to_adscout', wc_get_order_statuses()); $options::set_option('order_status_synced_to_adscout_timestamp', current_time('timestamp')); } } $message = 'Settings saved'; if ('' !== $statuses_not_synced or !$request) { $message .= '. Could not sync save ' . rtrim($statuses_not_synced, ',') . ' to order statuses. Allowed statuses via the Ajax call: ' . wp_json_encode(wc_get_order_statuses()); wp_send_json_error([ 'message' => $message, ], 409); } wp_send_json_success([ 'message' => $message, ]); wp_die(); } }