insert( $table, array( 'event' => sanitize_key( $event ), 'object_type' => sanitize_key( $object_type ), 'object_id' => $object_id ? absint( $object_id ) : null, 'job_id' => $job_id ? absint( $job_id ) : null, 'user_id' => absint( $user_id ), 'before_value' => is_array( $before_value ) || is_object( $before_value ) ? wp_json_encode( $before_value ) : (string) $before_value, 'after_value' => is_array( $after_value ) || is_object( $after_value ) ? wp_json_encode( $after_value ) : (string) $after_value, 'created_at' => current_time( 'mysql', true ), ), array( '%s', '%s', '%d', '%d', '%d', '%s', '%s', '%s' ) ); return $inserted ? $wpdb->insert_id : false; } /** * Retrieve audit log entries with pagination. * * @param array $args Query arguments (limit, offset, event, object_type, job_id). * @return array{items: array, total: int} */ public static function query( array $args = array() ) { global $wpdb; $table = Schema::get_table_name( 'slugh_audit_log' ); $limit = isset( $args['limit'] ) ? max( 1, absint( $args['limit'] ) ) : 20; $offset = isset( $args['offset'] ) ? max( 0, absint( $args['offset'] ) ) : 0; $where = 'WHERE 1=1'; $binds = array(); if ( ! empty( $args['event'] ) ) { $where .= ' AND event = %s'; $binds[] = $args['event']; } if ( ! empty( $args['object_type'] ) ) { $where .= ' AND object_type = %s'; $binds[] = $args['object_type']; } if ( ! empty( $args['job_id'] ) ) { $where .= ' AND job_id = %d'; $binds[] = absint( $args['job_id'] ); } // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare, WordPress.DB.PreparedSQLPlaceholders.ReplacementsWrongNumber, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- The WHERE clause is built from fixed SQL fragments; every value is bound by prepare(). $total = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM %i {$where}", array_merge( array( $table ), $binds ) ) ); $binds[] = $limit; $binds[] = $offset; // phpcs:ignore PluginCheck.Security.DirectDB.UnescapedDBParameter -- The WHERE clause is built from fixed SQL fragments; every value is bound by prepare(). $items = $wpdb->get_results( $wpdb->prepare( "SELECT * FROM %i {$where} ORDER BY id DESC LIMIT %d OFFSET %d", array_merge( array( $table ), $binds ) ) ); // phpcs:enable return array( 'items' => $items, 'total' => $total, ); } /** * Count entries older than a cutoff. * * @param string $before_gmt GMT datetime cutoff. * @return int */ public static function count_before( $before_gmt ) { global $wpdb; $table = Schema::get_table_name( 'slugh_audit_log' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared return (int) $wpdb->get_var( $wpdb->prepare( 'SELECT COUNT(*) FROM %i WHERE created_at < %s', $table, $before_gmt ) ); } /** * Purge entries older than a cutoff. Only ever called from an explicit administrator action * after an export was offered; ordinary cleanup never deletes audit entries. * * @param string $before_gmt GMT datetime cutoff. * @return int Number of deleted entries. */ public static function purge_before( $before_gmt ) { global $wpdb; $table = Schema::get_table_name( 'slugh_audit_log' ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared $deleted = (int) $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE created_at < %s', $table, $before_gmt ) ); self::log( 'audit_purge', 'audit_log', 0, array( 'before' => $before_gmt ), array( 'deleted' => $deleted ) ); return $deleted; } /** * All entries for CSV export, optionally only those older than a cutoff. * * @param string $before_gmt Optional GMT cutoff. * @return array */ public static function export_rows( $before_gmt = '' ) { global $wpdb; $table = Schema::get_table_name( 'slugh_audit_log' ); // phpcs:disable WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching, WordPress.DB.PreparedSQL.InterpolatedNotPrepared if ( '' !== $before_gmt ) { return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i WHERE created_at < %s ORDER BY id ASC', $table, $before_gmt ) ); } return $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM %i ORDER BY id ASC', $table ) ); // phpcs:enable } }