| 1 |
<?php |
| 2 |
/** |
| 3 |
* Database Table class |
| 4 |
* |
| 5 |
* @credits Jhon James Jacob (https://jjj.blog) |
| 6 |
* |
| 7 |
* @author GamiPress <contact@gamipress.com>, Ruben Garcia <rubengcdev@gamil.com> |
| 8 |
* |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
// Exit if accessed directly |
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
if ( ! class_exists( 'CT_DataBase' ) ) : |
| 15 |
|
| 16 |
class CT_DataBase { |
| 17 |
|
| 18 |
/** |
| 19 |
* @var string Table name, without the global table prefix |
| 20 |
*/ |
| 21 |
protected $name = ''; |
| 22 |
|
| 23 |
/** |
| 24 |
* @var string Table primary key |
| 25 |
*/ |
| 26 |
public $primary_key = ''; |
| 27 |
|
| 28 |
/** |
| 29 |
* @var int Database version |
| 30 |
*/ |
| 31 |
protected $version = 0; |
| 32 |
|
| 33 |
/** |
| 34 |
* @var boolean Is this table for a site, or global |
| 35 |
*/ |
| 36 |
public $global = false; |
| 37 |
|
| 38 |
/** |
| 39 |
* @var string Database version key (saved in _options or _sitemeta) |
| 40 |
*/ |
| 41 |
protected $db_version_key = ''; |
| 42 |
|
| 43 |
/** |
| 44 |
* @var string Current database version |
| 45 |
*/ |
| 46 |
protected $db_version = 0; |
| 47 |
|
| 48 |
/** |
| 49 |
* @var string Table name |
| 50 |
*/ |
| 51 |
public $table_name = ''; |
| 52 |
|
| 53 |
/** |
| 54 |
* @var CT_DataBase_Schema Table schema |
| 55 |
*/ |
| 56 |
public $schema = ''; |
| 57 |
|
| 58 |
/** |
| 59 |
* @var string Database engine for table (default InnoDB) |
| 60 |
*/ |
| 61 |
public $engine = ''; |
| 62 |
|
| 63 |
/** |
| 64 |
* @var string Database character-set & collation for table |
| 65 |
*/ |
| 66 |
public $charset_collation = ''; |
| 67 |
|
| 68 |
/** |
| 69 |
* @var WPDB Database object (usually $GLOBALS['wpdb']) |
| 70 |
*/ |
| 71 |
public $db = false; |
| 72 |
|
| 73 |
/** |
| 74 |
* @var string Used to meet if database is in a group of tables to reduce database exists query calls |
| 75 |
*/ |
| 76 |
public $group = ''; |
| 77 |
|
| 78 |
/** |
| 79 |
* @var bool Stores if database has been found to reduce query calls |
| 80 |
*/ |
| 81 |
protected $exists = false; |
| 82 |
|
| 83 |
/** Methods ***************************************************************/ |
| 84 |
|
| 85 |
/** |
| 86 |
* Hook into queries, admin screens, and more! |
| 87 |
* |
| 88 |
* @since 1.0.0 |
| 89 |
*/ |
| 90 |
public function __construct( $name, $args ) { |
| 91 |
|
| 92 |
$this->name = $name; |
| 93 |
|
| 94 |
$this->primary_key = ( isset( $args['primary_key'] ) ) ? $args['primary_key'] : ''; |
| 95 |
|
| 96 |
$this->version = ( isset( $args['version'] ) ) ? $args['version'] : 1; |
| 97 |
|
| 98 |
$this->global = ( isset( $args['global'] ) && $args['global'] === true ) ? true : false; |
| 99 |
|
| 100 |
$this->schema = ( isset( $args['schema'] ) ) ? new CT_DataBase_Schema( $args['schema'] ) : ''; |
| 101 |
|
| 102 |
$this->group = ( isset( $args['group'] ) ) ? $args['group'] : strtok( $this->name, '_'); |
| 103 |
|
| 104 |
// If not primary key given, then look at out schema |
| 105 |
if( $this->schema && ! $this->primary_key ) { |
| 106 |
foreach( $this->schema->fields as $field_id => $field_args ) { |
| 107 |
if( $field_args['primary_key'] === true ) { |
| 108 |
$this->primary_key = $field_id; |
| 109 |
break; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
$this->engine = ( isset( $args['engine'] ) ) ? $args['engine'] : 'InnoDB'; |
| 115 |
|
| 116 |
// Bail if no database object or table name |
| 117 |
if ( empty( $GLOBALS['wpdb'] ) || empty( $this->name ) ) { |
| 118 |
return; |
| 119 |
} |
| 120 |
|
| 121 |
// Setup the database |
| 122 |
$this->set_db(); |
| 123 |
|
| 124 |
// Get the version of he table currently in the database |
| 125 |
$this->get_db_version(); |
| 126 |
|
| 127 |
// Add the table to the object |
| 128 |
$this->set_wpdb_tables(); |
| 129 |
|
| 130 |
// Setup the database schema |
| 131 |
$this->set_schema(); |
| 132 |
|
| 133 |
// Add hooks to WordPress actions |
| 134 |
$this->add_hooks(); |
| 135 |
} |
| 136 |
|
| 137 |
/** Abstract **************************************************************/ |
| 138 |
|
| 139 |
/** |
| 140 |
* Setup this database table |
| 141 |
* |
| 142 |
* @since 1.0.0 |
| 143 |
*/ |
| 144 |
protected function set_schema() { |
| 145 |
|
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Upgrade this database table |
| 150 |
* |
| 151 |
* @since 1.0.0 |
| 152 |
*/ |
| 153 |
protected function upgrade() { |
| 154 |
|
| 155 |
$schema_updater = new CT_DataBase_Schema_Updater( $this ); |
| 156 |
|
| 157 |
$schema_updater->run(); |
| 158 |
|
| 159 |
} |
| 160 |
|
| 161 |
/** Public ****************************************************************/ |
| 162 |
|
| 163 |
/** |
| 164 |
* Update table version & references. |
| 165 |
* |
| 166 |
* Hooked to the "switch_blog" action. |
| 167 |
* |
| 168 |
* @since 1.0.0 |
| 169 |
* |
| 170 |
* @param int $site_id |
| 171 |
*/ |
| 172 |
public function switch_blog( $site_id = 0 ) { |
| 173 |
|
| 174 |
// Update DB version based on the current site |
| 175 |
if ( false === $this->global ) { |
| 176 |
$this->db_version = get_blog_option( $site_id, $this->db_version_key, false ); |
| 177 |
} |
| 178 |
|
| 179 |
// Update table references based on th current site |
| 180 |
$this->set_wpdb_tables(); |
| 181 |
} |
| 182 |
|
| 183 |
/** |
| 184 |
* Maybe upgrade the database table. Handles creation & schema changes. |
| 185 |
* |
| 186 |
* Hooked to the "admin_init" action. |
| 187 |
* |
| 188 |
* @since 1.0.0 |
| 189 |
*/ |
| 190 |
public function maybe_upgrade() { |
| 191 |
|
| 192 |
// Bail if no upgrade needed |
| 193 |
if ( version_compare( (int) $this->db_version, (int) $this->version, '>=' ) && $this->exists() ) { |
| 194 |
return; |
| 195 |
} |
| 196 |
|
| 197 |
// Include file with dbDelta() for create/upgrade usages |
| 198 |
if ( ! function_exists( 'dbDelta' ) ) { |
| 199 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 200 |
} |
| 201 |
|
| 202 |
// Bail if global and upgrading global tables is not allowed |
| 203 |
if ( ( true === $this->global ) && ! wp_should_upgrade_global_tables() ) { |
| 204 |
return; |
| 205 |
} |
| 206 |
|
| 207 |
// Create or upgrade? |
| 208 |
$this->exists() |
| 209 |
? $this->upgrade() |
| 210 |
: $this->create(); |
| 211 |
|
| 212 |
// Set the database version |
| 213 |
if ( $this->exists() ) { |
| 214 |
$this->set_db_version(); |
| 215 |
} |
| 216 |
} |
| 217 |
|
| 218 |
public function get( $id ) { |
| 219 |
|
| 220 |
return $this->db->get_row( $this->db->prepare( "SELECT * FROM {$this->table_name} WHERE {$this->primary_key} = %s", $id ) ); |
| 221 |
|
| 222 |
} |
| 223 |
|
| 224 |
public function query( $args = array(), $output = OBJECT ) { |
| 225 |
|
| 226 |
return $this->db->get_results( "SELECT * FROM {$this->table_name}" ); |
| 227 |
|
| 228 |
} |
| 229 |
|
| 230 |
public function insert( $data ) { |
| 231 |
|
| 232 |
if( $this->db->insert( $this->table_name, $data ) ) { |
| 233 |
return $this->db->insert_id; |
| 234 |
} |
| 235 |
|
| 236 |
return false; |
| 237 |
|
| 238 |
} |
| 239 |
|
| 240 |
public function update( $data, $where ) { |
| 241 |
|
| 242 |
$table_data = array(); |
| 243 |
$schema_fields = array_keys( $this->schema->fields ); |
| 244 |
|
| 245 |
// Filter extra data to prevent insert data outside table schema |
| 246 |
foreach( $data as $field => $value ) { |
| 247 |
if( ! in_array( $field, $schema_fields ) ) { |
| 248 |
continue; |
| 249 |
} |
| 250 |
|
| 251 |
$table_data[$field] = $value; |
| 252 |
} |
| 253 |
|
| 254 |
return $this->db->update( $this->table_name, $table_data, $where ); |
| 255 |
|
| 256 |
} |
| 257 |
|
| 258 |
public function delete( $value ) { |
| 259 |
|
| 260 |
return $this->db->query( $this->db->prepare( "DELETE FROM {$this->table_name} WHERE {$this->primary_key} = %s", $value ) ); |
| 261 |
|
| 262 |
} |
| 263 |
|
| 264 |
/** Private ***************************************************************/ |
| 265 |
|
| 266 |
/** |
| 267 |
* Setup the necessary WPDB variables |
| 268 |
* |
| 269 |
* @since 1.0.0 |
| 270 |
*/ |
| 271 |
private function set_db() { |
| 272 |
|
| 273 |
// Setup database |
| 274 |
$this->db = $GLOBALS['wpdb']; |
| 275 |
$this->name = sanitize_key( $this->name ); |
| 276 |
|
| 277 |
// Maybe create database key |
| 278 |
if ( empty( $this->db_version_key ) ) { |
| 279 |
$this->db_version_key = "wpdb_{$this->name}_version"; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
/** |
| 284 |
* Modify the database object and add the table to it |
| 285 |
* |
| 286 |
* This is necessary to do directly because WordPress does have a mechanism |
| 287 |
* for manipulating them safely. It's pretty fragile, but oh well. |
| 288 |
* |
| 289 |
* @since 1.0.0 |
| 290 |
*/ |
| 291 |
private function set_wpdb_tables() { |
| 292 |
|
| 293 |
// Global |
| 294 |
if ( true === $this->global ) { |
| 295 |
$prefix = $this->db->get_blog_prefix( 0 ); |
| 296 |
$this->db->{$this->name} = "{$prefix}{$this->name}"; |
| 297 |
$this->db->ms_global_tables[] = $this->name; |
| 298 |
|
| 299 |
// Site |
| 300 |
} else { |
| 301 |
$prefix = $this->db->get_blog_prefix( null ); |
| 302 |
$this->db->{$this->name} = "{$prefix}{$this->name}"; |
| 303 |
$this->db->tables[] = $this->name; |
| 304 |
} |
| 305 |
|
| 306 |
// Set the table name locally |
| 307 |
$this->table_name = $this->db->{$this->name}; |
| 308 |
|
| 309 |
// Charset |
| 310 |
if ( ! empty( $this->db->charset ) ) { |
| 311 |
$this->charset_collation = "DEFAULT CHARACTER SET {$this->db->charset}"; |
| 312 |
} |
| 313 |
|
| 314 |
// Collation |
| 315 |
if ( ! empty( $this->db->collate ) ) { |
| 316 |
$this->charset_collation .= " COLLATE {$this->db->collate}"; |
| 317 |
} |
| 318 |
} |
| 319 |
|
| 320 |
/** |
| 321 |
* Set the database version to the table version. |
| 322 |
* |
| 323 |
* Saves global table version to "wp_sitemeta" to the main network |
| 324 |
* |
| 325 |
* @since 1.0.0 |
| 326 |
*/ |
| 327 |
private function set_db_version() { |
| 328 |
|
| 329 |
// Set the class version |
| 330 |
$this->db_version = $this->version; |
| 331 |
|
| 332 |
// Update the DB version |
| 333 |
( true === $this->global ) |
| 334 |
? update_network_option( null, $this->db_version_key, $this->version ) |
| 335 |
: update_option( $this->db_version_key, $this->version ); |
| 336 |
} |
| 337 |
|
| 338 |
/** |
| 339 |
* Get the table version from the database. |
| 340 |
* |
| 341 |
* Gets global table version from "wp_sitemeta" to the main network |
| 342 |
* |
| 343 |
* @since 1.0.0 |
| 344 |
*/ |
| 345 |
private function get_db_version() { |
| 346 |
$this->db_version = ( true === $this->global ) |
| 347 |
? get_network_option( null, $this->db_version_key, false ) |
| 348 |
: get_option( $this->db_version_key, false ); |
| 349 |
} |
| 350 |
|
| 351 |
/** |
| 352 |
* Add class hooks to WordPress actions |
| 353 |
* |
| 354 |
* @since 1.0.0 |
| 355 |
*/ |
| 356 |
private function add_hooks() { |
| 357 |
|
| 358 |
// Activation hook |
| 359 |
register_activation_hook( __FILE__, array( $this, 'maybe_upgrade' ) ); |
| 360 |
|
| 361 |
// Add table to the global database object |
| 362 |
add_action( 'switch_blog', array( $this, 'switch_blog' ) ); |
| 363 |
add_action( 'admin_init', array( $this, 'maybe_upgrade' ) ); |
| 364 |
} |
| 365 |
|
| 366 |
/** |
| 367 |
* Create the table |
| 368 |
* |
| 369 |
* @since 1.0.0 |
| 370 |
*/ |
| 371 |
private function create() { |
| 372 |
|
| 373 |
// Run CREATE TABLE query |
| 374 |
$created = dbDelta( "CREATE TABLE `{$this->table_name}` ( {$this->schema} ) ENGINE={$this->engine} {$this->charset_collation};" ); |
| 375 |
|
| 376 |
// Was anything created? |
| 377 |
$this->exists = ! empty( $created ); |
| 378 |
|
| 379 |
// Add the table to the group when created |
| 380 |
if( $this->exists && $this->group !== '' ) { |
| 381 |
ct_add_table_to_group( $this->group, $this->table_name ); |
| 382 |
} |
| 383 |
|
| 384 |
return ! empty( $created ); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Check if table already exists |
| 389 |
* |
| 390 |
* @since 1.0.0 |
| 391 |
* |
| 392 |
* @return bool |
| 393 |
*/ |
| 394 |
public function exists() { |
| 395 |
|
| 396 |
if( $this->exists === true ) { |
| 397 |
return $this->exists; |
| 398 |
} |
| 399 |
|
| 400 |
if( $this->group === '' ) { |
| 401 |
// Table not in group |
| 402 |
$table_exist = $this->db->get_var( $this->db->prepare( |
| 403 |
"SHOW TABLES LIKE %s", |
| 404 |
$this->db->esc_like( $this->table_name ) |
| 405 |
) ); |
| 406 |
|
| 407 |
$this->exists = ! empty( $table_exist ); |
| 408 |
} else { |
| 409 |
// Table in group |
| 410 |
$tables = ct_get_tables_in_group( $this->group ); |
| 411 |
|
| 412 |
$this->exists = in_array( $this->table_name, $tables ); |
| 413 |
} |
| 414 |
|
| 415 |
return $this->exists; |
| 416 |
|
| 417 |
} |
| 418 |
|
| 419 |
} |
| 420 |
endif; |