__('This action requires a POST request.', 'valuespace')], 405); return; } try { // Verify nonce check_ajax_referer('vasp_admin_nonce', 'nonce'); // Validate admin permissions if (!current_user_can('manage_options')) { vasp_log('AJAX: ValueSpace integration denied - insufficient permissions', 'warning'); wp_send_json_error([ 'message' => __('Insufficient permissions. Only administrators can connect.', 'valuespace') ]); return; } vasp_log('AJAX: ValueSpace integration requested', 'info'); // A terminal refresh failure retains credentials. Let an explicit // reconnect reuse the bootstrap flow without resetting integration state. if (vasp_valuespace_is_connected() && !vasp_get_token_refresh_blocked_error()) { wp_send_json_error([ 'message' => __('Already connected to ValueSpace.', 'valuespace') ]); return; } // Remove credentials left by plugin versions that used a shared SSO secret. delete_option(VASP_OPTION_SSO_SECRET); delete_option(VASP_OPTION_SSO_SECRET_ESTABLISHED_AT); // Generate BOOTSTRAP SSO token (unsigned, for initial connection) // Explicitly pass true to force bootstrap mode $sso_token = vasp_generate_sso_token(true); if (!$sso_token) { vasp_log('AJAX: ValueSpace bootstrap token generation failed', 'error', [ 'user_id' => get_current_user_id(), ]); wp_send_json_error([ 'message' => __('Technical issue while preparing the ValueSpace connection. Please try again later.', 'valuespace'), 'hint' => __('If the issue continues, send a report from the System Info page.', 'valuespace'), 'code' => 'sso_token_generation_failed', ]); } // Build Dashboard SSO URL for bootstrap connection $config = vasp_get_config(); $dashboard_domain = $config['dashboard_domain'] ?? ''; if (empty($dashboard_domain)) { vasp_log('AJAX: ValueSpace dashboard domain missing from config', 'error'); wp_send_json_error([ 'message' => __('Technical issue while preparing the ValueSpace connection. Please try again later.', 'valuespace'), 'hint' => __('Dashboard URL is not configured for this plugin package.', 'valuespace'), 'code' => 'dashboard_domain_missing', ]); } $dashboard_url = $dashboard_domain . '/auth/wp-sso?' . http_build_query([ 'token' => $sso_token ]); delete_transient(VASP_TRANSIENT_ADMIN_NOTICE); vasp_log('AJAX: ValueSpace SSO connection initiated', 'info', [ 'user_id' => get_current_user_id(), 'environment' => function_exists('vasp_get_environment') ? vasp_get_environment() : '', 'ajax_action' => 'vasp_integrate_valuespace', 'redirect_method' => 'browser_navigation_after_ajax_success', ]); wp_send_json_success([ 'redirect_url' => $dashboard_url, 'message' => __('Connecting to ValueSpace...', 'valuespace') ]); } catch (Throwable $e) { vasp_log('AJAX: ValueSpace integration failed with exception', 'error', [ 'exception' => get_class($e), ]); wp_send_json_error([ 'message' => __('Technical issue while preparing the ValueSpace connection. Please try again later.', 'valuespace'), 'hint' => __('If the issue continues, send a report from the System Info page.', 'valuespace'), 'code' => 'connect_exception', ]); } } /** * Handle the WordPress site's widget display toggle. * * @since 1.0.12 * @return void */ function vasp_ajax_toggle_widget_site_visibility() { if (($_SERVER['REQUEST_METHOD'] ?? '') !== 'POST') { wp_send_json_error(['message' => __('Invalid request method.', 'valuespace')], 405); return; } // Verify nonce check_ajax_referer('vasp_admin_nonce', 'nonce'); // Validate admin permissions if (!current_user_can('manage_options')) { vasp_log('Site widget visibility change denied due to insufficient permissions', 'warning'); wp_send_json_error([ 'message' => __('Insufficient permissions. Only administrators can perform this action.', 'valuespace') ]); } // Retain the persisted published/draft values as local display preferences. $status = isset($_POST['status']) ? vasp_normalize_site_widget_visibility_status(wp_unslash($_POST['status'])) : ''; if ($status === '') { vasp_log('Site widget visibility change rejected due to invalid status', 'warning'); wp_send_json_error([ 'message' => __('An error occurred. Please try again.', 'valuespace'), 'code' => 'vasp_site_widget_visibility_invalid', 'integration' => 'valuespace', ]); return; } $response = vasp_set_site_widget_visibility_status($status); if (is_wp_error($response)) { $user_error = vasp_map_error_to_user_friendly( vasp_error_technical_code($response), $response->get_error_message(), 'valuespace' ); wp_send_json_error([ 'message' => vasp_get_integration_display_name('valuespace') . ': ' . $user_error['description'], 'hint' => $user_error['hint'], 'code' => vasp_error_technical_code($response), 'integration' => 'valuespace' ]); return; } $status = $response; wp_send_json_success([ 'message' => __('Chat widget display updated for this website.', 'valuespace'), 'status' => $status ]); } // ============================================= // Deactivation Feedback // ============================================= /** * Receive deactivation feedback from the modal survey. * * Fire-and-forget: returns success immediately so the browser * can proceed with plugin deactivation regardless of API outcome. * * @since 0.9.2 * @return void */ function vasp_ajax_submit_deactivation_reason() { // Verify nonce check_ajax_referer('vasp_deactivation_nonce', 'nonce'); // Validate admin permissions if (!current_user_can('manage_options')) { wp_send_json_error(['message' => __('Insufficient permissions.', 'valuespace')]); return; } $reason = isset($_POST['reason']) && is_string($_POST['reason']) ? sanitize_text_field(wp_unslash($_POST['reason'])) : ''; $details = isset($_POST['details']) && is_string($_POST['details']) ? sanitize_textarea_field(wp_unslash($_POST['details'])) : ''; vasp_log('Deactivation feedback received', 'info'); // Transmit to ValueSpace Gateway (non-blocking — ignore errors) if (!empty($reason) && function_exists('vasp_api_request')) { $payload = [ 'reason' => $reason, 'details' => $details, 'pluginVersion' => VASP_VERSION, ]; $api_result = vasp_api_request('plugin_deactivation_feedback_endpoint', 'POST', $payload); if (is_wp_error($api_result)) { vasp_log('Deactivation feedback: failed to transmit to Gateway', 'warning', [ 'error' => $api_result->get_error_message(), ]); } else { vasp_log('Deactivation feedback: transmitted to Gateway successfully', 'debug'); } } // Always return success — do not block deactivation on API outcome wp_send_json_success(['message' => __('Feedback received.', 'valuespace')]); }