id !== 'toplevel_page_valuespace') { return ''; } $nonce = isset($_GET['vasp_workflow_nonce']) && is_string($_GET['vasp_workflow_nonce']) ? sanitize_text_field(wp_unslash($_GET['vasp_workflow_nonce'])) : ''; if (!wp_verify_nonce($nonce, 'vasp_workflow_notice')) { return ''; } $workflow_success = isset($_GET['workflow_success']) && is_string($_GET['workflow_success']) ? sanitize_key(wp_unslash($_GET['workflow_success'])) : ''; if ($workflow_success === '1') { $service = isset($_GET['service']) && is_string($_GET['service']) ? sanitize_key(wp_unslash($_GET['service'])) : ''; if ($service === 'wordpress') { $message_code = VASP_MSG_CONNECTED_WORDPRESS; } elseif ($service === 'woocommerce') { $message_code = VASP_MSG_CONNECTED_WOOCOMMERCE; } else { $message_code = VASP_MSG_WORKFLOW_SUCCESS; } return vasp_show_success(vasp_get_error_message($message_code)); } $workflow_error = isset($_GET['workflow_error']) && is_string($_GET['workflow_error']) ? sanitize_key(wp_unslash($_GET['workflow_error'])) : ''; if ($workflow_error === '1') { return vasp_show_error(vasp_get_error_message(VASP_ERROR_UNKNOWN_ERROR)); } return ''; } /** * Enqueue admin scripts and styles */ function vasp_enqueue_admin_settings_assets() { // Only load on the settings page. Other plugin pages own their assets. $screen = get_current_screen(); if (!$screen || $screen->id !== 'toplevel_page_valuespace') { return; } // Enqueue WordPress media scripts wp_enqueue_media(); // Enqueue integration scripts (new system) $integration_js_path = VASP_PLUGIN_DIR . 'admin/js/settings.js'; wp_enqueue_script( 'valuespace-integration', VASP_PLUGIN_URL . 'admin/js/settings.js', array('jquery', 'valuespace-notices'), file_exists($integration_js_path) ? filemtime($integration_js_path) : vasp_get_plugin_version(), true ); // Localize script with AJAX URL and nonce $config = vasp_get_config(); wp_localize_script( 'valuespace-integration', 'vaspAdmin', array( 'ajaxUrl' => admin_url('admin-ajax.php'), 'nonce' => wp_create_nonce('vasp_admin_nonce'), 'workflowNonce' => wp_create_nonce('vasp_workflow_notice'), 'systemInfoUrl' => admin_url('admin.php?page=valuespace-info'), 'feeds' => vasp_diagnostics_sync_feeds(), 'config' => array( 'dashboard_domain' => $config['dashboard_domain'] ?? '' ) ) ); // Localize strings for translation (new integration system) wp_localize_script( 'valuespace-integration', 'vaspAdminL10n', array( 'connectingValueSpace' => __('Connecting...', 'valuespace'), 'integratingWordPress' => __('Integrating WordPress...', 'valuespace'), 'integratingWooCommerce' => __('Integrating WooCommerce...', 'valuespace'), 'disconnectingWordPress' => __('Disconnecting...', 'valuespace'), 'disconnectingWooCommerce' => __('Disconnecting...', 'valuespace'), 'openingDashboard' => __('Opening Dashboard...', 'valuespace'), 'syncingWooCommerce' => __('Sending...', 'valuespace'), 'catalogSyncing' => __('Syncing...', 'valuespace'), 'syncNow' => __('Sync now', 'valuespace'), 'syncCatalog' => __('Send now', 'valuespace'), 'syncPendingChanges' => __('Sync changes', 'valuespace'), 'syncedCatalog' => __('Synced', 'valuespace'), 'stopSync' => __('Stop', 'valuespace'), 'syncError' => __('Update failed', 'valuespace'), 'retryNow' => __('Retry', 'valuespace'), 'moreDetails' => __('More details', 'valuespace'), 'catalogStatusUnavailable' => __('WordPress could not check the catalog update status.', 'valuespace'), 'catalogStatusUnavailableHint' => __('WordPress will check again automatically.', 'valuespace'), 'syncTooltip' => __('Send the latest store catalog to ValueSpace and keep automatic updates on.', 'valuespace'), 'pendingChangesTooltip' => __('Store changes are waiting to be sent to ValueSpace. Select Sync changes to send them now.', 'valuespace'), 'syncedTooltip' => __('Send the latest store catalog to ValueSpace and keep automatic updates on.', 'valuespace'), 'retryTooltip' => __('Try the interrupted catalog update again.', 'valuespace'), 'turnOffTooltip' => __('Turn off automatic catalog updates. Your existing catalog remains available.', 'valuespace'), 'stopTooltip' => __('Stop WordPress from sending more catalog data and turn off automatic updates. Information already received may continue processing.', 'valuespace'), 'catalogNoticeTitle' => __('E-commerce catalog', 'valuespace'), 'catalogFeedFailed' => __('The catalog update could not be completed.', 'valuespace'), 'imageFeedFailed' => __('Some catalogue images still need to be updated.', 'valuespace'), 'imageAuditFailed' => __('WordPress could not verify the product image status.', 'valuespace'), 'feedRetryScheduled' => __('WordPress will try again automatically.', 'valuespace'), 'feedNeedsAttention' => __('Retry the update.', 'valuespace'), 'startSync' => __('Start sync', 'valuespace'), 'disconnectSync' => __('Turn off', 'valuespace'), 'disablingSync' => __('Turning off...', 'valuespace'), 'disableSyncConfirm' => __("Stop catalog updates? WordPress will stop sending new store information, and automatic catalog updates will be turned off.\n\nAnything already received by ValueSpace may still finish updating.", 'valuespace'), 'disableRealtimeSyncConfirm' => __('Turn off realtime catalog updates? WordPress will stop sending store changes immediately. Your connected WooCommerce integration will continue scheduled catalog synchronization.', 'valuespace'), 'ajaxError' => __('The action could not be completed.', 'valuespace'), 'unknownError' => __('This action could not be completed.', 'valuespace'), 'requestFailedHint' => __('Try again later.', 'valuespace'), 'vaspUnavailable' => __('We are having trouble connecting to ValueSpace AI right now. Your saved connection is unchanged.', 'valuespace'), 'vaspChecking' => __('Checking...', 'valuespace'), 'reconnectValueSpace' => __('Reconnect ValueSpace', 'valuespace'), 'dashboardNotConfigured' => __('Dashboard URL not configured', 'valuespace'), 'resetConfirm' => __('Are you sure you want to reset the connection? This will remove all stored credentials and you will need to reconnect.', 'valuespace'), 'resetting' => __('Resetting...', 'valuespace'), 'disconnectWpConfirm' => __("You are about to disconnect WordPress.\n\nThis will prevent your ValueSpace assistant from reading your site's pages and posts. You can reconnect it later.\n\nDo you want to proceed?", 'valuespace'), 'disconnectWcConfirm' => __("You are about to disconnect WooCommerce.\n\nThis will result in your ValueSpace assistant losing access to your products, orders, and cart tracking data.\n\nDo you want to proceed?", 'valuespace'), 'publishConfirm' => __('Show the ValueSpace chat widget on this WordPress site?', 'valuespace'), 'unpublishConfirm' => __('Hide the ValueSpace chat widget on this WordPress site?', 'valuespace'), 'aiAssistantPublished' => __('The chat widget will appear on this WordPress site when its ValueSpace settings are ready.', 'valuespace'), 'aiAssistantUnpublished' => __('The chat widget is hidden on this WordPress site.', 'valuespace'), 'indexingWp' => __('Updating your AI Assistant with your website content...', 'valuespace'), 'syncingWc' => __('Updating your store catalog in ValueSpace...', 'valuespace'), 'wordpressSyncInfo' => __('ValueSpace is updating the AI Assistant with your published pages and posts. You can leave this page while it continues.', 'valuespace'), 'catalogSyncInfo' => __('This update includes published products, descriptions, prices, availability, categories, brands, attributes, and product images. Customer and order information is not included. You can leave this page while it continues.', 'valuespace'), /* translators: %s is the current catalog processing activity, for example "Preparing product search data". */ 'catalogLongRunningProcessing' => __('%s may take a while. You can leave this page and come back later.', 'valuespace'), 'catalogImagesRemaining' => /* translators: %d: Number of catalog images remaining. */ __('%d images remaining', 'valuespace'), 'catalogStageProgress' => /* translators: 1: Completed item count, 2: Total item count. */ __('%1$d of %2$d', 'valuespace'), 'catalogStageProcessed' => /* translators: %d: Completed item count. */ __('%d completed', 'valuespace'), 'catalogStageAcquisition' => __('Starting catalog update...', 'valuespace'), 'catalogStageCatalog' => __('Sending categories...', 'valuespace'), 'catalogStageBrands' => __('Sending brands...', 'valuespace'), 'catalogStageProductsChecking' => __('Checking for product updates...', 'valuespace'), 'catalogStageProducts' => __('Sending products...', 'valuespace'), 'catalogStageImages' => __('Sending missing product images...', 'valuespace'), 'catalogStageServerProducts' => __('Processing received products...', 'valuespace'), 'catalogStageServerImages' => __('Checking which product images need updating...', 'valuespace'), 'catalogStageServerPending' => __('Preparing ValueSpace processing...', 'valuespace'), 'catalogStageImagesWaiting' => __('Waiting to send product images...', 'valuespace'), 'catalogStageImagesChecking' => __('Finding product images to update...', 'valuespace'), /* translators: %d: Number of catalog items found so far. */ 'catalogItemsFound' => __('%d found so far', 'valuespace'), 'catalogStageProcessing' => __('Processing catalogue changes...', 'valuespace'), 'catalogStageAiProducts' => __('Integrating product information...', 'valuespace'), 'catalogStageAiBrands' => __('Integrating brands...', 'valuespace'), 'catalogStageAiImages' => __('Integrating product pictures...', 'valuespace'), 'catalogStageSearch' => __('Updating catalogue search...', 'valuespace'), 'catalogSending' => __('Sending your store information to ValueSpace...', 'valuespace'), 'catalogLastSync' => /* translators: %s: Date of the last successful catalog data delivery. */ __('Last sent to ValueSpace: %s', 'valuespace') ) ); } /** * Render the settings page */ function vasp_render_settings_page() { // Check user capabilities if (!current_user_can('manage_options')) { wp_die(esc_html__( 'You do not have sufficient permissions to access this page.', 'valuespace' )); } $vasp_connected = vasp_valuespace_is_connected(); // Render immediately from durable/cached state. JavaScript performs one // live availability check after the page is visible. $vasp_billing = $vasp_connected ? get_transient(VASP_TRANSIENT_BILLING_SUMMARY_CACHE) : null; $vasp_billing = is_array($vasp_billing) ? $vasp_billing : null; $vasp_widget_visibility_status = vasp_get_site_widget_visibility_status(); $wp_connected = function_exists('vasp_wordpress_is_connected') && vasp_wordpress_is_connected(); $wc_active = is_plugin_active('woocommerce/woocommerce.php') || class_exists('WooCommerce'); $wc_connected = function_exists('vasp_woocommerce_is_connected') && vasp_woocommerce_is_connected(); // Collect all notices (integration errors/denials + billing errors) before rendering. ob_start(); vasp_display_integration_notices(); echo wp_kses_post(vasp_settings_workflow_notice_html()); $notices_html = ob_get_clean(); // Billing data array (safe to use; null if unavailable or not connected). $vasp_billing_data = is_array($vasp_billing) ? $vasp_billing : []; $vasp_plan_name = $vasp_billing_data['planName'] ?? null; $is_trial = $vasp_billing_data['isInTrial'] ?? false; $trial_exp = $vasp_billing_data['trialExpiresAt'] ?? null; $cancellation_date = $vasp_billing_data['cancellationDate'] ?? null; $sub_status = $vasp_billing_data['subscriptionStatus'] ?? 'unknown'; // Never infer a subscription while the canonical status response is // unavailable. A null planName means Free only inside a confirmed billing // block; otherwise omit the plan label until status refresh succeeds. $display_plan_name = is_array($vasp_billing) ? ($vasp_plan_name ?: esc_html__('Free', 'valuespace')) : null; $days_left = 0; $trial_date = null; if ($is_trial && $trial_exp) { $trial_date = new DateTime($trial_exp); $days_left = max(0, (int) ceil(($trial_date->getTimestamp() - time()) / DAY_IN_SECONDS)); } ?>

>

VALUESPACE

getTimestamp())) ); ?>


