class-events.php 7.86 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
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
	}

	/**
	 * 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
		$items = $wpdb->get_results( $wpdb->prepare( "SELECT SQL_CALC_FOUND_ROWS ID, 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 ) );

		// 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
117
		// Include totals for pagination etc
118
		$total_items = (int) $wpdb->get_var( 'SELECT FOUND_ROWS()' );
Erick Hitter's avatar
Erick Hitter committed
119
		$total_pages = ceil( $total_items / $limit );
120

Erick Hitter's avatar
Erick Hitter committed
121
		return compact( 'status', 'limit', 'page', 'offset', 'items', 'total_items', 'total_pages' );
122
123
124
	}

	/**
125
	 * Format event data into something human-readable
126
	 */
127
128
129
130
131
132
	private function format_events( $events ) {
		$formatted_events = array();

		// Reformat events
		foreach ( $events as $event ) {
			$row = array(
Erick Hitter's avatar
Erick Hitter committed
133
				'ID'                => $event->ID,
134
135
				'action'            => '',
				'instance'          => '',
Erick Hitter's avatar
Erick Hitter committed
136
				'next_run_gmt'      => date( TIME_FORMAT, strtotime( $event->post_date_gmt ) ),
Erick Hitter's avatar
Erick Hitter committed
137
				'next_run_relative' => $this->calculate_interval( strtotime( $event->post_date_gmt ) - time() ),
Erick Hitter's avatar
Erick Hitter committed
138
				'last_updated_gmt'  => date( TIME_FORMAT, strtotime( $event->post_modified_gmt ) ),
139
				'recurrence'        => __( 'Non-repeating', 'automattic-cron-control' ),
140
				'internal_event'    => '',
141
				'schedule_name'     => __( 'n/a', 'automattic-cron-control' ),
142
143
144
				'event_args'        => '',
			);

Erick Hitter's avatar
Erick Hitter committed
145
146
147
148
149
			// 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'] ) ) {
150
151
					$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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
				}

				// 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'] );
					}

168
					// Human-readable version of next run
Erick Hitter's avatar
Erick Hitter committed
169
170
171
					if ( isset( $args['interval'] ) && $args['interval'] ) {
						$row['recurrence'] = $this->calculate_interval( $args['interval'] );
					}
172
173
174
175
176

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

180
181
182
183
184
			$formatted_events[] = $row;
		}

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

	/**
	 * 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;
	}
246
247
}

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