prefix . 'hyperdrop_codes'; } public static function redemptions_table() { global $wpdb; return $wpdb->prefix . 'hyperdrop_redemptions'; } public static function install() { self::create_tables(); // Coming from the plugin's previous name? Copy that data into place. if ( class_exists( 'HyperDrop_Migrate' ) && ! get_option( HyperDrop_Migrate::DONE_OPTION ) && ! HyperDrop_Migrate::old_plugin_active() && HyperDrop_Migrate::has_old_data() ) { HyperDrop_Migrate::run(); } update_option( 'hyperdrop_db_version', self::DB_VERSION ); } /** * Create / update the plugin tables. */ public static function create_tables() { global $wpdb; require_once ABSPATH . 'wp-admin/includes/upgrade.php'; $charset = $wpdb->get_charset_collate(); $codes = self::codes_table(); $reds = self::redemptions_table(); // status: active | pending (waiting for admin approval) | disabled. // source: manual | signup. dbDelta( "CREATE TABLE {$codes} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, code varchar(64) NOT NULL, product_id bigint(20) unsigned NOT NULL DEFAULT 0, term_id bigint(20) unsigned NOT NULL DEFAULT 0, email varchar(190) NOT NULL DEFAULT '', status varchar(20) NOT NULL DEFAULT 'active', source varchar(20) NOT NULL DEFAULT 'manual', max_uses int(11) NOT NULL DEFAULT 1, discount_type varchar(10) NOT NULL DEFAULT '', discount_amount decimal(12,2) NOT NULL DEFAULT 0, uses int(11) NOT NULL DEFAULT 0, expires_at datetime NULL DEFAULT NULL, sent_at datetime NULL DEFAULT NULL, lang varchar(5) NOT NULL DEFAULT '', created_at datetime NOT NULL, PRIMARY KEY (id), UNIQUE KEY code (code), KEY product_id (product_id), KEY term_id (term_id), KEY email (email) ) {$charset};" ); dbDelta( "CREATE TABLE {$reds} ( id bigint(20) unsigned NOT NULL AUTO_INCREMENT, code_id bigint(20) unsigned NOT NULL, order_id bigint(20) unsigned NOT NULL, product_id bigint(20) unsigned NOT NULL, qty int(11) NOT NULL DEFAULT 1, counted tinyint(1) NOT NULL DEFAULT 1, scope varchar(40) NOT NULL DEFAULT '', email varchar(190) NOT NULL DEFAULT '', user_id bigint(20) unsigned NOT NULL DEFAULT 0, created_at datetime NOT NULL, PRIMARY KEY (id), KEY code_id (code_id), KEY order_id (order_id), KEY scope (scope) ) {$charset};" ); } public static function maybe_upgrade() { if ( get_option( 'hyperdrop_db_version' ) !== self::DB_VERSION ) { self::install(); } } }