ryanwelcher
11/14/2017 - 1:30 PM

Create a WP-CLI command, passing 1 parameter

Create a WP-CLI command, passing 1 parameter

<?php

/**
 *
 * Plugin Name: Demo - MJ WP-CLI Command
 * Description:  Create WP CLI Command
 * Author: MJ
 * Version: 1.0
 */

namespace MJ\Migration;


/**
 * Class Migration
 * @package MJ\Migration
 */
class Migration {

	/**
	 * Registers all the hooks bla bla
	 */
	function __construct() {
		
	}

	/**
	 * @param $param
	 */
	function run_import( $param ) {

		if ( empty( $param ) ) {
			$param = 'nothing set';
		}
		
		\WP_CLI::line( esc_html( "param is $param " ) );

	}
}


/**
 * Class Example_Command
 * @package MJ\Migration
 *          
 *  Generate our command object and give it a sub-command 'import'
 *  Usage: wp example-command import --param=bob
 */
class Example_Command extends \WP_CLI_Command {

	/**
	 * @param $args
	 * @param $assoc_args
	 */
	public function import( $args, $assoc_args ) {

		$demoObject = new Migration();
		$demoObject->run_import( $assoc_args['param'] );

		\WP_CLI::success( "All done!" );
	}
}


// Make sure WP-CLI exists
if ( defined( 'WP_CLI' ) && \WP_CLI ) {

	// Our new command will be 'example-command'
	\WP_CLI::add_command( 'example-command',  __NAMESPACE__ . '\\Example_Command'  );


}