prefix . 'aiforya_al_events'; } /** * Insère un évènement. * * @param array $event Données de l'évènement. * @return void */ public function insert( array $event ): void { global $wpdb; $row = [ 'event_time' => isset( $event['event_time'] ) ? (string) $event['event_time'] : current_time( 'mysql' ), 'user_id' => isset( $event['user_id'] ) ? (int) $event['user_id'] : 0, 'user_login' => isset( $event['user_login'] ) ? substr( (string) $event['user_login'], 0, 180 ) : '', 'action' => isset( $event['action'] ) ? substr( (string) $event['action'], 0, 60 ) : '', 'object_type' => isset( $event['object_type'] ) ? substr( (string) $event['object_type'], 0, 30 ) : '', 'object_id' => isset( $event['object_id'] ) ? (int) $event['object_id'] : 0, 'description' => isset( $event['description'] ) ? substr( (string) $event['description'], 0, 500 ) : '', 'ip' => isset( $event['ip'] ) ? substr( (string) $event['ip'], 0, 45 ) : '', ]; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching $wpdb->insert( $this->table_name(), $row, [ '%s', '%d', '%s', '%s', '%s', '%d', '%s', '%s' ] ); } /** * Récupère les évènements récents. * * @param int $limit Nombre maximum de lignes. * @return array */ public function recent( int $limit = 50 ): array { global $wpdb; $limit = max( 1, min( 500, $limit ) ); $table = $this->table_name(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $rows = $wpdb->get_results( $wpdb->prepare( "SELECT id, event_time, user_id, user_login, action, object_type, object_id, description, ip FROM %i ORDER BY event_time DESC, id DESC LIMIT %d", $table, $limit ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared return is_array( $rows ) ? $rows : []; } /** * Nombre total d'évènements. * * @return int */ public function count_all(): int { global $wpdb; $table = $this->table_name(); $sql = "SELECT COUNT(*) FROM `{$table}`"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $count = $wpdb->get_var( $sql ); return (int) $count; } /** * Vide tout le journal. * * @return void */ public function clear(): void { global $wpdb; $table = $this->table_name(); $sql = "DELETE FROM `{$table}`"; // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.NotPrepared $wpdb->query( $sql ); } /** * Supprime les évènements plus vieux que N jours. * * @param int $days Nombre de jours de conservation. * @return void */ public function purge_old( int $days ): void { global $wpdb; $days = max( 1, min( 3650, $days ) ); $table = $this->table_name(); $cutoff = gmdate( 'Y-m-d H:i:s', time() - ( $days * DAY_IN_SECONDS ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $wpdb->query( $wpdb->prepare( "DELETE FROM %i WHERE event_time < %s", $table, $cutoff ) ); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared } } endif;