class-rest-api.php 3.01 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php

namespace Automattic\WP\Cron_Control\CLI;

/**
 * Make requests to Cron Control's REST API
 */
class REST_API extends \WP_CLI_Command {
	/**
	 * Retrieve the current event queue
	 *
	 * @subcommand get-queue
	 */
	public function get_queue( $args, $assoc_args ) {
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
		// Build and make request
		$queue_request = new \WP_REST_Request( 'POST', '/' . \Automattic\WP\Cron_Control\REST_API::API_NAMESPACE . '/' . \Automattic\WP\Cron_Control\REST_API::ENDPOINT_LIST );
		$queue_request->add_header( 'Content-Type', 'application/json' );
		$queue_request->set_body( wp_json_encode( array(
			'secret' => \WP_CRON_CONTROL_SECRET,
		) ) );

		$queue_request = rest_do_request( $queue_request );

		// Oh well
		if ( $queue_request->is_error() ) {
			\WP_CLI::error( $queue_request->as_error()->get_error_message() );
		}

		// Get the decoded JSON object returned by the API
		$queue_response = $queue_request->get_data();

		// No events, nothing more to do
		if ( empty( $queue_response['events'] ) ) {
			\WP_CLI::warning( __( 'No events in the current queue', 'automattic-cron-control' ) );
			return;
		}

		// Prepare items for display
		$events_for_display      = $this->format_events( $queue_response['events'] );
		$total_events_to_display = count( $events_for_display );
41
		\WP_CLI::log( sprintf( _n( 'Displaying one event', 'Displaying %s events', $total_events_to_display, 'automattic-cron-control' ), number_format_i18n( $total_events_to_display ) ) );
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

		// And reformat
		$format = 'table';
		if ( isset( $assoc_args['format'] ) ) {
			if ( 'ids' === $assoc_args['format'] ) {
				\WP_CLI::error( __( 'Invalid output format requested', 'automattic-cron-control' ) );
			} else {
				$format = $assoc_args[ 'format' ];
			}
		}

		\WP_CLI\Utils\format_items( $format, $events_for_display, array(
			'timestamp',
			'action',
			'instance',
			'scheduled_for',
			'internal_event',
			'schedule_name',
			'event_args',
		) );
	}

	/**
	 * Format event data into something human-readable
	 *
	 * @param $events
	 *
	 * @return array
	 */
	private function format_events( $events ) {
		$formatted_events = array();

		foreach ( $events as $event ) {
75
			$event_data = \Automattic\WP\Cron_Control\get_event_by_attributes( array(
Erick Hitter's avatar
Erick Hitter committed
76
77
78
				'timestamp'     => $event['timestamp'],
				'action_hashed' => $event['action'],
				'instance'      => $event['instance'],
79
			) );
80
81

			$formatted_events[] = array(
82
83
84
				'timestamp'      => $event_data->timestamp,
				'action'         => $event_data->action,
				'instance'       => $event_data->instance,
85
				'scheduled_for'  => date_i18n( TIME_FORMAT, $event_data->timestamp ),
86
87
88
				'internal_event' => \Automattic\WP\Cron_Control\is_internal_event( $event_data->action ) ? __( 'true', 'automattic-cron-control' ) : '',
				'schedule_name'  => false === $event_data->schedule ? __( 'n/a', 'automattic-cron-control' ) : $event_data->schedule,
				'event_args'     => maybe_serialize( $event_data->args ),
89
90
91
92
			);
		}

		return $formatted_events;
93
94
95
96
	}
}

\WP_CLI::add_command( 'cron-control rest-api', 'Automattic\WP\Cron_Control\CLI\REST_API' );