*/ function vasp_diagnostics_credential_state(): array { $access_expires_at = (int) get_transient(VASP_TRANSIENT_TOKEN_EXPIRES_AT); $access_token = get_transient(VASP_TRANSIENT_ACCESS_TOKEN); $refresh_token = get_option(defined('VASP_OPTION_REFRESH_TOKEN') ? VASP_OPTION_REFRESH_TOKEN : 'vasp_refresh_token', false); return [ 'has_access_token' => is_string($access_token) && $access_token !== '', 'has_refresh_token' => is_string($refresh_token) && $refresh_token !== '', 'access_expires_at' => $access_expires_at, 'access_expired' => $access_expires_at > 0 ? $access_expires_at <= time() : null, ]; } /** Return the bounded identity of one persisted error for support reports. */ function vasp_diagnostics_stored_error(string $option): ?array { if ($option === '') { return null; } $raw = get_option($option, ''); $error = is_string($raw) && $raw !== '' ? json_decode($raw, true) : null; if (!is_array($error)) { return null; } return [ 'code' => substr(sanitize_key((string) ($error['technical_code'] ?? '')), 0, 64), 'category' => substr(sanitize_key((string) ($error['category'] ?? '')), 0, 64), 'operation' => substr(sanitize_key((string) ($error['step'] ?? '')), 0, 64), 'recorded_at' => max(0, (int) ($error['timestamp'] ?? 0)), ]; } /** Count rows in a plugin-owned table without exposing table names or contents. */ function vasp_diagnostics_table_count(string $vasp_table): ?int { global $wpdb; $vasp_allowed_tables = []; foreach (['vasp_wc_catalog_hash_table', 'vasp_wc_catalog_queue_table'] as $vasp_owner) { if (function_exists($vasp_owner)) { $vasp_allowed_tables[] = $vasp_owner(); } } if (!in_array($vasp_table, $vasp_allowed_tables, true)) { return null; } $vasp_count = $wpdb->get_var($wpdb->prepare('SELECT COUNT(*) FROM %i', $vasp_table)); return $wpdb->last_error === '' && is_numeric($vasp_count) ? max(0, (int) $vasp_count) : null; } /** Explain only recorded failure facts; never display upstream response text. */ function vasp_diagnostics_feed_error_reason(string $vasp_code, array $vasp_details, string $vasp_integration = 'catalog_sync'): string { if ($vasp_code === '') { return ''; } $vasp_display = vasp_format_error_for_display([ 'technical_code' => $vasp_code, 'integration' => $vasp_integration, 'step' => sanitize_key((string) ($vasp_details['step'] ?? '')), 'error_details' => $vasp_details, ], 'system_info'); return is_array($vasp_display) ? (string) ($vasp_display['description'] ?? '') : ''; } /** Build the one nontechnical feed story shared by administrator surfaces. */ function vasp_diagnostics_feed_presentation(string $vasp_name, array $vasp_feed): array { $vasp_status = (string) ($vasp_feed['status'] ?? 'idle'); $vasp_stage = (string) ($vasp_feed['stage'] ?? ''); $vasp_catalog_product_scan = $vasp_name === 'woocommerce_catalog' && $vasp_stage === 'product_check' && (int) ($vasp_feed['product_scopes_total'] ?? 0) > 0; $vasp_catalog_product_send = $vasp_name === 'woocommerce_catalog' && $vasp_stage === 'products' && (int) ($vasp_feed['product_send_total'] ?? 0) > 0; $vasp_image_scan = $vasp_name === 'woocommerce_images' && $vasp_stage === 'image_check' && (int) ($vasp_feed['image_scan_total'] ?? 0) > 0; $vasp_remaining = $vasp_name === 'woocommerce_catalog' ? ($vasp_catalog_product_scan ? max(0, (int) ($vasp_feed['product_scopes_remaining'] ?? 0)) : ($vasp_catalog_product_send ? max(0, (int) ($vasp_feed['product_send_total'] ?? 0) - (int) ($vasp_feed['product_send_processed'] ?? 0)) : max(0, (int) ($vasp_feed['catalog_packets'] ?? 0)))) : ($vasp_name === 'woocommerce_images' ? ($vasp_image_scan ? max(0, (int) ($vasp_feed['image_scan_total'] ?? 0) - (int) ($vasp_feed['image_scan_processed'] ?? 0)) : max(0, (int) ($vasp_feed['image_references'] ?? ($vasp_feed['image_records'] ?? 0)))) : max(0, (int) ($vasp_feed['counts']['pending_count'] ?? 0))); $vasp_blocker = ''; if ($vasp_name === 'woocommerce_images' && !empty($vasp_feed['blocked_by_catalog'])) { $vasp_blocker = __('Waiting for ValueSpace to receive the catalog data before sending images.', 'valuespace'); } elseif ($vasp_status === 'paused') { $vasp_blocker = __('Synchronization is paused until the connection and settings are ready.', 'valuespace'); } if ($vasp_name === 'events' && in_array((string) ($vasp_feed['stage'] ?? ''), ['queued', 'processing'], true)) { $vasp_summary = __('The latest events reached ValueSpace and are being processed.', 'valuespace'); } elseif ($vasp_name === 'events' && (string) ($vasp_feed['stage'] ?? '') === 'needs_attention') { $vasp_summary = __('Some events could not be processed and need attention.', 'valuespace'); } elseif ($vasp_status === 'warning') { $vasp_summary = (string) ($vasp_feed['error_reason'] ?? __('Some product images need attention.', 'valuespace')); } elseif ($vasp_status === 'error') { $vasp_summary = $vasp_name === 'events' ? __('Some events could not be sent. Events waiting in WordPress will be tried again automatically.', 'valuespace') : __('The catalog update could not be completed.', 'valuespace'); } elseif ($vasp_status === 'running') { if ($vasp_catalog_product_scan) { /* translators: %d: Number of catalog products remaining to check. */ $vasp_summary = sprintf(__('Checking catalog products — %d remaining.', 'valuespace'), $vasp_remaining); } elseif ($vasp_image_scan) { /* translators: %d: Number of products remaining to check for image updates. */ $vasp_summary = sprintf(__('Checking products for image updates — %d remaining.', 'valuespace'), $vasp_remaining); } elseif ($vasp_name === 'events') { /* translators: %d: Number of events waiting to be sent. */ $vasp_summary = sprintf(__('Sending events to ValueSpace — %d waiting.', 'valuespace'), $vasp_remaining); } elseif ($vasp_name === 'woocommerce_images') { /* translators: %d: Number of catalog images remaining to send. */ $vasp_summary = sprintf(__('Sending catalog images — %d remaining.', 'valuespace'), $vasp_remaining); } elseif ($vasp_catalog_product_send) { /* translators: %d: Number of catalog products remaining to send. */ $vasp_summary = sprintf(__('Sending catalog products — %d remaining.', 'valuespace'), $vasp_remaining); } else { /* translators: %d: Number of catalog upload steps remaining. */ $vasp_summary = sprintf(__('Sending catalog data — %d upload steps remaining.', 'valuespace'), $vasp_remaining); } } elseif (in_array($vasp_status, ['queued', 'scheduled', 'waiting', 'recovery', 'unscheduled'], true)) { $vasp_summary = $vasp_blocker !== '' ? $vasp_blocker : ($vasp_catalog_product_send /* translators: %d: Number of catalog products remaining to send. */ ? sprintf(__('Waiting to continue sending catalog products — %d remaining.', 'valuespace'), $vasp_remaining) /* translators: %d: Number of items remaining to send. */ : sprintf(__('Waiting to try again automatically — %d remaining.', 'valuespace'), $vasp_remaining)); } elseif ($vasp_status === 'paused') { $vasp_summary = $vasp_blocker; } else { $vasp_summary = __('Nothing is waiting to be sent from WordPress.', 'valuespace'); } return [ 'phase' => sanitize_key((string) ($vasp_feed['stage'] ?? 'idle')), 'summary' => $vasp_summary, 'blocker' => $vasp_blocker, 'remaining' => $vasp_remaining, 'started_at' => max(0, (int) ($vasp_feed['started_at'] ?? 0)), 'completed_at' => max(0, (int) ($vasp_feed['completed_at'] ?? 0)), 'next_recovery_at' => max(0, (int) ($vasp_feed['recovery_at'] ?? 0), (int) ($vasp_feed['next_attempt_at'] ?? 0)), 'recoverable' => in_array($vasp_status, ['warning', 'error', 'queued', 'scheduled', 'waiting', 'recovery', 'unscheduled'], true), ]; } /** * Describe local feed work without scheduling it or exposing worker tokens. */ function vasp_diagnostics_sync_feeds(): array { global $wpdb; $vasp_now = time(); $vasp_catalog_status = (string) get_option(VASP_OPTION_WC_CATALOG_STATUS, ''); $vasp_source_pending = vasp_wc_catalog_active(); $vasp_local_progress = vasp_wc_catalog_local_progress_metrics(); $vasp_product_scopes_total = (int) ($vasp_local_progress['productScanTotal'] ?? 0); $vasp_product_scopes_processed = (int) ($vasp_local_progress['productScanProcessed'] ?? 0); $vasp_product_scopes_remaining = max(0, $vasp_product_scopes_total - $vasp_product_scopes_processed); $vasp_product_send_total = (int) ($vasp_local_progress['productsQueued'] ?? 0); $vasp_product_send_processed = (int) ($vasp_local_progress['productsProcessed'] ?? 0); $vasp_catalog_packets = (int) ($vasp_local_progress['productsRemaining'] ?? 0); $vasp_catalog_packets += $vasp_source_pending ? 1 : 0; // Table image work is measured in product owners, not individual binaries. $vasp_image_count = vasp_wc_catalog_storage_ready() ? vasp_wc_catalog_pending_count(true) : 0; $vasp_image_counts = ['entities' => $vasp_image_count, 'products' => $vasp_image_count]; $vasp_image_total = max(0, (int) ($vasp_local_progress['imagesTotal'] ?? 0)); $vasp_image_processed = min( $vasp_image_total, max(0, (int) ($vasp_local_progress['imagesProcessed'] ?? 0)) ); $vasp_image_scan_total = max(0, (int) ($vasp_local_progress['imageScanTotal'] ?? 0)); $vasp_image_scan_processed = min( $vasp_image_scan_total, max(0, (int) ($vasp_local_progress['imageScanProcessed'] ?? 0)) ); $vasp_delivery_step = (string) get_option(VASP_OPTION_WC_CATALOG_STEP, ''); $vasp_delivery_stage = function_exists('vasp_wc_catalog_display_stage_for_step') ? vasp_wc_catalog_display_stage_for_step($vasp_delivery_step) : 'start_requested'; $vasp_event_counts = ['indexed_count' => 0, 'pending_count' => 0, 'acknowledged_count' => 0, 'discarded_count' => 0, 'ignored_count' => 0]; if (vasp_event_storage_schema_ready()) { $vasp_row = $wpdb->get_row( $wpdb->prepare("SELECT COUNT(*) indexed_count, SUM(delivery_status = 'pending') pending_count, SUM(delivery_status = 'acknowledged') acknowledged_count, SUM(delivery_status = 'discarded') discarded_count, SUM(delivery_status = 'ignored') ignored_count FROM %i", vasp_event_index_table()), ARRAY_A ); if (is_array($vasp_row)) { foreach ($vasp_event_counts as $vasp_key => $vasp_value) { $vasp_event_counts[$vasp_key] = max(0, (int) ($vasp_row[$vasp_key] ?? 0)); } } } $vasp_reconcile = vasp_scheduler_get_next_event('vasp_reconcile_sync_jobs', null); $vasp_last_reconciliation = max(0, (int) get_option(VASP_OPTION_SYNC_RECONCILIATION_LAST_COMPLETED, 0)); $vasp_cron_missing = !is_array($vasp_reconcile); $vasp_cron_overdue = $vasp_cron_missing || (int) ($vasp_reconcile['timestamp'] ?? 0) < $vasp_now - 5 * MINUTE_IN_SECONDS || ($vasp_last_reconciliation > 0 && $vasp_last_reconciliation < $vasp_now - 10 * MINUTE_IN_SECONDS); $vasp_work = [ 'woocommerce_catalog' => $vasp_catalog_packets > 0, 'woocommerce_images' => $vasp_image_count > 0, 'events' => $vasp_event_counts['pending_count'] > 0 || vasp_event_gateway_status_refresh_needed(), ]; $vasp_feeds = []; // The catalog option stores the administrator-facing message. Technical // details remain on the scheduler failure; do not decode the message as // legacy JSON. $vasp_catalog_failed = $vasp_catalog_status === 'failed'; $vasp_catalog_error_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $vasp_catalog_error = is_string($vasp_catalog_error_raw) && $vasp_catalog_error_raw !== '' ? json_decode($vasp_catalog_error_raw, true) : null; $vasp_catalog_error = is_array($vasp_catalog_error) ? $vasp_catalog_error : []; $vasp_catalog_error_step = sanitize_key((string) ($vasp_catalog_error['step'] ?? '')); $vasp_catalog_error_feed = in_array($vasp_catalog_error_step, ['images', 'image_download', 'image_audit'], true) ? 'woocommerce_images' : 'woocommerce_catalog'; $vasp_catalog_error_code = sanitize_key((string) ($vasp_catalog_error['technical_code'] ?? '')); $vasp_catalog_item_issue = function_exists('vasp_is_catalog_image_item_error') && vasp_is_catalog_image_item_error($vasp_catalog_error_code); $vasp_catalog_error_details = is_array($vasp_catalog_error['error_details'] ?? null) ? vasp_scheduler_error_details($vasp_catalog_error['error_details']) : []; $vasp_catalog_error_display = $vasp_catalog_error && function_exists('vasp_format_error_for_display') ? vasp_format_error_for_display($vasp_catalog_error) : []; $vasp_event_health = vasp_event_get_delivery_status(); // An expired settings cache is unknown, not a disabled collection setting. // Diagnostics must not contact Gateway to refresh it. $vasp_events_paused = !vasp_valuespace_is_connected() || get_transient(VASP_EVENT_TRACKING_CONFIGURATION_TRANSIENT) === 'disabled'; foreach ($vasp_work as $vasp_name => $vasp_has_work) { $vasp_is_catalog_item_issue = $vasp_name === $vasp_catalog_error_feed && $vasp_catalog_item_issue; $vasp_paused = $vasp_name === 'events' && $vasp_events_paused; $vasp_pending = get_option(vasp_scheduler_pending_option($vasp_name), false); $vasp_lock = get_option(vasp_scheduler_running_option($vasp_name), false); $vasp_expires = is_array($vasp_lock) ? (int) ($vasp_lock['expires_at'] ?? 0) : 0; $vasp_dispatch = get_option(vasp_scheduler_dispatch_option($vasp_name), false); $vasp_dispatch_expires = is_array($vasp_dispatch) ? (int) ($vasp_dispatch['expires_at'] ?? 0) : 0; $vasp_wake = vasp_scheduler_get_next_event('vasp_run_sync_service', [$vasp_name]); $vasp_wake_at = is_array($vasp_wake) ? (int) ($vasp_wake['timestamp'] ?? 0) : 0; $vasp_running = $vasp_expires > $vasp_now; $vasp_run_at = is_array($vasp_pending) ? (int) ($vasp_pending['run_at'] ?? 0) : 0; $vasp_recovery = is_array($vasp_pending) && !empty($vasp_pending['watchdog']); $vasp_owns_lock = $vasp_running && ($vasp_name === 'events' || ($vasp_recovery && hash_equals((string) ($vasp_lock['token'] ?? ''), (string) $vasp_pending['watchdog']))); $vasp_interrupted = $vasp_recovery && !$vasp_running; $vasp_error = is_array($vasp_pending) ? (string) ($vasp_pending['error'] ?? '') : ''; if ($vasp_error === '' && $vasp_name === $vasp_catalog_error_feed && ($vasp_catalog_failed || $vasp_catalog_item_issue)) { $vasp_error = $vasp_catalog_error_code !== '' ? $vasp_catalog_error_code : 'catalog_sync_failed'; } elseif ($vasp_error === '' && $vasp_name === 'events') { $vasp_error = (string) ($vasp_event_health['last_error'] ?? ''); } if ($vasp_error === '' && $vasp_interrupted) { $vasp_error = 'worker_interrupted'; } if ($vasp_name === 'events' && !$vasp_has_work && !is_array($vasp_pending) && $vasp_error === '') { $vasp_error = ''; } $vasp_error = $vasp_error === '' || preg_match('/^[a-z0-9_:-]{1,96}$/i', $vasp_error) ? $vasp_error : 'sync_failed'; $vasp_pending_has_error = $vasp_error !== '' && is_array($vasp_pending) && ($vasp_pending['error'] ?? '') === $vasp_error; $vasp_error_details = $vasp_pending_has_error ? vasp_scheduler_error_details($vasp_pending['error_details'] ?? []) : ($vasp_error !== '' && $vasp_name === $vasp_catalog_error_feed ? $vasp_catalog_error_details : []); $vasp_error_reason = vasp_diagnostics_feed_error_reason( $vasp_error, $vasp_error_details, $vasp_name === 'events' ? 'valuespace' : 'catalog_sync' ); $vasp_error_hint = ''; $vasp_catalog_error_matches = $vasp_error !== '' && $vasp_name === $vasp_catalog_error_feed && $vasp_catalog_error_display && $vasp_error === $vasp_catalog_error_code; if ($vasp_catalog_error_matches) { $vasp_error_reason = (string) ($vasp_catalog_error_display['description'] ?? $vasp_error_reason); $vasp_error_hint = (string) ($vasp_catalog_error_display['hint'] ?? ''); } $vasp_status = 'idle'; if ($vasp_paused) { $vasp_status = 'paused'; } elseif ($vasp_owns_lock) { $vasp_status = 'running'; } elseif ($vasp_error !== '') { $vasp_status = $vasp_catalog_error_matches && $vasp_is_catalog_item_issue && !$vasp_catalog_failed ? 'warning' : 'error'; } elseif (($vasp_running && ($vasp_has_work || is_array($vasp_pending))) || ($vasp_name === 'woocommerce_images' && $vasp_has_work && $vasp_catalog_packets > 0)) { $vasp_status = 'waiting'; } elseif (($vasp_expires > 0 && ($vasp_has_work || is_array($vasp_pending))) || $vasp_recovery) { $vasp_status = 'recovery'; } elseif (is_array($vasp_pending)) { $vasp_status = $vasp_run_at > $vasp_now ? 'scheduled' : 'queued'; } elseif ($vasp_has_work) { $vasp_status = 'unscheduled'; } $vasp_feeds[$vasp_name] = [ 'status' => $vasp_status, 'has_work' => $vasp_has_work, 'lock_expires_at' => $vasp_expires, 'next_attempt_at' => ($vasp_paused || $vasp_running || $vasp_recovery) ? 0 : $vasp_run_at, 'followup_at' => is_array($vasp_pending) ? (int) ($vasp_pending['followup_run_at'] ?? 0) : 0, 'recovery_at' => $vasp_recovery ? max($vasp_run_at, $vasp_expires) : 0, 'service_wake_at' => $vasp_wake_at, 'service_wake_missing' => is_array($vasp_pending) && $vasp_wake_at === 0, 'service_wake_overdue' => $vasp_wake_at > 0 && $vasp_wake_at < $vasp_now - MINUTE_IN_SECONDS, 'dispatch_claim_expires_at' => $vasp_dispatch_expires, 'worker_claim_expires_at' => $vasp_expires, // Compatibility fields retained for existing support tooling. These // describe the global reconciliation cron, not this feed's wake. 'cron_overdue' => $vasp_cron_overdue, 'cron_missing' => $vasp_cron_missing, 'reconciliation_cron_overdue' => $vasp_cron_overdue, 'reconciliation_cron_missing' => $vasp_cron_missing, 'last_reconciliation_at' => $vasp_last_reconciliation, 'error_code' => $vasp_error, 'item_issue' => $vasp_is_catalog_item_issue, 'error_details' => $vasp_error_details, 'error_reason' => $vasp_error_reason, 'error_hint' => $vasp_error_hint, 'affected_product' => $vasp_catalog_error_matches ? (array) ($vasp_catalog_error_display['affected_product'] ?? []) : [], 'can_retry' => $vasp_catalog_error_matches && !empty($vasp_catalog_error_display['can_retry']), 'error_at' => $vasp_pending_has_error ? max(0, (int) ($vasp_pending['error_at'] ?? 0)) : ($vasp_error !== '' && $vasp_name === $vasp_catalog_error_feed ? max(0, (int) ($vasp_catalog_error['timestamp'] ?? 0)) : ($vasp_error !== '' && $vasp_name === 'events' ? max(0, (int) ($vasp_event_health['last_attempt'] ?? 0)) : 0)), 'interrupted' => $vasp_interrupted, 'retry_scheduled' => !$vasp_paused && $vasp_error !== '' && !$vasp_is_catalog_item_issue && is_array($vasp_pending), ]; } $vasp_feeds['woocommerce_catalog']['catalog_packets'] = $vasp_catalog_packets; $vasp_feeds['woocommerce_catalog']['product_scopes_total'] = $vasp_product_scopes_total; $vasp_feeds['woocommerce_catalog']['product_scopes_processed'] = $vasp_product_scopes_processed; $vasp_feeds['woocommerce_catalog']['product_scopes_remaining'] = $vasp_product_scopes_remaining; $vasp_feeds['woocommerce_catalog']['product_updates'] = max(0, (int) ( $vasp_local_progress['productUpdates'] ?? 0 )); $vasp_feeds['woocommerce_catalog']['product_send_processed'] = $vasp_product_send_processed; $vasp_feeds['woocommerce_catalog']['product_send_total'] = $vasp_product_send_total; $vasp_feeds['woocommerce_catalog']['stage'] = $vasp_source_pending ? $vasp_delivery_stage : ($vasp_catalog_status === 'pending' ? 'start_requested' : ($vasp_catalog_packets > 0 ? 'realtime' : 'idle')); $vasp_feeds['woocommerce_images']['image_entities'] = $vasp_image_counts['entities']; $vasp_feeds['woocommerce_images']['image_products'] = $vasp_image_counts['products']; $vasp_feeds['woocommerce_images']['image_references'] = $vasp_image_count; $vasp_feeds['woocommerce_images']['image_processed'] = $vasp_image_processed; $vasp_feeds['woocommerce_images']['image_total'] = $vasp_image_total; $vasp_feeds['woocommerce_images']['image_scan_processed'] = $vasp_image_scan_processed; $vasp_feeds['woocommerce_images']['image_scan_total'] = $vasp_image_scan_total; // Preserve the existing diagnostics field while correcting its displayed unit. $vasp_feeds['woocommerce_images']['image_records'] = $vasp_image_count; $vasp_feeds['woocommerce_images']['stage'] = $vasp_source_pending && in_array($vasp_delivery_stage, ['image_check', 'images'], true) ? $vasp_delivery_stage : ($vasp_image_count > 0 ? 'images' : 'idle'); $vasp_feeds['woocommerce_images']['blocked_by_catalog'] = $vasp_image_count > 0 && $vasp_catalog_packets > 0; $vasp_feeds['events']['counts'] = $vasp_event_counts; $vasp_feeds['events']['stage'] = vasp_event_gateway_status_refresh_needed() ? sanitize_key((string) ($vasp_event_health['gateway_status'] ?? 'queued')) : (vasp_event_gateway_attention_needed() ? 'needs_attention' : ($vasp_event_counts['pending_count'] > 0 ? 'events' : 'idle')); foreach ($vasp_feeds as $vasp_name => &$vasp_feed) { $vasp_feed['presentation'] = vasp_diagnostics_feed_presentation($vasp_name, $vasp_feed); } unset($vasp_feed); return $vasp_feeds; } /** * Return one bounded diagnostic snapshot shared by System Information and reports. * * @param string $user_message Compatibility argument; only its length is recorded. * @return array */ function vasp_collect_diagnostics(string $user_message = ''): array { $config = function_exists('vasp_get_config') ? vasp_get_config() : []; $system = function_exists('vasp_get_system_info') ? vasp_get_system_info() : []; $catalog_status = defined('VASP_OPTION_WC_CATALOG_STATUS') ? get_option(VASP_OPTION_WC_CATALOG_STATUS, null) : null; $catalog_progress = function_exists('vasp_wc_catalog_local_progress_metrics') ? vasp_wc_catalog_local_progress_metrics() : []; $vasp_feeds = vasp_diagnostics_sync_feeds(); $event_sync = array_merge(vasp_event_get_delivery_status(), $vasp_feeds['events']['counts'], [ 'state' => $vasp_feeds['events']['status'], 'next_run' => $vasp_feeds['events']['next_attempt_at'], ]); $vasp_reconcile = vasp_scheduler_get_next_event('vasp_reconcile_sync_jobs', null); $vasp_push_refresh = vasp_scheduler_get_next_event('vasp_wc_catalog_push_refresh', []); $vasp_catalog_storage_ready = function_exists('vasp_wc_catalog_storage_ready') && vasp_wc_catalog_storage_ready(); $vasp_catalog_table_rows = $vasp_catalog_storage_ready && function_exists('vasp_wc_catalog_hash_table') ? vasp_diagnostics_table_count(vasp_wc_catalog_hash_table()) : null; $vasp_catalog_queue_rows = $vasp_catalog_storage_ready && function_exists('vasp_wc_catalog_queue_table') ? vasp_diagnostics_table_count(vasp_wc_catalog_queue_table()) : null; $vasp_event_configuration = get_transient(VASP_EVENT_TRACKING_CONFIGURATION_TRANSIENT); return [ 'report' => [ 'generated_at_utc' => gmdate('c'), 'message_length' => function_exists('mb_strlen') ? mb_strlen($user_message, 'UTF-8') : strlen($user_message), ], 'site' => [ 'host' => (string) wp_parse_url(home_url(), PHP_URL_HOST), 'locale' => get_locale(), 'timezone' => wp_timezone_string(), 'multisite' => is_multisite(), ], 'versions' => [ 'valuespace_plugin' => defined('VASP_VERSION') ? VASP_VERSION : '', 'wordpress' => $system['wp_version'] ?? get_bloginfo('version'), 'php' => $system['php_version'] ?? phpversion(), 'woocommerce' => defined('WC_VERSION') ? WC_VERSION : null, ], 'environment' => [ 'name' => function_exists('vasp_get_environment') ? vasp_get_environment() : '', 'dashboard_host' => (string) wp_parse_url($config['dashboard_domain'] ?? '', PHP_URL_HOST), 'gateway_host' => (string) wp_parse_url($config['gateway_domain'] ?? '', PHP_URL_HOST), 'ssl_verify' => isset($config['ssl_verify']) ? (bool) $config['ssl_verify'] : true, ], 'credentials' => vasp_diagnostics_credential_state(), 'scheduler' => [ 'wp_cron_disabled' => defined('DISABLE_WP_CRON') && DISABLE_WP_CRON, 'execution_model' => [ 'fast_path' => 'signed_local_loopback', 'durable_feed_fallback' => 'per_service_wp_cron_wake', 'repair_path' => 'five_minute_reconciliation', 'nightly_catalog_trigger' => 'wp_cron', ], 'reconciliation_next_at' => is_array($vasp_reconcile) ? (int) ($vasp_reconcile['timestamp'] ?? 0) : 0, 'reconciliation_last_started_at' => max(0, (int) get_option(VASP_OPTION_SYNC_RECONCILIATION_LAST_STARTED, 0)), 'reconciliation_last_completed_at' => max(0, (int) get_option(VASP_OPTION_SYNC_RECONCILIATION_LAST_COMPLETED, 0)), 'service_wake_hook' => 'vasp_run_sync_service', ], 'feeds' => $vasp_feeds, 'integrations' => [ 'valuespace' => [ 'connected' => function_exists('vasp_valuespace_is_connected') ? (bool) vasp_valuespace_is_connected() : null, 'has_last_error' => function_exists('vasp_valuespace_last_error') ? !empty(vasp_valuespace_last_error()) : null, 'last_error' => vasp_diagnostics_stored_error(defined('VASP_VALUESPACE_LAST_ERROR') ? VASP_VALUESPACE_LAST_ERROR : ''), 'was_denied' => function_exists('vasp_valuespace_was_denied') ? (bool) vasp_valuespace_was_denied() : null, ], 'wordpress' => [ 'connected' => function_exists('vasp_wordpress_is_connected') ? (bool) vasp_wordpress_is_connected() : null, 'has_last_error' => function_exists('vasp_wordpress_last_error') ? !empty(vasp_wordpress_last_error()) : null, 'last_error' => vasp_diagnostics_stored_error(defined('VASP_WORDPRESS_LAST_ERROR') ? VASP_WORDPRESS_LAST_ERROR : ''), 'was_denied' => function_exists('vasp_wordpress_was_denied') ? (bool) vasp_wordpress_was_denied() : null, ], 'woocommerce' => [ 'installed' => class_exists('WooCommerce'), 'connected' => function_exists('vasp_woocommerce_is_connected') ? (bool) vasp_woocommerce_is_connected() : null, 'has_last_error' => function_exists('vasp_woocommerce_last_error') ? !empty(vasp_woocommerce_last_error()) : null, 'last_error' => vasp_diagnostics_stored_error(defined('VASP_WOOCOMMERCE_LAST_ERROR') ? VASP_WOOCOMMERCE_LAST_ERROR : ''), 'was_denied' => function_exists('vasp_woocommerce_was_denied') ? (bool) vasp_woocommerce_was_denied() : null, ], ], 'catalog' => [ 'pipeline' => 'push', 'status' => is_scalar($catalog_status) ? substr(sanitize_key((string) $catalog_status), 0, 64) : null, 'active_step' => defined('VASP_OPTION_WC_CATALOG_STEP') ? substr(sanitize_key((string) get_option(VASP_OPTION_WC_CATALOG_STEP, '')), 0, 64) : null, 'pending_mode' => defined('VASP_OPTION_WC_CATALOG_PENDING_MODE') ? substr(sanitize_key((string) get_option(VASP_OPTION_WC_CATALOG_PENDING_MODE, '')), 0, 64) : null, 'total' => (int) ($catalog_progress['total'] ?? 0), 'processed' => (int) ($catalog_progress['processed'] ?? 0), 'has_last_error' => defined('VASP_OPTION_WC_CATALOG_LAST_ERROR') ? !empty(get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, null)) : null, 'last_error' => vasp_diagnostics_stored_error(defined('VASP_OPTION_WC_CATALOG_LAST_ERROR') ? VASP_OPTION_WC_CATALOG_LAST_ERROR : ''), 'push_enabled' => defined('VASP_OPTION_WC_CATALOG_PUSH_ENABLED') ? (bool) get_option(VASP_OPTION_WC_CATALOG_PUSH_ENABLED, false) : null, 'nightly_refresh_at' => is_array($vasp_push_refresh) ? (int) ($vasp_push_refresh['timestamp'] ?? 0) : 0, 'storage' => [ 'model' => 'receipt_baseline_and_coalescing_outbox', 'schema_ready' => $vasp_catalog_storage_ready, 'schema_version' => defined('VASP_OPTION_WC_CATALOG_STORAGE_SCHEMA') ? max(0, (int) get_option(VASP_OPTION_WC_CATALOG_STORAGE_SCHEMA, 0)) : null, 'expected_schema_version' => defined('VASP_WC_CATALOG_STORAGE_SCHEMA_VERSION') ? VASP_WC_CATALOG_STORAGE_SCHEMA_VERSION : null, 'baseline_rows' => $vasp_catalog_table_rows, 'outbox_rows' => $vasp_catalog_queue_rows, 'pending_product_actions' => function_exists('vasp_wc_catalog_pending_count') && $vasp_catalog_storage_ready ? vasp_wc_catalog_pending_count(false) : null, 'pending_image_actions' => function_exists('vasp_wc_catalog_pending_count') && $vasp_catalog_storage_ready ? vasp_wc_catalog_pending_count(true) : null, ], ], 'events' => is_array($event_sync) ? [ 'pipeline' => 'indexed_outbox', 'configured' => in_array($vasp_event_configuration, ['enabled', 'disabled'], true) ? $vasp_event_configuration : 'unknown', 'effective_enabled' => function_exists('vasp_is_event_tracking_enabled') ? (bool) vasp_is_event_tracking_enabled() : null, 'schema_ready' => function_exists('vasp_event_storage_schema_ready') ? (bool) vasp_event_storage_schema_ready() : null, 'schema_version' => defined('VASP_EVENT_STORAGE_SCHEMA_OPTION') ? max(0, (int) get_option(VASP_EVENT_STORAGE_SCHEMA_OPTION, 0)) : null, 'expected_schema_version' => defined('VASP_EVENT_STORAGE_SCHEMA_VERSION') ? VASP_EVENT_STORAGE_SCHEMA_VERSION : null, 'state' => sanitize_key((string) ($event_sync['state'] ?? '')), 'indexed' => (int) ($event_sync['indexed_count'] ?? 0), 'acknowledged' => (int) ($event_sync['acknowledged_count'] ?? 0), 'pending' => (int) ($event_sync['pending_count'] ?? 0), 'discarded' => (int) ($event_sync['discarded_count'] ?? 0), 'ignored' => (int) ($event_sync['ignored_count'] ?? 0), 'gateway_pending' => (int) ($event_sync['gateway_pending'] ?? 0), 'consecutive_failures' => (int) ($event_sync['consecutive_failures'] ?? 0), 'has_last_error' => !empty($event_sync['last_error']), 'last_success' => (int) ($event_sync['last_success'] ?? 0), 'next_run' => (int) ($event_sync['next_run'] ?? 0), 'gateway_status' => substr(sanitize_key((string) ($event_sync['gateway_status'] ?? '')), 0, 64), 'gateway_status_refresh_needed' => function_exists('vasp_event_gateway_status_refresh_needed') ? (bool) vasp_event_gateway_status_refresh_needed() : null, ] : [ 'state' => 'disabled', ], 'runtime' => [ 'wp_debug' => defined('WP_DEBUG') && WP_DEBUG, 'memory_limit' => (string) ini_get('memory_limit'), 'woocommerce_currency' => function_exists('get_woocommerce_currency') ? get_woocommerce_currency() : null, ], ]; } /** * Encode the allowlisted snapshot for display or attachment. * * @param string $user_message Compatibility argument. * @return string */ function vasp_build_diagnostic_text(string $user_message = ''): string { $json = wp_json_encode(vasp_collect_diagnostics($user_message), JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); return is_string($json) ? $json : ''; }