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