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
94 changes: 83 additions & 11 deletions src/Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace Idearia\WP_CLI;

use WP_CLI;
use Idearia\WP_CLI\Utils;

/**
* Register new WP CLI commands.
Expand All @@ -17,6 +18,31 @@ abstract class Command
*/
protected static string $usage = 'Wrong arguments';

/**
* Command handled by this class, set automatically
*/
protected static string $command;

/**
* Short description of the command
*/
protected static string $shortdesc = '';

/**
* Long description of the command
*/
protected static string $longdesc = '';

/**
* Whether to allow the command to run on all sites in a multisite network
*/
protected static bool $allow_all_sites_flag = false;

/**
* Synopsis of the command
*/
protected static array $synopsis = [];

/**
* Number of times before_invoke is run
*/
Expand All @@ -27,17 +53,49 @@ abstract class Command
*/
protected static int $count_after_invoke = 0;

public function __invoke( array $args, array $assoc_args )
{
if ( ! is_multisite() ) {
static::invoke( $args, $assoc_args );
} else {
static::invoke_multisite( $args, $assoc_args );
}
}

/**
* Handle the command for multisite installations
*/
public static function invoke_multisite( array $args, array $assoc_args )
{
$all_sites_flag = Utils::get_flag_value( $assoc_args, 'all-sites' );

// Throw an error if the --all-sites flag is set but the command does not allow it.
if ( $all_sites_flag && ! static::$allow_all_sites_flag ) {
WP_CLI::error( 'The --all-sites flag is not allowed for this command.' );
}

// If the --all-sites flag is set then run the handler on all sites.
if ( $all_sites_flag ) {
Utils::run_on_all_sites( $args, $assoc_args );
} else {
// Run the handler on the current site.
static::invoke( $args, $assoc_args );
}
}

/**
* Register the command with WP-CLI
*
* @param string $command CLI command handled by this class
*/
public static function init( string $command ): void
{
if ( ! static::isCliRunning() ) {
if ( ! Utils::is_cli_running() ) {
return;
}

static::$command = $command;

static::register( $command );
}

Expand All @@ -52,8 +110,11 @@ protected static function register( string $command ): void
static::class,
[
'before_invoke' => [ static::class, '_before_invoke' ],
'after_invoke' => [ static::class, '_after_invoke' ]
]
'after_invoke' => [ static::class, '_after_invoke' ],
'shortdesc' => static::$shortdesc,
'synopsis' => static::get_synopsis(),
'longdesc' => static::$longdesc,
],
);

// Allow to do stuff just before the command is executed
Expand Down Expand Up @@ -86,14 +147,6 @@ function( array $args, array $assoc_args, array $options ) use ( $command )
);
}

/**
* Check if we are running from WP-CLI
*/
protected static function isCliRunning(): bool
{
return defined( 'WP_CLI' ) && WP_CLI;
}

/**
* Override to inject code just before any command in
* the class is found (runs before before_invoked)
Expand Down Expand Up @@ -177,4 +230,23 @@ public static function _after_invoke(): void
static::$count_after_invoke++;
}

/**
* Get the command synopsis
*
* @return array[]
*/
public static function get_synopsis(): array
{
// If the command allows it, then add the --all-sites flag
// at the end of the synopsis array
if ( static::$allow_all_sites_flag ) {
static::$synopsis[] = [
'type' => 'flag',
'name' => 'all-sites',
'description' => 'Run the command on all sites in the network',
];
}

return static::$synopsis;
}
}
58 changes: 58 additions & 0 deletions src/Utils.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php
namespace Idearia\WP_CLI;

abstract class Utils
{
/**
* Check if WP-CLI is running
*
* @return bool
*/
public static function is_cli_running(): bool
{
return defined( 'WP_CLI' ) && WP_CLI;
}

/**
* Return the flag value or, if it's not set, the $default value.
*
* Because flags can be negated (e.g. --no-quiet to negate --quiet), this
* function provides a safer alternative to using
* `isset( $assoc_args['quiet'] )` or similar.
*
* @access public
* @category Input
*
* @param array $assoc_args Arguments array.
* @param string $flag Flag to get the value.
* @param mixed $default Default value for the flag. Default: NULL.
* @return mixed
*/
public static function get_flag_value( $assoc_args, $flag, $default = null )
{
return isset( $assoc_args[ $flag ] ) ? $assoc_args[ $flag ] : $default;
}

/**
* Loop through all not deleted sites and run the command on each one.
*/
public static function run_on_all_sites( array $args, array $assoc_args )
{
// Get all active sites
$sites = get_sites( array(
'deleted' => 0,
) );

// Loop through all sites.
foreach ( $sites as $site ) {
// Switch to the site.
switch_to_blog( $site->blog_id );

// Run invoke.
self::__invoke( $args, $assoc_args );

// Restore the site.
restore_current_blog();
}
}
}