> */ function vasp_system_info_connection_diagnostics(): array { $registered = apply_filters('vasp_system_info_connection_diagnostics', []); $diagnostics = []; foreach ((array) $registered as $key => $diagnostic) { if (!is_array($diagnostic)) { continue; } $key = sanitize_key((string) $key); $action = sanitize_key((string) ($diagnostic['action'] ?? '')); if ($key === '' || $action === '') { continue; } $requires = array_values(array_filter(array_map('sanitize_key', (array) ($diagnostic['requires'] ?? [])))); $diagnostics[$key] = [ 'key' => $key, 'label' => sanitize_text_field((string) ($diagnostic['label'] ?? $key)), 'action' => $action, 'status_id' => $key . '-connection-status', 'connected' => !empty($diagnostic['connected']), 'connected_time' => max(0, (int) ($diagnostic['connected_time'] ?? 0)), 'last_error_option' => sanitize_key((string) ($diagnostic['last_error_option'] ?? '')), 'available' => !empty($diagnostic['available']), 'enabled' => !empty($diagnostic['enabled']), 'requires' => $requires, ]; } foreach ($diagnostics as $key => $diagnostic) { $testable = $diagnostic['available'] && $diagnostic['enabled']; foreach ($diagnostic['requires'] as $required_key) { if (empty($diagnostics[$required_key]['available']) || empty($diagnostics[$required_key]['enabled'])) { $testable = false; break; } } $diagnostics[$key]['testable'] = $testable; } return $diagnostics; } /** * Get safe credential presence and expiry information. * * This diagnostic never reads plaintext credentials. * * @return array */ function vasp_get_token_info() { $state = function_exists('vasp_diagnostics_credential_state') ? vasp_diagnostics_credential_state() : []; $expires_at = (int) ($state['access_expires_at'] ?? get_transient(VASP_TRANSIENT_TOKEN_EXPIRES_AT)); $now = time(); $fallback_access_token = get_transient(VASP_TRANSIENT_ACCESS_TOKEN); $fallback_refresh_token = get_option(VASP_OPTION_REFRESH_TOKEN, false); return [ 'has_access_token' => (bool) ($state['has_access_token'] ?? (is_string($fallback_access_token) && $fallback_access_token !== '')), 'has_refresh_token' => (bool) ($state['has_refresh_token'] ?? (is_string($fallback_refresh_token) && $fallback_refresh_token !== '')), 'expires_at' => $expires_at, 'current_time' => $now, 'is_expired' => $expires_at > 0 && $expires_at <= $now, 'time_remaining' => $expires_at > 0 ? $expires_at - $now : 0, ]; } /** * Enqueue admin styles */ function vasp_admin_enqueue_styles($hook) { // For debugging - uncomment to see the actual hook name // Check both possible hook names if (!empty($hook) && (strpos($hook, 'valuespace-info') !== false || strpos($hook, 'valuespace_info') !== false)) { $css_url = VASP_PLUGIN_URL . 'admin/css/info.css'; $css_path = VASP_PLUGIN_DIR . 'admin/css/info.css'; wp_enqueue_style( 'valuespace-admin-css', $css_url, array(), file_exists($css_path) ? filemtime($css_path) : VASP_VERSION ); wp_enqueue_script('vasp-system-info', VASP_PLUGIN_URL . 'admin/js/system-info.js', ['jquery', 'valuespace-notices'], VASP_VERSION, true); $is_manual_refresh_post = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST' && isset($_POST['vasp_refresh_token_nonce'], $_POST['action']) && sanitize_key(wp_unslash($_POST['action'])) === 'vasp_refresh_token'; $connection_tests = []; foreach (vasp_system_info_connection_diagnostics() as $diagnostic) { if (!$diagnostic['testable']) { continue; } $connection_tests[] = [ 'key' => $diagnostic['key'], 'action' => $diagnostic['action'], 'statusId' => $diagnostic['status_id'], 'requires' => $diagnostic['requires'], ]; } wp_localize_script('vasp-system-info', 'vaspSystemInfo', [ 'nonce' => wp_create_nonce('vasp_system_info_nonce'), 'eventsNonce' => wp_create_nonce('vasp_events_admin_nonce'), 'wordpressNonce' => wp_create_nonce('vasp_admin_nonce'), 'ajaxurl' => admin_url('admin-ajax.php'), 'autoRunConnectionTests' => !$is_manual_refresh_post, 'connectionTests' => $connection_tests, 'i18n' => [ 'testing' => __('Testing...', 'valuespace'), 'working' => __('Working', 'valuespace'), 'failed' => __('Failed', 'valuespace'), 'testFailed' => __('Test failed', 'valuespace'), 'requestUnavailable' => __('The connection test could not be completed. Please try again.', 'valuespace'), 'tokenValid' => __('Valid', 'valuespace'), 'tokenInvalid' => __('Invalid', 'valuespace'), 'tokenNotVerified' => __('Not verified', 'valuespace'), 'copied' => __('Copied', 'valuespace'), 'copyFailed' => __('Copy failed', 'valuespace'), 'sendingEvents' => __('Queuing...', 'valuespace'), 'sendEventsFailed' => __('The waiting events could not be queued. Please try again.', 'valuespace'), 'syncing' => __('Syncing...', 'valuespace'), 'syncFailed' => __('The synchronization could not be started. Please try again.', 'valuespace'), ], ]); } } /** * Get system version information * * @return array System version information */ function vasp_get_system_info() { global $wp_version; $info = array( 'plugin_version' => defined('VASP_VERSION') ? VASP_VERSION : 'Unknown', 'wp_version' => $wp_version, 'php_version' => phpversion() ); // Check if WooCommerce is active if (in_array('woocommerce/woocommerce.php', apply_filters('active_plugins', get_option('active_plugins')))) { if (defined('WC_VERSION')) { $info['woocommerce_version'] = WC_VERSION; } else { $info['woocommerce_version'] = 'Active (version unknown)'; } } else { $info['woocommerce_version'] = 'Not installed'; } return $info; } /** * Format a timestamp for the info page. * * @param int $timestamp Unix timestamp. * @return string */ function vasp_info_format_time(int $timestamp): string { if ($timestamp <= 0) { return ''; } return wp_date(get_option('date_format') . ' ' . get_option('time_format'), $timestamp); } /** * Format a datetime string for the info page. * * @param string $datetime Datetime string. * @return string */ function vasp_info_format_datetime_string(string $datetime): string { $timestamp = strtotime($datetime); return $timestamp ? vasp_info_format_time($timestamp) : $datetime; } /** Return a readable WordPress-side synchronization status. */ function vasp_info_feed_status_label(string $vasp_status): string { $vasp_labels = [ 'idle' => __('Nothing waiting to be sent', 'valuespace'), 'running' => __('Sending now', 'valuespace'), 'scheduled' => __('Will start automatically', 'valuespace'), 'queued' => __('Waiting to start', 'valuespace'), 'waiting' => __('Waiting', 'valuespace'), 'paused' => __('Paused', 'valuespace'), 'warning' => __('Needs attention', 'valuespace'), 'error' => __('Error', 'valuespace'), 'recovery' => __('Will try again automatically', 'valuespace'), 'unscheduled' => __('Waiting data is not scheduled', 'valuespace'), ]; return $vasp_labels[$vasp_status] ?? __('Not available', 'valuespace'); } /** Return the local catalog delivery status and its display style. */ function vasp_info_catalog_status_label(string $status): array { switch ($status) { case 'started': case 'in_progress': return [ 'text' => __('Sync in progress', 'valuespace'), 'class' => 'vasp-txt-warning-text', 'spinner' => true, ]; case 'completed': return [ 'text' => __('Received by ValueSpace', 'valuespace'), 'class' => 'vasp-txt-success', 'spinner' => false, ]; case 'failed': return [ 'text' => __('Failed', 'valuespace'), 'class' => 'vasp-txt-error', 'spinner' => false, ]; case 'disabled': return [ 'text' => __('Off', 'valuespace'), 'class' => 'vasp-txt-inactive', 'spinner' => false, ]; default: return [ 'text' => __('Not started', 'valuespace'), 'class' => 'vasp-txt-inactive', 'spinner' => false, ]; } } /** * Return a user-friendly catalog sync error and its safe diagnostic context. * * @param string $error Error from the current progress payload. * @param string $error_code Stable error code from the current progress payload. * @param string $stage Current synchronization stage. * @return array */ function vasp_info_catalog_error_display(string $error, string $error_code = '', string $stage = ''): array { $fallback = __('Data Sync stopped before the catalog was fully updated.', 'valuespace'); $error_data = trim($error) !== '' ? json_decode($error, true) : null; $error_code = sanitize_key($error_code); if (!is_array($error_data) && defined('VASP_OPTION_WC_CATALOG_LAST_ERROR')) { $error_raw = get_option(VASP_OPTION_WC_CATALOG_LAST_ERROR, ''); $stored_error = is_string($error_raw) && $error_raw !== '' ? json_decode($error_raw, true) : null; $stored_code = is_array($stored_error) ? sanitize_key((string) ($stored_error['technical_code'] ?? '')) : ''; // Progress exposes a localized sentence, while the stored record owns // the safe product/image context. Rejoin only the matching failure. if (is_array($stored_error) && ($error_code === '' || $stored_code === $error_code)) { $error_data = $stored_error; } } if (is_array($error_data) && function_exists('vasp_format_error_for_display')) { return vasp_format_error_for_display($error_data, 'system_info'); } if ($error_code !== '' && function_exists('vasp_format_error_for_display')) { $display = vasp_format_error_for_display([ 'integration' => 'catalog_sync', 'technical_code' => $error_code, 'step' => sanitize_key($stage), ], 'system_info'); if (is_array($display)) { return $display; } } return [ 'description' => $fallback, 'hint' => __('Select Sync Now to restart the catalog update.', 'valuespace'), 'technical_code' => sanitize_key($error_code), 'timestamp' => 0, 'step' => sanitize_key($stage), 'details' => [], ]; } /** Preserve the previous string helper for callers that need only the summary. */ function vasp_info_catalog_error_label(string $error): string { $display = vasp_info_catalog_error_display($error); return (string) ($display['description'] ?? ''); } /** Read one structured integration error without exposing upstream response text. */ function vasp_info_stored_error_display(string $option): ?array { if ($option === '') { return null; } $raw = get_option($option, ''); $error = is_string($raw) && $raw !== '' ? json_decode($raw, true) : null; return is_array($error) && function_exists('vasp_format_error_for_display') ? vasp_format_error_for_display($error, 'system_info') : null; } /** Project one feed's current safe error state through the shared display boundary. */ function vasp_info_feed_error_display( array $feed, string $default_step = '', string $integration = 'catalog_sync' ): ?array { $technical_code = sanitize_key((string) ($feed['error_code'] ?? '')); if ($technical_code === '') { return null; } $details = is_array($feed['error_details'] ?? null) ? $feed['error_details'] : []; $step = sanitize_key((string) ($details['step'] ?? $default_step)); $error = [ 'integration' => sanitize_key($integration), 'technical_code' => $technical_code, 'timestamp' => max(0, (int) ($feed['error_at'] ?? 0)), 'step' => $step, 'error_details' => $details, ]; if (array_key_exists('can_retry', $feed)) { $error['can_retry'] = (bool) $feed['can_retry']; } $display = function_exists('vasp_format_error_for_display') ? vasp_format_error_for_display($error, 'system_info') : null; if (!is_array($display)) { $display = [ 'description' => (string) ($feed['error_reason'] ?? ''), 'hint' => (string) ($feed['error_hint'] ?? ''), 'timestamp' => $error['timestamp'], 'step' => $step, 'details' => $details, ]; } return $display; } /** Translate a stored operation key into a concise diagnostic label. */ function vasp_info_error_step_label(string $step): string { $labels = [ 'authentication' => __('Renewing the ValueSpace connection', 'valuespace'), 'token_exchange' => __('Renewing the ValueSpace connection', 'valuespace'), 'authorization' => __('Connecting the integration', 'valuespace'), 'integration' => __('Connecting the integration', 'valuespace'), 'registration' => __('Connecting the integration', 'valuespace'), 'sso_oauth_callback' => __('Connecting the integration', 'valuespace'), 'sso_bootstrap' => __('Connecting the integration', 'valuespace'), 'content_ingestion' => __('Updating WordPress content in ValueSpace', 'valuespace'), 'ingestion' => __('Updating WordPress content in ValueSpace', 'valuespace'), 'ingestion_start' => __('Starting the WordPress content update', 'valuespace'), 'ingestion_progress' => __('Checking the WordPress content update', 'valuespace'), 'events' => __('Sending visitor events', 'valuespace'), 'image_download' => __('Reading a product image from WordPress', 'valuespace'), 'image_audit' => __('Checking product images', 'valuespace'), 'reconciliation' => __('Checking catalog records', 'valuespace'), ]; if (isset($labels[$step])) { return $labels[$step]; } return $step !== '' ? vasp_info_catalog_stage_label($step) : ''; } /** Render the useful fact rows shared by every structured System Info error. */ function vasp_info_render_error_rows(?array $display): void { if (!$display || empty($display['description'])) { return; } $details = is_array($display['details'] ?? null) ? $display['details'] : []; $display_step = sanitize_key((string) ($display['step'] ?? '')); $detail_step = sanitize_key((string) ($details['step'] ?? '')); $step = $detail_step !== '' ? $detail_step : $display_step; $files = array_slice(array_values(array_unique(array_filter(array_map(static function ($file): string { return sanitize_text_field(substr(basename(str_replace('\\', '/', (string) $file)), 0, 180)); }, array_merge( [(string) ($details['image_file'] ?? '')], is_array($details['image_files'] ?? null) ? $details['image_files'] : [] ))))), 0, 10); $products = array_slice(array_values(array_unique(array_filter(array_map('absint', array_merge( [(int) ($details['product_id'] ?? 0)], is_array($details['product_ids'] ?? null) ? $details['product_ids'] : [] ))))), 0, 10); $image_ids = array_slice(array_values(array_unique(array_filter(array_map('absint', is_array($details['image_ids'] ?? null) ? $details['image_ids'] : [] )))), 0, 10); ?>
' . esc_html($image_label) . '' : esc_html($image_label); } if (!$image_ids) { $image_references = array_map('esc_html', $files); } echo wp_kses_post(implode(', ', $image_references)); ?>
' . esc_html($product_label) . '' : esc_html($product_label); }, $products); echo wp_kses_post(implode(', ', $product_references)); ?>
403] ); } // Initialize variables $refresh_message = ''; $system_info = vasp_get_system_info(); // Check if this is a token refresh request $is_refresh_post = strtoupper((string) ($_SERVER['REQUEST_METHOD'] ?? 'GET')) === 'POST' && isset($_POST['action'], $_POST['vasp_refresh_token_nonce']) && sanitize_key(wp_unslash($_POST['action'])) === 'vasp_refresh_token'; $nonce_valid = $is_refresh_post && wp_verify_nonce( sanitize_text_field(wp_unslash($_POST['vasp_refresh_token_nonce'])), 'vasp_refresh_token_action' ); // Process token refresh if requested and nonce is valid. if ($nonce_valid && current_user_can('manage_options')) { check_admin_referer('vasp_refresh_token_action', 'vasp_refresh_token_nonce'); $refresh_result = vasp_refresh_access_token(); if (is_wp_error($refresh_result)) { $notice = vasp_format_auth_error_notice($refresh_result); $actions = $notice['can_retry'] ? [] : $notice['actions']; $refresh_message = vasp_show_notice( $notice['title'], $notice['type'], 0, $actions, '', $notice['description'] ); } else { $refresh_message = vasp_show_success(__('Token refreshed successfully!', 'valuespace')); } } // Get current token info $token_info = vasp_get_token_info(); $diagnostic_text = function_exists('vasp_build_diagnostic_text') ? vasp_build_diagnostic_text('Manual diagnostic copy from ValueSpace System Info page.') : ''; $vasp_sync_feeds = vasp_diagnostics_sync_feeds(); $event_tracking_enabled = $vasp_sync_feeds['events']['status'] !== 'paused'; $event_sync = array_merge(vasp_event_get_delivery_status(), $vasp_sync_feeds['events']['counts']); ?>

