class-orchestrate.php 3.21 KB
Newer Older
1
<?php
Erick Hitter's avatar
Erick Hitter committed
2
3
4
5
6
/**
 * Enable and disable automatic event execution via WP-CLI
 *
 * @package a8c_Cron_Control
 */
7
8
9
10
11
12
13
14

namespace Automattic\WP\Cron_Control\CLI;

/**
 * Commands to manage automatic event execution
 */
class Orchestrate extends \WP_CLI_Command {
	/**
15
16
17
	 * Check the status of automatic event execution
	 *
	 * @subcommand check-status
Erick Hitter's avatar
Erick Hitter committed
18
19
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
20
21
	 */
	public function get_automatic_execution_status( $args, $assoc_args ) {
22
		$status = \Automattic\WP\Cron_Control\Events::instance()->run_disabled();
23
24
25
26
27
28
29
30
31
32
33

		switch ( $status ) {
			case 0 :
				$status = __( 'Automatic execution is enabled', 'automattic-cron-control' );
				break;

			case 1 :
				$status = __( 'Automatic execution is disabled indefinitely', 'automattic-cron-control' );
				break;

			default :
Erick Hitter's avatar
Erick Hitter committed
34
				$status = sprintf( __( 'Automatic execution is disabled for %1$s (until %2$s UTC)', 'automattic-cron-control' ), human_time_diff( $status ), date_i18n( TIME_FORMAT, $status ) );
35
36
37
38
39
40
41
42
				break;
		}

		\WP_CLI::log( $status );
	}

	/**
	 * Change status of automatic event execution
43
44
45
46
47
	 *
	 * When using the Go-based runner, it may be necessary to stop execution for a period, or indefinitely
	 *
	 * @subcommand manage-automatic-execution
	 * @synopsis [--enable] [--disable] [--disable_until=<disable_until>]
Erick Hitter's avatar
Erick Hitter committed
48
49
	 * @param array $args Array of positional arguments.
	 * @param array $assoc_args Array of flags.
50
	 */
51
	public function manage_automatic_execution( $args, $assoc_args ) {
Erick Hitter's avatar
Erick Hitter committed
52
		// Update execution status.
53
54
55
56
		$disable_ts = \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable_until', 0 );
		$disable_ts = absint( $disable_ts );

		if ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'enable', false ) ) {
57
58
59
60
61
62
63
64
			$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( 0 );

			if ( $updated ) {
				\WP_CLI::success( __( 'Enabled', 'automattic-cron-control' ) );
				return;
			}

			\WP_CLI::error( __( 'Could not enable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
65
		} elseif ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable', false ) ) {
66
67
68
69
70
71
72
73
			$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( 1 );

			if ( $updated ) {
				\WP_CLI::success( __( 'Disabled', 'automattic-cron-control' ) );
				return;
			}

			\WP_CLI::error( __( 'Could not disable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
Erick Hitter's avatar
Erick Hitter committed
74
		} elseif ( $disable_ts > 0 ) {
75
			if ( $disable_ts > time() ) {
76
77
78
				$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( $disable_ts );

				if ( $updated ) {
79
					\WP_CLI::success( sprintf( __( 'Disabled for %1$s (until %2$s UTC)', 'automattic-cron-control' ), human_time_diff( $disable_ts ), date_i18n( TIME_FORMAT, $disable_ts ) ) );
80
81
82
83
					return;
				}

				\WP_CLI::error( __( 'Could not disable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
84
			} else {
85
				\WP_CLI::error( __( 'Timestamp is in the past.', 'automattic-cron-control' ) );
86
87
88
			}
		}

89
		\WP_CLI::error( __( 'Please provide a valid action.', 'automattic-cron-control' ) );
90
91
92
93
	}
}

\WP_CLI::add_command( 'cron-control orchestrate', 'Automattic\WP\Cron_Control\CLI\Orchestrate' );