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