Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,33 @@ Installing the plugin ...
Success: The plugin is successfully installed
```

Multisite
---------

On a multisite installation, you need to pass a --blog parameter, so that WP knows which site it's supposed to be operating on:

```
wp theme status --blog=localhost/wp/test
```

If you have a subdomain installation, it would look like this:

```
wp theme status --blog=test.example.com
```

If you're usually working on the same site most of the time, you can put the url of that site in a file called 'wp-cli-blog' in your root WP dir:

```
echo 'test.example.com' > wp-cli-blog
```

Then, you can call `wp` without the --blog parameter again:

```
wp theme status
```

Adding commands
---------------

Expand All @@ -72,7 +99,6 @@ class ExampleCommand extends WP_CLI_Command {
*
* @param string $args
* @return void
* @author Andreas Creten
*/
function example($args = array()) {
// Print a success message
Expand Down
143 changes: 80 additions & 63 deletions class-wp-cli-command.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,90 +7,107 @@
* @author Andreas Creten
*/
abstract class WP_CLI_Command {

/**
* Return a short description for the command.
*
* @return string
*/
public static function get_description() {
return false;
}

/**
* Keeps a reference to the current command name
*
* @param string
*/
protected $command;

/**
* Construct for this class, transfers the cli arguments to the right class
*
* @param Array $args
* @param string $command
* @param array $args
* @param array $assoc_args
*/
function __construct($args = array()) {
function __construct( $command, $args, $assoc_args ) {
$this->command = $command;

$this->dispatch( $args, $assoc_args );
}

/**
* Transfers the handling to the appropriate method
*
* @param array $args
* @param array $assoc_args
*/
protected function dispatch( $args, $assoc_args ) {
// The first command is the sub command
$sub_command = array_shift($args);

// If the method exists, try to load it
if(method_exists($this, $sub_command)) {
$this->$sub_command($args);
}
// If a dummy method exists, use it. This if for reserved keywords in php (like list, isset)
elseif(method_exists($this, '_'.$sub_command)) {
$sub_command = '_'.$sub_command;
$this->$sub_command($args);
}
// Otherwise, show the help for this command
else {
$this->help($args);
}

if ( !method_exists($this, $sub_command) ) {
// This if for reserved keywords in php (like list, isset)
$sub_command = '_'.$sub_command;
}

if ( !method_exists($this, $sub_command) ) {
$sub_command = 'help';
}

$this->$sub_command($args, $assoc_args);
}

/**
* General help function for this command
*
* @param Array $args
* @param string $args
* @param string $assoc_args
* @return void
*/
public function help($args = array()) {
// Get the cli arguments
$arguments = $GLOBALS['argv'];

// Remove the first entry
array_shift($arguments);

// Get the command
$used_command = array_shift($arguments);

public function help( $args = array(), $assoc_args = array() ) {
$desc = $this->get_description();
if ( $desc ) {
WP_CLI::line( $desc );
WP_CLI::line();
}

// Show the list of sub-commands for this command
WP_CLI::line('Example usage:');
WP_CLI::out(' wp '.$used_command);
$methods = WP_CLI_Command::getMethods($this);
if(!empty($methods)) {
WP_CLI::line( 'Example usage:' );
WP_CLI::out( ' wp '.$this->command );

$methods = WP_CLI_Command::get_methods($this);
if ( !empty( $methods ) ) {
WP_CLI::out(' ['.implode('|', $methods).']');
}
WP_CLI::line(' ...');
WP_CLI::line();

// Send a warning to the user because there is no custom help function defined in the command
// Make usure there always is a help method in your command class
WP_CLI::warning('The command has no dedicated help function, ask the creator to fix it.');
}

/**
* Get the filtered list of methods for a class
* Get the filtered list of methods for a class.
*
* @param string $class
* @return Array The list of methods
* @return array The list of methods
*/
static function getMethods($class) {
// Methods that don't need to be included in the method list
$blacklist = array('__construct', 'getMethods');

// Get all the methods of the class
$methods = get_class_methods($class);

// Remove the blacklisted methods
foreach($blacklist as $method) {
$in_array = array_search($method, $methods);
if($in_array !== false) {
unset($methods[$in_array]);
}
}

// Fix dummy function names
foreach($methods as $key => $method) {
if(strpos($method, '_') === 0) {
$methods[$key] = substr($method, 1, strlen($method));
}
static function get_methods($class) {
$reflection = new ReflectionClass( $class );

$methods = array();

foreach ( $reflection->getMethods() as $method ) {
if ( $method->isPublic() && !$method->isStatic() && !$method->isConstructor() ) {
$name = $method->name;

if ( strpos( $name, '_' ) === 0 ) {
$name = substr( $method, 1 );
}

$methods[] = $name;
}
}

// Only return the values, to fill up the gaps
return array_values($methods);

return $methods;
}
}
}
48 changes: 23 additions & 25 deletions class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,28 @@ static function errorToString($errors) {
}
}

/**
* Splits regular arguments from associative arguments.
*
* @return array
*/
static function parse_args( $arguments ) {
$global_arg_names = array( 'blog' );

$regular_args = array();
$assoc_args = array();

foreach ( $arguments as $arg ) {
if ( preg_match( '|^--(\w+)=(.+)|', $arg, $matches ) ) {
$assoc_args[ $matches[1] ] = $matches[2];
} else {
$regular_args[] = $arg;
}
}

return array( $regular_args, $assoc_args );
}

/**
* Display the help function for the wp-cli
*
Expand All @@ -102,36 +124,12 @@ static function generalHelp() {
self::line('Example usage:');
foreach(self::$commands as $name => $command) {
self::out(' wp '.$name);
$methods = WP_CLI_Command::getMethods($command);
$methods = WP_CLI_Command::get_methods($command);
if(!empty($methods)) {
self::out(' ['.implode('|', $methods).']');
}
self::line(' ...');
}

self::block( <<<EOB

Built-in commands:
core Update the WordPress core
home Open the wp-cli project on Github
option Manipulate the WordPress options
plugin Do cool things with the installed plugins
theme Do cool things with the installed themes

See 'wp <command> help' for more information on a specific command.
EOB
);
}

/**
* Display multiple lines of content
*
* @param string $content
* @return void
*/
function block( $content ) {
foreach ( explode( "\n", $content ) as $line )
self::line( $line );
}

/**
Expand Down
33 changes: 19 additions & 14 deletions commands/community/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,60 +11,65 @@
* @author Andreas Creten
*/
class ExampleCommand extends WP_CLI_Command {

public static function get_description() {
return 'An example command.';
}

/**
* Example method
*
* @param string $args
* @param string $args
* @return void
*/
function example($args = array()) {
// Print a string
WP_CLI::out('Prints a string -- ');

// Print a second string
WP_CLI::out('Prints a second string -- ');

// Print a single line
WP_CLI::line('Prints out a line');

// Run through the commands
foreach ($args as $arg) {
WP_CLI::line($arg);
}

// Print an error message
WP_CLI::error('Error message');
// Result: Error: Error message

// Print a warning message
WP_CLI::warning('Warning message');
// Result: Warning: Warning message

// Print a success message
WP_CLI::success('Success message');
// Result: Success: Success message
}

/**
* Help function for this command
*
* @param string $args
* @param string $args
* @return void
*/
public function help($args = array()) {
// Get the cli arguments
$arguments = $GLOBALS['argv'];

// Remove the first entry
array_shift($arguments);

// Get the command
$used_command = array_shift($arguments);

// Show the list of sub-commands for this command
WP_CLI::line('Example usage:');
$methods = WP_CLI_Command::getMethods($this);

$methods = WP_CLI_Command::get_methods($this);
foreach ($methods as $method) {
if($method != 'help') {
WP_CLI::line(' wp '.$used_command.' '.$method.' <plugin-name>');
Expand All @@ -74,4 +79,4 @@ public function help($args = array()) {
}
}
}
}
}
Loading