PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / abstracts / ActionScheduler_Abstract_Schema.php

ActionScheduler_Abstract_Schema.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler_Abstract_Schema.php

182 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 = array();
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 * Get table definition.
71 *
72 * @param string $table The name of the table.
73 *
74 * @return string The CREATE TABLE statement, suitable for passing to dbDelta
75 */
76 abstract protected function get_table_definition( $table );
77
78 /**
79 * Determine if the database schema is out of date
80 * by comparing the integer found in $this->schema_version
81 * with the option set in the WordPress options table
82 *
83 * @return bool
84 */
85 private function schema_update_required() {
86 $option_name = 'schema-' . static::class;
87 $this->db_version = get_option( $option_name, 0 );
88
89 // Check for schema option stored by the Action Scheduler Custom Tables plugin in case site has migrated from that plugin with an older schema.
90 if ( 0 === $this->db_version ) {
91
92 $plugin_option_name = 'schema-';
93
94 switch ( static::class ) {
95 case 'ActionScheduler_StoreSchema':
96 $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Store_Table_Maker';
97 break;
98 case 'ActionScheduler_LoggerSchema':
99 $plugin_option_name .= 'Action_Scheduler\Custom_Tables\DB_Logger_Table_Maker';
100 break;
101 }
102
103 $this->db_version = get_option( $plugin_option_name, 0 );
104
105 delete_option( $plugin_option_name );
106 }
107
108 return version_compare( $this->db_version, $this->schema_version, '<' );
109 }
110
111 /**
112 * Update the option in WordPress to indicate that
113 * our schema is now up to date
114 *
115 * @return void
116 */
117 private function mark_schema_update_complete() {
118 $option_name = 'schema-' . static::class;
119
120 // work around race conditions and ensure that our option updates.
121 $value_to_save = (string) $this->schema_version . '.0.' . time();
122
123 update_option( $option_name, $value_to_save );
124 }
125
126 /**
127 * Update the schema for the given table
128 *
129 * @param string $table The name of the table to update.
130 *
131 * @return void
132 */
133 private function update_table( $table ) {
134 require_once ABSPATH . 'wp-admin/includes/upgrade.php';
135 $definition = $this->get_table_definition( $table );
136 if ( $definition ) {
137 $updated = dbDelta( $definition );
138 foreach ( $updated as $updated_table => $update_description ) {
139 if ( strpos( $update_description, 'Created table' ) === 0 ) {
140 do_action( 'action_scheduler/created_table', $updated_table, $table );
141 }
142 }
143 }
144 }
145
146 /**
147 * Get full table name.
148 *
149 * @param string $table Table name.
150 *
151 * @return string The full name of the table, including the
152 * table prefix for the current blog
153 */
154 protected function get_full_table_name( $table ) {
155 return $GLOBALS['wpdb']->prefix . $table;
156 }
157
158 /**
159 * Confirms that all of the tables registered by this schema class have been created.
160 *
161 * @return bool
162 */
163 public function tables_exist() {
164 global $wpdb;
165
166 $tables_exist = true;
167
168 foreach ( $this->tables as $table_name ) {
169 $table_name = $wpdb->prefix . $table_name;
170 $pattern = str_replace( '_', '\\_', $table_name );
171 $existing_table = $wpdb->get_var( $wpdb->prepare( 'SHOW TABLES LIKE %s', $pattern ) );
172
173 if ( $existing_table !== $table_name ) {
174 $tables_exist = false;
175 break;
176 }
177 }
178
179 return $tables_exist;
180 }
181 }
182