-
Notifications
You must be signed in to change notification settings - Fork 14
/
repair-action-scheduler.php
327 lines (288 loc) · 10.1 KB
/
repair-action-scheduler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<?php // @codingStandardsIgnoreLine
/**
* Rank Math SEO - Action Scheduler Repair Plugin.
*
* @package RANK_MATH
* @copyright Copyright (C) 2019, Rank Math - [email protected]
* @link https://rankmath.com
* @since 0.9.0
*
* @wordpress-plugin
* Plugin Name: Repair Action Scheduler
* Version: 1.1
* Plugin URI: https://s.rankmath.com/home
* Description: Fix database errors related to the Action Scheduler library. The plugin checks and creates the tables necessary for Action Scheduler version 3.3.0. The faulty tables will be renamed and new ones will be created instead. This plugin runs once and does NOT need to stay activated on the site.
* Author: Rank Math
* Author URI: https://s.rankmath.com/home
* License: GPL-2.0+
* License URI: http://www.gnu.org/licenses/gpl-2.0.txt
* Text Domain: repair-action-scheduler
* Domain Path: /languages
*/
defined( 'ABSPATH' ) || exit;
/**
* Updater class.
*/
class Repair_Action_Scheduler {
const ACTIONS_TABLE = 'actionscheduler_actions';
const CLAIMS_TABLE = 'actionscheduler_claims';
const GROUPS_TABLE = 'actionscheduler_groups';
const LOG_TABLE = 'actionscheduler_logs';
/**
* Tables and their primary columns.
*
* @var array
*/
private $tables = array();
/**
* Admin notices.
*/
private $notices = array();
/**
* 1. Check if the actionscheduler tables exist in the DB.
* 2. Check if PRIMARY KEY is set.
* 3. If not set, then rename it and create a new table.
* 4. Check if AUTO_INCREMENT is set.
* 5. If not set, then rename it and create a new table.
*/
public function __construct() {
if ( get_option( 'ras_notices' ) === false ) {
$this->do_repair();
return;
}
$this->notices = get_option( 'ras_notices', array() );
add_action( 'admin_notices', array( $this, 'show_notices' ) );
}
/**
* Add admin notice.
*/
private function add_notice( $message ) {
$this->notices[] = $message;
}
/**
* Show admin notices.
*/
public function show_notices() {
if ( empty( $this->notices ) ) {
return;
}
echo '<div class="notice notice-info is-dismissible repair-action-scheduler-notice">';
if ( count( $this->notices ) == 1 ) {
$this->add_notice( '<em>' . __( 'No actions performed.', 'repair-action-scheduler' ) . '</em>' );
}
$this->add_notice( '<em>' . __( 'The Repair Action Scheduler plugin has been automatically deactivated.', 'repair-action-scheduler' ) . '</em>' );
foreach ( $this->notices as $message ) {
echo '<p>'.$message.'</p>';
}
echo '</div>';
update_option( 'ras_notices', array() );
deactivate_plugins( plugin_basename( __FILE__ ) );
}
/**
* Save plugin data to DB.
*/
private function save_data() {
update_option( 'ras_notices', $this->notices );
}
/**
* Run the repair process.
*/
private function do_repair() {
if ( substr( get_option( 'schema-ActionScheduler_StoreSchema', '' ), 0, 1 ) > 6 ) {
$this->add_notice( __( 'The Repair Action Scheduler could not run because the repair database schema is obsolete.', 'repair-action-scheduler' ) );
$this->save_data();
return;
}
$this->suffix = '_' . substr( md5( microtime() ), 0, 4 );
$this->add_notice( '<strong>' . __( 'The Repair Action Scheduler process is complete.', 'repair-action-scheduler' ) . '</strong>' . __( 'The following actions have been performed:', 'repair-action-scheduler' ) );
$this->tables = array(
self::ACTIONS_TABLE => 'action_id',
self::CLAIMS_TABLE => 'claim_id',
self::GROUPS_TABLE => 'group_id',
self::LOG_TABLE => 'log_id',
);
$do_reset = false;
foreach ( $this->tables as $table => $primary_column ) {
if ( ! $this->table_exists( $table ) ) {
$this->create_table( $table );
}
if ( ! $do_reset && ( ! $this->has_primary_key( $table ) || ! $this->has_auto_increment( $table ) ) ) {
$do_reset = true;
}
}
// Re-add all the tables, otherwise interlinked IDs might not match.
if ( $do_reset ) {
foreach ( $this->tables as $table => $primary_column ) {
$this->rename_table( $table );
$this->create_table( $table );
}
}
$this->save_data();
}
/**
* Check if table has a primary key set.
*
* @param string $table Table name.
*
* @return bool
*/
private function has_primary_key( $table ) {
global $wpdb;
$table_name = $wpdb->prefix . $table;
$column_name = $this->tables[ $table ];
$has_primary = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_KEY = 'PRI' AND TABLE_NAME = '{$table_name}' AND COLUMN_NAME='{$column_name}'" ); // phpcs:ignore
return (bool) $has_primary;
}
/**
* Check if table has the auto increment property set on a column.
*
* @param string $table Table name.
*
* @return bool
*/
private function has_auto_increment( $table ) {
global $wpdb;
$table_name = $wpdb->prefix . $table;
$column_name = $this->tables[ $table ];
$has_auto_increment = $wpdb->query( "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '{$table_name}' AND COLUMN_NAME='{$column_name}' AND EXTRA like '%auto_increment%'" ); // phpcs:ignore
return (bool) $has_auto_increment;
}
/**
* Rename schema options.
*/
private function rename_options() {
$options = array(
'schema-ActionScheduler_LoggerSchema' => '6',
'schema-ActionScheduler_StoreSchema' => '3'
);
foreach ( $options as $option => $version ) {
if ( $stored = get_option( $option ) ) {
$value_to_save = (string) $version . '.0.' . time();
update_option( $option, $value_to_save );
// Translators: placeholder is the table name wrapped in CODE tags.
$this->add_notice( sprintf( __( 'Renamed option: %1$s to %2$s', 'repair-action-scheduler' ), '<code>' . $option . '</code>', '<code>' . $option . $this->suffix . '</code>' ) );
}
}
}
/**
* Check if a table exists.
*
* @param string $table Table name.
*/
private function table_exists( $table ) {
global $wpdb;
$tables = $wpdb->query( "SHOW TABLES LIKE '{$wpdb->prefix}{$table}'" ); // phpcs:ignore
return (bool) $tables;
}
/**
* Create a table.
*
* @param string $table Table name.
*/
private function create_table( $table ) {
global $wpdb;
$wpdb->query( $this->get_table_schema( $table ) ); // phpcs:ignore
// Translators: placeholder is the table name wrapped in CODE tags.
$this->add_notice( sprintf( __( 'Created table: %s', 'repair-action-scheduler' ), '<code>' . $table . '</code>' ) );
}
/**
* Deactivate the Analytics module in Rank Math.
*/
private function maybe_deactivate_analytics_module() {
$modules = get_option( 'rank_math_modules' );
if ( ! is_array( $modules ) ) {
return;
}
$new_modules = array_values( array_diff( $modules, array( 'analytics', 'search-console' ) ) );
$this->add_notice( __( 'Deactivated the Analytics module in Rank Math.', 'repair-action-scheduler' ) );
}
/**
* Rename a table.
*
* @param string $table Table name.
*/
private function rename_table( $table ) {
global $wpdb;
$wpdb->query( "ALTER TABLE {$wpdb->prefix}{$table} RENAME TO {$wpdb->prefix}{$table}{$this->suffix};" ); // phpcs:ignore
// Translators: placeholder is the table name wrapped in CODE tags.
$this->add_notice( sprintf( __( 'Renamed table: %1$s to %2$s', 'repair-action-scheduler' ), '<code>' . $table . '</code>', '<code>' . $table . $this->suffix . '</code>' ) );
}
/**
* Get table schema.
*
* @param string $table Table name.
*
* @return string
*/
private function get_table_schema( $table ) {
global $wpdb;
$table_name = $wpdb->prefix . $table;
$charset_collate = $wpdb->get_charset_collate();
$max_index_length = 191; // @see wp_get_db_schema()
$default_date = '0000-00-00 00:00:00';
switch ( $table ) {
case self::ACTIONS_TABLE:
return "CREATE TABLE {$table_name} (
action_id bigint(20) unsigned NOT NULL auto_increment,
hook varchar(191) NOT NULL,
status varchar(20) NOT NULL,
scheduled_date_gmt datetime NULL default '${default_date}',
scheduled_date_local datetime NULL default '${default_date}',
args varchar($max_index_length),
schedule longtext,
group_id bigint(20) unsigned NOT NULL default '0',
attempts int(11) NOT NULL default '0',
last_attempt_gmt datetime NULL default '${default_date}',
last_attempt_local datetime NULL default '${default_date}',
claim_id bigint(20) unsigned NOT NULL default '0',
extended_args varchar(8000) DEFAULT NULL,
PRIMARY KEY (action_id),
KEY hook (hook($max_index_length)),
KEY status (status),
KEY scheduled_date_gmt (scheduled_date_gmt),
KEY args (args($max_index_length)),
KEY group_id (group_id),
KEY last_attempt_gmt (last_attempt_gmt),
KEY `claim_id_status_scheduled_date_gmt` (`claim_id`, `status`, `scheduled_date_gmt`)
) $charset_collate";
case self::CLAIMS_TABLE:
return "CREATE TABLE {$table_name} (
claim_id bigint(20) unsigned NOT NULL auto_increment,
date_created_gmt datetime NULL default '${default_date}',
PRIMARY KEY (claim_id),
KEY date_created_gmt (date_created_gmt)
) $charset_collate";
case self::GROUPS_TABLE:
return "CREATE TABLE {$table_name} (
group_id bigint(20) unsigned NOT NULL auto_increment,
slug varchar(255) NOT NULL,
PRIMARY KEY (group_id),
KEY slug (slug($max_index_length))
) $charset_collate";
case self::LOG_TABLE:
return "CREATE TABLE {$table_name} (
log_id bigint(20) unsigned NOT NULL auto_increment,
action_id bigint(20) unsigned NOT NULL,
message text NOT NULL,
log_date_gmt datetime NULL default '${default_date}',
log_date_local datetime NULL default '${default_date}',
PRIMARY KEY (log_id),
KEY action_id (action_id),
KEY log_date_gmt (log_date_gmt)
) $charset_collate";
default:
return '';
}
}
}
/**
* Cleanup the database after the plugin is deactivated.
*/
function repair_action_scheduler_clean() {
delete_option( 'ras_notices' );
}
register_deactivation_hook( __FILE__, 'repair_action_scheduler_clean' );
/**
* Initialize the plugin.
*/
$repair_action_scheduler = new Repair_Action_Scheduler();