v0.2.0
This commit is contained in:
commit
8a85a4d9af
1 changed files with 3258 additions and 0 deletions
170
includes/class-robotstxt-telemetry-db.php
Normal file
170
includes/class-robotstxt-telemetry-db.php
Normal file
|
|
@ -0,0 +1,170 @@
|
|||
<?php
|
||||
/**
|
||||
* Database handler.
|
||||
*
|
||||
* @package RobotstxtTelemetry
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
if ( ! class_exists( 'Robotstxt_Telemetry_DB' ) ) {
|
||||
/**
|
||||
* Class Robotstxt_Telemetry_DB
|
||||
*/
|
||||
class Robotstxt_Telemetry_DB {
|
||||
|
||||
/**
|
||||
* Whether the schema was checked in this request.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
private static bool $did_check = false;
|
||||
|
||||
/**
|
||||
* Get the table name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public static function get_table_name(): string {
|
||||
global $wpdb;
|
||||
|
||||
return $wpdb->prefix . 'robotstxt_telemetry_logs';
|
||||
}
|
||||
|
||||
/**
|
||||
* Activation hook.
|
||||
*/
|
||||
public static function activate(): void {
|
||||
self::create_table();
|
||||
update_option( 'robotstxt_telemetry_db_version', ROBOTSTXT_TELEMETRY_DB_VERSION );
|
||||
}
|
||||
|
||||
/**
|
||||
* Ensure the database schema is up to date.
|
||||
*/
|
||||
public static function maybe_upgrade(): void {
|
||||
if ( self::$did_check ) {
|
||||
return;
|
||||
}
|
||||
|
||||
self::$did_check = true;
|
||||
|
||||
$installed_version = get_option( 'robotstxt_telemetry_db_version' );
|
||||
if ( ROBOTSTXT_TELEMETRY_DB_VERSION !== $installed_version ) {
|
||||
self::create_table();
|
||||
update_option( 'robotstxt_telemetry_db_version', ROBOTSTXT_TELEMETRY_DB_VERSION );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create or update the logs table.
|
||||
*/
|
||||
public static function create_table(): void {
|
||||
global $wpdb;
|
||||
|
||||
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
|
||||
|
||||
$table_name = self::get_table_name();
|
||||
$charset_collate = $wpdb->get_charset_collate();
|
||||
|
||||
$sql = "CREATE TABLE {$table_name} (
|
||||
id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
|
||||
created_at DATETIME NOT NULL,
|
||||
method VARCHAR(10) NOT NULL,
|
||||
url TEXT NOT NULL,
|
||||
host VARCHAR(255) NULL,
|
||||
path TEXT NULL,
|
||||
query_json LONGTEXT NULL,
|
||||
body_json LONGTEXT NULL,
|
||||
headers_json LONGTEXT NULL,
|
||||
user_agent TEXT NULL,
|
||||
raw_body LONGTEXT NULL,
|
||||
caller TEXT NULL,
|
||||
redacted TINYINT(1) NOT NULL DEFAULT 1,
|
||||
response_code SMALLINT UNSIGNED NULL,
|
||||
response_time_ms FLOAT NULL,
|
||||
PRIMARY KEY (id),
|
||||
KEY created_at (created_at),
|
||||
KEY host (host),
|
||||
KEY method_host (method, host)
|
||||
) {$charset_collate};";
|
||||
|
||||
dbDelta( $sql );
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a log row.
|
||||
*
|
||||
* @param array $data Log data.
|
||||
*/
|
||||
public static function insert_log( array $data ): void {
|
||||
global $wpdb;
|
||||
|
||||
$wpdb->insert( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Insert into the plugin's custom table via the wpdb API.
|
||||
self::get_table_name(),
|
||||
$data,
|
||||
array(
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
'%d',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fetch a single log row.
|
||||
*
|
||||
* @param int $log_id Log ID.
|
||||
* @return array|null
|
||||
*/
|
||||
public static function get_log( int $log_id ): ?array {
|
||||
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,
|
||||
$log_id
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if ( empty( $log ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $log;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a log row.
|
||||
*
|
||||
* @param int $log_id Log ID.
|
||||
* @return bool
|
||||
*/
|
||||
public static function delete_log( int $log_id ): bool {
|
||||
global $wpdb;
|
||||
|
||||
$deleted = $wpdb->delete( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- Delete from the plugin's custom table via the wpdb API.
|
||||
self::get_table_name(),
|
||||
array( 'id' => $log_id ),
|
||||
array( '%d' )
|
||||
);
|
||||
|
||||
return false !== $deleted;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue