prefix . 'authkey_' . $name; } public static function install() : void { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $collate = $wpdb->get_charset_collate(); $automations_table = self::table( 'automations' ); $logs_table = self::table( 'logs' ); $automations_sql = "CREATE TABLE {$automations_table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, name varchar(191) NOT NULL, enabled tinyint(1) NOT NULL DEFAULT 1, trigger_name varchar(100) NOT NULL, template varchar(191) NOT NULL, template_id bigint(20) unsigned NOT NULL DEFAULT 0, recipient_type varchar(50) NOT NULL, recipient_value varchar(191) NOT NULL DEFAULT '', mappings longtext NULL, conditions longtext NULL, delay_minutes int(11) NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY trigger_name (trigger_name) ) {$collate};"; $logs_sql = "CREATE TABLE {$logs_table} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, created_at datetime NOT NULL, order_id bigint(20) unsigned NULL, customer varchar(191) NOT NULL DEFAULT '', template varchar(191) NOT NULL DEFAULT '', trigger_name varchar(100) NOT NULL DEFAULT '', status varchar(20) NOT NULL, response longtext NULL, retry_count tinyint(3) NOT NULL DEFAULT 0, PRIMARY KEY (id), KEY created_at (created_at), KEY order_id (order_id), KEY status (status) ) {$collate};"; dbDelta( $automations_sql ); dbDelta( $logs_sql ); self::ensure_automation_columns(); } /** Additive migration for sites which installed an earlier 1.0 schema. */ private static function ensure_automation_columns() : void { global $wpdb; $table = self::table( 'automations' ); $exists = $wpdb->get_var( "SHOW COLUMNS FROM {$table} LIKE 'template_id'" ); // Table name is generated from the WP prefix. if ( null === $exists ) { $wpdb->query( "ALTER TABLE {$table} ADD template_id bigint(20) unsigned NOT NULL DEFAULT 0 AFTER template" ); } } public static function maybe_upgrade() : void { self::ensure_automation_columns(); if ( AUTHKEY_VERSION !== get_option( 'authkey_db_version' ) ) { self::install(); update_option( 'authkey_db_version', AUTHKEY_VERSION, false ); } } }