Skip to content

Instantly share code, notes, and snippets.

@cklosowski
Last active August 27, 2019 16:40
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cklosowski/1aba412c4476c8d6fb5107eba9f90741 to your computer and use it in GitHub Desktop.
Save cklosowski/1aba412c4476c8d6fb5107eba9f90741 to your computer and use it in GitHub Desktop.
<?php
/**
* Plugin Name: chrisk.io - Example WP-CLI Progress Bar
* Plugin URI: https://chrisk.io/adding-progress-bars-wp-cli-processes/
* Description: An example plugin for creating progress bars in your WP-CLI processes.
* Version: 1.0
* Author: Chris Klosowski
* Author URI: https://chrisk.io
* License: GPL-2.0+
*/
// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) exit;
if ( defined( 'WP_CLI' ) && WP_CLI ) {
/**
* Example WPCLI extension class
*
* Example_CLI_Progress_Bar Class
*
* @since 1.0
*/
class Example_CLI_Progress_Bar extends WP_CLI_Command {
public function progress_bar( $args, $assoc_args ) {
$total = isset( $assoc_args ) && array_key_exists( 'total', $assoc_args ) ? absint( $assoc_args[ 'total' ] ) : 100;
WP_CLI::line( 'Starting Example' );
$progress = \WP_CLI\Utils\make_progress_bar( 'Progress Bar', $total );
$i = 0;
while ( $i < $total ) {
$progress->tick();
sleep(1); // Remove this from your production code, only here to slow down the process so you can see it work.
$i++;
}
$progress->finish();
WP_CLI::line( 'Example Complete' );
}
}
WP_CLI::add_command( 'chriskio', 'Example_CLI_Progress_Bar' );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment