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2.0-rc.1 3.0.9 6.2.0-rc.2 3.1.0 6.2.1 3.1.1 6.2.2 3.1.2 6.2.3 3.2.0 6.3.0 3.2.1 6.3.0-beta.1 3.2.2 6.3.0-rc.1 3.2.3 6.3.0-rc.2 3.2.4 6.3.1 3.2.5 6.3.2 3.2.6 6.4.0 3.3.0 6.4.0-beta.1 3.3.1 6.4.0-rc.1 3.3.2 6.4.1 3.3.2-rc.1 6.4.2 3.3.3 6.5.0 3.3.4 6.5.0-beta.1 3.3.5 6.5.0-rc.1 3.3.6 6.5.0-rc.2 3.4.0 6.5.1 3.4.0-beta.1 6.5.2 3.4.0-rc.2 6.6.0 3.4.1 6.6.0-beta.1 3.4.2 6.6.0-rc.1 3.4.3 6.6.0-rc.2 3.4.4 6.6.1 3.4.5 6.6.2 3.4.6 6.7.0 3.4.7 6.7.0-beta.1 3.4.8 6.7.0-beta.2 3.5.0 6.7.0-rc.1 3.5.0-beta.1 6.7.1 3.5.0-rc.1 6.8.0 3.5.0-rc.2 6.8.0-beta.1 3.5.1 6.8.0-beta.2 3.5.10 6.8.0-rc.1 3.5.2 6.8.1 3.5.3 6.8.2 3.5.4 6.8.3 3.5.5 6.9.0 3.5.6 6.9.0-beta.1 3.5.7 6.9.0-beta.2 3.5.8 6.9.0-rc.1 3.5.9 6.9.1 3.6.0 6.9.2 3.6.0-beta.1 6.9.3 3.6.0-rc.1 6.9.4 3.6.0-rc.2 6.9.5 3.6.0-rc.3 7.0.0 3.6.1 7.0.0-beta.1 3.6.2 7.0.0-beta.2 3.6.3 7.0.0-beta.3 3.6.4 7.0.0-rc.1 3.6.5 7.0.0-rc.2 3.6.6 7.0.1 3.6.7 7.0.2 3.7.0 7.1.0 3.7.0-beta.1 7.1.0-beta.1 3.7.0-rc.1 7.1.0-beta.2 3.7.0-rc.2 7.1.0-rc.1 3.7.1 7.1.0-rc.2 3.7.2 7.1.1
woocommerce / src / Internal / ProductAttributesLookup / CLIRunner.php
woocommerce / src / Internal / ProductAttributesLookup Last commit date
CLIRunner.php 4 days ago DataRegenerator.php 4 days ago Filterer.php 4 days ago LookupDataStore.php 4 days ago TaxQuery.php 1 month ago
CLIRunner.php
517 lines
1 <?php
2
3 namespace Automattic\WooCommerce\Internal\ProductAttributesLookup;
4
5 use Automattic\WooCommerce\Enums\ProductStatus;
6 use Automattic\WooCommerce\Internal\Utilities\ProductUtil;
7 use WP_CLI;
8
9 /**
10 * Command line tools to handle the regeneration of the product attributes lookup table.
11 */
12 class CLIRunner {
13
14 /**
15 * The instance of DataRegenerator to use.
16 *
17 * @var DataRegenerator
18 */
19 private DataRegenerator $data_regenerator;
20
21 /**
22 * The instance of DataRegenerator to use.
23 *
24 * @var LookupDataStore
25 */
26 private LookupDataStore $lookup_data_store;
27
28 /**
29 * Creates a new instance of the class.
30 *
31 * Normally we define a public 'init' method with the class dependencies passed as arguments
32 * and then the DI container executes it, but if we do that a dummy command will be created
33 * for that method. Therefore, in this case we retrieve the dependencies manually instead.
34 */
35 public function __construct() {
36 $container = wc_get_container();
37 $this->data_regenerator = $container->get( DataRegenerator::class );
38 $this->lookup_data_store = $container->get( LookupDataStore::class );
39 }
40
41 // phpcs:disable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
42
43 /**
44 * Enable the usage of the product attributes lookup table.
45 *
46 * @param array $args Positional arguments passed to the command.
47 * @param array $assoc_args Associative arguments (options) passed to the command.
48 */
49 public function enable( array $args = array(), array $assoc_args = array() ) {
50 return $this->invoke( 'enable_core', $args, $assoc_args );
51 }
52
53 /**
54 * Core method for the "enable" command.
55 *
56 * @param array $args Positional arguments passed to the command.
57 * @param array $assoc_args Associative arguments (options) passed to the command.
58 */
59 private function enable_core( array $args, array $assoc_args ) {
60 $table_name = $this->lookup_data_store->get_lookup_table_name();
61 if ( 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' ) ) {
62 $this->warning( "The usage of the of the %W{$table_name}%n table is already enabled." );
63 return;
64 }
65
66 if ( ! array_key_exists( 'force', $assoc_args ) ) {
67 $must_confirm = true;
68 if ( $this->lookup_data_store->regeneration_is_in_progress() ) {
69 $this->warning( "The regeneration of the %W{$table_name}%n table is currently in process." );
70 } elseif ( $this->lookup_data_store->regeneration_was_aborted() ) {
71 $this->warning( "The regeneration of the %W{$table_name}%n table was aborted." );
72 } elseif ( 0 === $this->get_lookup_table_info()['total_rows'] ) {
73 $this->warning( "The %W{$table_name}%n table is empty." );
74 } else {
75 $must_confirm = false;
76 }
77
78 if ( $must_confirm ) {
79 WP_CLI::confirm( 'Are you sure that you want to enable the table usage?' );
80 }
81 }
82
83 update_option( 'woocommerce_attribute_lookup_enabled', 'yes' );
84 $table_name = $this->lookup_data_store->get_lookup_table_name();
85 $this->success( "The usage of the %W{$table_name}%n table for product attribute lookup has been enabled." );
86 }
87
88 /**
89 * Disable the usage of the product attributes lookup table.
90 *
91 * @param array $args Positional arguments passed to the command.
92 * @param array $assoc_args Associative arguments (options) passed to the command.
93 */
94 public function disable( array $args = array(), array $assoc_args = array() ) {
95 return $this->invoke( 'disable_core', $args, $assoc_args );
96 }
97
98 /**
99 * Core method for the "disable" command.
100 *
101 * @param array $args Positional arguments passed to the command.
102 * @param array $assoc_args Associative arguments (options) passed to the command.
103 */
104 private function disable_core( array $args, array $assoc_args ) {
105 if ( 'yes' !== get_option( 'woocommerce_attribute_lookup_enabled' ) ) {
106 $table_name = $this->lookup_data_store->get_lookup_table_name();
107 $this->warning( "The usage of the of the %W{$table_name}%n table is already disabled." );
108 return;
109 }
110 update_option( 'woocommerce_attribute_lookup_enabled', 'no' );
111 $table_name = $this->lookup_data_store->get_lookup_table_name();
112 $this->success( "The usage of the %W{$table_name}%n table for product attribute lookup has been disabled." );
113 }
114
115 /**
116 * Regenerate the product attributes lookup table data for one single product.
117 *
118 * ## OPTIONS
119 *
120 * <product-id>
121 * : The id of the product for which the data will be regenerated.
122 *
123 * [--disable-db-optimization]
124 * : Don't use optimized database access even if products are stored as custom post types.
125 *
126 * ## EXAMPLES
127 *
128 * wp wc palt regenerate_for_product 34 --disable-db-optimization
129 *
130 * @param array $args Positional arguments passed to the command.
131 * @param array $assoc_args Associative arguments (options) passed to the command.
132 */
133 public function regenerate_for_product( array $args = array(), array $assoc_args = array() ) {
134 return $this->invoke( 'regenerate_for_product_core', $args, $assoc_args );
135 }
136
137 /**
138 * Core method for the "regenerate_for_product" command.
139 *
140 * @param array $args Positional arguments passed to the command.
141 * @param array $assoc_args Associative arguments (options) passed to the command.
142 */
143 private function regenerate_for_product_core( array $args = array(), array $assoc_args = array() ) {
144 $product_id = current( $args );
145 $this->data_regenerator->check_can_do_lookup_table_regeneration( $product_id );
146 $use_db_optimization = ! array_key_exists( 'disable-db-optimization', $assoc_args );
147 $this->check_can_use_db_optimization( $use_db_optimization );
148 $start_time = microtime( true );
149 $this->lookup_data_store->create_data_for_product( $product_id, $use_db_optimization );
150
151 if ( $this->lookup_data_store->get_last_create_operation_failed() ) {
152 $this->error( "Lookup data regeneration failed.\nSee the WooCommerce logs (source is %9palt-updates%n) for details." );
153 } else {
154 $total_time = microtime( true ) - $start_time;
155 WP_CLI::success( sprintf( 'Attributes lookup data for product %d regenerated in %f seconds.', $product_id, $total_time ) );
156 }
157 }
158
159 /**
160 * If database access optimization is requested but can't be used, show a warning.
161 *
162 * @param bool $use_db_optimization True if database access optimization is requested.
163 */
164 private function check_can_use_db_optimization( bool $use_db_optimization ) {
165 if ( $use_db_optimization && ! $this->lookup_data_store->can_use_optimized_db_access() ) {
166 $this->warning( "Optimized database access can't be used (products aren't stored as custom post types)." );
167 }
168 }
169
170 /**
171 * Obtain information about the product attributes lookup table.
172 *
173 * @param array $args Positional arguments passed to the command.
174 * @param array $assoc_args Associative arguments (options) passed to the command.
175 */
176 public function info( array $args = array(), array $assoc_args = array() ) {
177 return $this->invoke( 'info_core', $args, $assoc_args );
178 }
179
180 /**
181 * Core method for the "info" command.
182 *
183 * @param array $args Positional arguments passed to the command.
184 * @param array $assoc_args Associative arguments (options) passed to the command.
185 */
186 private function info_core( array $args, array $assoc_args ) {
187 global $wpdb;
188
189 $enabled = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
190
191 $table_name = $this->lookup_data_store->get_lookup_table_name();
192 $info = $this->get_lookup_table_info();
193
194 $this->log( "Table name: %W{$table_name}%n" );
195 $this->log( 'Table usage is ' . ( $enabled ? '%Genabled%n' : '%Ydisabled%n' ) );
196 $this->log( "The table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
197
198 if ( $info['total_rows'] > 0 ) {
199 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
200 $highest_product_id_in_table = $wpdb->get_var( 'select max(product_or_parent_id) from ' . $table_name );
201 $this->log( "The highest product id in the table is %B{$highest_product_id_in_table}%n." );
202 }
203
204 if ( $this->lookup_data_store->regeneration_is_in_progress() ) {
205 $max_product_id_to_process = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process', '???' );
206 WP_CLI::log( '' );
207 $this->warning( 'Full regeneration of the table is currently %Gin progress.%n' );
208 if ( ! $this->data_regenerator->has_scheduled_action_for_regeneration_step() ) {
209 $this->log( 'However, there are %9NO%n actions scheduled to run the regeneration steps (a %9wp cli palt regenerate%n command was aborted?).' );
210 }
211 $this->log( "The last product id that will be processed is %Y{$max_product_id_to_process}%n." );
212 $this->log( "\nRun %9wp cli palt abort_regeneration%n to abort the regeneration process," );
213 $this->log( "then you'll be able to run %9wp cli palt resume_regeneration%n to resume the regeneration process," );
214 } elseif ( $this->lookup_data_store->regeneration_was_aborted() ) {
215 $max_product_id_to_process = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process', '???' );
216 WP_CLI::log( '' );
217 $this->warning( "Full regeneration of the table has been %Raborted.%n\nThe last product id that will be processed is %Y{$max_product_id_to_process}%n." );
218 $this->log( "\nRun %9wp cli palt resume_regeneration%n to resume the regeneration process." );
219 }
220 }
221
222 /**
223 * Abort the background regeneration of the product attributes lookup table that is happening in the background.
224 *
225 * [--cleanup]
226 * : Also cleanup temporary data (so regeneration can't be resumed, but it can be restarted).
227 *
228 * ## EXAMPLES
229 *
230 * wp wc palt abort_regeneration --cleanup
231 *
232 * @param array $args Positional arguments passed to the command.
233 * @param array $assoc_args Associative arguments (options) passed to the command.
234 */
235 public function abort_regeneration( array $args = array(), array $assoc_args = array() ) {
236 return $this->invoke( 'abort_regeneration_core', $args, $assoc_args );
237 }
238
239 /**
240 * Core method for the "abort_regeneration" command.
241 *
242 * @param array $args Positional arguments passed to the command.
243 * @param array $assoc_args Associative arguments (options) passed to the command.
244 */
245 private function abort_regeneration_core( array $args, array $assoc_args ) {
246 $this->data_regenerator->abort_regeneration( false );
247 $table_name = $this->lookup_data_store->get_lookup_table_name();
248 $this->success( "The regeneration of the data in the %W{$table_name}%n table has been aborted." );
249 if ( array_key_exists( 'cleanup', $assoc_args ) ) {
250 $this->cleanup_regeneration_progress( array(), array() );
251 }
252 }
253
254 /**
255 * Resume the background regeneration of the product attributes lookup table after it has been aborted.
256 *
257 * @param array $args Positional arguments passed to the command.
258 * @param array $assoc_args Associative arguments (options) passed to the command.
259 */
260 public function resume_regeneration( array $args = array(), array $assoc_args = array() ) {
261 return $this->invoke( 'resume_regeneration_core', $args, $assoc_args );
262 }
263
264 /**
265 * Core method for the "resume_regeneration" command.
266 *
267 * @param array $args Positional arguments passed to the command.
268 * @param array $assoc_args Associative arguments (options) passed to the command.
269 */
270 private function resume_regeneration_core( array $args, array $assoc_args ) {
271 $this->data_regenerator->resume_regeneration( false );
272 $table_name = $this->lookup_data_store->get_lookup_table_name();
273 $this->success( "The regeneration of the data in the %W{$table_name}%n table has been resumed." );
274 }
275
276 /**
277 * Delete the temporary data used during the regeneration of the product attributes lookup table. This data is normally deleted automatically after the regeneration process finishes.
278 *
279 * @param array $args Positional arguments passed to the command.
280 * @param array $assoc_args Associative arguments (options) passed to the command.
281 */
282 public function cleanup_regeneration_progress( array $args = array(), array $assoc_args = array() ) {
283 return $this->invoke( 'cleanup_regeneration_progress_core', $args, $assoc_args );
284 }
285
286 /**
287 * Core method for the "cleanup_regeneration_progress" command.
288 *
289 * @param array $args Positional arguments passed to the command.
290 * @param array $assoc_args Associative arguments (options) passed to the command.
291 */
292 private function cleanup_regeneration_progress_core( array $args, array $assoc_args ) {
293 $this->data_regenerator->finalize_regeneration( false );
294 $table_name = $this->lookup_data_store->get_lookup_table_name();
295 $this->success( "The temporary data used for regeneration of the data in the %W{$table_name}%n table has been deleted." );
296 }
297
298 /**
299 * Initiate the background regeneration of the product attributes lookup table. The regeneration will happen in the background, using scheduled actions.
300 *
301 * ## OPTIONS
302 *
303 * [--force]
304 * : Don't prompt for confirmation if the product attributes lookup table isn't empty.
305 *
306 * ## EXAMPLES
307 *
308 * wp wc palt initiate_regeneration --force
309 *
310 * @param array $args Positional arguments passed to the command.
311 * @param array $assoc_args Associative arguments (options) passed to the command.
312 */
313 public function initiate_regeneration( array $args = array(), array $assoc_args = array() ) {
314 return $this->invoke( 'initiate_regeneration_core', $args, $assoc_args );
315 }
316
317 /**
318 * Core method for the "initiate_regeneration" command.
319 *
320 * @param array $args Positional arguments passed to the command.
321 * @param array $assoc_args Associative arguments (options) passed to the command.
322 */
323 private function initiate_regeneration_core( array $args, array $assoc_args ) {
324 $this->data_regenerator->check_can_do_lookup_table_regeneration();
325 $info = $this->get_lookup_table_info();
326 if ( $info['total_rows'] > 0 && ! array_key_exists( 'force', $assoc_args ) ) {
327 $table_name = $this->lookup_data_store->get_lookup_table_name();
328 $this->warning( "The %W{$table_name}%n table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
329 WP_CLI::confirm( 'Initiating the regeneration will first delete the data. Are you sure?' );
330 }
331
332 $this->data_regenerator->initiate_regeneration();
333 $table_name = $this->lookup_data_store->get_lookup_table_name();
334 $this->log( "%GSuccess:%n The regeneration of the data in the %W{$table_name}%n table has been initiated." );
335 }
336
337 /**
338 * Regenerate the product attributes lookup table immediately, without using scheduled tasks.
339 *
340 * ## OPTIONS
341 *
342 * [--force]
343 * : Don't prompt for confirmation if the product attributes lookup table isn't empty.
344 *
345 * [--from-scratch]
346 * : Start table regeneration from scratch even if a regeneration is already in progress.
347 *
348 * [--disable-db-optimization]
349 * : Don't use optimized database access even if products are stored as custom post types.
350 *
351 * [--batch-size=<size>]
352 * : How many products to process in each iteration of the loop.
353 * ---
354 * default: 10
355 * ---
356 *
357 * ## EXAMPLES
358 *
359 * wp wc palt regenerate --force --from-scratch --batch-size=20
360 *
361 * @param array $args Positional arguments passed to the command.
362 * @param array $assoc_args Associative arguments (options) passed to the command.
363 */
364 public function regenerate( array $args = array(), array $assoc_args = array() ) {
365 return $this->invoke( 'regenerate_core', $args, $assoc_args );
366 }
367
368 /**
369 * Core method for the "regenerate" command.
370 *
371 * @param array $args Positional arguments passed to the command.
372 * @param array $assoc_args Associative arguments (options) passed to the command.
373 * @throws \Exception Invalid batch size argument.
374 */
375 private function regenerate_core( array $args = array(), array $assoc_args = array() ) {
376 global $wpdb;
377
378 $table_name = $this->lookup_data_store->get_lookup_table_name();
379
380 $batch_size = $assoc_args['batch-size'] ?? DataRegenerator::PRODUCTS_PER_GENERATION_STEP;
381 if ( ! is_numeric( $batch_size ) || $batch_size < 1 ) {
382 throw new \Exception( 'batch_size must be a number bigger than 0' );
383 }
384
385 $was_enabled = 'yes' === get_option( 'woocommerce_attribute_lookup_enabled' );
386
387 $products_count = wc_get_container()->get( ProductUtil::class )->get_counts_for_type( 'product' );
388 $products_count = ( $products_count[ ProductStatus::PUBLISH ] ?? 0 ) + ( $products_count[ ProductStatus::PENDING ] ?? 0 ) + ( $products_count[ ProductStatus::DRAFT ] ?? 0 );
389
390 if ( ! $this->lookup_data_store->regeneration_is_in_progress() || array_key_exists( 'from-scratch', $assoc_args ) ) {
391 $info = $this->get_lookup_table_info();
392 if ( $info['total_rows'] > 0 && ! array_key_exists( 'force', $assoc_args ) ) {
393 $this->warning( "The %W{$table_name}%n table contains %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
394 WP_CLI::confirm( 'Triggering the regeneration will first delete the data. Are you sure?' );
395 }
396
397 $this->data_regenerator->finalize_regeneration( false );
398 $last_product_id = $this->data_regenerator->initiate_regeneration( false );
399 if ( 0 === $last_product_id ) {
400 $this->data_regenerator->finalize_regeneration( $was_enabled );
401 WP_CLI::log( 'No products exist in the database, the table is left empty.' );
402 return;
403 }
404 $processed_count = 0;
405 } else {
406 $last_product_id = get_option( 'woocommerce_attribute_lookup_last_product_id_to_process' );
407 if ( false === $last_product_id ) {
408 WP_CLI::error( 'Regeneration seems to be already in progress, but the woocommerce_attribute_lookup_last_product_id_to_process option isn\'t there. Try %9wp cli palt cleanup_regeneration_progress%n first." );' );
409 return 1;
410 }
411 $processed_count = get_option( 'woocommerce_attribute_lookup_processed_count', 0 );
412 $this->log( "Resuming regeneration, %C{$processed_count}%n products have been processed already" );
413 $this->lookup_data_store->set_regeneration_in_progress_flag();
414 }
415
416 $this->data_regenerator->cancel_regeneration_scheduled_action();
417
418 $use_db_optimization = ! array_key_exists( 'disable-db-optimization', $assoc_args );
419 $this->check_can_use_db_optimization( $use_db_optimization );
420 $progress = WP_CLI\Utils\make_progress_bar( '', $products_count );
421 $this->log( "Regenerating %W{$table_name}%n..." );
422 $progress->tick( $processed_count );
423
424 $regeneration_step_failed = false;
425 while ( $this->data_regenerator->do_regeneration_step( $batch_size, $use_db_optimization ) ) {
426 $progress->tick( $batch_size );
427 $regeneration_step_failed = $regeneration_step_failed || $this->data_regenerator->get_last_regeneration_step_failed();
428 }
429
430 $this->data_regenerator->finalize_regeneration( $was_enabled );
431 $time = $progress->formatTime( $progress->elapsed() );
432 $progress->finish();
433
434 if ( $regeneration_step_failed ) {
435 $this->warning( "Lookup data regeneration failed for at least one product.\nSee the WooCommerce logs (source is %9palt-updates%n) for details.\n" );
436 $this->log( "Table %W{$table_name}%n regenerated in {$time}." );
437 } else {
438 $this->log( "%GSuccess:%n Table %W{$table_name}%n regenerated in {$time}." );
439 }
440
441 $info = $this->get_lookup_table_info();
442 $this->log( "The table contains now %C{$info['total_rows']}%n rows corresponding to %G{$info['products_count']}%n products." );
443 }
444
445 // phpcs:enable Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed
446
447 /**
448 * Get information about the product attributes lookup table.
449 *
450 * @return array Array containing the 'total_rows' and 'products_count' keys.
451 */
452 private function get_lookup_table_info(): array {
453 global $wpdb;
454
455 $table_name = $this->lookup_data_store->get_lookup_table_name();
456 // phpcs:ignore WordPress.DB.PreparedSQL.NotPrepared
457 $info = $wpdb->get_row( 'select count(1), count(distinct(product_or_parent_id)) from ' . $table_name, ARRAY_N );
458 return array(
459 'total_rows' => absint( $info[0] ),
460 'products_count' => absint( $info[1] ),
461 );
462 }
463
464 /**
465 * Invoke a method from the class, and if an exception is thrown, show it using WP_CLI::error.
466 *
467 * @param string $method_name Name of the method to invoke.
468 * @param array $args Positional arguments to pass to the method.
469 * @param array $assoc_args Associative arguments to pass to the method.
470 * @return mixed Result from the method, or 1 if an exception is thrown.
471 */
472 private function invoke( string $method_name, array $args, array $assoc_args ) {
473 try {
474 return call_user_func( array( $this, $method_name ), $args, $assoc_args );
475 } catch ( \Exception $e ) {
476 WP_CLI::error( $e->getMessage() );
477 return 1;
478 }
479 }
480
481 /**
482 * Show a log message using the WP_CLI text colorization feature.
483 *
484 * @param string $text Text to show.
485 */
486 private function log( string $text ) {
487 WP_CLI::log( WP_CLI::colorize( $text ) );
488 }
489
490 /**
491 * Show a warning message using the WP_CLI text colorization feature.
492 *
493 * @param string $text Text to show.
494 */
495 private function warning( string $text ) {
496 WP_CLI::warning( WP_CLI::colorize( $text ) );
497 }
498
499 /**
500 * Show a success message using the WP_CLI text colorization feature.
501 *
502 * @param string $text Text to show.
503 */
504 private function success( string $text ) {
505 WP_CLI::success( WP_CLI::colorize( $text ) );
506 }
507
508 /**
509 * Show an error message using the WP_CLI text colorization feature.
510 *
511 * @param string $text Text to show.
512 */
513 private function error( string $text ) {
514 WP_CLI::error( WP_CLI::colorize( $text ) );
515 }
516 }
517