22namespace Idearia \WP_CLI ;
33
44use 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}
0 commit comments