' . sprintf( /* translators: %s: Date and time associated with this status. */ esc_html__('Since %s', 'valuespace'), esc_html(vasp_info_format_time((int) $vasp_connected_time)) ) . ''; } ?>
' . sprintf( /* translators: %s: Synchronization start date and time. */ esc_html__('Started: %s', 'valuespace'), esc_html(vasp_info_format_time((int) $wp_triggered_time)) ) . ''; } if ($wp_sync_running) { echo '
' . esc_html__('Sync: In Progress', 'valuespace'); echo ' '; } elseif ($wp_completed_time && is_numeric($wp_completed_time)) { echo '
' . sprintf( /* translators: %s: Synchronization completion date and time. */ esc_html__('Completed: %s', 'valuespace'), esc_html(vasp_info_format_time((int) $wp_completed_time)) ) . ''; } ?>
' . sprintf( /* translators: %s: Date and time associated with this status. */ esc_html__('Since %s', 'valuespace'), esc_html(vasp_info_format_time($wc_connected_time)) ) . ''; } ?>


()
0; $event_feed = $vasp_sync_feeds['events']; $event_error_display = vasp_info_feed_error_display($event_feed, 'events', 'valuespace'); if ($event_state === 'paused') { $event_health = __('Paused', 'valuespace'); $event_reason = __('Event delivery is paused. Check the event collection setting and ValueSpace connection.', 'valuespace'); } elseif ($event_state === 'running') { $event_health = __('Sending', 'valuespace'); $event_reason = __('Events are being sent to ValueSpace.', 'valuespace'); } elseif ($event_pending_count > 0 && $event_has_failures) { $event_health = __('Needs attention', 'valuespace'); $event_public_error = function_exists('vasp_event_delivery_public_error') ? vasp_event_delivery_public_error($event_sync) : ['message' => __('Event delivery could not be completed. The events remain queued and will be retried automatically.', 'valuespace')]; $event_reason = $event_public_error['message']; } elseif ($event_pending_count > 0 && $vasp_sync_feeds['events']['cron_overdue']) { $event_health = __('Needs attention', 'valuespace'); $event_reason = __('Automatic event sending is overdue. Your site may not be starting scheduled tasks.', 'valuespace'); } elseif ($event_pending_count > 0 && $event_state === 'unscheduled') { $event_health = __('Needs attention', 'valuespace'); $event_reason = __('Waiting events are not scheduled to be sent. Automatic tasks may need attention.', 'valuespace'); } elseif ($event_pending_count > 0) { $event_health = __('Scheduled', 'valuespace'); $event_reason = __('Waiting events will be sent automatically.', 'valuespace'); } else { $event_health = __('Up to date', 'valuespace'); $event_reason = __('No events are waiting for delivery to ValueSpace.', 'valuespace'); } $event_health_class = $event_health === __('Needs attention', 'valuespace') ? 'vasp-txt-warning' : 'vasp-txt-success'; $event_pending_url = add_query_arg([ 'page' => 'valuespace-events', 'delivery_status' => 'pending', ], admin_url('admin.php')) . '#vasp-event-list'; ?>

0) : ?> ·
__('Received and waiting to be processed', 'valuespace'), 'processing' => __('Being processed', 'valuespace'), 'processed' => __('Processed', 'valuespace'), 'needs_attention' => __('Needs attention', 'valuespace'), ]; echo esc_html($vasp_gateway_event_labels[$event_sync['gateway_status'] ?? ''] ?? __('Received by ValueSpace', 'valuespace')); if (!empty($event_sync['gateway_quarantined']) || !empty($event_sync['gateway_rejected'])) { printf( /* translators: 1: Event count that needs review, 2: Event count not accepted by ValueSpace. */ esc_html__(' — %1$d need review, %2$d were not accepted', 'valuespace'), (int) ($event_sync['gateway_quarantined'] ?? 0), (int) ($event_sync['gateway_rejected'] ?? 0) ); } ?>