From 4df02514fe1902e6bdccc98b5eafd518a4c4383a Mon Sep 17 00:00:00 2001 From: Simone Tarantino Date: Tue, 10 Jan 2023 19:07:26 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20Multisite=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Command.php | 94 +++++++++++++++++++++++++++++++++++++++++++------ src/Utils.php | 58 ++++++++++++++++++++++++++++++ 2 files changed, 141 insertions(+), 11 deletions(-) create mode 100644 src/Utils.php diff --git a/src/Command.php b/src/Command.php index 778d1fb..0b7307c 100644 --- a/src/Command.php +++ b/src/Command.php @@ -2,6 +2,7 @@ namespace Idearia\WP_CLI; use WP_CLI; +use Idearia\WP_CLI\Utils; /** * Register new WP CLI commands. @@ -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 */ @@ -27,6 +53,36 @@ 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 * @@ -34,10 +90,12 @@ abstract class Command */ public static function init( string $command ): void { - if ( ! static::isCliRunning() ) { + if ( ! Utils::is_cli_running() ) { return; } + static::$command = $command; + static::register( $command ); } @@ -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 @@ -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) @@ -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; + } } diff --git a/src/Utils.php b/src/Utils.php new file mode 100644 index 0000000..73461d1 --- /dev/null +++ b/src/Utils.php @@ -0,0 +1,58 @@ + 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(); + } + } +} \ No newline at end of file