client = $client; $this->logger = $logger; } /** * The status: cached when fresh, otherwise fetched (and cached when it succeeds). * * @return StoreStatus|CoreError The status, or why it couldn't be fetched (already logged). */ public function get(): StoreStatus|CoreError { $cached = StoreStatus::from_array( get_transient( self::TRANSIENT ) ); if ( null !== $cached ) { return $cached; } $response = $this->client->get( self::PATH ); if ( ! $response->is_success() ) { // Without a connection nothing was sent; the page explains that state itself. if ( ! in_array( $response->error->code, array( CoreError::NOT_ACTIVATED, CoreError::CONNECTION_UNREADABLE ), true ) ) { $this->logger->core_error( self::OPERATION, $response->error ); } return $response->error; } $status = StoreStatus::from_response( $response->data, time() ); if ( null === $status ) { $error = new CoreError( CoreError::INVALID_RESPONSE, $response->status, $response->request_id ); $this->logger->core_error( self::OPERATION, $error ); return $error; } set_transient( self::TRANSIENT, $status->to_array(), self::CACHE_SECONDS ); return $status; } /** * Forgets the cached status, so the next page load asks the core. */ public static function clear(): void { delete_transient( self::TRANSIENT ); } }