'toplevel_page_valuespace', 'billing' => 'valuespace_page_valuespace-billing', 'events' => 'valuespace_page_valuespace-events', 'info' => 'options_page_vasp-info', ]; return $map[$page] ?? ''; } // --------------------------------------------------------------------------- // Main admin_notices callback // --------------------------------------------------------------------------- /** * Evaluate all state-checked notice slots and echo applicable ones. * Renders only on ValueSpace administrator screens. */ function vasp_render_admin_notices(): void { if (!current_user_can('manage_options') || !vasp_is_plugin_admin_screen()) { return; } $screen = get_current_screen(); if (!$screen) { return; } $screen_id = $screen->id; $is_connected = function_exists('vasp_valuespace_is_connected') && vasp_valuespace_is_connected(); $was_connected = get_option(VASP_VALUESPACE_WAS_CONNECTED, false); // ----------------------------------------------------------------- // Slot 1 — Connection lost (error, plugin screens except settings) // ----------------------------------------------------------------- if (!$is_connected && $was_connected) { if ( $screen_id !== vasp_get_screen_id('settings') && !vasp_is_notice_suppressed('connection_lost') ) { echo wp_kses_post(vasp_show_notice( __('ValueSpace connection lost. Please reconnect the plugin to restore the AI assistant.', 'valuespace'), 'error', 0, [ [ 'label' => __('Reconnect ValueSpace', 'valuespace'), 'url' => admin_url('admin.php?page=valuespace'), 'style' => 'primary', ], ], 'connection_lost' )); } return; // Show one state notice at a time. } // ----------------------------------------------------------------- // Slot 2 — Never connected, 24 h after install (plugin screens except settings) // ----------------------------------------------------------------- if (!$is_connected && !$was_connected) { $install_date = get_option(VASP_OPTION_INSTALLED); if ( $install_date && time() > strtotime($install_date) + DAY_IN_SECONDS && $screen_id !== vasp_get_screen_id('settings') && !vasp_is_notice_suppressed('never_connected') ) { echo wp_kses_post(vasp_show_notice( __("You installed ValueSpace but haven't connected it yet. Connect now to enable your AI assistant.", 'valuespace'), 'info', 0, [ [ 'label' => __('Connect Now', 'valuespace'), 'url' => admin_url('admin.php?page=valuespace'), 'style' => 'primary', ], ], 'never_connected' )); } return; } // ----------------------------------------------------------------- // Billing alerts (only when connected) // ----------------------------------------------------------------- // Notices never delay an admin page with a remote request. Dedicated page // actions and background maintenance refresh this cache. $cached = get_transient(VASP_TRANSIENT_BILLING_SUMMARY_CACHE); $billing = ($cached !== false) ? $cached : null; if (!is_array($billing)) { return; // API unavailable, not connected, or cache empty on non-plugin page } $alert = vasp_get_billing_alert_state($billing); if (!$alert) { return; } // ----------------------------------------------------------------- // Slot 3 — Conversations = 0 (error, plugin screens except billing) // Slot 4 — Low or critical conversations (warning, plugin screens except billing) // ----------------------------------------------------------------- $suppress_key = ($alert['type'] === 'error') ? 'billing_exhausted' : 'billing_low'; if ( $screen_id !== vasp_get_screen_id('billing') && !vasp_is_notice_suppressed($suppress_key) ) { echo wp_kses_post(vasp_show_notice( $alert['message'], $alert['type'], 0, [ [ 'label' => $alert['action_text'], 'url' => $alert['action_url'], 'style' => 'primary', ], ], $suppress_key )); } } // --------------------------------------------------------------------------- // Billing alert state helper // --------------------------------------------------------------------------- /** * Compute billing alert level from a billing summary array. * * @param array $billing Result of vasp_get_billing_summary(). * @return array|false Alert data array, or false when no alert is warranted. */ function vasp_get_billing_alert_state(array $billing) { $vasp_usage = is_array($billing['usage'] ?? null) ? $billing['usage'] : vasp_normalize_billing_usage($billing); $vasp_level = (string) ($vasp_usage['level'] ?? 'normal'); if ($vasp_level === 'exhausted') { return [ 'type' => 'error', 'message' => __('ValueSpace: AI conversations are fully used up. The AI Assistant is disabled until you add more credits.', 'valuespace'), 'action_text' => __('Go to Billing', 'valuespace'), 'action_url' => admin_url('admin.php?page=valuespace-billing'), ]; } if (in_array($vasp_level, ['low', 'critical'], true)) { return [ 'type' => 'warning', 'message' => __('ValueSpace: AI conversations are running low. Add more credits to avoid interruption.', 'valuespace'), 'action_text' => __('Go to Billing', 'valuespace'), 'action_url' => admin_url('admin.php?page=valuespace-billing'), ]; } return false; }