class-events.php 18.6 KB
Newer Older
1
2
3
4
5
<?php

namespace Automattic\WP\Cron_Control\CLI;

/**
6
 * Manage Cron Control's data
7
 */
8
class Events extends \WP_CLI_Command {
9
10
11
12
13
	/**
	 * List cron events
	 *
	 * Intentionally bypasses caching to ensure latest data is shown
	 *
14
	 * @subcommand list
15
	 * @synopsis [--status=<pending|completed>] [--page=<page>] [--limit=<limit>] [--format=<format>]
16
	 */
17
	public function list_events( $args, $assoc_args ) {
18
		$events = $this->get_events( $args, $assoc_args );
19
20
21
22
23
24
25
26
27
28
29
30

		// Output in the requested format
		if ( isset( $assoc_args['format'] ) && 'ids' === $assoc_args['format'] ) {
			echo implode( ' ', wp_list_pluck( $events['items'], 'ID' ) );
		} else {
			// Lest someone think the `completed` record should be...complete
			if ( isset( $assoc_args['status'] ) && 'completed' === $assoc_args['status'] ) {
				\WP_CLI::warning( __( 'Entries are purged automatically, so this cannot be relied upon as a record of past event execution.', 'automattic-cron-control' ) );
			}

			// Not much to do
			if ( 0 === $events['total_items'] ) {
31
				\WP_CLI::success( __( 'No events to display', 'automattic-cron-control' ) );
32
33
34
35
36
37
38
39
40
				return;
			}

			// Prepare events for display
			$events_for_display      = $this->format_events( $events['items'] );
			$total_events_to_display = count( $events_for_display );

			// Count, noting if showing fewer than all
			if ( $events['total_items'] <= $total_events_to_display ) {
Erick Hitter's avatar
Erick Hitter committed
41
				\WP_CLI::line( sprintf( _n( 'Displaying one entry', 'Displaying all %s entries', $total_events_to_display, 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ) ) );
42
			} else {
Erick Hitter's avatar
Erick Hitter committed
43
				\WP_CLI::line( sprintf( __( 'Displaying %s of %s entries, page %s of %s', 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ), number_format_i18n( $events['total_items'] ), number_format_i18n( $events['page'] ), number_format_i18n( $events['total_pages'] ) ) );
44
45
46
			}

			// And reformat
Erick Hitter's avatar
Erick Hitter committed
47
48
49
50
51
			$format = 'table';
			if ( isset( $assoc_args['format'] ) ) {
				$format = $assoc_args['format'];
			}

52
53
54
55
56
57
			\WP_CLI\Utils\format_items( $format, $events_for_display, array(
				'ID',
				'action',
				'instance',
				'next_run_gmt',
				'next_run_relative',
Erick Hitter's avatar
Erick Hitter committed
58
				'last_updated_gmt',
59
				'recurrence',
60
				'internal_event',
61
				'schedule_name',
62
63
64
				'event_args',
			) );
		}
65
66
	}

67
68
69
70
71
72
73
74
75
76
	/**
	 * Remove events by ID or action
	 *
	 * @subcommand delete
	 * @synopsis [--event_id=<event_id>] [--action=<action>]
	 */
	public function delete_events( $args, $assoc_args ) {
		// Remove a specific event
		if ( isset( $assoc_args['event_id'] ) ) {
			$this->delete_event_by_id( $args, $assoc_args );
77
			return;
78
79
80
81
		}

		// Remove all events with a given action
		if ( isset( $assoc_args['action'] ) ) {
Erick Hitter's avatar
Typo    
Erick Hitter committed
82
			$this->delete_event_by_action( $args, $assoc_args );
83
			return;
84
85
86
87
88
		}

		\WP_CLI::error( __( 'Specify something to delete, or see the `cron-control-fixers` command to remove all data.', 'automattic-cron-control' ) );
	}

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
	/**
	 * Run an event given an ID
	 *
	 * @subcommand run
	 * @synopsis <event_id>
	 */
	public function run_event( $args, $assoc_args ) {
		// Validate ID
		if ( ! is_numeric( $args[0] ) ) {
			\WP_CLI::error( __( 'Specify the ID of an event to run', 'automattic-cron-control' ) );
		}

		// Validate event ID and get the information needed to execute it
		global $wpdb;

		$event = $wpdb->get_var( $wpdb->prepare( "SELECT post_title FROM {$wpdb->posts} WHERE ID = %d AND post_type = %s AND post_status = %s LIMIT 1", $args[0], \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING ) );

		if ( empty( $event ) ) {
			\WP_CLI::error( sprintf( __( 'Failed to locate event %d. Please confirm that the entry exists and that the ID is that of an event.', 'automattic-cron-control' ), $args[0] ) );
		}

		// Event data
		$event_data = $this->get_event_details_from_post_title( $event );

		\WP_CLI::line( sprintf( __( 'Found event %d with action `%s` and instance identifier `%s`', 'automattic-cron-control' ), $args[0], $event_data['action'], $event_data['instance'] ) );

		// Proceed?
116
117
118
		$now = time();
		if ( $event_data['timestamp'] > $now ) {
			\WP_CLI::warning( sprintf( __( 'This event is not scheduled to run until %1$s GMT (%2$s)', 'automattic-cron-control' ), date( TIME_FORMAT, $event_data['timestamp'] ), $this->calculate_interval( $event_data['timestamp'] - $now ) ) );
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
		}

		\WP_CLI::confirm( sprintf( __( 'Run this event?', 'automattic-cron-control' ) ) );

		// Environment preparation
		if ( ! defined( 'DOING_CRON' ) ) {
			define( 'DOING_CRON', true );
		}

		// Run the event
		$run = \Automattic\WP\Cron_Control\Events::instance()->run_event( $event_data['timestamp'], md5( $event_data['action'] ), $event_data['instance'], true );

		// Output based on run attempt
		if ( is_array( $run ) ) {
			\WP_CLI::success( $run['message'] );
		} elseif ( is_wp_error( $run ) ) {
			\WP_CLI::error( $run->get_error_message() );
		} else {
			\WP_CLI::error( __( 'Failed to run event', 'automattic-cron-control' ) );
		}
	}

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
	/**
	 * Retrieve list of events, and related data, for a given request
	 */
	private function get_events( $args, $assoc_args ) {
		global $wpdb;

		// Validate status, with a default
		$status = 'pending';
		if ( isset( $assoc_args['status'] ) ) {
			$status = $assoc_args['status'];
		}

		if ( 'pending' !== $status && 'completed' !== $status ) {
			\WP_CLI::error( __( 'Invalid status requested', 'automattic-cron-control' ) );
		}

		// Convert to post status
		$post_status = null;
		switch ( $status ) {
			case 'pending' :
				$post_status = \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_PENDING;
				break;

			case 'completed' :
				$post_status = \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_STATUS_COMPLETED;
				break;
		}

		// Total to show
		$limit = 25;
		if ( isset( $assoc_args['limit'] ) && is_numeric( $assoc_args['limit'] ) ) {
			$limit = max( 1, min( absint( $assoc_args['limit'] ), 500 ) );
		}

		// Pagination
		$page = 1;
		if ( isset( $assoc_args['page'] ) && is_numeric( $assoc_args['page'] ) ) {
			$page = absint( $assoc_args['page'] );
		}

		$offset = absint( ( $page - 1 ) * $limit );

		// Query
184
		$items = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS ID, post_title, post_content_filtered, post_date_gmt, post_modified_gmt FROM {$wpdb->posts} WHERE post_type = %s AND post_status = %s ORDER BY post_date ASC LIMIT %d,%d", \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, $post_status, $offset, $limit ) );
185
186
187
188
189
190

		// Bail if we don't get results
		if ( ! is_array( $items ) ) {
			\WP_CLI::error( __( 'Problem retrieving events', 'automattic-cron-control' ) );
		}

Erick Hitter's avatar
Erick Hitter committed
191
		// Include totals for pagination etc
192
		$total_items = (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' );
Erick Hitter's avatar
Erick Hitter committed
193
		$total_pages = ceil( $total_items / $limit );
194

Erick Hitter's avatar
Erick Hitter committed
195
		return compact( 'status', 'limit', 'page', 'offset', 'items', 'total_items', 'total_pages' );
196
197
198
	}

	/**
199
	 * Format event data into something human-readable
200
	 */
201
202
203
204
205
206
	private function format_events( $events ) {
		$formatted_events = array();

		// Reformat events
		foreach ( $events as $event ) {
			$row = array(
Erick Hitter's avatar
Erick Hitter committed
207
				'ID'                => $event->ID,
208
209
				'action'            => '',
				'instance'          => '',
Erick Hitter's avatar
Erick Hitter committed
210
				'next_run_gmt'      => date( TIME_FORMAT, strtotime( $event->post_date_gmt ) ),
Erick Hitter's avatar
Erick Hitter committed
211
				'next_run_relative' => $this->calculate_interval( strtotime( $event->post_date_gmt ) - time() ),
Erick Hitter's avatar
Erick Hitter committed
212
				'last_updated_gmt'  => date( TIME_FORMAT, strtotime( $event->post_modified_gmt ) ),
213
				'recurrence'        => __( 'Non-repeating', 'automattic-cron-control' ),
214
				'internal_event'    => '',
215
				'schedule_name'     => __( 'n/a', 'automattic-cron-control' ),
216
217
218
				'event_args'        => '',
			);

Erick Hitter's avatar
Erick Hitter committed
219
220
221
222
223
			// Most data serialized in the post
			$all_args = maybe_unserialize( $event->post_content_filtered );
			if ( is_array( $all_args ) ) {
				// Action
				if ( isset( $all_args['action'] ) ) {
224
225
					$row['action']         = $all_args['action'];
					$row['internal_event'] = \Automattic\WP\Cron_Control\is_internal_event( $all_args['action'] ) ? __( 'true', 'automattic-cron-control' ) : '';
Erick Hitter's avatar
Erick Hitter committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
				}

				// Instance
				if ( isset( $all_args['instance'] ) ) {
					$row['instance'] = $all_args['instance'];
				}

				// Additional arguments
				if ( isset( $all_args['args'] ) ) {
					$args = $all_args['args'];

					// Event arguments themselves
					if ( isset( $args['args'] ) ) {
						$row['event_args'] = maybe_serialize( $args['args'] );
					}

242
					// Human-readable version of next run
Erick Hitter's avatar
Erick Hitter committed
243
244
245
					if ( isset( $args['interval'] ) && $args['interval'] ) {
						$row['recurrence'] = $this->calculate_interval( $args['interval'] );
					}
246
247
248
249
250

					// Named schedule
					if ( isset( $args['schedule'] ) && $args['schedule'] ) {
						$row['schedule_name'] = $args['schedule'];
					}
Erick Hitter's avatar
Erick Hitter committed
251
252
253
				}
			}

254
255
256
257
258
			$formatted_events[] = $row;
		}

		return $formatted_events;
	}
Erick Hitter's avatar
Erick Hitter committed
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

	/**
	 * Convert a time interval into human-readable format.
	 *
	 * Similar to WordPress' built-in `human_time_diff()` but returns two time period chunks instead of just one.
	 *
	 * Borrowed from WP-CLI
	 *
	 * @param int $since An interval of time in seconds
	 * @return string The interval in human readable format
	 */
	private function calculate_interval( $since ) {
		if ( $since <= 0 ) {
			return 'now';
		}

		$since = absint( $since );

		// array of time period chunks
		$chunks = array(
			array( 60 * 60 * 24 * 365 , \_n_noop( '%s year', '%s years' ) ),
			array( 60 * 60 * 24 * 30 , \_n_noop( '%s month', '%s months' ) ),
			array( 60 * 60 * 24 * 7, \_n_noop( '%s week', '%s weeks' ) ),
			array( 60 * 60 * 24 , \_n_noop( '%s day', '%s days' ) ),
			array( 60 * 60 , \_n_noop( '%s hour', '%s hours' ) ),
			array( 60 , \_n_noop( '%s minute', '%s minutes' ) ),
			array(  1 , \_n_noop( '%s second', '%s seconds' ) ),
		);

		// we only want to output two chunks of time here, eg:
		// x years, xx months
		// x days, xx hours
		// so there's only two bits of calculation below:

		// step one: the first chunk
		for ( $i = 0, $j = count( $chunks ); $i < $j; $i++ ) {
			$seconds = $chunks[$i][0];
			$name = $chunks[$i][1];

			// finding the biggest chunk (if the chunk fits, break)
			if ( ( $count = floor( $since / $seconds ) ) != 0 ){
				break;
			}
		}

		// set output var
		$output = sprintf( \_n( $name[0], $name[1], $count ), $count );

		// step two: the second chunk
		if ( $i + 1 < $j ) {
			$seconds2 = $chunks[$i + 1][0];
			$name2    = $chunks[$i + 1][1];

			if ( ( $count2 = floor( ( $since - ( $seconds * $count ) ) / $seconds2 ) ) != 0 ) {
				// add to output var
				$output .= ' ' . sprintf( \_n( $name2[0], $name2[1], $count2 ), $count2 );
			}
		}

		return $output;
	}
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339

	/**
	 * Delete an event by ID
	 */
	private function delete_event_by_id( $args, $assoc_args ) {
		$jid = absint( $assoc_args['event_id'] );

		// Validate ID
		if ( ! $jid ) {
			\WP_CLI::error( __( 'Invalid event ID', 'automattic-cron-control' ) );
		}

		\WP_CLI::line( __( 'Locating event...', 'automattic-cron-control' ) . "\n" );

		// Look up full object and confirm that the entry belongs to this plugin's CPT
		global $wpdb;

		$event_post = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_type = %s AND ID = %d LIMIT 1", \Automattic\WP\Cron_Control\Cron_Options_CPT::POST_TYPE, $jid ) );

		if ( is_object( $event_post ) && ! is_wp_error( $event_post ) ) {
Erick Hitter's avatar
Erick Hitter committed
340
			// Parse basic event data and output, lest someone delete the wrong thing
341
			$event_details = $this->get_event_details_from_post_title( $event_post->post_title );
342

343
344
345
346
347
			// Warning about Internal Events
			if ( \Automattic\WP\Cron_Control\is_internal_event( $event_details['action'] ) ) {
				\WP_CLI::warning( __( 'This is an event created by the Cron Control plugin. It will recreated automatically.', 'automattic-cron-control' ) );
			}

348
349
350
			\WP_CLI::line( sprintf( __( 'Execution time: %s GMT', 'automattic-cron-control' ), date( TIME_FORMAT, $event_details['timestamp'] ) ) );
			\WP_CLI::line( sprintf( __( 'Action: %s', 'automattic-cron-control' ), $event_details['action'] ) );
			\WP_CLI::line( sprintf( __( 'Instance identifier: %s', 'automattic-cron-control' ), $event_details['instance'] ) );
351
352
353
			\WP_CLI::line( '' );
			\WP_CLI::confirm( sprintf( __( 'Are you sure you want to delete this event?', 'automattic-cron-control' ) ) );

Erick Hitter's avatar
Erick Hitter committed
354
			// Try to delete the item and provide some relevant output
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
			$trashed = wp_delete_post( $event_post->ID, true );

			if ( false === $trashed ) {
				\WP_CLI::error( sprintf( __( 'Failed to delete event %d', 'automattic-cron-control' ), $jid ) );
			} else {
				\Automattic\WP\Cron_Control\_flush_internal_caches();
				\WP_CLI::success( sprintf( __( 'Removed event %d', 'automattic-cron-control' ), $jid ) );
				return;
			}
		}

		\WP_CLI::error( sprintf( __( 'Failed to delete event %d. Please confirm that the entry exists and that the ID is that of an event.', 'automattic-cron-control' ), $jid ) );
	}

	/**
	 * Delete all events of the same action
	 */
	private function delete_event_by_action( $args, $assoc_args ) {
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
		$action = $assoc_args['action'];

		// Validate entry
		if ( empty( $action ) ) {
			\WP_CLI::error( __( 'Invalid action', 'automattic-cron-control' ) );
		}

		// Warning about Internal Events
		if ( \Automattic\WP\Cron_Control\is_internal_event( $action ) ) {
			\WP_CLI::warning( __( 'This is an event created by the Cron Control plugin. It will recreated automatically.', 'automattic-cron-control' ) );
		}

		// Set defaults needed to gather all events
		$assoc_args['page']  = 1;
		$assoc_args['limit'] = 50;

		// Gather events
		\WP_CLI::line( __( 'Gathering events...', 'automattic-cron-control' ) );

		$events_to_delete = array();

		$events = $this->get_events( $args, $assoc_args );

		\WP_CLI::line( sprintf( _n( 'Found one event to check', 'Found %s events to check', $events['total_items'], 'automattic-cron-control' ), number_format_i18n( $events['total_items'] ) ) );

		$search_progress = \WP_CLI\Utils\make_progress_bar( sprintf( __( 'Searching events for those with the action `%s`', 'automattic-cron-control' ), $action ), $events['total_items'] );

		// Loop and pull out events to be deleted
		do {
			if ( ! is_array( $events ) || empty( $events['items'] ) ) {
				break;
			}

			// Check events for those that should be deleted
			foreach ( $events['items'] as $single_event ) {
				$event_details = $this->get_event_details_from_post_title( $single_event->post_title );

				if ( $event_details['action'] === $action ) {
					$events_to_delete[] = array_merge( $event_details, array(
						'ID'                => (int) $single_event->ID,
						'post_date_gmt'     => $single_event->post_date_gmt,
						'post_modified_gmt' => $single_event->post_modified_gmt,
					) );
				}

				$search_progress->tick();
			}

			// Proceed to next batch
			$assoc_args['page']++;

			if ( $assoc_args['page'] > $events['total_pages'] ) {
				break;
			}

			$events = $this->get_events( $args, $assoc_args );
		} while( $events['page'] <= $events['total_pages'] );

		$search_progress->finish();

		\WP_CLI::line( '' );

		// Nothing more to do
		if ( empty( $events_to_delete ) ) {
			\WP_CLI::error( sprintf( __( 'No events with action `%s` found', 'automattic-cron-control' ), $action ) );
		}

		// List the items to remove
		$total_to_delete = count( $events_to_delete );

		\WP_CLI::line( sprintf( _n( 'Found one event with action `%2$s`:', 'Found %1$s events with action `%2$s`:', $total_to_delete, 'automattic-cron-control' ), number_format_i18n( $total_to_delete ), $action ) );

		if ( $total_to_delete <= $assoc_args['limit'] ) {
			\WP_CLI\Utils\format_items( 'table', $events_to_delete, array(
				'ID',
				'post_date_gmt',
				'post_modified_gmt',
				'timestamp',
				'instance',
			) );
		} else {
			\WP_CLI::warning( sprintf( __( 'Events are not displayed as there are more than %s to remove', 'automattic-cron-control' ), number_format_i18n( $assoc_args['limit'] ) ) );
		}

		\WP_CLI::line( '' );
		\WP_CLI::confirm( _n( 'Are you sure you want to delete this event?', 'Are you sure you want to delete these events?', $total_to_delete, 'automattic-cron-control' ) );

		// Remove the items
		$delete_progress = \WP_CLI\Utils\make_progress_bar( __( 'Deleting events', 'automattic-cron-control' ), $total_to_delete );

		$events_deleted       = array();
		$events_deleted_count = $events_failed_delete = 0;

		foreach ( $events_to_delete as $event_to_delete ) {
			$deleted = wp_delete_post( $event_to_delete['ID'], true );

			$events_deleted[] = array(
				'ID'      => $event_to_delete['ID'],
				'deleted' => false === $deleted ? 'no' : 'yes',
			);

			if ( $deleted ) {
				$events_deleted_count++;
			} else {
				$events_failed_delete++;
			}

			$delete_progress->tick();
		}

		$delete_progress->finish();

		// When deletes succeed, sync internal caches
		if ( $events_deleted_count > 0 ) {
			\Automattic\WP\Cron_Control\_flush_internal_caches();
		}

		// List the removed items
		\WP_CLI::line( "\n" . __( 'RESULTS:', 'automattic-cron-control' ) );

		if ( 1 === $total_to_delete && 1 === $events_deleted_count ) {
			\WP_CLI::success( sprintf( __( 'Deleted one event: %d', 'automattic-cron-control' ), $events_deleted[0]['ID'] ) );
		} else {
			if ( $events_deleted_count === $total_to_delete ) {
				\WP_CLI::success( sprintf( __( 'Deleted %s events', 'automattic-cron-control' ), number_format_i18n( $events_deleted_count ) ) );
			} else {
				\WP_CLI::warning( sprintf( __( 'Expected to delete %1$s events, but could only delete %2$s events. It\'s likely that some events were executed while this command ran.', 'automattic-cron-control' ), number_format_i18n( $total_to_delete ), number_format_i18n( $events_deleted_count ) ) );
			}

			// Limit just to failed deletes when many events are removed
			if ( count( $events_deleted ) > $assoc_args['limit'] ) {
				$events_deleted_unfiltered = $events_deleted;

				$events_deleted = array_filter( $events_deleted, function( $event ) {
					if ( 'no' === $event['deleted'] ) {
						return $event;
					} else {
						return false;
					}
				} );

				if ( count( $events_deleted ) > 0 ) {
					\WP_CLI::line( "\n" . __( 'Events that couldn\'t be deleted:', 'automattic-cron-control' ) );
				}
			} else {
				\WP_CLI::line( "\n" . __( 'Events deleted:', 'automattic-cron-control' ) );
			}

			// Don't display a table if there's nothing to display
			if ( count( $events_deleted ) > 0 ) {
				\WP_CLI\Utils\format_items( 'table', $events_deleted, array(
					'ID',
					'deleted',
				) );
			}
		}
529
530
531

		return;
	}
532
533
534
535
536
537
538
539
540

	/**
	 * Parse event details stored in an item's post_title
	 */
	private function get_event_details_from_post_title( $title ) {
		$event_details = explode( '|', $title );
		$event_details = array_map( 'trim', $event_details );

		return array(
541
			'timestamp' => (int) $event_details[0],
542
543
544
545
			'action'    => $event_details[1],
			'instance'  => $event_details[2],
		);
	}
546
547
}

548
\WP_CLI::add_command( 'cron-control events', 'Automattic\WP\Cron_Control\CLI\Events' );