Skip to content

Commit 2b5d5d5

Browse files
Add filter site__user_in on wp site list (#438)
* Add filter site__user_in on wp site list * Return error if cannot find a site * Update src/Site_Command.php Co-authored-by: Daniel Bachhuber <[email protected]> * Change filter to user__in * Update flag name and output blank list when none found * Fix spaces in = * Update flag name to site_user * Add a test for the zero sites state * Update README --------- Co-authored-by: Daniel Bachhuber <[email protected]> Co-authored-by: Daniel Bachhuber <[email protected]>
1 parent 93eb93d commit 2b5d5d5

File tree

3 files changed

+65
-1
lines changed

3 files changed

+65
-1
lines changed

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3542,7 +3542,7 @@ WP_CLI::add_hook( 'after_invoke:site empty', function(){
35423542
Lists all sites in a multisite installation.
35433543

35443544
~~~
3545-
wp site list [--network=<id>] [--<field>=<value>] [--site__in=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
3545+
wp site list [--network=<id>] [--<field>=<value>] [--site__in=<value>] [--site_user=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
35463546
~~~
35473547

35483548
**OPTIONS**
@@ -3557,6 +3557,9 @@ wp site list [--network=<id>] [--<field>=<value>] [--site__in=<value>] [--field=
35573557
[--site__in=<value>]
35583558
Only list the sites with these blog_id values (comma-separated).
35593559

3560+
[--site_user=<value>]
3561+
Only list the sites with this user.
3562+
35603563
[--field=<field>]
35613564
Prints the value of a single field for each site.
35623565

features/site.feature

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,43 @@ Feature: Manage sites in a multisite installation
9898
{SCHEME}://example.com/first/
9999
"""
100100

101+
Scenario: Filter site list by user
102+
Given a WP multisite install
103+
104+
When I run `wp site create --slug=first --porcelain`
105+
Then STDOUT should be a number
106+
And save STDOUT as {SITE_ID}
107+
And I run `wp site list --blog_id={SITE_ID} --field=url`
108+
And save STDOUT as {SITE_URL}
109+
And I run `wp user create newuser newuser@example.com --porcelain --url={SITE_URL}`
110+
Then STDOUT should be a number
111+
And save STDOUT as {USER_ID}
112+
And I run `wp user get {USER_ID} --field=user_login`
113+
And save STDOUT as {USER_LOGIN}
114+
115+
When I run `wp site list --field=url --site_user={USER_LOGIN}`
116+
Then STDOUT should be:
117+
"""
118+
{SITE_URL}
119+
"""
120+
121+
When I try `wp site list --site_user=invalid_user`
122+
Then the return code should be 1
123+
And STDERR should be:
124+
"""
125+
Error: Invalid user ID, email or login: 'invalid_user'
126+
"""
127+
128+
When I run `wp user remove-role {USER_LOGIN} --url={SITE_URL}`
129+
Then STDOUT should contain:
130+
"""
131+
Success: Removed
132+
"""
133+
134+
When I run `wp site list --field=url --site_user={USER_LOGIN}`
135+
Then STDOUT should be empty
136+
137+
101138
Scenario: Delete a site by slug
102139
Given a WP multisite install
103140

src/Site_Command.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use WP_CLI\Iterators\Table as TableIterator;
88
use WP_CLI\Utils;
99
use WP_CLI\Formatter;
10+
use WP_CLI\Fetchers\User as UserFetcher;
1011

1112
/**
1213
* Creates, deletes, empties, moderates, and lists one or more sites on a multisite installation.
@@ -526,6 +527,9 @@ private function get_network( $network_id ) {
526527
* [--site__in=<value>]
527528
* : Only list the sites with these blog_id values (comma-separated).
528529
*
530+
* [--site_user=<value>]
531+
* : Only list the sites with this user.
532+
*
529533
* [--field=<field>]
530534
* : Prints the value of a single field for each site.
531535
*
@@ -611,6 +615,26 @@ public function list_( $args, $assoc_args ) {
611615
$where['site_id'] = $assoc_args['network'];
612616
}
613617

618+
if ( isset( $assoc_args['site_user'] ) ) {
619+
$user = ( new UserFetcher() )->get_check( $assoc_args['site_user'] );
620+
621+
if ( $user ) {
622+
$blogs = get_blogs_of_user( $user->ID );
623+
624+
foreach ( $blogs as $blog ) {
625+
$where['blog_id'][] = $blog->userblog_id;
626+
}
627+
}
628+
629+
if ( ! isset( $where['blog_id'] ) || empty( $where['blog_id'] ) ) {
630+
$formatter = new Formatter( $assoc_args, [], 'site' );
631+
$formatter->display_items( [] );
632+
return;
633+
}
634+
635+
$append = 'ORDER BY FIELD( blog_id, ' . implode( ',', array_map( 'intval', $where['blog_id'] ) ) . ' )';
636+
}
637+
614638
$iterator_args = [
615639
'table' => $wpdb->blogs,
616640
'where' => $where,

0 commit comments

Comments
 (0)