*/ public static function read( bool $fresh = false ): array { if ( ! $fresh ) { $cached = get_transient( self::CACHE ); if ( is_array( $cached ) ) { return $cached; } } $facts = array(); $blockers = array(); // ── Engines ───────────────────────────────────────────────────────── $keys = get_option( 'airanksuite_ai_keys', array() ); $engines = is_array( $keys ) ? count( $keys ) : 0; $connected = $engines > 0; // ── The agent ─────────────────────────────────────────────────────── $state = get_option( 'airanksuite_autopilot_state', array() ); $state = is_array( $state ) ? $state : array(); $queue = (array) ( $state['queue'] ?? array() ); $unfinished = array() !== $queue && (int) ( $state['cursor'] ?? 0 ) < count( $queue ); // "Unfinished" is not "running". The run loop lives in the browser, so // closing the tab leaves a half-done queue behind for ever. A run that // has not ticked in five minutes is abandoned, and saying so is the // difference between a light that means something and one that is // permanently purple. $ticked = (int) ( $state['ticked'] ?? $state['started'] ?? 0 ); $running = $unfinished && $ticked > 0 && ( time() - $ticked ) < 5 * MINUTE_IN_SECONDS; $stalled = $unfinished && ! $running; $now = $running ? ( $state['now'] ?? null ) : null; if ( $running ) { $facts[] = array( 'kind' => 'running', 'text' => is_array( $now ) && isset( $now['title'] ) /* translators: %s: page title. */ ? sprintf( __( 'Working on “%s”', 'ai-rank-suite' ), (string) $now['title'] ) : __( 'The autopilot is running', 'ai-rank-suite' ), 'done' => (int) ( $state['done'] ?? 0 ), 'total' => count( $queue ), ); } if ( $stalled ) { $facts[] = array( 'kind' => 'stalled', 'text' => sprintf( /* translators: %d: number of pages left. */ __( 'An autopilot run was left unfinished — %d pages still queued', 'ai-rank-suite' ), count( $queue ) - (int) ( $state['cursor'] ?? 0 ) ), 'href' => 'autopilot', ); } if ( (int) ( $state['failed'] ?? 0 ) >= 5 ) { $blockers[] = array( 'text' => __( 'The last autopilot run stopped after five failures — usually a wrong key or an account out of credit.', 'ai-rank-suite' ), 'href' => 'autopilot', ); } // ── Front-end faults ──────────────────────────────────────────────── // // A callback of ours threw on the public site and the guard caught it, // so the visitor got a working page. That is the right behaviour and it // is also the reason nobody would ever find out: there is no error, no // white screen, nothing in the logs of a production install with // WP_DEBUG off. It has to arrive somewhere, and the toolbar light is // the one surface every editor already looks at. $faults = \AiRankSuite\Support\Guard::failures(); if ( array() !== $faults ) { $blockers[] = array( 'text' => sprintf( /* translators: %d: number of failing callbacks. */ _n( '%d part of the plugin is failing on the public site — pages still render, but that output is missing.', '%d parts of the plugin are failing on the public site — pages still render, but that output is missing.', count( $faults ), 'ai-rank-suite' ), count( $faults ) ), 'href' => 'tools', ); } // ── Content ───────────────────────────────────────────────────────── $missing = 0; $total = 0; foreach ( Library::overview() as $type ) { $missing += (int) $type['no_desc']; $total += (int) $type['total']; } // ── Site audit ────────────────────────────────────────────────────── // // The count from the last completed run, not a fresh audit: auditing // five thousand pages is a background job, and running one because // somebody loaded wp-admin would be indefensible. $job = get_option( 'airanksuite_audit_job', array() ); $job = is_array( $job ) ? $job : array(); $audited = 'done' === (string) ( $job['state'] ?? '' ); $failed = $audited ? (int) ( $job['issues'] ?? 0 ) : 0; // ── Verdict ───────────────────────────────────────────────────────── // // Order matters: a blocker outranks a count, and a count outranks // "everything is fine". The share is what keeps green reachable — one // missing description out of a thousand pages is not an amber site. $share = $total > 0 ? $missing / $total : 0.0; if ( array() !== $blockers ) { $level = 'red'; $label = __( 'Needs attention', 'ai-rank-suite' ); } elseif ( $running ) { $level = 'working'; $label = __( 'Working', 'ai-rank-suite' ); } elseif ( $failed > 0 || $share > 0.25 ) { $level = 'amber'; $label = __( 'Room to improve', 'ai-rank-suite' ); } else { $level = 'green'; $label = $connected ? __( 'All clear', 'ai-rank-suite' ) : __( 'Healthy', 'ai-rank-suite' ); } if ( $missing > 0 ) { $facts[] = array( 'kind' => 'content', 'text' => sprintf( /* translators: %d: number of pages. */ _n( '%d page has no meta description', '%d pages have no meta description', $missing, 'ai-rank-suite' ), $missing ), 'href' => 'content', ); } if ( $failed > 0 ) { $facts[] = array( 'kind' => 'audit', 'text' => sprintf( /* translators: %d: number of issues. */ _n( '%d issue from the last site audit', '%d issues from the last site audit', $failed, 'ai-rank-suite' ), $failed ), 'href' => 'audit', ); } if ( ! $audited ) { $facts[] = array( 'kind' => 'audit', 'text' => __( 'The site has not been audited yet', 'ai-rank-suite' ), 'href' => 'audit', ); } if ( ! $connected ) { $facts[] = array( 'kind' => 'engine', 'text' => __( 'No AI engine connected', 'ai-rank-suite' ), 'href' => 'ai', ); } if ( array() === $facts && 'green' === $level ) { $facts[] = array( 'kind' => 'ok', 'text' => __( 'Nothing is waiting for you.', 'ai-rank-suite' ), ); } $out = array( 'level' => $level, 'label' => $label, 'engines' => $engines, 'connected' => $connected, 'running' => $running, 'now' => is_array( $now ) ? $now : null, 'progress' => $running ? array( 'done' => (int) ( $state['done'] ?? 0 ), 'total' => count( $queue ), ) : null, 'missing' => $missing, 'pages' => $total, 'failing' => $failed, 'facts' => $facts, 'blockers' => $blockers, 'at' => time(), ); set_transient( self::CACHE, $out, self::TTL ); return $out; } /** * A running agent changes state every few seconds, so the cache is the * wrong tool while it is working. Callers that need the truth right now * (the poll endpoint) ask for it; the toolbar does not. */ public static function flush(): void { delete_transient( self::CACHE ); } }