Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WDS WP-CLI 101 Complete
<?php
/*
Plugin Name: WDS Hello World
Plugin URI: https://webdevstudios.com/
Description: Teaching the basics of WP-CLI
Author: Web Dev Studios
Version: 1.0.0
Author URI: https://webdevstudios.com/
*/
class WDS_CLI {
/**
* Returns 'Hello World'
*
* @since 1.0.0
* @author Scott Anderson
*/
public function hello_world() {
WP_CLI::line( 'Hello World!' );
}
/**
* Returns all arguments passed into command to demonstrate how to access them.
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
* @since 1.0.0
* @author Scott Anderson
*/
public function display_arguments( $args, $assoc_args ) {
// Run command wp wds display_arguments John Doe 'Jane Doe' 32 --title='Moby Dick' --author='Herman Melville' --published=1851 --publish --no-archive
// Examples of Arguments.
WP_CLI::line( var_export($args[0]) ); // John
WP_CLI::line( var_export($args[1]) ); // Doe
WP_CLI::line( var_export($args[2]) ); // Jane Doe
WP_CLI::line( var_export($args[3]) ); // 32
// Example of Associated Arguments
WP_CLI::line( var_export($assoc_args['title']) ); // Moby Dick
WP_CLI::line( var_export($assoc_args['author']) ); // Herman Melville
WP_CLI::line( var_export($assoc_args['published']) ); // 1851
// Example of Associated Arguments as flag
WP_CLI::line( var_export($assoc_args['publish']) ); // True
WP_CLI::line( var_export($assoc_args['archive']) ); // False
}
/**
* Returns multiple messages to demonstrate different command return types.
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
* @since 1.0.0
* @author Scott Anderson
*/
public function display_messages( $args, $assoc_args ) {
// No prepends.
WP_CLI::line( 'Standard line return.' ); // No prefix on line return.
WP_CLI::log( 'Standard line returned that wont be silenced.' ); // No prefix on line but ignores --quiet
// Color make sure to use %n at the end of a style or else the style will apply to the next output.
WP_CLI::line(WP_CLI::colorize( '%BBlue text%n' )); // Returns text in blue color. Ignores --quiet.
WP_CLI::line(WP_CLI::colorize( '%MMagenta text%n' )); // Returns text in magenta. Ignores --quiet.
WP_CLI::line(WP_CLI::colorize( '%UUnderline text%n' )); // Returns text underlined. Ignores --quiet.
// Only prepends.
WP_CLI::success( 'Post updated!' ); // Prepends Success to message
WP_CLI::warning( 'No match was found.' ); // Prepends Warning to message.
// Special conditions.
WP_CLI::debug( 'Breakpoint comment.' ); // Displays only when --debug flag is used.
WP_CLI::error_multi_line( ['Error found!', 'Post not updated!','User not updated!'] ); // Displays multi-line error in red box. Doesn't exit script. Ignores --quiet but looses formating.
//Returns error message if --error custom flag is added.
if( isset($assoc_args['error']) && $assoc_args['error']) {
WP_CLI::error( 'Error found!' ); // Prepends message with Error and exits script.
}
WP_CLI::halt( 200 ); // Halts script execution with a specific return code. Could for calling your subcommand from another.
}
/**
* Displays progress bar to demonstrate progression through a time consuming process.
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
* @since 1.0.0
* @author Scott Anderson
*/
public function generate_posts_progress_bar( $args, $assoc_args ) {
$desired_posts_to_generate = (int) $args[0];
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating Posts', $desired_posts_to_generate );
for ( $i = 0; $i < $desired_posts_to_generate; $i++ ) {
// Code used to generate a post.
$progress->tick();
}
$progress->finish();
}
/**
* Generate posts with meta values.
*
* @param Array $args Arguments in array format.
* @param Array $assoc_args Key value arguments stored in associated array format.
* @since 1.0.0
* @author Scott Anderson
*/
public function generate_posts( $args, $assoc_args ) {
// Get Post Details.
$desired_posts_to_generate = (int) $args[0]; // First argument is how many posts should be generated.
$title_prepend = $args[1]; // Second argument should be the title of posts generated. This will be used with index in loop to generate a title.
$author_id = (int) $args[2]; // Id of author who to assign generated post to.
$progress = \WP_CLI\Utils\make_progress_bar( 'Generating Posts', $desired_posts_to_generate );
for ( $i = 0; $i < $desired_posts_to_generate; $i++ ) {
// Code used to generate a post.
$my_post = array(
'post_title' => $title_prepend . ' ' . ($i + 1),
'post_status' => 'publish',
'post_author' => $author_id,
'post_type' => 'post',
'tags_input' => [ 'generated' ],
'meta_input' => $assoc_args, // Simply passes all key value pairs to posts generated that can be used in testing.
);
// Insert the post into the database.
wp_insert_post( $my_post );
$progress->tick();
}
$progress->finish();
WP_CLI::success( $desired_posts_to_generate. ' posts generated!' ); // Prepends Success to message
}
}
/**
* Registers our command when cli get's initialized.
*
* @since 1.0.0
* @author Scott Anderson
*/
function wds_cli_register_commands() {
WP_CLI::add_command( 'wds', 'WDS_CLI' );
}
add_action( 'cli_init', 'wds_cli_register_commands' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.