$selectedAgentIds * @var bool $agentsFetchFailed * * phpcs:disable WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound * phpcs:disable WordPress.Security.NonceVerification.Recommended */ declare(strict_types=1); use Agent001\Admin\AgentSelectionController; use Agent001\Admin\ConnectController; use Agent001\Admin\SettingsPage; use Agent001\Admin\SyncProgress; if ( ! defined( 'ABSPATH' ) ) { exit; } $phase = (string) ( $progress['phase'] ?? SyncProgress::PHASE_IDLE ); $isRunning = (bool) ( $progress['running'] ?? false ); $processed = (int) ( $progress['processed'] ?? 0 ); $total = (int) ( $progress['total'] ?? 0 ); $expectedBatches = (int) ( $progress['expected_batches'] ?? 0 ); if ( in_array( $phase, [ SyncProgress::PHASE_DONE, SyncProgress::PHASE_ERROR ], true ) ) { $percent = 100; } elseif ( $phase === SyncProgress::PHASE_INDEXING ) { $percent = 50 + ( $expectedBatches > 0 ? (int) round( min( 1, (int) ( $progress['processed_batches'] ?? 0 ) / $expectedBatches ) * 49 ) : 0 ); } else { $percent = $total > 0 ? min( 50, (int) floor( ( $processed / $total ) * 50 ) ) : 0; } $lastMsg = (string) ( $progress['message'] ?? '' ); $connectErrors = [ 'invalid' => \__( 'Connection callback was missing required data. Please try again.', 'agent001-support-assistant-for-woocommerce' ), 'state' => \__( 'Connection state expired or did not match. Please start over.', 'agent001-support-assistant-for-woocommerce' ), 'exchange' => \__( 'Backend rejected the authorization code. Please try connecting again.', 'agent001-support-assistant-for-woocommerce' ), 'locked' => \__( 'API key is hard-coded via AGENT001_API_KEY. Remove the constant from wp-config.php to use the Connect button.', 'agent001-support-assistant-for-woocommerce' ), ]; $connectErrorKey = isset( $_GET['connect_error'] ) && is_string( $_GET['connect_error'] ) ? \sanitize_key( \wp_unslash( (string) $_GET['connect_error'] ) ) : ''; $flagConnected = isset( $_GET['connected'] ) && is_string( $_GET['connected'] ) && '1' === \sanitize_key( \wp_unslash( (string) $_GET['connected'] ) ); $flagDisconnect = isset( $_GET['disconnected'] ) && is_string( $_GET['disconnected'] ) && '1' === \sanitize_key( \wp_unslash( (string) $_GET['disconnected'] ) ); $flagAgentsRefreshed = isset( $_GET['agents_refreshed'] ) && is_string( $_GET['agents_refreshed'] ) && '1' === \sanitize_key( \wp_unslash( (string) $_GET['agents_refreshed'] ) ); $selectedAgentId = (string) ( $selectedAgentIds[0] ?? '' ); $hasAgent = $selectedAgentId !== ''; $selectedAgent = null; foreach ( $availableAgents as $agent ) { if ( (string) ( $agent['id'] ?? '' ) === $selectedAgentId ) { $selectedAgent = $agent; break; } } $connectUrl = \wp_nonce_url( \admin_url( 'admin-post.php?action=' . ConnectController::START_ACTION ), ConnectController::START_ACTION ); $disconnectUrl = \wp_nonce_url( \admin_url( 'admin-post.php?action=' . ConnectController::DISCONNECT_ACTION ), ConnectController::DISCONNECT_ACTION ); $refreshUrl = \wp_nonce_url( \admin_url( 'admin-post.php?action=' . AgentSelectionController::REFRESH_ACTION ), AgentSelectionController::REFRESH_ACTION ); $phaseLabels = [ SyncProgress::PHASE_IDLE => \__( 'Idle', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_QUEUED => \__( 'Queued…', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_READING => \__( 'Reading products…', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_POSTING => \__( 'Sending to API…', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_INDEXING => \__( 'Saving…', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_DONE => \__( 'Sync completed.', 'agent001-support-assistant-for-woocommerce' ), SyncProgress::PHASE_ERROR => \__( 'Sync failed.', 'agent001-support-assistant-for-woocommerce' ), ]; ?>