(int) ($state['placement_revision'] ?? 0), 'enabled' => !empty($state['widget_enabled']), 'intent_revision' => (int) ($state['placement_intent_revision'] ?? 0), 'operation_id' => $state['placement_operation'] ?? null, 'origin' => $state['placement_origin'] ?? null, 'observe_existing' => !empty($state['placement_observe_existing'])]; } public static function capability(): array { $state = Chat4U_Protocol::connection(); return ['version' => 1, 'grant_version' => (int) ($state['pending']['placement_grant'] ?? $state['placement_grant'] ?? 0), 'state' => self::snapshot($state)]; } private static function compare_and_set(array $before, array $after): bool { global $wpdb; $changed = $wpdb->query($wpdb->prepare('UPDATE %i SET option_value = %s WHERE option_name = %s AND option_value = %s', $wpdb->options, maybe_serialize($after), 'chat4u_connection', maybe_serialize($before))); wp_cache_delete('chat4u_connection', 'options'); wp_cache_delete('alloptions', 'options'); return $changed === 1; } public static function control(WP_REST_Request $request) { $state = Chat4U_Protocol::connection(); if (($state['placement_grant'] ?? 0) !== 1 || ($state['state'] ?? '') !== 'connected') { return new WP_Error('chat4u_placement_permission_required', __('Confirm the new setup controls from WordPress.', 'chat4u'), ['status' => 409]); } if ($request->get_method() === 'GET') { return Chat4U_Protocol::response($request, self::snapshot($state)); } $operation = $request->get_param('operation_id'); $enabled = $request->get_param('enabled'); $expected = $request->get_param('expected_revision'); $intent = $request->get_param('intent_revision'); $observe = $request->get_param('observe_existing') ?? false; if (!is_string($operation) || !preg_match('/^[a-f0-9]{8}-(?:[a-f0-9]{4}-){3}[a-f0-9]{12}$/Di', $operation) || !is_bool($enabled) || !is_int($expected) || $expected < 0 || !is_int($intent) || $intent < 1 || !is_bool($observe)) { return new WP_Error('chat4u_placement_invalid', __('Review the website display choice in Chat4U.', 'chat4u'), ['status' => 422]); } $receipts = $state['placement_receipts'] ?? []; $command_hash = hash('sha256', wp_json_encode([$enabled, $expected, $intent, $observe])); if (isset($receipts[$operation])) { if (($receipts[$operation]['command_hash'] ?? '') !== $command_hash) { return new WP_Error('chat4u_placement_changed', __('Review the saved display choice in Chat4U.', 'chat4u'), ['status' => 409]); } return Chat4U_Protocol::response($request, self::snapshot($state) + ['original_receipt' => $receipts[$operation]]); } if ($expected !== (int) ($state['placement_revision'] ?? 0)) { return Chat4U_Protocol::response($request, self::snapshot($state) + ['conflict' => true]); } $next = $state; $next['widget_enabled'] = $enabled; $next['placement_revision'] = $expected + 1; $next['placement_intent_revision'] = $intent; $next['placement_operation'] = $operation; $next['placement_origin'] = 'chat4u'; $next['placement_observe_existing'] = $observe; $receipts[$operation] = self::snapshot($next) + ['command_hash' => $command_hash]; $next['placement_receipts'] = array_slice($receipts, -100, null, true); if (!self::compare_and_set($state, $next)) { return Chat4U_Protocol::response($request, self::snapshot() + ['conflict' => true]); } self::invalidate_home(); return Chat4U_Protocol::response($request, self::snapshot($next)); } public static function local_save(bool $enabled): bool { for ($attempt = 0; $attempt < 3; $attempt++) { $state = Chat4U_Protocol::connection(); if (($state['state'] ?? '') !== 'connected') { return false; } $next = $state; $next['widget_enabled'] = $enabled; $next['placement_revision'] = (int) ($state['placement_revision'] ?? 0) + 1; $next['placement_origin'] = 'wordpress'; $next['placement_operation'] = wp_generate_uuid4(); $next['placement_observe_existing'] = false; if (self::compare_and_set($state, $next)) { update_option('chat4u_placement_delivery', ['delivery_id' => wp_generate_uuid4(), 'placement' => self::snapshot($next)], false); wp_schedule_single_event(time() + 1, 'chat4u_deliver_placement'); self::invalidate_home(); return true; } } return false; } public static function deliver(): void { $delivery = get_option('chat4u_placement_delivery', []); $state = Chat4U_Protocol::connection(); if (!$delivery || ($state['state'] ?? '') !== 'connected') { return; } $result = Chat4U_Protocol::remote('events', ['installation_id' => $state['scope'], 'topic' => 'visibility', 'removed' => [], 'delivery_id' => $delivery['delivery_id'], 'placement' => $delivery['placement']], $state); if (!is_wp_error($result)) { if (get_option('chat4u_placement_delivery') === $delivery) { delete_option('chat4u_placement_delivery'); } } else { wp_schedule_single_event(time() + 60, 'chat4u_deliver_placement'); } } public static function public_state(): WP_REST_Response { $state = Chat4U_Protocol::connection(); $connected = ($state['state'] ?? '') === 'connected' && ($state['site_url'] ?? '') === untrailingslashit(get_option('home')); $value = self::snapshot($state); $response = new WP_REST_Response($connected ? $value + ['installation_id' => $state['scope'], 'generation' => $state['generation']] : ['enabled' => false]); $response->header('Cache-Control', 'no-store, max-age=0'); return $response; } private static function invalidate_home(): void { // Only the affected public homepage; never flush unrelated site/CDN data. $front = (int) get_option('page_on_front'); if ($front) { clean_post_cache($front); } do_action('litespeed_purge_url', home_url('/')); if (function_exists('rocket_clean_files')) { rocket_clean_files([home_url('/')]); } } }