imagify
/
inc
/
Dependencies
/
ActionScheduler
/
classes
/
WP_CLI
/
ActionScheduler_WPCLI_Clean_Command.php
ActionScheduler_WPCLI_Clean_Command.php in Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF 2.2.9, at inc/Dependencies/ActionScheduler/classes/WP_CLI/ActionScheduler_WPCLI_Clean_Command.php
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Commands for Action Scheduler. |
| 5 | */ |
| 6 | class ActionScheduler_WPCLI_Clean_Command extends WP_CLI_Command { |
| 7 | /** |
| 8 | * Run the Action Scheduler Queue Cleaner |
| 9 | * |
| 10 | * ## OPTIONS |
| 11 | * |
| 12 | * [--batch-size=<size>] |
| 13 | * : The maximum number of actions to delete per batch. Defaults to 20. |
| 14 | * |
| 15 | * [--batches=<size>] |
| 16 | * : Limit execution to a number of batches. Defaults to 0, meaning batches will continue all eligible actions are deleted. |
| 17 | * |
| 18 | * [--status=<status>] |
| 19 | * : Only clean actions with the specified status. Defaults to Canceled, Completed. Define multiple statuses as a comma separated string (without spaces), e.g. `--status=complete,failed,canceled` |
| 20 | * |
| 21 | * [--before=<datestring>] |
| 22 | * : Only delete actions with scheduled date older than this. Defaults to 31 days. e.g `--before='7 days ago'`, `--before='02-Feb-2020 20:20:20'` |
| 23 | * |
| 24 | * [--pause=<seconds>] |
| 25 | * : The number of seconds to pause between batches. Default no pause. |
| 26 | * |
| 27 | * @param array $args Positional arguments. |
| 28 | * @param array $assoc_args Keyed arguments. |
| 29 | * @throws \WP_CLI\ExitException When an error occurs. |
| 30 | * |
| 31 | * @subcommand clean |
| 32 | */ |
| 33 | public function clean( $args, $assoc_args ) { |
| 34 | // Handle passed arguments. |
| 35 | $batch = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batch-size', 20 ) ); |
| 36 | $batches = absint( \WP_CLI\Utils\get_flag_value( $assoc_args, 'batches', 0 ) ); |
| 37 | $status = explode( ',', WP_CLI\Utils\get_flag_value( $assoc_args, 'status', '' ) ); |
| 38 | $status = array_filter( array_map( 'trim', $status ) ); |
| 39 | $before = \WP_CLI\Utils\get_flag_value( $assoc_args, 'before', '' ); |
| 40 | $sleep = \WP_CLI\Utils\get_flag_value( $assoc_args, 'pause', 0 ); |
| 41 | |
| 42 | $batches_completed = 0; |
| 43 | $actions_deleted = 0; |
| 44 | $unlimited = 0 === $batches; |
| 45 | try { |
| 46 | $lifespan = as_get_datetime_object( $before ); |
| 47 | } catch ( Exception $e ) { |
| 48 | $lifespan = null; |
| 49 | } |
| 50 | |
| 51 | try { |
| 52 | // Custom queue cleaner instance. |
| 53 | $cleaner = new ActionScheduler_QueueCleaner( null, $batch ); |
| 54 | |
| 55 | // Clean actions for as long as possible. |
| 56 | while ( $unlimited || $batches_completed < $batches ) { |
| 57 | if ( $sleep && $batches_completed > 0 ) { |
| 58 | sleep( $sleep ); |
| 59 | } |
| 60 | |
| 61 | $deleted = count( $cleaner->clean_actions( $status, $lifespan, null, 'CLI' ) ); |
| 62 | if ( $deleted <= 0 ) { |
| 63 | break; |
| 64 | } |
| 65 | $actions_deleted += $deleted; |
| 66 | $batches_completed++; |
| 67 | $this->print_success( $deleted ); |
| 68 | } |
| 69 | } catch ( Exception $e ) { |
| 70 | $this->print_error( $e ); |
| 71 | } |
| 72 | |
| 73 | $this->print_total_batches( $batches_completed ); |
| 74 | if ( $batches_completed > 1 ) { |
| 75 | $this->print_success( $actions_deleted ); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * Print WP CLI message about how many batches of actions were processed. |
| 81 | * |
| 82 | * @param int $batches_processed Number of batches processed. |
| 83 | */ |
| 84 | protected function print_total_batches( int $batches_processed ) { |
| 85 | WP_CLI::log( |
| 86 | sprintf( |
| 87 | /* translators: %d refers to the total number of batches processed */ |
| 88 | _n( '%d batch processed.', '%d batches processed.', $batches_processed, 'action-scheduler' ), |
| 89 | $batches_processed |
| 90 | ) |
| 91 | ); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Convert an exception into a WP CLI error. |
| 96 | * |
| 97 | * @param Exception $e The error object. |
| 98 | */ |
| 99 | protected function print_error( Exception $e ) { |
| 100 | WP_CLI::error( |
| 101 | sprintf( |
| 102 | /* translators: %s refers to the exception error message */ |
| 103 | __( 'There was an error deleting an action: %s', 'action-scheduler' ), |
| 104 | $e->getMessage() |
| 105 | ) |
| 106 | ); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Print a success message with the number of completed actions. |
| 111 | * |
| 112 | * @param int $actions_deleted Number of deleted actions. |
| 113 | */ |
| 114 | protected function print_success( int $actions_deleted ) { |
| 115 | WP_CLI::success( |
| 116 | sprintf( |
| 117 | /* translators: %d refers to the total number of actions deleted */ |
| 118 | _n( '%d action deleted.', '%d actions deleted.', $actions_deleted, 'action-scheduler' ), |
| 119 | $actions_deleted |
| 120 | ) |
| 121 | ); |
| 122 | } |
| 123 | } |
| 124 |