prefix . 'pixelkeep_log'; } /** * Writes an event. * * @param string $level `info`, `warning` or `error`. * @param string $code Event or error code. * @param int $attachment_id Related attachment, or 0. * @param string $detail Technical detail, stored only when the debug log is on. * @return void */ public function write( string $level, string $code, int $attachment_id = 0, string $detail = '' ): void { global $wpdb; if ( 'info' === $level && ! $this->settings->flag( 'debug_log' ) ) { return; } $wpdb->insert( $this->table(), array( 'level' => $level, 'code' => $code, 'attachment_id' => $attachment_id, 'message' => $this->settings->flag( 'debug_log' ) ? $detail : '', 'created_at' => current_time( 'mysql', true ), ) ); } /** * Returns the newest entries. * * @param int $limit Maximum rows. * @return array> */ public function recent( int $limit = 50 ): array { global $wpdb; $table = $this->table(); $rows = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM {$table} ORDER BY id DESC LIMIT %d", $limit ), ARRAY_A ); return is_array( $rows ) ? $rows : array(); } /** * Counts entries. * * @return int */ public function count(): int { global $wpdb; $table = $this->table(); return (int) $wpdb->get_var( "SELECT COUNT(*) FROM {$table}" ); } /** * Deletes the oldest rows beyond the cap. * * @return int Rows removed. */ public function prune(): int { global $wpdb; $table = $this->table(); $excess = $this->count() - self::MAX_ROWS; if ( $excess <= 0 ) { return 0; } return (int) $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} ORDER BY id ASC LIMIT %d", $excess ) ); } /** * Empties the log. * * @return void */ public function clear(): void { global $wpdb; $table = $this->table(); $wpdb->query( "TRUNCATE TABLE {$table}" ); } }