| 1 |
<?php |
| 2 |
/** |
| 3 |
* Persistent store for WpStream log entries. |
| 4 |
* |
| 5 |
* Wraps a single wp_options row: prepends new WpStream_Log_Entry records, |
| 6 |
* caps the list at a fixed size, reads the full history back, and prunes |
| 7 |
* entries older than 30 days. |
| 8 |
* |
| 9 |
* @package Wpstream |
| 10 |
* @subpackage Wpstream/includes/Logger |
| 11 |
*/ |
| 12 |
|
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
if ( ! defined( 'ABSPATH' ) ) { |
| 16 |
exit; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Class WpStream_Log_Entry |
| 21 |
* |
| 22 |
* Represents a single log entry in the WpStream logging system. |
| 23 |
*/ |
| 24 |
class WpStream_Logger { |
| 25 |
/** |
| 26 |
* The option name where the logs are stored in wp_options |
| 27 |
* |
| 28 |
* @var string |
| 29 |
*/ |
| 30 |
private $option_name = 'wpstream_logs'; |
| 31 |
|
| 32 |
/** |
| 33 |
* Maximum number of logs to store |
| 34 |
* |
| 35 |
* @var int |
| 36 |
*/ |
| 37 |
private $max_logs = 100; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Constructor; no initialization is required. |
| 42 |
*/ |
| 43 |
public function __construct() { |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Add a log entry to the logs |
| 48 |
* |
| 49 |
* @param WpStream_Log_Entry $entry Log entry object |
| 50 |
* |
| 51 |
* @return bool True on success, false on failure |
| 52 |
*/ |
| 53 |
public function add( $entry ): bool { |
| 54 |
// Only accept genuine log-entry objects; reject anything else. |
| 55 |
if ( ! ($entry instanceof WpStream_Log_Entry) ) { |
| 56 |
return false; |
| 57 |
} |
| 58 |
|
| 59 |
// Load the current history so the new entry can be prepended to it. |
| 60 |
$logs = $this->getAll(); |
| 61 |
|
| 62 |
// Flatten the entry object into the plain array shape stored in wp_options. |
| 63 |
$log_data = [ |
| 64 |
'timestamp' => $entry->timestamp, |
| 65 |
'type' => $entry->type, |
| 66 |
'description' => $entry->description, |
| 67 |
]; |
| 68 |
|
| 69 |
// Newest entry goes to the front of the list. |
| 70 |
array_unshift( $logs, $log_data ); |
| 71 |
|
| 72 |
// Enforce the size cap by trimming the oldest overflow off the tail. |
| 73 |
if ( count( $logs ) > $this->max_logs ) { |
| 74 |
$logs = array_slice( $logs, 0, $this->max_logs ); |
| 75 |
} |
| 76 |
|
| 77 |
// Persist the updated history back to the options table. autoload=false: |
| 78 |
// this 100-entry blob must not ride in alloptions on every request — |
| 79 |
// and since each add changes the value, legacy autoloaded rows get |
| 80 |
// their flag corrected here too. |
| 81 |
return update_option( $this->option_name, $logs, false ); |
| 82 |
} |
| 83 |
|
| 84 |
/** |
| 85 |
* Get all logs |
| 86 |
* |
| 87 |
* @return array Array of log entries |
| 88 |
*/ |
| 89 |
public function getAll(): array { |
| 90 |
// Read the stored history, defaulting to an empty array when unset. |
| 91 |
$logs = get_option( $this->option_name, array() ); |
| 92 |
|
| 93 |
// Guard against a corrupted/non-array option value. |
| 94 |
return is_array( $logs ) ? $logs : array(); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Clear logs older than 30 days |
| 99 |
* |
| 100 |
* @return bool True on success, false otherwise |
| 101 |
*/ |
| 102 |
public function clear_old_logs(): bool { |
| 103 |
// Start from the full stored history. |
| 104 |
$logs = $this->getAll(); |
| 105 |
|
| 106 |
// Compute the cutoff instant: 30 days before now. |
| 107 |
$one_month_ago = time() - (30 * DAY_IN_SECONDS); |
| 108 |
// Keep only entries whose timestamp is at or after the cutoff. |
| 109 |
$filtered_logs = array_filter( $logs, function( $log ) use ( $one_month_ago ) { |
| 110 |
return $log['timestamp'] >= $one_month_ago; |
| 111 |
}); |
| 112 |
|
| 113 |
// Save the pruned history back to the options table (never autoloaded). |
| 114 |
return update_option( $this->option_name, $filtered_logs, false ); |
| 115 |
} |
| 116 |
} |