class-orchestrate.php 2.83 KB
Newer Older
1
2
3
4
5
6
7
8
9
<?php

namespace Automattic\WP\Cron_Control\CLI;

/**
 * Commands to manage automatic event execution
 */
class Orchestrate extends \WP_CLI_Command {
	/**
10
11
12
13
14
	 * Check the status of automatic event execution
	 *
	 * @subcommand check-status
	 */
	public function get_automatic_execution_status( $args, $assoc_args ) {
15
		$status = \Automattic\WP\Cron_Control\Events::instance()->run_disabled();
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

		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 :
				$status = sprintf( __( 'Automatic execution is disabled until %s', 'automattic-cron-control' ), date_i18n( 'Y-m-d H:i:s T', $status ) );
				break;
		}

		\WP_CLI::log( $status );
	}

	/**
	 * Change status of automatic event execution
36
37
38
39
40
41
	 *
	 * 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>]
	 */
42
	public function manage_automatic_execution( $args, $assoc_args ) {
43
44
45
46
47
		// Update execution status
		$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 ) ) {
48
49
50
51
52
53
54
55
			$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' ) );
56
		} elseif ( \WP_CLI\Utils\get_flag_value( $assoc_args, 'disable', false ) ) {
57
58
59
60
61
62
63
64
			$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' ) );
65
66
		} elseif( $disable_ts > 0 ) {
			if ( $disable_ts > time() ) {
67
68
69
70
71
72
73
74
				$updated = \Automattic\WP\Cron_Control\Events::instance()->update_run_status( $disable_ts );

				if ( $updated ) {
					\WP_CLI::success( sprintf( __( 'Disabled until %s', 'automattic-cron-control' ), date_i18n( 'Y-m-d H:i:s T', $disable_ts ) ) );
					return;
				}

				\WP_CLI::error( __( 'Could not disable automatic execution. Please check the current status.', 'automattic-cron-control' ) );
75
			} else {
76
				\WP_CLI::error( __( 'Timestamp is in the past.', 'automattic-cron-control' ) );
77
78
79
			}
		}

80
		\WP_CLI::error( __( 'Please provide a valid action.', 'automattic-cron-control' ) );
81
82
83
84
	}
}

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