| 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 |
/** @var integer */ |
| 21 |
protected $total_ticks; |
| 22 |
|
| 23 |
/** @var integer */ |
| 24 |
protected $count; |
| 25 |
|
| 26 |
/** @var integer */ |
| 27 |
protected $interval; |
| 28 |
|
| 29 |
/** @var string */ |
| 30 |
protected $message; |
| 31 |
|
| 32 |
/** @var \cli\progress\Bar */ |
| 33 |
protected $progress_bar; |
| 34 |
|
| 35 |
/** |
| 36 |
* ProgressBar constructor. |
| 37 |
* |
| 38 |
* @param string $message Text to display before the progress bar. |
| 39 |
* @param integer $count Total number of ticks to be performed. |
| 40 |
* @param integer $interval Optional. The interval in milliseconds between updates. Default 100. |
| 41 |
* |
| 42 |
* @throws Exception When this is not run within WP CLI |
| 43 |
*/ |
| 44 |
public function __construct( $message, $count, $interval = 100 ) { |
| 45 |
if ( ! ( defined( 'WP_CLI' ) && WP_CLI ) ) { |
| 46 |
/* translators: %s php class name */ |
| 47 |
throw new \Exception( sprintf( __( 'The %s class can only be run within WP CLI.', 'action-scheduler' ), __CLASS__ ) ); |
| 48 |
} |
| 49 |
|
| 50 |
$this->total_ticks = 0; |
| 51 |
$this->message = $message; |
| 52 |
$this->count = $count; |
| 53 |
$this->interval = $interval; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Increment the progress bar ticks. |
| 58 |
*/ |
| 59 |
public function tick() { |
| 60 |
if ( null === $this->progress_bar ) { |
| 61 |
$this->setup_progress_bar(); |
| 62 |
} |
| 63 |
|
| 64 |
$this->progress_bar->tick(); |
| 65 |
$this->total_ticks++; |
| 66 |
|
| 67 |
do_action( 'action_scheduler/progress_tick', $this->total_ticks ); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Get the progress bar tick count. |
| 72 |
* |
| 73 |
* @return int |
| 74 |
*/ |
| 75 |
public function current() { |
| 76 |
return $this->progress_bar ? $this->progress_bar->current() : 0; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Finish the current progress bar. |
| 81 |
*/ |
| 82 |
public function finish() { |
| 83 |
if ( null !== $this->progress_bar ) { |
| 84 |
$this->progress_bar->finish(); |
| 85 |
} |
| 86 |
|
| 87 |
$this->progress_bar = null; |
| 88 |
} |
| 89 |
|
| 90 |
/** |
| 91 |
* Set the message used when creating the progress bar. |
| 92 |
* |
| 93 |
* @param string $message The message to be used when the next progress bar is created. |
| 94 |
*/ |
| 95 |
public function set_message( $message ) { |
| 96 |
$this->message = $message; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Set the count for a new progress bar. |
| 101 |
* |
| 102 |
* @param integer $count The total number of ticks expected to complete. |
| 103 |
*/ |
| 104 |
public function set_count( $count ) { |
| 105 |
$this->count = $count; |
| 106 |
$this->finish(); |
| 107 |
} |
| 108 |
|
| 109 |
/** |
| 110 |
* Set up the progress bar. |
| 111 |
*/ |
| 112 |
protected function setup_progress_bar() { |
| 113 |
$this->progress_bar = \WP_CLI\Utils\make_progress_bar( |
| 114 |
$this->message, |
| 115 |
$this->count, |
| 116 |
$this->interval |
| 117 |
); |
| 118 |
} |
| 119 |
} |
| 120 |
|