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
27 changes: 27 additions & 0 deletions 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 Down
36 changes: 24 additions & 12 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 @@ -109,7 +131,7 @@ static function generalHelp() {
self::line(' ...');
}

self::block( <<<EOB
self::out( <<<EOB

Built-in commands:
core Update the WordPress core
Expand All @@ -119,21 +141,11 @@ static function generalHelp() {
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 );
}

/**
* Display a legend
*
Expand Down
58 changes: 34 additions & 24 deletions wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@
\cli\register_autoload();

// Taken from https://github.com/88mph/wpadmin/blob/master/wpadmin.php
// Does the user have access to read the directory? If so, allow them to use the command line tool.
if(true == is_readable(WP_ROOT . 'wp-load.php')){
// Load WordPress libs.
require_once(WP_ROOT . 'wp-load.php');
require_once(ABSPATH . WPINC . '/template-loader.php');
require_once(ABSPATH . 'wp-admin/includes/admin.php');
}
else {
if ( !is_readable( WP_ROOT . 'wp-load.php' ) ) {
WP_CLI::error('Either this is not a WordPress document root or you do not have permission to administer this site.');
exit();
}

// Get the cli arguments
list( $arguments, $assoc_args ) = WP_CLI::parse_args( array_slice( $GLOBALS['argv'], 1 ) );

// Handle --blog parameter
if ( isset( $assoc_args['blog'] ) ) {
$blog = $assoc_args['blog'];
unset( $assoc_args['blog'] );
} elseif ( is_readable( WP_ROOT . '/wp-cli-blog' ) ) {
$blog = file_get_contents( WP_ROOT . '/wp-cli-blog' );
}

if ( isset( $blog ) ) {
list( $domain, $path ) = explode( '/', $blog, 2 );

$_SERVER['HTTP_HOST'] = $domain;

$_SERVER['REQUEST_URI'] = '/' . $path;
}

// Load WordPress libs
require_once(WP_ROOT . 'wp-load.php');
require_once(ABSPATH . 'wp-admin/includes/admin.php');

// Load all internal commands
foreach (glob(WP_CLI_ROOT.'/commands/internals/*.php') as $filename) {
include $filename;
Expand All @@ -50,27 +66,21 @@
include $filename;
}

// Get the cli arguments
$arguments = $GLOBALS['argv'];

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

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

// Check if there are commands installed
if(empty(WP_CLI::$commands)) {
WP_CLI::error('No commands installed');
WP_CLI::line();
WP_CLI::line('Visit the wp-cli page on github on more information on how to install commands.');
exit();
}
// Try to load the class, otherwise it's an Unknown command
elseif(isset(WP_CLI::$commands[$command])) {
new WP_CLI::$commands[$command]($arguments);
}
// Show the general help for wp-cli
else {

// Get the top-level command
$command = array_shift( $arguments );

if ( !isset( WP_CLI::$commands[$command] ) ) {
WP_CLI::generalHelp();
}
exit();
}

new WP_CLI::$commands[$command]( $arguments, $assoc_args );