Deviates from `SPEC.md` §5 Storage, deliberately. The spec's schema pairs a * > full-precision `occurred_at DATETIME` with `UNIQUE KEY dedupe (occurred_at, * > host, source)`, which cannot aggregate anything: two calls one second apart * > are two rows, and `count` never rises above 1. Splitting the day key from * > the timestamp is what the spec's own `count` column implies was intended. * * @since 1.0.0 */ class BlockedCalls { /** * The call was allowed through and only recorded. * * @var int */ const OBSERVED = 0; /** * The call was refused. * * @var int */ const BLOCKED = 1; /** * Days of history kept before pruning. * * @var int */ const RETENTION_DAYS = 30; /** * Option holding the installed schema version. * * @var string */ const SCHEMA_OPTION = 'aiblackout_blocked_calls_schema'; /** * Current schema version. * * @var int */ const SCHEMA_VERSION = 1; /** * Returns the fully-qualified table name. * * @return string Table name. */ public static function table_name(): string { global $wpdb; return $wpdb->prefix . 'aiblackout_blocked_calls'; } /** * Creates or updates the table. * * @return void */ public static function install(): void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $table = self::table_name(); $collate = $wpdb->get_charset_collate(); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name cannot be a placeholder, and it is built from $wpdb->prefix. $sql = "CREATE TABLE {$table} ( id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, occurred_on DATE NOT NULL, last_seen DATETIME NOT NULL, host VARCHAR(190) NOT NULL, source VARCHAR(190) NOT NULL, provider VARCHAR(64) NOT NULL DEFAULT '', outcome TINYINT UNSIGNED NOT NULL DEFAULT 0, hits INT UNSIGNED NOT NULL DEFAULT 1, PRIMARY KEY (id), UNIQUE KEY dedupe (occurred_on,host,source,outcome), KEY pruning (occurred_on) ) {$collate};"; // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared dbDelta( $sql ); update_option( self::SCHEMA_OPTION, self::SCHEMA_VERSION, false ); } /** * Creates or upgrades the table when the stored schema version does not match. * * `install()` runs from the activation hook, and an activation hook is the * one thing WordPress does *not* re-run when a plugin is updated. Without a * check somewhere in the request cycle the stored schema version is written * once and never read again, which breaks three ordinary situations: * * - A future release that changes the table would never migrate it. The * table would stay on the old shape and every insert would fail against * it, and `$wpdb->query()` does not raise, so the log would simply stop * recording without saying why. * - Network activation fires the activation hook once, for one site. Every * other site in the network gets no table. Options are per-site too, so * the version is absent there and this check installs it on first admin * request. * - A table lost to a migration, a restore or a hand-edited database comes * back on its own instead of needing a deactivate/reactivate cycle. * * Hooked to `admin_init`, so it costs one option read on admin requests and * nothing at all on the front end. `dbDelta()` only runs when the versions * actually differ. * * @return void */ public static function maybe_install(): void { if ( (int) get_option( self::SCHEMA_OPTION, 0 ) === self::SCHEMA_VERSION ) { return; } self::install(); } /** * Drops the table. * * @return void */ public static function uninstall(): void { global $wpdb; $table = self::table_name(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.SchemaChange, WordPress.DB.DirectDatabaseQuery.NoCaching -- Dropping our own table on uninstall; the name is built from $wpdb->prefix. $wpdb->query( "DROP TABLE IF EXISTS {$table}" ); delete_option( self::SCHEMA_OPTION ); } /** * Whether the table exists. * * @return bool True when the table is present. */ public function is_installed(): bool { global $wpdb; $table = self::table_name(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Schema introspection; there is nothing to cache and it runs only on the audit screen. $found = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $wpdb->esc_like( $table ) ) ); return $found === $table; } /** * Records one call. * * @param string $host The host that was contacted. * @param string $source Slug of the plugin, theme or component responsible. * @param string $provider Provider identifier from the bundled list. * @param int $outcome One of OBSERVED or BLOCKED. * @return void */ public function record( string $host, string $source, string $provider, int $outcome ): void { global $wpdb; if ( ! $this->is_installed() ) { return; } $table = self::table_name(); $now = current_time( 'mysql' ); $day = substr( $now, 0, 10 ); $outcome = self::BLOCKED === $outcome ? self::BLOCKED : self::OBSERVED; /* * One statement, so two concurrent requests cannot both read zero and * both insert. The unique key is what makes the upsert land on the * right row. */ // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name cannot be a placeholder; every value is prepared. $wpdb->query( $wpdb->prepare( "INSERT INTO {$table} (occurred_on, last_seen, host, source, provider, outcome, hits) VALUES (%s, %s, %s, %s, %s, %d, 1) ON DUPLICATE KEY UPDATE hits = hits + 1, last_seen = VALUES(last_seen)", $day, $now, substr( $host, 0, 190 ), substr( $source, 0, 190 ), substr( $provider, 0, 64 ), $outcome ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching } /** * Returns recent activity, most hits first. * * @param int $days How many days back to include. * @param int $limit Maximum rows. * @return array List of rows. */ public function get_recent( int $days = 30, int $limit = 100 ): array { global $wpdb; if ( ! $this->is_installed() ) { return array(); } $table = self::table_name(); $since = gmdate( 'Y-m-d', strtotime( current_time( 'mysql' ) ) - ( max( 1, $days ) * DAY_IN_SECONDS ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name cannot be a placeholder; every value is prepared. Admin-screen read, nothing to cache. $rows = $wpdb->get_results( $wpdb->prepare( "SELECT occurred_on, last_seen, host, source, provider, outcome, hits FROM {$table} WHERE occurred_on >= %s ORDER BY hits DESC, last_seen DESC LIMIT %d", $since, max( 1, $limit ) ), ARRAY_A ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return is_array( $rows ) ? $rows : array(); } /** * Deletes rows older than the retention window. * * @return int Rows removed. */ public function prune(): int { global $wpdb; if ( ! $this->is_installed() ) { return 0; } $table = self::table_name(); $cutoff = gmdate( 'Y-m-d', strtotime( current_time( 'mysql' ) ) - ( self::RETENTION_DAYS * DAY_IN_SECONDS ) ); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Table name cannot be a placeholder; the value is prepared. $removed = $wpdb->query( $wpdb->prepare( "DELETE FROM {$table} WHERE occurred_on < %s", $cutoff ) ); // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching return is_numeric( $removed ) ? (int) $removed : 0; } /** * Empties the log. * * @return void */ public function clear(): void { global $wpdb; if ( ! $this->is_installed() ) { return; } $table = self::table_name(); // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- Truncating our own table on an explicit user action. $wpdb->query( "TRUNCATE TABLE {$table}" ); } }