Skip to content
Closed
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
54 changes: 3 additions & 51 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ Installing

**Via package manager:**

Ubuntu, Debian: [.deb package](https://github.com/downloads/andreascreten/wp-cli/wp-cli_0.1.deb)
* Ubuntu, Debian: [.deb package](https://github.com/downloads/andreascreten/wp-cli/wp-cli_0.1.deb)
* Mac: install [Homebrew](http://mxcl.github.com/homebrew/) and run `brew install wp-cli`.

**From source:**

Expand Down Expand Up @@ -110,56 +111,7 @@ Adding commands
---------------

Adding commands to wp-cli is very easy. You can even add them from within your own plugin.

Each command has its own class, the methods of that class are the sub commands of the command. The base class for the commands is the abstract `WP_CLI_Command`, it handles some essential functionality (like default help for your command).

You can add new commands to the `commands/community` folder in the wp-cli plugin, they will be auto-loaded on startup. You can also add commands from within your plugins by just calling the wp-cli hooks from there.

A wp-cli class is structured like this:

``` php
<?php
/**
* Implement example command
*
* @package wp-cli
* @subpackage commands/community
* @author Andreas Creten
*/
class ExampleCommand extends WP_CLI_Command {
/**
* Example method
*
* @param string $args
* @return void
*/
function example($args = array()) {
// Print a success message
WP_CLI::success('Success message');
}
}
```

To register this class under the `example` command, add the following line to the top of your command class file.

``` php
<?php
// Add the command to the wp-cli
WP_CLI::addCommand('example', 'ExampleCommand');
```

This will register the comand `wp example` and the subcommand `wp example example`. If you run `wp example example`, the text `Success: Success message` will be printed to the command line and the script will end.

You can take a look at the example command file in `commands/community/example.php` for more details. For the ways to interact with the command line, you should take a look at the WP_CLI class in the `class-wp-cli.php` file.

If you want to register the command from within your plugin you might want to add a check to see if wp-cli is running. By doing this you can implement your wp-cli command by default, even if wp-cli is not installed on the WordPress installation. You can use the `WP_CLI` constant to check if wp-cli is running:

```php
<?php
if(defined('WP_CLI') && WP_CLI) {
// Define and register your command in here
}
```
You can find more information about adding commands in the [Commands Cookbook](https://github.com/andreascreten/wp-cli/wiki/Commands-Cookbook) on our Wiki.

**Please share the commands you make, issue a pull request to get them included in wp-cli by default.**

Expand Down
58 changes: 58 additions & 0 deletions commands/internals/version.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

WP_CLI::addCommand('version', 'VersionCommand');

/**
* Implement version command
*
* @package wp-cli
* @subpackage commands/internals
* @author Nikolay Bachiyski <nb@nikolay.bg>
*/
class VersionCommand extends WP_CLI_Command {

var $default_subcommand = 'core';

public function core( $args = array() ) {
global $wp_version;
$color = '%G';
$version_text = $wp_version;
$version_types = array(
'-RC' => array( 'release candidate', '%y' ),
'-beta' => array( 'beta', '%B' ),
'-' => array( 'in development', '%R' ),
);
foreach( $version_types as $needle => $type ) {
if ( stristr( $wp_version, $needle ) ) {
list( $version_text, $color ) = $type;
$version_text = "$color$wp_version%n (stability: $version_text)";
break;
}
}
WP_CLI::line( "WordPress version:\t$version_text" );
}

public function extra( $args = array() ) {
global $wp_version, $wp_db_version, $tinymce_version, $manifest_version;
$this->core( $args );
WP_CLI::line();
WP_CLI::line( "Database revision:\t$wp_db_version" );

preg_match( '/(\d)(\d+)-/', $tinymce_version, $match );
$human_readable_tiny_mce = $match? $match[1] . '.' . $match[2] : '';
WP_CLI::line( "TinyMCE version:\t" . ( $human_readable_tiny_mce? "$human_readable_tiny_mce ($tinymce_version)" : $tinymce_version ) );

WP_CLI::line( "Manifest revision:\t$manifest_version" );
}

public static function help() {
WP_CLI::line( <<<EOB
usage: wp version <sub-command>

Available sub-commands:
core WordPress core version
extra Detailed version information
EOB
);
}
}
74 changes: 37 additions & 37 deletions core/class-wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class WP_CLI {
* @param string $class The class to manage the command
* @return void
*/
public function addCommand($name, $class) {
public function addCommand( $name, $class ) {
self::$commands[$name] = $class;
}

Expand All @@ -26,7 +26,7 @@ public function addCommand($name, $class) {
* @param string $message
* @return void
*/
static function out($message) {
static function out( $message ) {
\cli\out($message);
}

Expand All @@ -36,7 +36,7 @@ static function out($message) {
* @param string $message
* @return void
*/
static function line($message = '') {
static function line( $message = '' ) {
\cli\line($message);
}

Expand All @@ -47,8 +47,8 @@ static function line($message = '') {
* @param string $label
* @return void
*/
static function error($message, $label = 'Error') {
\cli\line('%R'.$label.': %n'.self::errorToString($message));
static function error( $message, $label = 'Error' ) {
\cli\line( '%R' . $label . ': %n' . self::errorToString( $message ) );
}

/**
Expand All @@ -58,8 +58,8 @@ static function error($message, $label = 'Error') {
* @param string $label
* @return void
*/
static function success($message, $label = 'Success') {
\cli\line('%G'.$label.': %n'.$message);
static function success( $message, $label = 'Success' ) {
\cli\line( '%G' . $label . ': %n' . $message );
}

/**
Expand All @@ -69,8 +69,8 @@ static function success($message, $label = 'Success') {
* @param string $label
* @return void
*/
static function warning($message, $label = 'Warning') {
\cli\line('%C'.$label.': %n'.$message);
static function warning( $message, $label = 'Warning' ) {
\cli\line( '%C' . $label . ': %n' . $message );
}

/**
Expand All @@ -79,13 +79,12 @@ static function warning($message, $label = 'Warning') {
* @param mixed $errors
* @return string
*/
static function errorToString($errors) {
if(is_string($errors)){
static function errorToString( $errors ) {
if( is_string( $errors ) ) {
return $errors;
}
elseif(is_wp_error($errors) && $errors->get_error_code()){
foreach($errors->get_error_messages() as $message){
if($errors->get_error_data() )
} elseif( is_wp_error( $errors ) && $errors->get_error_code() ) {
foreach( $errors->get_error_messages() as $message ) {
if( $errors->get_error_data() )
return $message . ' ' . $errors->get_error_data();
else
return $message;
Expand Down Expand Up @@ -141,8 +140,8 @@ static function compose_args( $args, $assoc_args = array() ) {
* @param array( code => title ) $legend
* @return void
*/
static function legend($legend) {
$legend['%yU'] = 'Update Available';
static function legend( $legend ) {
$legend[ '%yU' ] = 'Update Available';

$legend_line = array();
foreach ( $legend as $key => $title )
Expand Down Expand Up @@ -180,13 +179,13 @@ class CLI_Upgrader_Skin {
var $done_header = false;
var $result = false;

function __construct($args = array()) {
$defaults = array('url' => '', 'nonce' => '', 'title' => '', 'context' => false);
$this->options = wp_parse_args($args, $defaults);
function __construct( $args = array() ) {
$defaults = array( 'url' => '', 'nonce' => '', 'title' => '', 'context' => false );
$this->options = wp_parse_args( $args, $defaults );
}

function set_upgrader(&$upgrader) {
if(is_object($upgrader)) {
function set_upgrader( &$upgrader ) {
if( is_object( $upgrader ) ) {
$this->upgrader =& $upgrader;
}

Expand All @@ -195,41 +194,42 @@ function set_upgrader(&$upgrader) {

function add_strings() {}

function set_result($result) {
function set_result( $result ) {
$this->result = $result;
}

function request_filesystem_credentials($error = false) {
function request_filesystem_credentials( $error = false ) {
$url = $this->options['url'];
$context = $this->options['context'];
if(!empty($this->options['nonce'])) {
$url = wp_nonce_url($url, $this->options['nonce']);
if( !empty( $this->options['nonce'] ) ) {
$url = wp_nonce_url( $url, $this->options['nonce']) ;
}

// Possible to bring inline, Leaving as is for now.
return request_filesystem_credentials($url, '', $error, $context);
return request_filesystem_credentials( $url, '', $error, $context );
}

function header() {}
function footer() {}

function error($errors) {
$this->feedback(WP_CLI::errorToString($errors));
function error( $errors ) {
$this->feedback( WP_CLI::errorToString($errors) );
}

function feedback($string) {
if(isset( $this->upgrader->strings[$string]))
function feedback( $string ) {
if(isset( $this->upgrader->strings[$string] ) )
$string = $this->upgrader->strings[$string];

if(strpos($string, '%') !== false) {
if( strpos( $string, '%' ) !== false ) {
$args = func_get_args();
$args = array_splice($args, 1);
if(!empty($args)) {
$string = vsprintf($string, $args);
$args = array_splice( $args, 1 );
if( !empty( $args ) ) {
$string = vsprintf( $string, $args );
}

}
if(empty($string)) {

if( empty( $string ) ) {
return;
}

Expand All @@ -238,4 +238,4 @@ function feedback($string) {

function before() {}
function after() {}
}
}
10 changes: 6 additions & 4 deletions core/wp-cli.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,13 @@
define('WP_CLI', true);

// Include the wp-cli classes
include WP_CLI_ROOT.'core/class-wp-cli.php';
include WP_CLI_ROOT.'core/class-wp-cli-command.php';
include WP_CLI_ROOT . 'core/class-wp-cli.php';
include WP_CLI_ROOT . 'core/class-wp-cli-command.php';

// Include the command line tools
include WP_CLI_ROOT.'php-cli-tools/lib/cli/cli.php';
// Include the command line tools
include WP_CLI_ROOT . 'php-cli-tools/lib/cli/cli.php';

// Register the cli tools autoload
\cli\register_autoload();

// Get the cli arguments
Expand Down