image-optimization
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
schema
/
ActionScheduler_LoggerSchema.php
ActionScheduler_LoggerSchema.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at vendor/woocommerce/action-scheduler/classes/schema/ActionScheduler_LoggerSchema.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class ActionScheduler_LoggerSchema |
| 5 | * |
| 6 | * @codeCoverageIgnore |
| 7 | * |
| 8 | * Creates a custom table for storing action logs |
| 9 | */ |
| 10 | class ActionScheduler_LoggerSchema extends ActionScheduler_Abstract_Schema { |
| 11 | const LOG_TABLE = 'actionscheduler_logs'; |
| 12 | |
| 13 | /** |
| 14 | * @var int Increment this value to trigger a schema update. |
| 15 | */ |
| 16 | protected $schema_version = 3; |
| 17 | |
| 18 | /** |
| 19 | * Construct. |
| 20 | */ |
| 21 | public function __construct() { |
| 22 | $this->tables = [ |
| 23 | self::LOG_TABLE, |
| 24 | ]; |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Performs additional setup work required to support this schema. |
| 29 | */ |
| 30 | public function init() { |
| 31 | add_action( 'action_scheduler_before_schema_update', array( $this, 'update_schema_3_0' ), 10, 2 ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Get table definition. |
| 36 | * |
| 37 | * @param string $table Table name. |
| 38 | */ |
| 39 | protected function get_table_definition( $table ) { |
| 40 | global $wpdb; |
| 41 | $table_name = $wpdb->$table; |
| 42 | $charset_collate = $wpdb->get_charset_collate(); |
| 43 | switch ( $table ) { |
| 44 | |
| 45 | case self::LOG_TABLE: |
| 46 | |
| 47 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
| 48 | return "CREATE TABLE $table_name ( |
| 49 | log_id bigint(20) unsigned NOT NULL auto_increment, |
| 50 | action_id bigint(20) unsigned NOT NULL, |
| 51 | message text NOT NULL, |
| 52 | log_date_gmt datetime NULL default '{$default_date}', |
| 53 | log_date_local datetime NULL default '{$default_date}', |
| 54 | PRIMARY KEY (log_id), |
| 55 | KEY action_id (action_id), |
| 56 | KEY log_date_gmt (log_date_gmt) |
| 57 | ) $charset_collate"; |
| 58 | |
| 59 | default: |
| 60 | return ''; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * Update the logs table schema, allowing datetime fields to be NULL. |
| 66 | * |
| 67 | * This is needed because the NOT NULL constraint causes a conflict with some versions of MySQL |
| 68 | * configured with sql_mode=NO_ZERO_DATE, which can for instance lead to tables not being created. |
| 69 | * |
| 70 | * Most other schema updates happen via ActionScheduler_Abstract_Schema::update_table(), however |
| 71 | * that method relies on dbDelta() and this change is not possible when using that function. |
| 72 | * |
| 73 | * @param string $table Name of table being updated. |
| 74 | * @param string $db_version The existing schema version of the table. |
| 75 | */ |
| 76 | public function update_schema_3_0( $table, $db_version ) { |
| 77 | global $wpdb; |
| 78 | |
| 79 | if ( 'actionscheduler_logs' !== $table || version_compare( $db_version, '3', '>=' ) ) { |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 84 | $table_name = $wpdb->prefix . 'actionscheduler_logs'; |
| 85 | $table_list = $wpdb->get_col( "SHOW TABLES LIKE '{$table_name}'" ); |
| 86 | $default_date = ActionScheduler_StoreSchema::DEFAULT_DATE; |
| 87 | |
| 88 | if ( ! empty( $table_list ) ) { |
| 89 | $query = " |
| 90 | ALTER TABLE {$table_name} |
| 91 | MODIFY COLUMN log_date_gmt datetime NULL default '{$default_date}', |
| 92 | MODIFY COLUMN log_date_local datetime NULL default '{$default_date}' |
| 93 | "; |
| 94 | $wpdb->query( $query ); // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared |
| 95 | } |
| 96 | // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared |
| 97 | } |
| 98 | } |
| 99 |