CreateDBTables.php
226 lines
| 1 | <?php |
| 2 | |
| 3 | namespace ProfilePress\Core\RegisterActivation; |
| 4 | |
| 5 | use ProfilePress\Core\Base as CoreBase; |
| 6 | |
| 7 | class CreateDBTables |
| 8 | { |
| 9 | public static function make() |
| 10 | { |
| 11 | global $wpdb; |
| 12 | |
| 13 | $collate = ''; |
| 14 | if ($wpdb->has_cap('collation')) { |
| 15 | $collate = $wpdb->get_charset_collate(); |
| 16 | } |
| 17 | |
| 18 | $forms_table = CoreBase::form_db_table(); |
| 19 | $forms_meta_table = CoreBase::form_meta_db_table(); |
| 20 | $meta_data_table = CoreBase::meta_data_db_table(); |
| 21 | |
| 22 | $sqls[] = "CREATE TABLE IF NOT EXISTS $forms_table ( |
| 23 | id bigint(20) NOT NULL AUTO_INCREMENT, |
| 24 | name varchar(100) NOT NULL, |
| 25 | form_id bigint(20) NOT NULL, |
| 26 | form_type varchar(20) NOT NULL DEFAULT '', |
| 27 | builder_type varchar(20) NOT NULL DEFAULT '', |
| 28 | date datetime NOT NULL DEFAULT '1000-01-01 00:00:00', |
| 29 | PRIMARY KEY (id), |
| 30 | UNIQUE KEY name (name), |
| 31 | KEY form_id (form_id) |
| 32 | ) $collate; |
| 33 | "; |
| 34 | // max index length is 191 to avoid this error "Index column size too large. The maximum column size is 767 bytes." |
| 35 | // @see wp_get_db_schema() |
| 36 | $sqls[] = "CREATE TABLE IF NOT EXISTS $forms_meta_table ( |
| 37 | meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 38 | form_id bigint(20) unsigned NOT NULL, |
| 39 | form_type varchar(20) DEFAULT NULL, |
| 40 | meta_key varchar(255) DEFAULT NULL, |
| 41 | meta_value longtext, |
| 42 | PRIMARY KEY (meta_id), |
| 43 | KEY form_id (form_id), |
| 44 | KEY form_type (form_type), |
| 45 | KEY meta_key (meta_key(191)) |
| 46 | ) $collate; |
| 47 | "; |
| 48 | $sqls[] = "CREATE TABLE IF NOT EXISTS $meta_data_table ( |
| 49 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 50 | meta_key varchar(50) DEFAULT NULL, |
| 51 | meta_value longtext, |
| 52 | flag varchar(20) DEFAULT NULL, |
| 53 | PRIMARY KEY (id), |
| 54 | KEY meta_key (meta_key), |
| 55 | KEY flag (flag) |
| 56 | ) $collate; |
| 57 | "; |
| 58 | |
| 59 | $sqls = apply_filters('ppress_create_database_tables', $sqls, $collate); |
| 60 | |
| 61 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 62 | |
| 63 | foreach ($sqls as $sql) { |
| 64 | dbDelta($sql); |
| 65 | } |
| 66 | |
| 67 | self::membership_db_make(); |
| 68 | } |
| 69 | |
| 70 | public static function membership_db_make() |
| 71 | { |
| 72 | global $wpdb; |
| 73 | |
| 74 | $collate = ''; |
| 75 | if ($wpdb->has_cap('collation')) { |
| 76 | $collate = $wpdb->get_charset_collate(); |
| 77 | } |
| 78 | |
| 79 | $subscriptions_table = CoreBase::subscriptions_db_table(); |
| 80 | $plans_table = CoreBase::subscription_plans_db_table(); |
| 81 | $orders_table = CoreBase::orders_db_table(); |
| 82 | $order_meta_table = CoreBase::order_meta_db_table(); |
| 83 | $coupons_table = CoreBase::coupons_db_table(); |
| 84 | $customers_table = CoreBase::customers_db_table(); |
| 85 | |
| 86 | //Not using CURRENT_TIMESTAMP because it uses mysql session/server timezone which always isn’t UTC |
| 87 | |
| 88 | $sqls[] = "CREATE TABLE IF NOT EXISTS $subscriptions_table ( |
| 89 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 90 | parent_order_id bigint(20) unsigned NOT NULL, |
| 91 | plan_id bigint(20) unsigned NOT NULL, |
| 92 | customer_id bigint(20) unsigned NOT NULL, |
| 93 | billing_frequency varchar(50) NOT NULL, |
| 94 | initial_amount decimal(26,8) NOT NULL, |
| 95 | initial_tax_rate mediumtext NOT NULL, |
| 96 | initial_tax mediumtext NOT NULL, |
| 97 | recurring_amount decimal(26,8) NOT NULL, |
| 98 | recurring_tax_rate mediumtext NOT NULL, |
| 99 | recurring_tax mediumtext NOT NULL, |
| 100 | total_payments bigint(20) unsigned NOT NULL DEFAULT '0', |
| 101 | trial_period varchar(50) DEFAULT NULL, |
| 102 | profile_id varchar(255) NOT NULL, |
| 103 | status varchar(20) NOT NULL, |
| 104 | notes longtext, |
| 105 | created_date datetime NOT NULL, |
| 106 | expiration_date datetime DEFAULT NULL, |
| 107 | PRIMARY KEY (id), |
| 108 | KEY plan_id (plan_id), |
| 109 | KEY customer_id (customer_id), |
| 110 | KEY status (status), |
| 111 | KEY parent_order_id (parent_order_id), |
| 112 | KEY customer_and_status (customer_id,status), |
| 113 | KEY profile_id (profile_id(191)) |
| 114 | ) $collate; |
| 115 | "; |
| 116 | |
| 117 | $sqls[] = "CREATE TABLE IF NOT EXISTS $plans_table ( |
| 118 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 119 | name varchar(255) NOT NULL, |
| 120 | description text, |
| 121 | price decimal(26,8) NOT NULL DEFAULT '0.00000000', |
| 122 | billing_frequency varchar(50) NOT NULL, |
| 123 | subscription_length varchar(50) NOT NULL, |
| 124 | total_payments int(11) DEFAULT NULL, |
| 125 | signup_fee decimal(26,8) DEFAULT '0.00000000', |
| 126 | free_trial varchar(50) DEFAULT NULL, |
| 127 | status enum('true','false') NOT NULL DEFAULT 'true', |
| 128 | meta_data longtext, |
| 129 | PRIMARY KEY (id) |
| 130 | ) $collate; |
| 131 | "; |
| 132 | $sqls[] = "CREATE TABLE IF NOT EXISTS $orders_table ( |
| 133 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 134 | order_key varchar(64) NOT NULL, |
| 135 | plan_id bigint(20) unsigned NOT NULL, |
| 136 | customer_id bigint(20) unsigned NOT NULL, |
| 137 | subscription_id bigint(20) unsigned NOT NULL DEFAULT '0', |
| 138 | order_type varchar(20) NOT NULL DEFAULT '', |
| 139 | transaction_id varchar(100) DEFAULT '', |
| 140 | payment_method varchar(100) NOT NULL DEFAULT '', |
| 141 | status varchar(20) NOT NULL, |
| 142 | coupon_code varchar(20) DEFAULT NULL, |
| 143 | subtotal decimal(26,8) NOT NULL DEFAULT '0.00000000', |
| 144 | tax decimal(26,8) NOT NULL DEFAULT '0.00000000', |
| 145 | tax_rate mediumtext NOT NULL, |
| 146 | discount decimal(26,8) NOT NULL DEFAULT '0.00000000', |
| 147 | total decimal(26,8) NOT NULL DEFAULT '0.00000000', |
| 148 | billing_address varchar(200) NOT NULL, |
| 149 | billing_city varchar(100) NOT NULL, |
| 150 | billing_state varchar(100) NOT NULL, |
| 151 | billing_postcode varchar(100) NOT NULL, |
| 152 | billing_country varchar(100) NOT NULL, |
| 153 | billing_phone varchar(100) NOT NULL, |
| 154 | mode enum('live','test') NOT NULL, |
| 155 | currency varchar(10) NOT NULL, |
| 156 | ip_address varchar(100) NOT NULL DEFAULT '', |
| 157 | date_created datetime NOT NULL, |
| 158 | date_completed datetime DEFAULT NULL, |
| 159 | PRIMARY KEY (id), |
| 160 | UNIQUE KEY order_key (order_key), |
| 161 | KEY status (status), |
| 162 | KEY mode (mode), |
| 163 | KEY plan_id (plan_id), |
| 164 | KEY transaction_id (transaction_id), |
| 165 | KEY date (date_created), |
| 166 | KEY coupon_code (coupon_code), |
| 167 | KEY customer_id (customer_id) |
| 168 | ) $collate; |
| 169 | "; |
| 170 | $sqls[] = "CREATE TABLE IF NOT EXISTS $order_meta_table ( |
| 171 | meta_id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 172 | ppress_order_id bigint(20) unsigned NOT NULL, |
| 173 | meta_key varchar(255) COLLATE utf8mb4_unicode_520_ci DEFAULT NULL, |
| 174 | meta_value longtext COLLATE utf8mb4_unicode_520_ci, |
| 175 | PRIMARY KEY (meta_id) |
| 176 | ) $collate; |
| 177 | "; |
| 178 | $sqls[] = "CREATE TABLE IF NOT EXISTS $coupons_table ( |
| 179 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 180 | code varchar(50) NOT NULL, |
| 181 | description mediumtext, |
| 182 | coupon_application varchar(50) NOT NULL, |
| 183 | type varchar(50) NOT NULL, |
| 184 | amount mediumtext NOT NULL, |
| 185 | unit varchar(50) NOT NULL, |
| 186 | plan_ids mediumtext, |
| 187 | usage_limit mediumint(8) unsigned DEFAULT NULL, |
| 188 | status enum('true','false') NOT NULL DEFAULT 'true', |
| 189 | start_date date DEFAULT NULL, |
| 190 | end_date date DEFAULT NULL, |
| 191 | PRIMARY KEY (id), |
| 192 | UNIQUE KEY code (code), |
| 193 | KEY type (type), |
| 194 | KEY status (status) |
| 195 | ) $collate; |
| 196 | "; |
| 197 | $sqls[] = "CREATE TABLE IF NOT EXISTS $customers_table ( |
| 198 | id bigint(20) unsigned NOT NULL AUTO_INCREMENT, |
| 199 | user_id bigint(20) unsigned DEFAULT NULL, |
| 200 | private_note longtext, |
| 201 | total_spend decimal(18,9) NOT NULL DEFAULT '0.000000000', |
| 202 | purchase_count bigint(20) unsigned NOT NULL DEFAULT '0', |
| 203 | last_login datetime DEFAULT NULL, |
| 204 | date_created datetime NOT NULL, |
| 205 | PRIMARY KEY (id), |
| 206 | UNIQUE KEY user_id (user_id), |
| 207 | KEY date_created (date_created) |
| 208 | ) $collate; |
| 209 | "; |
| 210 | |
| 211 | $sqls[] = "CREATE TABLE {$wpdb->prefix}ppress_sessions ( |
| 212 | session_id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT, |
| 213 | session_key char(32) NOT NULL, |
| 214 | session_value LONGTEXT NOT NULL, |
| 215 | session_expiry BIGINT(20) UNSIGNED NOT NULL, |
| 216 | PRIMARY KEY (session_key), |
| 217 | UNIQUE KEY session_id (session_id) |
| 218 | ) $collate;"; |
| 219 | |
| 220 | require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); |
| 221 | |
| 222 | foreach ($sqls as $sql) { |
| 223 | dbDelta($sql); |
| 224 | } |
| 225 | } |
| 226 | } |