Skip to content

Commit 4bbc59a

Browse files
✨ Multisite support
1 parent 09f4b09 commit 4bbc59a

File tree

2 files changed

+141
-11
lines changed

2 files changed

+141
-11
lines changed

src/Command.php

Lines changed: 83 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
namespace Idearia\WP_CLI;
33

44
use WP_CLI;
5+
use Idearia\WP_CLI\Utils;
56

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

21+
/**
22+
* Command handled by this class, set automatically
23+
*/
24+
protected static string $command;
25+
26+
/**
27+
* Short description of the command
28+
*/
29+
protected static string $shortdesc = '';
30+
31+
/**
32+
* Long description of the command
33+
*/
34+
protected static string $longdesc = '';
35+
36+
/**
37+
* Whether to allow the command to run on all sites in a multisite network
38+
*/
39+
protected static bool $allow_all_sites_flag = false;
40+
41+
/**
42+
* Synopsis of the command
43+
*/
44+
protected static array $synopsis = [];
45+
2046
/**
2147
* Number of times before_invoke is run
2248
*/
@@ -27,17 +53,49 @@ abstract class Command
2753
*/
2854
protected static int $count_after_invoke = 0;
2955

56+
public function __invoke( array $args, array $assoc_args )
57+
{
58+
if ( ! is_multisite() ) {
59+
static::invoke( $args, $assoc_args );
60+
} else {
61+
static::invoke_multisite( $args, $assoc_args );
62+
}
63+
}
64+
65+
/**
66+
* Handle the command for multisite installations
67+
*/
68+
public static function invoke_multisite( array $args, array $assoc_args )
69+
{
70+
$all_sites_flag = Utils::get_flag_value( $assoc_args, 'all-sites' );
71+
72+
// Throw an error if the --all-sites flag is set but the command does not allow it.
73+
if ( $all_sites_flag && ! static::$allow_all_sites_flag ) {
74+
WP_CLI::error( 'The --all-sites flag is not allowed for this command.' );
75+
}
76+
77+
// If the --all-sites flag is set then run the handler on all sites.
78+
if ( $all_sites_flag ) {
79+
Utils::run_on_all_sites( $args, $assoc_args );
80+
} else {
81+
// Run the handler on the current site.
82+
static::invoke( $args, $assoc_args );
83+
}
84+
}
85+
3086
/**
3187
* Register the command with WP-CLI
3288
*
3389
* @param string $command CLI command handled by this class
3490
*/
3591
public static function init( string $command ): void
3692
{
37-
if ( ! static::isCliRunning() ) {
93+
if ( ! Utils::is_cli_running() ) {
3894
return;
3995
}
4096

97+
static::$command = $command;
98+
4199
static::register( $command );
42100
}
43101

@@ -52,8 +110,11 @@ protected static function register( string $command ): void
52110
static::class,
53111
[
54112
'before_invoke' => [ static::class, '_before_invoke' ],
55-
'after_invoke' => [ static::class, '_after_invoke' ]
56-
]
113+
'after_invoke' => [ static::class, '_after_invoke' ],
114+
'shortdesc' => static::$shortdesc,
115+
'synopsis' => static::get_synopsis(),
116+
'longdesc' => static::$longdesc,
117+
],
57118
);
58119

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

89-
/**
90-
* Check if we are running from WP-CLI
91-
*/
92-
protected static function isCliRunning(): bool
93-
{
94-
return defined( 'WP_CLI' ) && WP_CLI;
95-
}
96-
97150
/**
98151
* Override to inject code just before any command in
99152
* the class is found (runs before before_invoked)
@@ -177,4 +230,23 @@ public static function _after_invoke(): void
177230
static::$count_after_invoke++;
178231
}
179232

233+
/**
234+
* Get the command synopsis
235+
*
236+
* @return array[]
237+
*/
238+
public static function get_synopsis(): array
239+
{
240+
// If the command allows it, then add the --all-sites flag
241+
// at the end of the synopsis array
242+
if ( static::$allow_all_sites_flag ) {
243+
static::$synopsis[] = [
244+
'type' => 'flag',
245+
'name' => 'all-sites',
246+
'description' => 'Run the command on all sites in the network',
247+
];
248+
}
249+
250+
return static::$synopsis;
251+
}
180252
}

src/Utils.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
namespace Idearia\WP_CLI;
3+
4+
abstract class Utils
5+
{
6+
/**
7+
* Check if WP-CLI is running
8+
*
9+
* @return bool
10+
*/
11+
public static function is_cli_running(): bool
12+
{
13+
return defined( 'WP_CLI' ) && WP_CLI;
14+
}
15+
16+
/**
17+
* Return the flag value or, if it's not set, the $default value.
18+
*
19+
* Because flags can be negated (e.g. --no-quiet to negate --quiet), this
20+
* function provides a safer alternative to using
21+
* `isset( $assoc_args['quiet'] )` or similar.
22+
*
23+
* @access public
24+
* @category Input
25+
*
26+
* @param array $assoc_args Arguments array.
27+
* @param string $flag Flag to get the value.
28+
* @param mixed $default Default value for the flag. Default: NULL.
29+
* @return mixed
30+
*/
31+
public static function get_flag_value( $assoc_args, $flag, $default = null )
32+
{
33+
return isset( $assoc_args[ $flag ] ) ? $assoc_args[ $flag ] : $default;
34+
}
35+
36+
/**
37+
* Loop through all not deleted sites and run the command on each one.
38+
*/
39+
public static function run_on_all_sites( array $args, array $assoc_args )
40+
{
41+
// Get all active sites
42+
$sites = get_sites( array(
43+
'deleted' => 0,
44+
) );
45+
46+
// Loop through all sites.
47+
foreach ( $sites as $site ) {
48+
// Switch to the site.
49+
switch_to_blog( $site->blog_id );
50+
51+
// Run invoke.
52+
self::__invoke( $args, $assoc_args );
53+
54+
// Restore the site.
55+
restore_current_blog();
56+
}
57+
}
58+
}

0 commit comments

Comments
 (0)