$current_user && $current_user->display_name !== '' ? (string) $current_user->display_name : (string) ($current_user->user_login ?? ''), 'email' => (string) ($current_user->user_email ?? ''), 'phone' => '', 'subject' => '', ]; } function vasp_build_support_message_text(string $user_message, array $contact = []): string { $contact = array_merge(vasp_get_report_contact_defaults(), $contact); return implode("\n", array_filter([ 'ValueSpace WordPress plugin report', 'Please create or update a support ticket for this request.', '', '**Subject:** ' . ($contact['subject'] !== '' ? $contact['subject'] : 'WordPress plugin issue / feedback'), '**Name:** ' . $contact['name'], '**Email:** ' . $contact['email'], $contact['phone'] !== '' ? '**Phone:** ' . $contact['phone'] : null, '**Website name:** ' . get_bloginfo('name'), '**Site:** ' . home_url(), !empty($contact['privacy_consent']) ? '**Privacy consent:** Explicitly provided in the WordPress report form' : null, '**Sent at UTC:** ' . gmdate('c'), '', '**User message:**', $user_message, ], static function ($line) { return $line !== null; })); } /** @return array{boundary:string,body:string,filename:string} */ function vasp_build_issue_report_multipart_body(string $message, string $diagnostic_text, string $turnstile_token = ''): array { $boundary = '----ValueSpaceIssueReport' . wp_generate_password(24, false, false); $filename = 'valuespace-diagnostics-' . gmdate('Ymd\THis\Z') . '.json'; $parts = []; foreach ([ 'messageType' => 'wordpress_plugin_support_report', 'privacyConsent' => '1', 'companyFax' => '', 'sourceUrl' => home_url(), 'message' => $message, 'turnstileToken' => $turnstile_token, ] as $name => $value) { $parts[] = '--' . $boundary . "\r\n"; $parts[] = 'Content-Disposition: form-data; name="' . $name . '"' . "\r\n\r\n"; $parts[] = $value . "\r\n"; } $parts[] = '--' . $boundary . "\r\n"; $parts[] = 'Content-Disposition: form-data; name="attachments"; filename="' . $filename . '"' . "\r\n"; $parts[] = "Content-Type: application/json\r\n\r\n"; $parts[] = $diagnostic_text . "\r\n"; $parts[] = '--' . $boundary . "--\r\n"; return ['boundary' => $boundary, 'body' => implode('', $parts), 'filename' => $filename]; } /** * Send a validated administrator report through the shared HTTP boundary. * * @return true|WP_Error */ function vasp_send_issue_report_to_assistant(string $user_message, string $diagnostic_text, array $contact = [], string $turnstile_token = '') { $config = function_exists('vasp_get_config') ? vasp_get_config() : []; $endpoint_url = !empty($config['support_contact_form_endpoint']) ? (string) $config['support_contact_form_endpoint'] : (string) ($config['contact_form_endpoint'] ?? ''); $request_id = function_exists('wp_generate_uuid4') ? wp_generate_uuid4() : uniqid('vasp_report_', true); if ($endpoint_url === '') { vasp_log('Admin support message blocked: missing endpoint', 'error', ['request_id' => $request_id]); return new WP_Error('vasp_issue_report_missing_config', __('The support service is not configured.', 'valuespace')); } $formatted_message = vasp_build_support_message_text($user_message, $contact); if (vasp_report_text_length($formatted_message) > VASP_SUPPORT_MESSAGE_MAX_CHARS) { return new WP_Error('vasp_issue_report_message_too_long', __('The support message is too long.', 'valuespace')); } if ($diagnostic_text === '' || strlen($diagnostic_text) > 1024 * 1024) { return new WP_Error('vasp_issue_report_invalid_diagnostics', __('The diagnostic attachment could not be prepared.', 'valuespace')); } $multipart = vasp_build_issue_report_multipart_body($formatted_message, $diagnostic_text, $turnstile_token); vasp_log('Admin support message send started', 'info', [ 'request_id' => $request_id, 'diagnostic_size_bytes' => strlen($diagnostic_text), ]); if (!function_exists('vasp_http_request_url')) { return new WP_Error('vasp_issue_report_transport_unavailable', __('The support service is temporarily unavailable.', 'valuespace')); } $response = vasp_http_request_url($endpoint_url, [ 'method' => 'POST', 'headers' => [ 'Content-Type' => 'multipart/form-data; boundary=' . $multipart['boundary'], 'Accept' => 'application/json', ], 'body' => $multipart['body'], 'timeout' => 20, 'sslverify' => (bool) ($config['ssl_verify'] ?? true), ], 'raw'); if (is_wp_error($response)) { vasp_log('Admin support message send failed', 'error', [ 'request_id' => $request_id, 'error_code' => $response->get_error_code(), ]); return $response; } $status_code = (int) wp_remote_retrieve_response_code($response); if ($status_code < 200 || $status_code >= 300) { vasp_log('Admin support message rejected', 'error', [ 'request_id' => $request_id, 'status_code' => $status_code, ]); return new WP_Error( 'vasp_issue_report_gateway_rejected', __('The support service could not accept this message.', 'valuespace'), ['status' => $status_code] ); } vasp_log('Admin support message sent', 'info', [ 'request_id' => $request_id, 'status_code' => $status_code, ]); return true; }