PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.6.5
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.6.5
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / WP_CLI / Action / Create_Command.php

Create_Command.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.6.5, at vendor/woocommerce/action-scheduler/classes/WP_CLI/Action/Create_Command.php

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