hostinger
/
vendor
/
woocommerce
/
action-scheduler
/
classes
/
WP_CLI
/
Action
/
Delete_Command.php
Delete_Command.php in Hostinger Tools 3.0.78, at vendor/woocommerce/action-scheduler/classes/WP_CLI/Action/Delete_Command.php
| 1 | <?php |
| 2 | |
| 3 | namespace Action_Scheduler\WP_CLI\Action; |
| 4 | |
| 5 | /** |
| 6 | * WP-CLI command: action-scheduler action delete |
| 7 | */ |
| 8 | class Delete_Command extends \ActionScheduler_WPCLI_Command { |
| 9 | |
| 10 | /** |
| 11 | * Array of action IDs to delete. |
| 12 | * |
| 13 | * @var int[] |
| 14 | */ |
| 15 | protected $action_ids = array(); |
| 16 | |
| 17 | /** |
| 18 | * Number of deleted, failed, and total actions deleted. |
| 19 | * |
| 20 | * @var array<string, int> |
| 21 | */ |
| 22 | protected $action_counts = array( |
| 23 | 'deleted' => 0, |
| 24 | 'failed' => 0, |
| 25 | 'total' => 0, |
| 26 | ); |
| 27 | |
| 28 | /** |
| 29 | * Construct. |
| 30 | * |
| 31 | * @param string[] $args Positional arguments. |
| 32 | * @param array<string, string> $assoc_args Keyed arguments. |
| 33 | */ |
| 34 | public function __construct( array $args, array $assoc_args ) { |
| 35 | parent::__construct( $args, $assoc_args ); |
| 36 | |
| 37 | $this->action_ids = array_map( 'absint', $args ); |
| 38 | $this->action_counts['total'] = count( $this->action_ids ); |
| 39 | |
| 40 | add_action( 'action_scheduler_deleted_action', array( $this, 'on_action_deleted' ) ); |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Execute. |
| 45 | * |
| 46 | * @return void |
| 47 | */ |
| 48 | public function execute() { |
| 49 | $store = \ActionScheduler::store(); |
| 50 | |
| 51 | $progress_bar = \WP_CLI\Utils\make_progress_bar( |
| 52 | sprintf( |
| 53 | /* translators: %d: number of actions to be deleted */ |
| 54 | _n( 'Deleting %d action', 'Deleting %d actions', $this->action_counts['total'], 'action-scheduler' ), |
| 55 | number_format_i18n( $this->action_counts['total'] ) |
| 56 | ), |
| 57 | $this->action_counts['total'] |
| 58 | ); |
| 59 | |
| 60 | foreach ( $this->action_ids as $action_id ) { |
| 61 | try { |
| 62 | $store->delete_action( $action_id ); |
| 63 | } catch ( \Exception $e ) { |
| 64 | $this->action_counts['failed']++; |
| 65 | \WP_CLI::warning( $e->getMessage() ); |
| 66 | } |
| 67 | |
| 68 | $progress_bar->tick(); |
| 69 | } |
| 70 | |
| 71 | $progress_bar->finish(); |
| 72 | |
| 73 | /* translators: %1$d: number of actions deleted */ |
| 74 | $format = _n( 'Deleted %1$d action', 'Deleted %1$d actions', $this->action_counts['deleted'], 'action-scheduler' ) . ', '; |
| 75 | /* translators: %2$d: number of actions deletions failed */ |
| 76 | $format .= _n( '%2$d failure.', '%2$d failures.', $this->action_counts['failed'], 'action-scheduler' ); |
| 77 | |
| 78 | \WP_CLI::success( |
| 79 | sprintf( |
| 80 | $format, |
| 81 | number_format_i18n( $this->action_counts['deleted'] ), |
| 82 | number_format_i18n( $this->action_counts['failed'] ) |
| 83 | ) |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Action: action_scheduler_deleted_action |
| 89 | * |
| 90 | * @param int $action_id Action ID. |
| 91 | * @return void |
| 92 | */ |
| 93 | public function on_action_deleted( $action_id ) { |
| 94 | if ( 'action_scheduler_deleted_action' !== current_action() ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $action_id = absint( $action_id ); |
| 99 | |
| 100 | if ( ! in_array( $action_id, $this->action_ids, true ) ) { |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | $this->action_counts['deleted']++; |
| 105 | \WP_CLI::debug( sprintf( 'Action %d was deleted.', $action_id ) ); |
| 106 | } |
| 107 | |
| 108 | } |
| 109 |