$meta Optional metadata. */ public function record( string $type, string $message, array $meta = array() ): void { $events = $this->all(); $events[] = array( 'type' => $type, 'message' => $message, 'meta' => $meta, 'time' => time(), ); if ( count( $events ) > self::MAX ) { $events = array_slice( $events, -1 * self::MAX ); } update_option( self::OPTION, $events, false ); } /** * All events, oldest first. * * @return array, time: int}> */ public function all(): array { $stored = get_option( self::OPTION, array() ); if ( ! is_array( $stored ) ) { return array(); } $out = array(); foreach ( $stored as $row ) { if ( ! is_array( $row ) || ! isset( $row['type'], $row['message'], $row['time'] ) ) { continue; } $out[] = array( 'type' => (string) $row['type'], 'message' => (string) $row['message'], 'meta' => isset( $row['meta'] ) && is_array( $row['meta'] ) ? $row['meta'] : array(), 'time' => (int) $row['time'], ); } return $out; } /** * Events since a unix timestamp. * * @param int $since Unix timestamp. * @return array, time: int}> */ public function since( int $since ): array { return array_values( array_filter( $this->all(), static function ( array $event ) use ( $since ): bool { return $event['time'] >= $since; } ) ); } /** * Clear the log. */ public function clear(): void { delete_option( self::OPTION ); } }