PluginProbe
Hostinger Tools / trunk
Hostinger Tools vtrunk
3.0.78 3.0.77 3.0.76 3.0.75 3.0.74 3.0.73 3.0.72 3.0.71 3.0.70 3.0.69 3.0.68 3.0.67 3.0.66 1.8.1 1.8.2 1.8.3 1.9.1 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 All 109 releases
hostinger / vendor / woocommerce / action-scheduler / classes / WP_CLI / Action / Cancel_Command.php

Cancel_Command.php in Hostinger Tools trunk, at vendor/woocommerce/action-scheduler/classes/WP_CLI/Action/Cancel_Command.php

121 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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