PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
imagify / inc / Dependencies / ActionScheduler / classes / WP_CLI / ActionScheduler_WPCLI_Scheduler_command.php

ActionScheduler_WPCLI_Scheduler_command.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF trunk, at inc/Dependencies/ActionScheduler/classes/WP_CLI/ActionScheduler_WPCLI_Scheduler_command.php

203 lines 6.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Commands for Action Scheduler.
5 */
6 class ActionScheduler_WPCLI_Scheduler_command extends WP_CLI_Command {
7
8 /**
9 * Force tables schema creation for Action Scheduler
10 *
11 * ## OPTIONS
12 *
13 * @param array $args Positional arguments.
14 * @param array $assoc_args Keyed arguments.
15 *
16 * @subcommand fix-schema
17 */
18 public function fix_schema( $args, $assoc_args ) {
19 $schema_classes = array( ActionScheduler_LoggerSchema::class, ActionScheduler_StoreSchema::class );
20
21 foreach ( $schema_classes as $classname ) {
22 if ( is_subclass_of( $classname, ActionScheduler_Abstract_Schema::class ) ) {
23 $obj = new $classname();
24 $obj->init();
25 $obj->register_tables( true );
26
27 WP_CLI::success(
28 sprintf(
29 /* translators: %s refers to the schema name*/
30 __( 'Registered schema for %s', 'action-scheduler' ),
31 $classname
32 )
33 );
34 }
35 }
36 }
37
38 /**
39 * Run the Action Scheduler
40 *
41 * ## OPTIONS
42 *
43 * [--batch-size=<size>]
44 * : The maximum number of actions to run. Defaults to 100.
45 *
46 * [--batches=<size>]
47 * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue being executed until all actions are complete.
48 *
49 * [--cleanup-batch-size=<size>]
50 * : The maximum number of actions to clean up. Defaults to the value of --batch-size.
51 *
52 * [--hooks=<hooks>]
53 * : Only run actions with the specified hook. Omitting this option runs actions with any hook. Define multiple hooks as a comma separated string (without spaces), e.g. `--hooks=hook_one,hook_two,hook_three`
54 *
55 * [--group=<group>]
56 * : Only run actions from the specified group. Omitting this option runs actions from all groups.
57 *
58 * [--exclude-groups=<groups>]
59 * : Run actions from all groups except the specified group(s). Define multiple groups as a comma separated string (without spaces), e.g. '--group_a,group_b'. This option is ignored when `--group` is used.
60 *
61 * [--free-memory-on=<count>]
62 * : The number of actions to process between freeing memory. 0 disables freeing memory. Default 50.
63 *
64 * [--pause=<seconds>]
65 * : The number of seconds to pause when freeing memory. Default no pause.
66 *
67 * [--force]
68 * : Whether to force execution despite the maximum number of concurrent processes being exceeded.
69 *
70 * @param array $args Positional arguments.
71 * @param array $assoc_args Keyed arguments.
72 * @throws \WP_CLI\ExitException When an error occurs.
73 *
74 * @subcommand run
75 */
76 public function run( $args, $assoc_args ) {
77 // Handle passed arguments.
78 $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 100 ) );
79 $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) );
80 $clean = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'cleanup-batch-size', $batch ) );
81 $hooks = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'hooks', '' ) );
82 $hooks = array_filter( array_map( 'trim', $hooks ) );
83 $group = \WP_CLI\Utils\get_flag_value( $assoc_args, 'group', '' );
84 $exclude_groups = \WP_CLI\Utils\get_flag_value( $assoc_args, 'exclude-groups', '' );
85 $free_on = \WP_CLI\Utils\get_flag_value( $assoc_args, 'free-memory-on', 50 );
86 $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 );
87 $force = \WP_CLI\Utils\get_flag_value( $assoc_args, 'force', false );
88
89 ActionScheduler_DataController::set_free_ticks( $free_on );
90 ActionScheduler_DataController::set_sleep_time( $sleep );
91
92 $batches_completed = 0;
93 $actions_completed = 0;
94 $unlimited = 0 === $batches;
95 if ( is_callable( array( ActionScheduler::store(), 'set_claim_filter' ) ) ) {
96 $exclude_groups = $this->parse_comma_separated_string( $exclude_groups );
97
98 if ( ! empty( $exclude_groups ) ) {
99 ActionScheduler::store()->set_claim_filter( 'exclude-groups', $exclude_groups );
100 }
101 }
102
103 try {
104 // Custom queue cleaner instance.
105 $cleaner = new ActionScheduler_QueueCleaner( null, $clean );
106
107 // Get the queue runner instance.
108 $runner = new ActionScheduler_WPCLI_QueueRunner( null, null, $cleaner );
109
110 // Determine how many tasks will be run in the first batch.
111 $total = $runner->setup( $batch, $hooks, $group, $force );
112
113 // Run actions for as long as possible.
114 while ( $total > 0 ) {
115 $this->print_total_actions( $total );
116 $actions_completed += $runner->run();
117 $batches_completed++;
118
119 // Maybe set up tasks for the next batch.
120 $total = ( $unlimited || $batches_completed < $batches ) ? $runner->setup( $batch, $hooks, $group, $force ) : 0;
121 }
122 } catch ( Exception $e ) {
123 $this->print_error( $e );
124 }
125
126 $this->print_total_batches( $batches_completed );
127 $this->print_success( $actions_completed );
128 }
129
130 /**
131 * Converts a string of comma-separated values into an array of those same values.
132 *
133 * @param string $string The string of one or more comma separated values.
134 *
135 * @return array
136 */
137 private function parse_comma_separated_string( $string ): array {
138 return array_filter( str_getcsv( $string ) );
139 }
140
141 /**
142 * Print WP CLI message about how many actions are about to be processed.
143 *
144 * @param int $total Number of actions found.
145 */
146 protected function print_total_actions( $total ) {
147 WP_CLI::log(
148 sprintf(
149 /* translators: %d refers to how many scheduled tasks were found to run */
150 _n( 'Found %d scheduled task', 'Found %d scheduled tasks', $total, 'action-scheduler' ),
151 $total
152 )
153 );
154 }
155
156 /**
157 * Print WP CLI message about how many batches of actions were processed.
158 *
159 * @param int $batches_completed Number of completed batches.
160 */
161 protected function print_total_batches( $batches_completed ) {
162 WP_CLI::log(
163 sprintf(
164 /* translators: %d refers to the total number of batches executed */
165 _n( '%d batch executed.', '%d batches executed.', $batches_completed, 'action-scheduler' ),
166 $batches_completed
167 )
168 );
169 }
170
171 /**
172 * Convert an exception into a WP CLI error.
173 *
174 * @param Exception $e The error object.
175 *
176 * @throws \WP_CLI\ExitException Under some conditions WP CLI may throw an exception.
177 */
178 protected function print_error( Exception $e ) {
179 WP_CLI::error(
180 sprintf(
181 /* translators: %s refers to the exception error message */
182 __( 'There was an error running the action scheduler: %s', 'action-scheduler' ),
183 $e->getMessage()
184 )
185 );
186 }
187
188 /**
189 * Print a success message with the number of completed actions.
190 *
191 * @param int $actions_completed Number of completed actions.
192 */
193 protected function print_success( $actions_completed ) {
194 WP_CLI::success(
195 sprintf(
196 /* translators: %d refers to the total number of tasks completed */
197 _n( '%d scheduled task completed.', '%d scheduled tasks completed.', $actions_completed, 'action-scheduler' ),
198 $actions_completed
199 )
200 );
201 }
202 }
203