'idle', 'service' => null, 'pipeline' => null, 'stage' => null, 'startedAt' => null, 'completedAt' => null, 'error' => null, 'startRequired' => false, 'processed' => null, 'total' => null, 'productsProcessed' => null, 'productsQueued' => null, 'productsRemaining' => null, 'productUpdates' => null, 'brands' => null, 'categories' => null, 'tags' => null, 'attributes' => null, 'sent' => null, 'skipped' => null, 'progress' => null, 'activitySource' => null, 'changesWaiting' => !empty($catalog_local_progress['changesWaiting']), ]; if ($wp_connected) { $wp_completed_time = get_option(VASP_OPTION_WORDPRESS_SYNC_COMPLETED_TIME, 0); if ($wp_completed_time > 0) { // Completed WordPress sync does not block the UI. $sync_state['status'] = 'idle'; } elseif (get_option(VASP_OPTION_WORDPRESS_INGESTION_STATUS) === 'started') { // /start confirmed dispatched; poll for completion. $sync_state['status'] = 'in_progress'; $sync_state['service'] = 'wordpress'; $sync_state['stage'] = 'ingestion'; } elseif (get_option(VASP_OPTION_WORDPRESS_INDEXATION_TRIGGERED)) { // Triggered but /start is not confirmed yet; re-fire the idempotent start. $sync_state['status'] = 'in_progress'; $sync_state['service'] = 'wordpress'; $sync_state['stage'] = 'start_pending'; $sync_state['startRequired'] = true; } } elseif (get_option(VASP_OPTION_WORDPRESS_INDEXATION_TRIGGERED)) { // Page reloaded while WP AJAX was still in flight; keep progress UI active. $sync_state['status'] = 'in_progress'; $sync_state['service'] = 'wordpress'; $sync_state['stage'] = 'connection_pending'; } // Preserve local catalog delivery across refresh. JavaScript replaces // this provisional phase with keyed scheduler diagnostics. if ($sync_state['status'] !== 'in_progress' && $catalog_locally_active) { $sync_state['status'] = 'in_progress'; $sync_state['service'] = 'woocommerce'; $sync_state['pipeline'] = 'push'; $sync_state['stage'] = function_exists('vasp_wc_catalog_display_stage_for_step') ? vasp_wc_catalog_display_stage_for_step((string) get_option( VASP_OPTION_WC_CATALOG_STEP, '' )) : 'start_requested'; $sync_state['activitySource'] = 'local_push'; } ?>

time()): ?>
getTimestamp(); printf( /* translators: %s: Subscription cancellation date. */ esc_html__('Cancels on %s', 'valuespace'), esc_html(wp_date('M j, Y', $cancel_ts)) ); ?>

' . esc_html($environment_label) . '
'; } ?>