| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) die('No direct access allowed'); |
| 4 |
|
| 5 |
if (!class_exists('WP_Optimize_Table_Management')) : |
| 6 |
|
| 7 |
class WP_Optimize_Table_Management { |
| 8 |
|
| 9 |
/** |
| 10 |
* Utilities |
| 11 |
* |
| 12 |
* @var array |
| 13 |
*/ |
| 14 |
private $plugin_utils = array(); |
| 15 |
|
| 16 |
/** |
| 17 |
* Populate `$this->plugin_utils` for later usage. |
| 18 |
* |
| 19 |
* @return void |
| 20 |
*/ |
| 21 |
private function __construct() { |
| 22 |
$this->plugin_utils[] = new WP_Optimize_Table_404_Detector(); |
| 23 |
} |
| 24 |
|
| 25 |
/** |
| 26 |
* Returns a singleton instance of this class |
| 27 |
* |
| 28 |
* @return WP_Optimize_Table_Management Singleton Instance |
| 29 |
*/ |
| 30 |
public static function get_instance() { |
| 31 |
static $instance = null; |
| 32 |
if (null === $instance) { |
| 33 |
$instance = new self(); |
| 34 |
} |
| 35 |
return $instance; |
| 36 |
} |
| 37 |
|
| 38 |
/** |
| 39 |
* Create all the tables that will be used by this plugin |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function create_plugin_tables(): void { |
| 44 |
/* @var $util WP_Optimize_Table_Interface */ |
| 45 |
foreach ($this->plugin_utils as $util) { |
| 46 |
$table_name = $util->get_table_name(); |
| 47 |
$table_description = $util->describe(); |
| 48 |
|
| 49 |
$this->create_table($table_name, $table_description); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Create the table if it does not exist. |
| 55 |
* |
| 56 |
* @param string $table_name The name of the table that needs to be checked and if not exists, then create it |
| 57 |
* @param array $table_description Table definition provider |
| 58 |
* |
| 59 |
* @return void |
| 60 |
*/ |
| 61 |
private function create_table($table_name, $table_description): void { |
| 62 |
if ($this->table_exists($table_name)) { |
| 63 |
return; |
| 64 |
} |
| 65 |
|
| 66 |
$create_table = $this->generate_create_statement($table_name, $table_description); |
| 67 |
|
| 68 |
require_once ABSPATH . 'wp-admin/includes/upgrade.php'; |
| 69 |
dbDelta($create_table); |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* Remove all tables that were created by this plugin |
| 74 |
* |
| 75 |
* @return void |
| 76 |
*/ |
| 77 |
public function delete_plugin_tables() { |
| 78 |
global $wpdb; |
| 79 |
|
| 80 |
foreach ($this->plugin_utils as $util) { |
| 81 |
$wpdb->query(sprintf("DROP TABLE IF EXISTS %s", esc_sql($util->get_table_name()))); |
| 82 |
} |
| 83 |
} |
| 84 |
|
| 85 |
/** |
| 86 |
* Check for the existence of a table |
| 87 |
* |
| 88 |
* @param string $table_name The name of the table we are searching for |
| 89 |
* @return bool |
| 90 |
*/ |
| 91 |
private function table_exists($table_name) { |
| 92 |
global $wpdb; |
| 93 |
|
| 94 |
return $wpdb->get_var($wpdb->prepare("SHOW TABLES LIKE %s", $table_name)) === $table_name; |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Compile the SQL that is going to be sent to WordPress's dbDelta() |
| 99 |
* |
| 100 |
* @param string $table_name The name of the table to be created |
| 101 |
* @param array $table_description Definition of the table to be created |
| 102 |
* @return string |
| 103 |
*/ |
| 104 |
private function generate_create_statement($table_name, $table_description) { |
| 105 |
global $wpdb; |
| 106 |
$charset_collate = $wpdb->get_charset_collate(); |
| 107 |
|
| 108 |
$keys_sql = $this->generate_keys_sql($table_description); |
| 109 |
|
| 110 |
$fields_sql = $this->generate_fields_sql($table_description); |
| 111 |
|
| 112 |
return "CREATE TABLE `$table_name` ( |
| 113 |
ID int(11) UNSIGNED NOT NULL auto_increment, |
| 114 |
" . implode(",\n", $fields_sql) . ", |
| 115 |
PRIMARY KEY (ID)" . implode(",\n", $keys_sql) . " |
| 116 |
) $charset_collate"; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Compile the SQL for the keys piece of the creation statement |
| 121 |
* |
| 122 |
* @param array $table_description Definition of the table, including keys |
| 123 |
* @return array |
| 124 |
*/ |
| 125 |
private function generate_keys_sql($table_description) { |
| 126 |
$keys_sql = array(); |
| 127 |
if (isset($table_description['keys']) && is_array($table_description['keys'])) { |
| 128 |
$keys_sql = array(''); // This will add the first comma after `PRIMARY KEY` block |
| 129 |
foreach ($table_description['keys'] as $key_name => $key_def) { |
| 130 |
$keys_sql[] = 'KEY `' . $key_name . '` ' . $key_def; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
if (isset($table_description['unique']) && is_string($table_description['unique'])) { |
| 135 |
if (empty($keys_sql)) $keys_sql = array(''); |
| 136 |
|
| 137 |
$keys_sql[] = 'UNIQUE (' . $table_description['unique'] . ')'; |
| 138 |
} |
| 139 |
|
| 140 |
return $keys_sql; |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* Compile the SQL for the fields piece of the `create` statement |
| 145 |
* |
| 146 |
* @param array $table_description Definition of the table, including keys |
| 147 |
* @return array |
| 148 |
*/ |
| 149 |
private function generate_fields_sql($table_description) { |
| 150 |
$fields_sql = array(); |
| 151 |
foreach ($table_description['fields'] as $field_name => $field_def) { |
| 152 |
$fields_sql[] = '`' . $field_name . '` ' . $field_def; |
| 153 |
} |
| 154 |
|
| 155 |
return $fields_sql; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
endif; |
| 160 |
|