| 1 |
<?php |
| 2 |
|
| 3 |
namespace Action_Scheduler\WP_CLI; |
| 4 |
|
| 5 |
/** |
| 6 |
* WP_CLI progress bar for Action Scheduler. |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Class ProgressBar |
| 11 |
* |
| 12 |
* @package Action_Scheduler\WP_CLI |
| 13 |
* |
| 14 |
* @since 3.0.0 |
| 15 |
* |
| 16 |
* @codeCoverageIgnore |
| 17 |
*/ |
| 18 |
class ProgressBar { |
| 19 |
|
| 20 |
/** |
| 21 |
* Current number of ticks. |
| 22 |
* |
| 23 |
* @var integer |
| 24 |
*/ |
| 25 |
protected $total_ticks; |
| 26 |
|
| 27 |
/** |
| 28 |
* Total number of ticks. |
| 29 |
* |
| 30 |
* @var integer |
| 31 |
*/ |
| 32 |
protected $count; |
| 33 |
|
| 34 |
/** |
| 35 |
* Progress bar update interval. |
| 36 |
* |
| 37 |
* @var integer |
| 38 |
*/ |
| 39 |
protected $interval; |
| 40 |
|
| 41 |
/** |
| 42 |
* Progress bar message. |
| 43 |
* |
| 44 |
* @var string |
| 45 |
*/ |
| 46 |
protected $message; |
| 47 |
|
| 48 |
/** |
| 49 |
* Instance. |
| 50 |
* |
| 51 |
* @var \cli\progress\Bar |
| 52 |
*/ |
| 53 |
protected $progress_bar; |
| 54 |
|
| 55 |
/** |
| 56 |
* ProgressBar constructor. |
| 57 |
* |
| 58 |
* @param string $message Text to display before the progress bar. |
| 59 |
* @param integer $count Total number of ticks to be performed. |
| 60 |
* @param integer $interval Optional. The interval in milliseconds between updates. Default 100. |
| 61 |
* |
| 62 |
* @throws \Exception When this is not run within WP CLI. |
| 63 |
*/ |
| 64 |
public function __construct( $message, $count, $interval = 100 ) { |
| 65 |
if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 66 |
/* translators: %s php class name */ |
| 67 |
throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
| 68 |
} |
| 69 |
|
| 70 |
$this->total_ticks = 0; |
| 71 |
$this->message = $message; |
| 72 |
$this->count = $count; |
| 73 |
$this->interval = $interval; |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Increment the progress bar ticks. |
| 78 |
*/ |
| 79 |
public function tick() { |
| 80 |
if ( null === $this->progress_bar ) { |
| 81 |
$this->setup_progress_bar(); |
| 82 |
} |
| 83 |
|
| 84 |
$this->progress_bar->tick(); |
| 85 |
$this->total_ticks++; |
| 86 |
|
| 87 |
do_action( 'action_scheduler/progress_tick', $this->total_ticks ); // phpcs:ignore WordPress.NamingConventions.ValidHookName.UseUnderscores |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Get the progress bar tick count. |
| 92 |
* |
| 93 |
* @return int |
| 94 |
*/ |
| 95 |
public function current() { |
| 96 |
return $this->progress_bar ? $this->progress_bar->current() : 0; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Finish the current progress bar. |
| 101 |
*/ |
| 102 |
public function finish() { |
| 103 |
if ( null !== $this->progress_bar ) { |
| 104 |
$this->progress_bar->finish(); |
| 105 |
} |
| 106 |
|
| 107 |
$this->progress_bar = null; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Set the message used when creating the progress bar. |
| 112 |
* |
| 113 |
* @param string $message The message to be used when the next progress bar is created. |
| 114 |
*/ |
| 115 |
public function set_message( $message ) { |
| 116 |
$this->message = $message; |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Set the count for a new progress bar. |
| 121 |
* |
| 122 |
* @param integer $count The total number of ticks expected to complete. |
| 123 |
*/ |
| 124 |
public function set_count( $count ) { |
| 125 |
$this->count = $count; |
| 126 |
$this->finish(); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Set up the progress bar. |
| 131 |
*/ |
| 132 |
protected function setup_progress_bar() { |
| 133 |
$this->progress_bar = \WP_CLI\Utils\make_progress_bar( |
| 134 |
$this->message, |
| 135 |
$this->count, |
| 136 |
$this->interval |
| 137 |
); |
| 138 |
} |
| 139 |
} |
| 140 |
|