Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?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