prefix . 'gruver_subscribe_rate'; } /** * Table of the state of the lists. */ public static function status_table(): string { global $wpdb; return $wpdb->prefix . 'gruver_subscribe_status'; } /** * Create or update the tables if this site has an older schema (hook init). */ public static function maybe_install(): void { if ( self::VERSION !== (int) get_option( self::OPTION, 0 ) ) { self::install(); } } /** * Create or update the tables and schedule the daily cleanup. */ public static function install(): void { global $wpdb; if ( ! function_exists( 'dbDelta' ) ) { require_once ABSPATH . 'wp-admin/includes/upgrade.php'; } $charset = $wpdb->get_charset_collate(); dbDelta( 'CREATE TABLE ' . self::rate_table() . " ( bucket char(64) NOT NULL, hits smallint(5) unsigned NOT NULL DEFAULT 0, window_end bigint(20) unsigned NOT NULL, PRIMARY KEY (bucket), KEY window_end (window_end) ) $charset;" ); dbDelta( 'CREATE TABLE ' . self::status_table() . " ( list_key varchar(40) NOT NULL, data longtext NOT NULL, updated_at bigint(20) unsigned NOT NULL, PRIMARY KEY (list_key) ) $charset;" ); update_option( self::OPTION, self::VERSION, true ); if ( false === wp_next_scheduled( self::CLEANUP_HOOK ) ) { wp_schedule_event( time() + HOUR_IN_SECONDS, 'daily', self::CLEANUP_HOOK ); } } /** * Daily cleanup (cron), in case nobody sends a form for a while. */ public static function cleanup(): void { RateLimit::purge( time() ); } /** * Stop the cleanup when the plugin is deactivated; the tables stay until it is deleted. */ public static function deactivate(): void { wp_clear_scheduled_hook( self::CLEANUP_HOOK ); } }