hostinger-reach
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
WP_CLI
/
Action
/
Cancel_Command.php
hostinger-reach
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
WP_CLI
/
Action
Last commit date
Cancel_Command.php
10 months ago
Create_Command.php
10 months ago
Delete_Command.php
10 months ago
Generate_Command.php
10 months ago
Get_Command.php
10 months ago
List_Command.php
10 months ago
Next_Command.php
10 months ago
Run_Command.php
10 months ago
Cancel_Command.php
121 lines
| 1 | <?php |
| 2 | |
| 3 | namespace Action_Scheduler\WP_CLI\Action; |
| 4 | |
| 5 | use function \WP_CLI\Utils\get_flag_value; |
| 6 | |
| 7 | /** |
| 8 | * WP-CLI command: action-scheduler action cancel |
| 9 | */ |
| 10 | class Cancel_Command extends \ActionScheduler_WPCLI_Command { |
| 11 | |
| 12 | /** |
| 13 | * Execute command. |
| 14 | * |
| 15 | * @return void |
| 16 | */ |
| 17 | public function execute() { |
| 18 | $hook = ''; |
| 19 | $group = get_flag_value( $this->assoc_args, 'group', '' ); |
| 20 | $callback_args = get_flag_value( $this->assoc_args, 'args', null ); |
| 21 | $all = get_flag_value( $this->assoc_args, 'all', false ); |
| 22 | |
| 23 | if ( ! empty( $this->args[0] ) ) { |
| 24 | $hook = $this->args[0]; |
| 25 | } |
| 26 | |
| 27 | if ( ! empty( $callback_args ) ) { |
| 28 | $callback_args = json_decode( $callback_args, true ); |
| 29 | } |
| 30 | |
| 31 | if ( $all ) { |
| 32 | $this->cancel_all( $hook, $callback_args, $group ); |
| 33 | return; |
| 34 | } |
| 35 | |
| 36 | $this->cancel_single( $hook, $callback_args, $group ); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Cancel single action. |
| 41 | * |
| 42 | * @param string $hook The hook that the job will trigger. |
| 43 | * @param array $callback_args Args that would have been passed to the job. |
| 44 | * @param string $group The group the job is assigned to. |
| 45 | * @return void |
| 46 | */ |
| 47 | protected function cancel_single( $hook, $callback_args, $group ) { |
| 48 | if ( empty( $hook ) ) { |
| 49 | \WP_CLI::error( __( 'Please specify hook of action to cancel.', 'action-scheduler' ) ); |
| 50 | } |
| 51 | |
| 52 | try { |
| 53 | $result = as_unschedule_action( $hook, $callback_args, $group ); |
| 54 | } catch ( \Exception $e ) { |
| 55 | $this->print_error( $e, false ); |
| 56 | } |
| 57 | |
| 58 | if ( null === $result ) { |
| 59 | $e = new \Exception( __( 'Unable to cancel scheduled action: check the logs.', 'action-scheduler' ) ); |
| 60 | $this->print_error( $e, false ); |
| 61 | } |
| 62 | |
| 63 | $this->print_success( false ); |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Cancel all actions. |
| 68 | * |
| 69 | * @param string $hook The hook that the job will trigger. |
| 70 | * @param array $callback_args Args that would have been passed to the job. |
| 71 | * @param string $group The group the job is assigned to. |
| 72 | * @return void |
| 73 | */ |
| 74 | protected function cancel_all( $hook, $callback_args, $group ) { |
| 75 | if ( empty( $hook ) && empty( $group ) ) { |
| 76 | \WP_CLI::error( __( 'Please specify hook and/or group of actions to cancel.', 'action-scheduler' ) ); |
| 77 | } |
| 78 | |
| 79 | try { |
| 80 | $result = as_unschedule_all_actions( $hook, $callback_args, $group ); |
| 81 | } catch ( \Exception $e ) { |
| 82 | $this->print_error( $e, $multiple ); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Because as_unschedule_all_actions() does not provide a result, |
| 87 | * neither confirm or deny actions cancelled. |
| 88 | */ |
| 89 | \WP_CLI::success( __( 'Request to cancel scheduled actions completed.', 'action-scheduler' ) ); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Print a success message. |
| 94 | * |
| 95 | * @return void |
| 96 | */ |
| 97 | protected function print_success() { |
| 98 | \WP_CLI::success( __( 'Scheduled action cancelled.', 'action-scheduler' ) ); |
| 99 | } |
| 100 | |
| 101 | /** |
| 102 | * Convert an exception into a WP CLI error. |
| 103 | * |
| 104 | * @param \Exception $e The error object. |
| 105 | * @param bool $multiple Boolean if multiple actions. |
| 106 | * @throws \WP_CLI\ExitException When an error occurs. |
| 107 | * @return void |
| 108 | */ |
| 109 | protected function print_error( \Exception $e, $multiple ) { |
| 110 | \WP_CLI::error( |
| 111 | sprintf( |
| 112 | /* translators: %1$s: singular or plural %2$s: refers to the exception error message. */ |
| 113 | __( 'There was an error cancelling the %1$s: %2$s', 'action-scheduler' ), |
| 114 | $multiple ? __( 'scheduled actions', 'action-scheduler' ) : __( 'scheduled action', 'action-scheduler' ), |
| 115 | $e->getMessage() |
| 116 | ) |
| 117 | ); |
| 118 | } |
| 119 | |
| 120 | } |
| 121 |