This commit is contained in:
Javier Casares 2026-08-18 16:28:38 +00:00
commit e9782eaa82

View file

@ -15,28 +15,41 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
*/
class Robotstxt_Telemetry_DB {
/**
* Maximum number of log entries kept, regardless of retention period.
*
* @var int
*/
const MAX_LOG_ROWS = 1000;
/**
* Whether the schema was checked in this request.
*
* @var bool
*/
private static bool $did_check = false;
private static $did_check = false;
/**
* Get the table name.
*
* In global mode all sites share the main site table.
*
* @return string
*/
public static function get_table_name(): string {
public static function get_table_name() {
global $wpdb;
if ( Robotstxt_Telemetry_Network::is_global() ) {
return $wpdb->get_blog_prefix( Robotstxt_Telemetry_Network::main_site_id() ) . 'robotstxt_telemetry_logs';
}
return $wpdb->prefix . 'robotstxt_telemetry_logs';
}
/**
* Activation hook.
*/
public static function activate(): void {
public static function activate() {
self::create_table();
update_option( 'robotstxt_telemetry_db_version', ROBOTSTXT_TELEMETRY_DB_VERSION );
}
@ -44,13 +57,18 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
/**
* Ensure the database schema is up to date.
*/
public static function maybe_upgrade(): void {
public static function maybe_upgrade() {
if ( self::$did_check ) {
return;
}
self::$did_check = true;
// In global mode the shared table belongs to the main site only.
if ( Robotstxt_Telemetry_Network::is_global() && ! is_main_site() ) {
return;
}
$installed_version = get_option( 'robotstxt_telemetry_db_version' );
if ( ROBOTSTXT_TELEMETRY_DB_VERSION !== $installed_version ) {
self::create_table();
@ -61,7 +79,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
/**
* Create or update the logs table.
*/
public static function create_table(): void {
public static function create_table() {
global $wpdb;
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
@ -99,7 +117,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
*
* @param array $data Log data.
*/
public static function insert_log( array $data ): void {
public static function insert_log( array $data ) {
global $wpdb;
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Insert into the plugin's custom table via the wpdb API.
@ -128,15 +146,14 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
* @param int $log_id Log ID.
* @return array|null
*/
public static function get_log( int $log_id ): ?array {
public static function get_log( $log_id ) {
global $wpdb;
$table_name = self::get_table_name();
$log = $wpdb->get_row( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; single-row view is never cached.
$wpdb->prepare(
'SELECT * FROM %i WHERE id = %d',
$table_name,
"SELECT * FROM {$table_name} WHERE id = %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
$log_id
),
ARRAY_A
@ -155,7 +172,7 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
* @param int $log_id Log ID.
* @return bool
*/
public static function delete_log( int $log_id ): bool {
public static function delete_log( $log_id ) {
global $wpdb;
$deleted = $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
@ -166,5 +183,65 @@ if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
return false !== $deleted;
}
/**
* Delete all log rows.
*
* @return void
*/
public static function delete_all_logs() {
global $wpdb;
$table_name = self::get_table_name();
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
"DELETE FROM {$table_name}" // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
);
}
/**
* Delete logs according to the retention settings.
*
* Removes entries older than the configured retention period and
* keeps at most MAX_LOG_ROWS entries (newest first). Runs when the
* logs list is visited and twice daily via cron.
*
* @return void
*/
public static function cleanup() {
global $wpdb;
$table_name = self::get_table_name();
$retention = Robotstxt_Telemetry_Network::get_setting( 'robotstxt_telemetry_retention_period', '12hours' );
$periods = array(
'12hours' => 12 * HOUR_IN_SECONDS,
'1day' => DAY_IN_SECONDS,
'3days' => 3 * DAY_IN_SECONDS,
);
if ( isset( $periods[ $retention ] ) ) {
$cutoff = gmdate( 'Y-m-d H:i:s', time() - $periods[ $retention ] );
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
$wpdb->prepare( "DELETE FROM {$table_name} WHERE created_at < %s", $cutoff ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
);
}
// Keep only the newest MAX_LOG_ROWS entries.
$threshold_id = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Custom table read via the wpdb API; cleanup threshold lookup.
$wpdb->prepare(
"SELECT id FROM {$table_name} ORDER BY id DESC LIMIT 1 OFFSET %d", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
self::MAX_LOG_ROWS - 1
)
);
if ( $threshold_id > 0 ) {
$wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
$wpdb->prepare( "DELETE FROM {$table_name} WHERE id < %d", $threshold_id ) // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is internal; the %i placeholder requires WordPress 6.2.
);
}
}
}
}