|null */ public function get( string $url ): ?array { $stored = get_transient( $this->key( $url ) ); if ( ! is_array( $stored ) ) { return null; } return array( 'title' => is_string( $stored['title'] ?? null ) ? $stored['title'] : '', 'items' => is_array( $stored['items'] ?? null ) ? array_values( $stored['items'] ) : array(), 'fetched_at' => (int) ( $stored['fetched_at'] ?? 0 ), 'last_attempt' => (int) ( $stored['last_attempt'] ?? 0 ), 'failures' => (int) ( $stored['failures'] ?? 0 ), 'error' => is_string( $stored['error'] ?? null ) ? $stored['error'] : '', ); } /** * Records a successful fetch, replacing the stored items. * * @param string $url Feed URL. * @param Fetch_Result $result Successful result. * @param int $now Current Unix time. */ public function store_success( string $url, Fetch_Result $result, int $now ): void { $this->write( $url, array( 'title' => $result->title, 'items' => $result->items, 'fetched_at' => $now, 'last_attempt' => $now, 'failures' => 0, 'error' => '', ) ); } /** * Records a failed fetch. The previously stored items are kept. * * @param string $url Feed URL. * @param string $error Error code. * @param int $now Current Unix time. */ public function store_failure( string $url, string $error, int $now ): void { $entry = $this->get( $url ) ?? array( 'title' => '', 'items' => array(), 'fetched_at' => 0, 'failures' => 0, ); $entry['last_attempt'] = $now; $entry['failures'] = (int) $entry['failures'] + 1; $entry['error'] = $error; $this->write( $url, $entry ); } /** * Removes a feed's entry. * * @param string $url Feed URL. */ public function delete( string $url ): void { delete_transient( $this->key( $url ) ); } /** * Writes an entry. * * @param string $url Feed URL. * @param array $entry Entry. */ private function write( string $url, array $entry ): void { set_transient( $this->key( $url ), $entry, self::HARD_TTL ); } }