prefix . 'afpp_subscribers'; } public static function broadcasts() { global $wpdb; return $wpdb->prefix . 'afpp_broadcasts'; } public static function queue() { global $wpdb; return $wpdb->prefix . 'afpp_queue'; } public static function maybe_upgrade() { if ( get_option( 'afpp_db_version' ) !== AFPP_DB_VERSION ) { self::install(); } } public static function install() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $s = self::subscribers(); $b = self::broadcasts(); $q = self::queue(); dbDelta( "CREATE TABLE {$s} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, phone varchar(20) NOT NULL, name varchar(100) NOT NULL DEFAULT '', status varchar(20) NOT NULL DEFAULT 'active', frequency varchar(10) NOT NULL DEFAULT 'instant', token char(32) NOT NULL, source varchar(255) NOT NULL DEFAULT '', ip varchar(45) NOT NULL DEFAULT '', consent_at datetime NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY phone (phone), UNIQUE KEY token (token), KEY status (status) ) {$charset};" ); dbDelta( "CREATE TABLE {$b} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, post_id bigint(20) unsigned NOT NULL DEFAULT 0, kind varchar(20) NOT NULL DEFAULT 'manual', status varchar(20) NOT NULL DEFAULT 'queued', total int(10) unsigned NOT NULL DEFAULT 0, sent int(10) unsigned NOT NULL DEFAULT 0, failed int(10) unsigned NOT NULL DEFAULT 0, skipped int(10) unsigned NOT NULL DEFAULT 0, payload longtext NULL, created_by bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, started_at datetime NULL, finished_at datetime NULL, PRIMARY KEY (id), KEY post_id (post_id), KEY status (status) ) {$charset};" ); dbDelta( "CREATE TABLE {$q} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, broadcast_id bigint(20) unsigned NOT NULL, subscriber_id bigint(20) unsigned NOT NULL, status varchar(20) NOT NULL DEFAULT 'pending', delivery varchar(20) NOT NULL DEFAULT '', error varchar(255) NOT NULL DEFAULT '', wamid varchar(128) NOT NULL DEFAULT '', sent_at datetime NULL, PRIMARY KEY (id), UNIQUE KEY broadcast_subscriber (broadcast_id,subscriber_id), KEY broadcast_status (broadcast_id,status), KEY wamid (wamid) ) {$charset};" ); update_option( 'afpp_db_version', AFPP_DB_VERSION ); } public static function drop() { global $wpdb; foreach ( array( self::queue(), self::broadcasts(), self::subscribers() ) as $table ) { $wpdb->query( $wpdb->prepare( 'DROP TABLE IF EXISTS %i', $table ) ); // phpcs:ignore WordPress.DB } delete_option( 'afpp_db_version' ); } }