get_charset_collate(); $tables = self::get_table_names(); $sql = array( "CREATE TABLE {$tables['message_logs']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, internal_id varchar(64) NOT NULL, campaign_id bigint(20) unsigned DEFAULT NULL, order_id bigint(20) unsigned DEFAULT NULL, send_type varchar(32) NOT NULL DEFAULT 'manual', recipient_phone varchar(32) NOT NULL, recipient_name varchar(191) DEFAULT '', recipient_role varchar(191) DEFAULT '', channel varchar(32) NOT NULL DEFAULT '', scheme varchar(32) NOT NULL DEFAULT '', message text NOT NULL, fallback_message text NULL, api_message_id varchar(191) DEFAULT '', api_status varchar(64) DEFAULT '', api_status_code int(10) unsigned DEFAULT NULL, error_code varchar(191) DEFAULT '', api_response longtext NULL, status varchar(64) NOT NULL DEFAULT 'created', error_reason text NULL, viber_ttl int(10) unsigned DEFAULT NULL, viber_sent_at datetime DEFAULT NULL, sms_fallback_sent tinyint(1) unsigned NOT NULL DEFAULT 0, sms_fallback_sent_at datetime DEFAULT NULL, duplicate_key char(64) DEFAULT '', created_at datetime NOT NULL, updated_at datetime NOT NULL, status_updated_at datetime DEFAULT NULL, PRIMARY KEY (id), UNIQUE KEY internal_id (internal_id), KEY api_message_id (api_message_id), KEY campaign_id (campaign_id), KEY order_id (order_id), KEY recipient_phone (recipient_phone), KEY send_type (send_type), KEY error_code (error_code), KEY status (status), KEY duplicate_key (duplicate_key), KEY created_at (created_at) ) {$charset_collate};", "CREATE TABLE {$tables['campaigns']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, uuid varchar(64) NOT NULL, author_id bigint(20) unsigned NOT NULL DEFAULT 0, scheme varchar(32) NOT NULL DEFAULT '', message text NOT NULL, fallback_message text NULL, recipients_total int(10) unsigned NOT NULL DEFAULT 0, processed_count int(10) unsigned NOT NULL DEFAULT 0, success_count int(10) unsigned NOT NULL DEFAULT 0, error_count int(10) unsigned NOT NULL DEFAULT 0, status varchar(32) NOT NULL DEFAULT 'created', created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY uuid (uuid), KEY author_id (author_id), KEY status (status), KEY created_at (created_at) ) {$charset_collate};", "CREATE TABLE {$tables['campaign_recipients']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, campaign_id bigint(20) unsigned NOT NULL, user_id bigint(20) unsigned DEFAULT NULL, order_id bigint(20) unsigned DEFAULT NULL, phone varchar(32) NOT NULL, name varchar(191) DEFAULT '', role varchar(191) DEFAULT '', channel varchar(32) DEFAULT '', status varchar(64) NOT NULL DEFAULT 'created', api_message_id varchar(191) DEFAULT '', api_response longtext NULL, error_reason text NULL, processed_at datetime DEFAULT NULL, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), KEY campaign_id (campaign_id), KEY user_id (user_id), KEY order_id (order_id), KEY phone (phone), KEY role (role), KEY status (status) ) {$charset_collate};", "CREATE TABLE {$tables['templates']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, recipient_type varchar(32) NOT NULL, order_status varchar(64) NOT NULL, scheme varchar(32) NOT NULL, message text NOT NULL, updated_by bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, updated_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY template_key (recipient_type, order_status, scheme), KEY recipient_type (recipient_type), KEY order_status (order_status), KEY updated_by (updated_by) ) {$charset_collate};", "CREATE TABLE {$tables['plugin_logs']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, level varchar(32) NOT NULL DEFAULT 'info', event varchar(64) NOT NULL DEFAULT '', source varchar(191) DEFAULT '', message text NOT NULL, context longtext NULL, user_id bigint(20) unsigned DEFAULT NULL, created_at datetime NOT NULL, PRIMARY KEY (id), KEY level (level), KEY event (event), KEY source (source), KEY user_id (user_id), KEY created_at (created_at) ) {$charset_collate};", "CREATE TABLE {$tables['webhook_logs']} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, route varchar(191) NOT NULL DEFAULT '', method varchar(16) NOT NULL DEFAULT '', remote_addr varchar(64) DEFAULT '', content_type varchar(191) DEFAULT '', headers longtext NULL, query_params longtext NULL, payload longtext NULL, raw_body longtext NULL, result longtext NULL, status varchar(32) NOT NULL DEFAULT 'received', created_at datetime NOT NULL, PRIMARY KEY (id), KEY route (route), KEY method (method), KEY remote_addr (remote_addr), KEY status (status), KEY created_at (created_at) ) {$charset_collate};", ); foreach ( $sql as $statement ) { dbDelta( $statement ); } update_option( self::OPTION_DB_VERSION, INTELTELE_DB_VERSION, false ); } /** * Upgrade tables when schema version changes. */ public static function maybe_upgrade(): void { $current = (string) get_option( self::OPTION_DB_VERSION, '' ); if ( INTELTELE_DB_VERSION !== $current ) { self::install(); } } /** * Get plugin table names. * * @return array */ public static function get_table_names(): array { global $wpdb; return array( 'message_logs' => $wpdb->prefix . 'inteltele_message_logs', 'campaigns' => $wpdb->prefix . 'inteltele_campaigns', 'campaign_recipients' => $wpdb->prefix . 'inteltele_campaign_recipients', 'templates' => $wpdb->prefix . 'inteltele_templates', 'plugin_logs' => $wpdb->prefix . 'inteltele_plugin_logs', 'webhook_logs' => $wpdb->prefix . 'inteltele_webhook_logs', ); } }