| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
/** |
| 5 |
* Class ActionScheduler_Abstract_Schema |
| 6 |
* |
| 7 |
* @package Action_Scheduler |
| 8 |
* |
| 9 |
* @codeCoverageIgnore |
| 10 |
* |
| 11 |
* Utility class for creating/updating custom tables |
| 12 |
*/ |
| 13 |
abstract class ActionScheduler_Abstract_Schema { |
| 14 |
|
| 15 |
/** |
| 16 |
* @var int Increment this value in derived class to trigger a schema update. |
| 17 |
*/ |
| 18 |
protected $schema_version = 1; |
| 19 |
|
| 20 |
/** |
| 21 |
* @var string Schema version stored in database. |
| 22 |
*/ |
| 23 |
protected $db_version; |
| 24 |
|
| 25 |
/** |
| 26 |
* @var array Names of tables that will be registered by this class. |
| 27 |
*/ |
| 28 |
protected $tables = []; |
| 29 |
|
| 30 |
/** |
| 31 |
* Can optionally be used by concrete classes to carry out additional initialization work |
| 32 |
* as needed. |
| 33 |
*/ |
| 34 |
public function init() {} |
| 35 |
|
| 36 |
/** |
| 37 |
* Register tables with WordPress, and create them if needed. |
| 38 |
* |
| 39 |
* @param bool $force_update Optional. Default false. Use true to always run the schema update. |
| 40 |
* |
| 41 |
* @return void |
| 42 |
*/ |
| 43 |
public function register_tables( $force_update = false ) { |
| 44 |
global $wpdb; |
| 45 |
|
| 46 |
// make WP aware of our tables |
| 47 |
foreach ( $this->tables as $table ) { |
| 48 |
$wpdb->tables[] = $table; |
| 49 |
$name = $this->get_full_table_name( $table ); |
| 50 |
$wpdb->$table = $name; |
| 51 |
} |
| 52 |
|
| 53 |
// create the tables |
| 54 |
if ( $this->schema_update_required() || $force_update ) { |
| 55 |
foreach ( $this->tables as $table ) { |
| 56 |
/** |
| 57 |
* Allow custom processing before updating a table schema. |
| 58 |
* |
| 59 |
* @param string $table Name of table being updated. |
| 60 |
* @param string $db_version Existing version of the table being updated. |
| 61 |
*/ |
| 62 |
do_action( 'action_scheduler_before_schema_update', $table, $this->db_version ); |
| 63 |
$this->update_table( $table ); |
| 64 |
} |
| 65 |
$this->mark_schema_update_complete(); |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* @param string $table The name of the table |
| 71 |
* |
| 72 |
* @return string The CREATE TABLE statement, suitable for passing to dbDelta |
| 73 |
*/ |
| 74 |
abstract protected function get_table_definition( $table ); |
| 75 |
|
| 76 |
/** |
| 77 |
* Determine if the database schema is out of date |
| 78 |
* by comparing the integer found in $this->schema_version |
| 79 |
* with the option set in the WordPress options table |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
private function schema_update_required() { |
| 84 |
$option_name = 'schema-' . static::class; |
| 85 |
$this->db_version = get_option( $option_name, 0 ); |
| 86 |
|
| 87 |
// Check for schema option stored by the Action Scheduler Custom Tables plugin in case site has migrated from that plugin with an older schema |
| 88 |
if ( 0 === $this->db_version ) { |
| 89 |
|
| 90 |
$plugin_option_name = 'schema-'; |
| 91 |
|
| 92 |
switch ( static::class ) { |
| 93 |
case 'ActionScheduler_StoreSchema' : |
| 94 |
$plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Store_Table_Maker'; |
| 95 |
break; |
| 96 |
case 'ActionScheduler_LoggerSchema' : |
| 97 |
$plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Logger_Table_Maker'; |
| 98 |
break; |
| 99 |
} |
| 100 |
|
| 101 |
$this->db_version = get_option( $plugin_option_name, 0 ); |
| 102 |
|
| 103 |
delete_option( $plugin_option_name ); |
| 104 |
} |
| 105 |
|
| 106 |
return version_compare( $this->db_version, $this->schema_version, '<' ); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Update the option in WordPress to indicate that |
| 111 |
* our schema is now up to date |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
private function mark_schema_update_complete() { |
| 116 |
$option_name = 'schema-' . static::class; |
| 117 |
|
| 118 |
// work around race conditions and ensure that our option updates |
| 119 |
$value_to_save = (string) $this->schema_version . '.0.' . time(); |
| 120 |
|
| 121 |
update_option( $option_name, $value_to_save ); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* Update the schema for the given table |
| 126 |
* |
| 127 |
* @param string $table The name of the table to update |
| 128 |
* |
| 129 |
* @return void |
| 130 |
*/ |
| 131 |
private function update_table( $table ) { |
| 132 |
require_once( ABSPATH . 'wp-admin/includes/upgrade.php' ); |
| 133 |
$definition = $this->get_table_definition( $table ); |
| 134 |
if ( $definition ) { |
| 135 |
$updated = dbDelta( $definition ); |
| 136 |
foreach ( $updated as $updated_table => $update_description ) { |
| 137 |
if ( strpos( $update_description, 'Created table' ) === 0 ) { |
| 138 |
do_action( 'action_scheduler/created_table', $updated_table, $table ); |
| 139 |
} |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* @param string $table |
| 146 |
* |
| 147 |
* @return string The full name of the table, including the |
| 148 |
* table prefix for the current blog |
| 149 |
*/ |
| 150 |
protected function get_full_table_name( $table ) { |
| 151 |
return $GLOBALS[ 'wpdb' ]->prefix . $table; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Confirms that all of the tables registered by this schema class have been created. |
| 156 |
* |
| 157 |
* @return bool |
| 158 |
*/ |
| 159 |
public function tables_exist() { |
| 160 |
global $wpdb; |
| 161 |
|
| 162 |
$existing_tables = $wpdb->get_col( 'SHOW TABLES' ); |
| 163 |
$expected_tables = array_map( |
| 164 |
function ( $table_name ) use ( $wpdb ) { |
| 165 |
return $wpdb->prefix . $table_name; |
| 166 |
}, |
| 167 |
$this->tables |
| 168 |
); |
| 169 |
|
| 170 |
return count( array_intersect( $existing_tables, $expected_tables ) ) === count( $expected_tables ); |
| 171 |
} |
| 172 |
} |
| 173 |
|