__('Security check failed. Please refresh the page and try again.', 'valuespace'), ], 403); return; } if (!current_user_can('manage_options')) { wp_send_json_error(['message' => __('Insufficient permissions.', 'valuespace')], 403); return; } $is_update = array_key_exists('enabled', $_POST); if ($is_update) { $raw_enabled = is_scalar($_POST['enabled']) ? sanitize_text_field(wp_unslash((string) $_POST['enabled'])) : ''; if (!in_array($raw_enabled, ['0', '1'], true)) { wp_send_json_error(['message' => __('Invalid event collection setting.', 'valuespace')], 400); return; } $enabled = vasp_apply_customer_event_tracking_configuration($raw_enabled === '1'); $message = $enabled === true ? __('Event collection enabled.', 'valuespace') : __('Event collection disabled.', 'valuespace'); } else { $enabled = vasp_sync_customer_event_tracking_configuration(); $message = ''; } if (is_wp_error($enabled)) { wp_send_json_error([ 'message' => $is_update ? __('Event collection could not be updated. Please try again.', 'valuespace') : __('Event collection could not be loaded. Please try again.', 'valuespace'), ], 502); return; } wp_send_json_success([ 'enabled' => $enabled, 'message' => $message, ]); } /** * Return a manual-delivery result and preserve its standard notice for reload. */ function vasp_ajax_send_pending_events_response( string $message, bool $success, int $remaining_pending, int $status_code = 200, bool $queue_notice = true, string $error_code = '' ): void { if ($success && $queue_notice && function_exists('vasp_queue_admin_notice')) { vasp_queue_admin_notice( $message, $success ? 'success' : 'error', [], 5 * MINUTE_IN_SECONDS, $success ? 6 : 0 ); } $data = [ 'message' => $message, 'remainingPending' => max(0, $remaining_pending), ]; if (!$success && $error_code !== '') { $data['error'] = [ 'code' => sanitize_key($error_code), 'message' => $message, ]; } if ($success) { wp_send_json_success($data, $status_code); return; } wp_send_json_error($data, $status_code); } /** * Queue pending event delivery without running a worker in the admin request. * * @return void */ function vasp_ajax_send_pending_events(): void { if (!check_ajax_referer('vasp_events_admin_nonce', 'nonce', false)) { vasp_ajax_send_pending_events_response( __('Security check failed. Please refresh the page and try again.', 'valuespace'), false, 0, 403, false ); return; } if (!current_user_can('manage_options')) { vasp_ajax_send_pending_events_response(__('Insufficient permissions.', 'valuespace'), false, 0, 403, false); return; } $vasp_sync = vasp_event_get_sync_status(); $vasp_remaining = max(0, (int) ($vasp_sync['pending_count'] ?? 0)); if (!vasp_is_event_tracking_enabled()) { vasp_ajax_send_pending_events_response( __('Event delivery is not currently available.', 'valuespace'), false, $vasp_remaining, 409 ); return; } if ($vasp_remaining === 0) { vasp_ajax_send_pending_events_response( __('There are no pending events to send.', 'valuespace'), true, 0 ); return; } if (!vasp_event_schedule_delivery(0)) { vasp_ajax_send_pending_events_response( __('The waiting events could not be queued. Please try again.', 'valuespace'), false, $vasp_remaining, 500 ); return; } vasp_ajax_send_pending_events_response( __('Event delivery queued.', 'valuespace'), true, $vasp_remaining ); }