File tree Expand file tree Collapse file tree 2 files changed +19
-7
lines changed
Expand file tree Collapse file tree 2 files changed +19
-7
lines changed Original file line number Diff line number Diff line change @@ -53,6 +53,11 @@ abstract class Command
5353 */
5454 protected static int $ count_after_invoke = 0 ;
5555
56+ /**
57+ * All subclasses must have an invoke method that handles the command
58+ */
59+ abstract protected static function invoke ( array $ args , array $ assoc_args );
60+
5661 public function __invoke ( array $ args , array $ assoc_args )
5762 {
5863 if ( ! is_multisite () ) {
@@ -73,10 +78,10 @@ public static function invoke_multisite( array $args, array $assoc_args )
7378 if ( $ all_sites_flag && ! static ::$ allow_all_sites_flag ) {
7479 WP_CLI ::error ( 'The --all-sites flag is not allowed for this command. ' );
7580 }
76-
81+
7782 // If the --all-sites flag is set then run the handler on all sites.
7883 if ( $ all_sites_flag ) {
79- Utils::run_on_all_sites ( $ args , $ assoc_args );
84+ Utils::run_on_all_sites ( [ static ::class, ' invoke ' ], $ args , $ assoc_args );
8085 } else {
8186 // Run the handler on the current site.
8287 static ::invoke ( $ args , $ assoc_args );
@@ -244,6 +249,7 @@ public static function get_synopsis(): array
244249 'type ' => 'flag ' ,
245250 'name ' => 'all-sites ' ,
246251 'description ' => 'Run the command on all sites in the network ' ,
252+ 'optional ' => true ,
247253 ];
248254 }
249255
Original file line number Diff line number Diff line change @@ -28,17 +28,23 @@ public static function is_cli_running(): bool
2828 * @param mixed $default Default value for the flag. Default: NULL.
2929 * @return mixed
3030 */
31- public static function get_flag_value ( $ assoc_args , $ flag , $ default = null )
31+ public static function get_flag_value ( array $ assoc_args , string $ flag , mixed $ default = null ) : mixed
3232 {
3333 return isset ( $ assoc_args [ $ flag ] ) ? $ assoc_args [ $ flag ] : $ default ;
3434 }
3535
3636 /**
3737 * Loop through all not deleted sites and run the command on each one.
38+ *
39+ * The callback must take two array arguments: $args and $assoc_args.
40+ *
41+ * @param callable $callback
42+ * @param array $args
43+ * @param array $assoc_args
3844 */
39- public static function run_on_all_sites ( array $ args , array $ assoc_args )
45+ public static function run_on_all_sites ( callable $ callback , array $ args , array $ assoc_args ) : void
4046 {
41- // Get all active sites
47+ // Get all active sites.
4248 $ sites = get_sites ( array (
4349 'deleted ' => 0 ,
4450 ) );
@@ -48,8 +54,8 @@ public static function run_on_all_sites( array $args, array $assoc_args )
4854 // Switch to the site.
4955 switch_to_blog ( $ site ->blog_id );
5056
51- // Run invoke .
52- self :: __invoke ( $ args , $ assoc_args );
57+ // Run callback .
58+ call_user_func ( $ callback , $ args , $ assoc_args );
5359
5460 // Restore the site.
5561 restore_current_blog ();
You can’t perform that action at this time.
0 commit comments