Skip to content

Commit ad1ace5

Browse files
author
Guido W. Pettinari
committed
✨ Allow to specify custom site query for --all-sites flag
1 parent 8ab0922 commit ad1ace5

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

phpstan.neon

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Make sure to use https://github.com/php-stubs/wp-cli-stubs
22

33
parameters:
4-
level: 8
5-
paths:
6-
- src
7-
scanFiles:
8-
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
9-
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php
10-
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-commands-stubs.php
11-
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-i18n-stubs.php
12-
ignoreErrors:
13-
- '~^Parameter #2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\): mixed, .+ given\.$~'
4+
level: 8
5+
paths:
6+
- src
7+
scanFiles:
8+
- %rootDir%/../../php-stubs/wordpress-stubs/wordpress-stubs.php
9+
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-stubs.php
10+
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-commands-stubs.php
11+
- %rootDir%/../../php-stubs/wp-cli-stubs/wp-cli-i18n-stubs.php
12+
ignoreErrors:
13+
- '~^Parameter #2 \$callable of static method WP_CLI::add_command\(\) expects callable\(\): mixed, .+ given\.$~'

src/Command.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,19 @@ abstract class Command
3838
*/
3939
protected static bool $allow_all_sites_flag = false;
4040

41+
/**
42+
* The site query for those commands that allow the --all-sites flag
43+
*
44+
* @see https://developer.wordpress.org/reference/functions/get_sites/
45+
*
46+
* @var array<string,mixed>
47+
*/
48+
protected static array $site_query = ['deleted' => 0, 'number' => PHP_INT_MAX];
49+
4150
/**
4251
* Synopsis of the command
52+
*
53+
* @var array<int,array<string|mixed>>
4354
*/
4455
protected static array $synopsis = [];
4556

@@ -55,10 +66,19 @@ abstract class Command
5566

5667
/**
5768
* All subclasses must have an invoke method that handles the command
69+
*
70+
* @param array<mixed> $args
71+
* @param array<string,mixed> $assoc_args
5872
*/
59-
abstract protected static function invoke( array $args, array $assoc_args );
73+
abstract protected static function invoke( array $args, array $assoc_args ): void;
6074

61-
public function __invoke( array $args, array $assoc_args )
75+
/**
76+
* Actual handler of the command
77+
*
78+
* @param array<mixed> $args
79+
* @param array<string,mixed> $assoc_args
80+
*/
81+
public function __invoke( array $args, array $assoc_args ): void
6282
{
6383
if ( ! is_multisite() ) {
6484
static::invoke( $args, $assoc_args );
@@ -69,8 +89,11 @@ public function __invoke( array $args, array $assoc_args )
6989

7090
/**
7191
* Handle the command for multisite installations
92+
*
93+
* @param array<mixed> $args
94+
* @param array<string,mixed> $assoc_args
7295
*/
73-
public static function invoke_multisite( array $args, array $assoc_args )
96+
public static function invoke_multisite( array $args, array $assoc_args ): void
7497
{
7598
$all_sites_flag = Utils::get_flag_value( $assoc_args, 'all-sites' );
7699

@@ -81,7 +104,7 @@ public static function invoke_multisite( array $args, array $assoc_args )
81104

82105
// If the --all-sites flag is set then run the handler on all sites.
83106
if ( $all_sites_flag ) {
84-
Utils::run_on_all_sites( [ static::class, 'invoke' ], $args, $assoc_args );
107+
Utils::run_on_all_sites( [ static::class, 'invoke' ], $args, $assoc_args, static::$site_query );
85108
} else {
86109
// Run the handler on the current site.
87110
static::invoke( $args, $assoc_args );
@@ -238,7 +261,7 @@ public static function _after_invoke(): void
238261
/**
239262
* Get the command synopsis
240263
*
241-
* @return array[]
264+
* @return array<int,array<string|mixed>>
242265
*/
243266
public static function get_synopsis(): array
244267
{

src/Utils.php

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<?php
22
namespace Idearia\WP_CLI;
33

4+
use WP_CLI;
5+
46
abstract class Utils
57
{
68
/**
@@ -23,9 +25,9 @@ public static function is_cli_running(): bool
2325
* @access public
2426
* @category Input
2527
*
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.
28+
* @param array<string,mixed> $assoc_args Arguments array.
29+
* @param string $flag Flag to get the value.
30+
* @param mixed $default Default value for the flag. Default: NULL.
2931
* @return mixed
3032
*/
3133
public static function get_flag_value( array $assoc_args, string $flag, mixed $default = null )
@@ -39,15 +41,24 @@ public static function get_flag_value( array $assoc_args, string $flag, mixed $d
3941
* The callback must take two array arguments: $args and $assoc_args.
4042
*
4143
* @param callable $callback
42-
* @param array $args
43-
* @param array $assoc_args
44+
* @param array<mixed> $args
45+
* @param array<string,mixed> $assoc_args
46+
* @param array<string,mixed> $site_query Optional. Query arguments for get_sites(). Default: ['deleted' => 0, 'number' => PHP_INT_MAX]
4447
*/
45-
public static function run_on_all_sites( callable $callback, array $args, array $assoc_args ) : void
48+
public static function run_on_all_sites(
49+
callable $callback,
50+
array $args,
51+
array $assoc_args,
52+
array $site_query = ['deleted' => 0, 'number' => PHP_INT_MAX]
53+
) : void
4654
{
4755
// Get all active sites.
48-
$sites = get_sites( array(
49-
'deleted' => 0,
50-
) );
56+
$sites = get_sites( $site_query );
57+
58+
if ( is_int( $sites ) ) {
59+
WP_CLI::error( "Wrong site query. Did you pass 'count' as a query var?" );
60+
die(); // Just in case.
61+
}
5162

5263
// Loop through all sites.
5364
foreach ( $sites as $site ) {

0 commit comments

Comments
 (0)