display-conditions
3 years ago
front
9 months ago
helpers
9 months ago
metas
3 years ago
multisite
3 years ago
palettes
3 years ago
provider
3 years ago
providers
9 months ago
templates
3 years ago
update
3 years ago
class-hustle-admin-page-abstract.php
9 months ago
class-hustle-black-friday-campaign.php
7 months ago
class-hustle-condition-factory.php
6 years ago
class-hustle-cross-sell.php
9 months ago
class-hustle-dashboard-admin.php
3 years ago
class-hustle-data.php
3 years ago
class-hustle-db.php
2 years ago
class-hustle-installer.php
4 years ago
class-hustle-meta.php
3 years ago
class-hustle-module-admin.php
9 months ago
class-hustle-module-collection.php
3 years ago
class-hustle-module-page-abstract.php
2 years ago
class-hustle-notifications.php
3 years ago
class-hustle-settings-admin.php
3 years ago
class-hustle-tutorials-page.php
4 years ago
class-hustle-wp-dashboard-page.php
3 years ago
hustle-deletion.php
3 years ago
hustle-embedded-admin.php
3 years ago
hustle-entries-admin.php
3 years ago
hustle-entry-model.php
3 years ago
hustle-general-data-protection.php
3 years ago
hustle-init.php
7 months ago
hustle-mail.php
3 years ago
hustle-migration.php
3 years ago
hustle-model.php
3 years ago
hustle-module-model.php
9 months ago
hustle-module-widget-legacy.php
3 years ago
hustle-module-widget.php
3 years ago
hustle-modules-common-admin-ajax.php
9 months ago
hustle-popup-admin.php
3 years ago
hustle-providers-admin.php
3 years ago
hustle-providers.php
3 years ago
hustle-settings-admin-ajax.php
3 years ago
hustle-settings-page.php
3 years ago
hustle-slidein-admin.php
3 years ago
hustle-sshare-admin.php
3 years ago
hustle-sshare-model.php
3 years ago
hustle-tracking-model.php
3 years ago
opt-in-geo.php
9 months ago
opt-in-utils.php
3 years ago
opt-in-wpmudev-api.php
3 years ago
class-hustle-db.php
337 lines
| 1 | <?php |
| 2 | /** |
| 3 | * File for the Hustle_Db class. |
| 4 | * |
| 5 | * @package Hustle |
| 6 | * @since unknown |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * File for Hustle_Db class. |
| 11 | * |
| 12 | * @package Hustle |
| 13 | * @since unknwon |
| 14 | */ |
| 15 | |
| 16 | require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 17 | if ( ! class_exists( 'Hustle_Db' ) ) : |
| 18 | |
| 19 | /** |
| 20 | * Class Hustle_Db |
| 21 | * |
| 22 | * Takes care of all the db initializations |
| 23 | */ |
| 24 | class Hustle_Db { |
| 25 | |
| 26 | const DB_VERSION_KEY = 'hustle_database_version'; |
| 27 | |
| 28 | const TABLE_HUSTLE_MODULES = 'hustle_modules'; |
| 29 | |
| 30 | const TABLE_HUSTLE_MODULES_META = 'hustle_modules_meta'; |
| 31 | |
| 32 | /** |
| 33 | * Last version where the db was updated. |
| 34 | * Change this if you want the 'create tables' function and migration (if conditions are met) to run. |
| 35 | * |
| 36 | * @since 4.0.0 |
| 37 | */ |
| 38 | const DB_VERSION = '4.8.2'; |
| 39 | |
| 40 | /** |
| 41 | * Store module's entries. |
| 42 | * |
| 43 | * @since 4.0.0 |
| 44 | */ |
| 45 | const TABLE_HUSTLE_ENTRIES = 'hustle_entries'; |
| 46 | |
| 47 | /** |
| 48 | * Store module's entries' meta. |
| 49 | * |
| 50 | * @since 4.0.0 |
| 51 | */ |
| 52 | const TABLE_HUSTLE_ENTRIES_META = 'hustle_entries_meta'; |
| 53 | |
| 54 | /** |
| 55 | * Store module's views and conversions. |
| 56 | * |
| 57 | * @since 4.0.0 |
| 58 | */ |
| 59 | const TABLE_HUSTLE_TRACKING = 'hustle_tracking'; |
| 60 | |
| 61 | /** |
| 62 | * Current tables. |
| 63 | * |
| 64 | * @since 4.0.0 |
| 65 | * |
| 66 | * @var array |
| 67 | */ |
| 68 | private static $tables = array(); |
| 69 | |
| 70 | /** |
| 71 | * It's true only for the FIRST load for fresh plugin installations |
| 72 | * |
| 73 | * @var bool |
| 74 | */ |
| 75 | public static $is_fresh_install = false; |
| 76 | |
| 77 | /** |
| 78 | * Check whether the db is up to date. |
| 79 | * |
| 80 | * @since 4.0.0 |
| 81 | * @return boolean |
| 82 | */ |
| 83 | public static function is_db_up_to_date() { |
| 84 | $stored_db_version = get_option( self::DB_VERSION_KEY, false ); |
| 85 | |
| 86 | // Check if current version is equal to database version. |
| 87 | if ( version_compare( $stored_db_version, self::DB_VERSION, '=' ) ) { |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | if ( false === $stored_db_version ) { |
| 92 | self::$is_fresh_install = true; |
| 93 | } |
| 94 | |
| 95 | return false; |
| 96 | } |
| 97 | |
| 98 | /** |
| 99 | * Creates plugin tables |
| 100 | * |
| 101 | * @since 1.0.0 |
| 102 | * |
| 103 | * @param bool $force Whether to create tables even if the db is up to date. |
| 104 | */ |
| 105 | public static function maybe_create_tables( $force = false ) { |
| 106 | if ( ! $force && self::is_db_up_to_date() ) { |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | $hustle_db = new self(); |
| 111 | foreach ( $hustle_db->get_tables() as $name => $columns ) { |
| 112 | $sql = $hustle_db->create_table_sql( $name, $columns ); |
| 113 | $result = dbDelta( $sql ); |
| 114 | } |
| 115 | |
| 116 | self::remove_old_columns(); |
| 117 | |
| 118 | update_option( self::DB_VERSION_KEY, self::DB_VERSION ); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Remove old columns |
| 123 | * |
| 124 | * @global object $wpdb |
| 125 | */ |
| 126 | private static function remove_old_columns() { |
| 127 | $stored_db_version = get_option( self::DB_VERSION_KEY, 0 ); |
| 128 | if ( version_compare( $stored_db_version, '4.8.2', '<' ) ) { |
| 129 | // Clean up the `test_mode` column removed from schema in version before 4.0. |
| 130 | global $wpdb; |
| 131 | $modules_table = self::modules_table(); |
| 132 | $test_mode_exists = $wpdb->get_var( "SHOW COLUMNS FROM {$modules_table} LIKE 'test_mode'" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 133 | if ( ! is_null( $test_mode_exists ) ) { |
| 134 | $wpdb->query( "ALTER TABLE {$modules_table} DROP COLUMN `test_mode`" ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared,WordPress.DB.DirectDatabaseQuery.SchemaChange |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Generates CREATE TABLE sql script for provided table name and columns list. |
| 141 | * |
| 142 | * @since 1.0.0 |
| 143 | * |
| 144 | * @access private |
| 145 | * @param string $name The name of a table. |
| 146 | * @param array $columns The array of columns, indexes, constraints. |
| 147 | * @return string The sql script for table creation. |
| 148 | */ |
| 149 | private function create_table_sql( $name, array $columns ) { |
| 150 | global $wpdb; |
| 151 | $charset = ''; |
| 152 | if ( ! empty( $wpdb->charset ) ) { |
| 153 | $charset = 'DEFAULT CHARACTER SET ' . $wpdb->charset; |
| 154 | } |
| 155 | $collate = ''; |
| 156 | if ( ! empty( $wpdb->collate ) ) { |
| 157 | $collate .= ' COLLATE ' . $wpdb->collate; |
| 158 | } |
| 159 | $name = $wpdb->prefix . $name; |
| 160 | return sprintf( |
| 161 | 'CREATE TABLE %s (%s%s%s)%s%s', |
| 162 | $name, |
| 163 | PHP_EOL, |
| 164 | implode( ',' . PHP_EOL, $columns ), |
| 165 | PHP_EOL, |
| 166 | $charset, |
| 167 | $collate |
| 168 | ); |
| 169 | } |
| 170 | |
| 171 | /** |
| 172 | * Returns "module_meta" table array with their "Create syntax" |
| 173 | * |
| 174 | * @since 4.0.0 |
| 175 | * @since 4.0 'module_mode' added. 'test_mode' removed. |
| 176 | * |
| 177 | * @return array |
| 178 | */ |
| 179 | public static function get_table_modules() { |
| 180 | return array( |
| 181 | 'module_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT', |
| 182 | "blog_id bigint(20) UNSIGNED NOT NULL DEFAULT '0'", |
| 183 | 'module_name varchar(255) NOT NULL', |
| 184 | 'module_type varchar(100) NOT NULL', |
| 185 | 'active tinyint DEFAULT 1', |
| 186 | 'module_mode varchar(100) NOT NULL', |
| 187 | 'PRIMARY KEY (module_id)', |
| 188 | 'KEY active (active)', |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Returns "module_meta" table array with their "Create syntax" |
| 194 | * |
| 195 | * @since 4.0.0 |
| 196 | * @since 4.0 'module_mode' added. 'test_mode' removed. |
| 197 | * |
| 198 | * @return array |
| 199 | */ |
| 200 | public static function get_table_modules_meta() { |
| 201 | global $wpdb; |
| 202 | $collate = ''; |
| 203 | if ( ! empty( $wpdb->collate ) ) { |
| 204 | $collate .= ' COLLATE ' . $wpdb->collate; |
| 205 | } |
| 206 | return array( |
| 207 | 'meta_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT', |
| 208 | "module_id bigint(20) UNSIGNED NOT NULL DEFAULT '0'", |
| 209 | 'meta_key varchar(191) ' . $collate . ' DEFAULT NULL', |
| 210 | 'meta_value longtext ' . $collate, |
| 211 | 'PRIMARY KEY (meta_id)', |
| 212 | 'KEY module_id (module_id)', |
| 213 | 'KEY meta_key (meta_key)', |
| 214 | ); |
| 215 | } |
| 216 | |
| 217 | /** |
| 218 | * Returns db table arrays with their "Create syntax" |
| 219 | * |
| 220 | * @since 1.0.0 |
| 221 | * @since 4.0 'module_mode' added. 'test_mode' removed. |
| 222 | * |
| 223 | * @return array |
| 224 | */ |
| 225 | private function get_tables() { |
| 226 | return array( |
| 227 | self::TABLE_HUSTLE_MODULES => self::get_table_modules(), |
| 228 | self::TABLE_HUSTLE_MODULES_META => self::get_table_modules_meta(), |
| 229 | self::TABLE_HUSTLE_ENTRIES => array( |
| 230 | 'entry_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT', |
| 231 | 'entry_type varchar(191) NOT NULL', |
| 232 | 'module_id bigint(20) UNSIGNED NOT NULL', |
| 233 | "date_created datetime NOT NULL default '0000-00-00 00:00:00'", |
| 234 | 'PRIMARY KEY (entry_id)', |
| 235 | 'KEY entry_type (entry_type)', |
| 236 | 'KEY entry_module_id (module_id)', |
| 237 | ), |
| 238 | self::TABLE_HUSTLE_ENTRIES_META => array( |
| 239 | 'meta_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT', |
| 240 | 'entry_id bigint(20) UNSIGNED NOT NULL', |
| 241 | 'meta_key varchar(191) DEFAULT NULL', |
| 242 | 'meta_value longtext NULL', |
| 243 | "date_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00'", |
| 244 | "date_updated datetime NOT NULL DEFAULT '0000-00-00 00:00:00'", |
| 245 | 'PRIMARY KEY (meta_id)', |
| 246 | 'KEY meta_key (meta_key)', |
| 247 | 'KEY meta_entry_id (entry_id ASC )', |
| 248 | 'KEY meta_key_object (entry_id ASC, meta_key ASC)', |
| 249 | ), |
| 250 | self::TABLE_HUSTLE_TRACKING => array( |
| 251 | 'tracking_id bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT', |
| 252 | 'module_id bigint(20) UNSIGNED NOT NULL', |
| 253 | 'page_id bigint(20) UNSIGNED NOT NULL', |
| 254 | 'module_type varchar(100) NOT NULL', |
| 255 | 'action varchar(100) NOT NULL', |
| 256 | 'ip varchar(191) DEFAULT NULL', |
| 257 | 'counter mediumint(8) UNSIGNED NOT NULL DEFAULT 1', |
| 258 | "date_created datetime NOT NULL DEFAULT '0000-00-00 00:00:00'", |
| 259 | "date_updated datetime NOT NULL DEFAULT '0000-00-00 00:00:00'", |
| 260 | 'PRIMARY KEY (tracking_id)', |
| 261 | 'KEY tracking_module_id (module_id ASC )', |
| 262 | 'KEY action (action)', |
| 263 | 'KEY tracking_module_object (action ASC, module_id ASC, module_type ASC)', |
| 264 | 'KEY tracking_module_object_ip (module_id ASC, tracking_id ASC, ip ASC)', |
| 265 | 'KEY tracking_date_created (date_created DESC)', |
| 266 | ), |
| 267 | ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Add $wpdb prefix to table name |
| 272 | * |
| 273 | * @since 4.0.0 |
| 274 | * |
| 275 | * @global object $wpdb |
| 276 | * @param string $table Table name. |
| 277 | * @return string |
| 278 | */ |
| 279 | private static function add_prefix( $table ) { |
| 280 | global $wpdb; |
| 281 | return $wpdb->prefix . $table; |
| 282 | } |
| 283 | |
| 284 | /** |
| 285 | * Get modules table name |
| 286 | * |
| 287 | * @since 4.0.0 |
| 288 | * |
| 289 | * @return string |
| 290 | */ |
| 291 | public static function modules_table() { |
| 292 | return self::add_prefix( self::TABLE_HUSTLE_MODULES ); |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Get modules meta table name |
| 297 | * |
| 298 | * @since 4.0 |
| 299 | * @return string |
| 300 | */ |
| 301 | public static function modules_meta_table() { |
| 302 | return self::add_prefix( self::TABLE_HUSTLE_MODULES_META ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get entries table name |
| 307 | * |
| 308 | * @since 4.0 |
| 309 | * @return string |
| 310 | */ |
| 311 | public static function entries_table() { |
| 312 | return self::add_prefix( self::TABLE_HUSTLE_ENTRIES ); |
| 313 | } |
| 314 | |
| 315 | /** |
| 316 | * Get entries meta table name |
| 317 | * |
| 318 | * @since 4.0 |
| 319 | * @return string |
| 320 | */ |
| 321 | public static function entries_meta_table() { |
| 322 | return self::add_prefix( self::TABLE_HUSTLE_ENTRIES_META ); |
| 323 | } |
| 324 | |
| 325 | /** |
| 326 | * Get tracking table name |
| 327 | * |
| 328 | * @since 4.0 |
| 329 | * @return string |
| 330 | */ |
| 331 | public static function tracking_table() { |
| 332 | return self::add_prefix( self::TABLE_HUSTLE_TRACKING ); |
| 333 | } |
| 334 | } |
| 335 | |
| 336 | endif; |
| 337 |