| 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 create |
| 9 |
*/ |
| 10 |
class Create_Command extends \ActionScheduler_WPCLI_Command { |
| 11 |
|
| 12 |
const ASYNC_OPTS = array( 'async', 0 ); |
| 13 |
|
| 14 |
/** |
| 15 |
* Execute command. |
| 16 |
* |
| 17 |
* @return void |
| 18 |
*/ |
| 19 |
public function execute() { |
| 20 |
$hook = $this->args[0]; |
| 21 |
$schedule_start = $this->args[1]; |
| 22 |
$callback_args = get_flag_value( $this->assoc_args, 'args', array() ); |
| 23 |
$group = get_flag_value( $this->assoc_args, 'group', '' ); |
| 24 |
$interval = absint( get_flag_value( $this->assoc_args, 'interval', 0 ) ); |
| 25 |
$cron = get_flag_value( $this->assoc_args, 'cron', '' ); |
| 26 |
$unique = get_flag_value( $this->assoc_args, 'unique', false ); |
| 27 |
$priority = absint( get_flag_value( $this->assoc_args, 'priority', 10 ) ); |
| 28 |
|
| 29 |
if ( ! empty( $callback_args ) ) { |
| 30 |
$callback_args = json_decode( $callback_args, true ); |
| 31 |
} |
| 32 |
|
| 33 |
$function_args = array( |
| 34 |
'start' => $schedule_start, |
| 35 |
'cron' => $cron, |
| 36 |
'interval' => $interval, |
| 37 |
'hook' => $hook, |
| 38 |
'callback_args' => $callback_args, |
| 39 |
'group' => $group, |
| 40 |
'unique' => $unique, |
| 41 |
'priority' => $priority, |
| 42 |
); |
| 43 |
|
| 44 |
try { |
| 45 |
// Generate schedule start if appropriate. |
| 46 |
if ( ! in_array( $schedule_start, static::ASYNC_OPTS, true ) ) { |
| 47 |
$schedule_start = as_get_datetime_object( $schedule_start ); |
| 48 |
$function_args['start'] = $schedule_start->format( 'U' ); |
| 49 |
} |
| 50 |
} catch ( \Exception $e ) { |
| 51 |
\WP_CLI::error( $e->getMessage() ); |
| 52 |
} |
| 53 |
|
| 54 |
// Default to creating single action. |
| 55 |
$action_type = 'single'; |
| 56 |
$function = 'as_schedule_single_action'; |
| 57 |
|
| 58 |
if ( ! empty( $interval ) ) { // Creating recurring action. |
| 59 |
$action_type = 'recurring'; |
| 60 |
$function = 'as_schedule_recurring_action'; |
| 61 |
|
| 62 |
$function_args = array_filter( |
| 63 |
$function_args, |
| 64 |
static function( $key ) { |
| 65 |
return in_array( $key, array( 'start', 'interval', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); |
| 66 |
}, |
| 67 |
ARRAY_FILTER_USE_KEY |
| 68 |
); |
| 69 |
} elseif ( ! empty( $cron ) ) { // Creating cron action. |
| 70 |
$action_type = 'cron'; |
| 71 |
$function = 'as_schedule_cron_action'; |
| 72 |
|
| 73 |
$function_args = array_filter( |
| 74 |
$function_args, |
| 75 |
static function( $key ) { |
| 76 |
return in_array( $key, array( 'start', 'cron', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); |
| 77 |
}, |
| 78 |
ARRAY_FILTER_USE_KEY |
| 79 |
); |
| 80 |
} elseif ( in_array( $function_args['start'], static::ASYNC_OPTS, true ) ) { // Enqueue async action. |
| 81 |
$action_type = 'async'; |
| 82 |
$function = 'as_enqueue_async_action'; |
| 83 |
|
| 84 |
$function_args = array_filter( |
| 85 |
$function_args, |
| 86 |
static function( $key ) { |
| 87 |
return in_array( $key, array( 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); |
| 88 |
}, |
| 89 |
ARRAY_FILTER_USE_KEY |
| 90 |
); |
| 91 |
} else { // Enqueue single action. |
| 92 |
$function_args = array_filter( |
| 93 |
$function_args, |
| 94 |
static function( $key ) { |
| 95 |
return in_array( $key, array( 'start', 'hook', 'callback_args', 'group', 'unique', 'priority' ), true ); |
| 96 |
}, |
| 97 |
ARRAY_FILTER_USE_KEY |
| 98 |
); |
| 99 |
} |
| 100 |
|
| 101 |
$function_args = array_values( $function_args ); |
| 102 |
|
| 103 |
try { |
| 104 |
$action_id = call_user_func_array( $function, $function_args ); |
| 105 |
} catch ( \Exception $e ) { |
| 106 |
$this->print_error( $e ); |
| 107 |
} |
| 108 |
|
| 109 |
if ( 0 === $action_id ) { |
| 110 |
$e = new \Exception( __( 'Unable to create a scheduled action.', 'action-scheduler' ) ); |
| 111 |
$this->print_error( $e ); |
| 112 |
} |
| 113 |
|
| 114 |
$this->print_success( $action_id, $action_type ); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Print a success message with the action ID. |
| 119 |
* |
| 120 |
* @param int $action_id Created action ID. |
| 121 |
* @param string $action_type Type of action. |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
protected function print_success( $action_id, $action_type ) { |
| 126 |
\WP_CLI::success( |
| 127 |
sprintf( |
| 128 |
/* translators: %1$s: type of action, %2$d: ID of the created action */ |
| 129 |
__( '%1$s action (%2$d) scheduled.', 'action-scheduler' ), |
| 130 |
ucfirst( $action_type ), |
| 131 |
$action_id |
| 132 |
) |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Convert an exception into a WP CLI error. |
| 138 |
* |
| 139 |
* @param \Exception $e The error object. |
| 140 |
* @throws \WP_CLI\ExitException When an error occurs. |
| 141 |
* @return void |
| 142 |
*/ |
| 143 |
protected function print_error( \Exception $e ) { |
| 144 |
\WP_CLI::error( |
| 145 |
sprintf( |
| 146 |
/* translators: %s refers to the exception error message. */ |
| 147 |
__( 'There was an error creating the scheduled action: %s', 'action-scheduler' ), |
| 148 |
$e->getMessage() |
| 149 |
) |
| 150 |
); |
| 151 |
} |
| 152 |
|
| 153 |
} |
| 154 |
|