Skip to content

Commit

Permalink
Add filter site__user_in on wp site list (#438)
Browse files Browse the repository at this point in the history
* 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 <daniel@bachhuber.co>

* 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 <daniel@bachhuber.co>
Co-authored-by: Daniel Bachhuber <daniel.bachhuber@automattic.com>
  • Loading branch information
3 people committed Feb 6, 2024
1 parent 93eb93d commit 2b5d5d5
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Expand Up @@ -3542,7 +3542,7 @@ WP_CLI::add_hook( 'after_invoke:site empty', function(){
Lists all sites in a multisite installation.

~~~
wp site list [--network=<id>] [--<field>=<value>] [--site__in=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
wp site list [--network=<id>] [--<field>=<value>] [--site__in=<value>] [--site_user=<value>] [--field=<field>] [--fields=<fields>] [--format=<format>]
~~~

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

[--site_user=<value>]
Only list the sites with this user.

[--field=<field>]
Prints the value of a single field for each site.

Expand Down
37 changes: 37 additions & 0 deletions features/site.feature
Expand Up @@ -98,6 +98,43 @@ Feature: Manage sites in a multisite installation
{SCHEME}://example.com/first/
"""

Scenario: Filter site list by user
Given a WP multisite install

When I run `wp site create --slug=first --porcelain`
Then STDOUT should be a number
And save STDOUT as {SITE_ID}
And I run `wp site list --blog_id={SITE_ID} --field=url`
And save STDOUT as {SITE_URL}
And I run `wp user create newuser newuser@example.com --porcelain --url={SITE_URL}`
Then STDOUT should be a number
And save STDOUT as {USER_ID}
And I run `wp user get {USER_ID} --field=user_login`
And save STDOUT as {USER_LOGIN}

When I run `wp site list --field=url --site_user={USER_LOGIN}`
Then STDOUT should be:
"""
{SITE_URL}
"""

When I try `wp site list --site_user=invalid_user`
Then the return code should be 1
And STDERR should be:
"""
Error: Invalid user ID, email or login: 'invalid_user'
"""

When I run `wp user remove-role {USER_LOGIN} --url={SITE_URL}`
Then STDOUT should contain:
"""
Success: Removed
"""

When I run `wp site list --field=url --site_user={USER_LOGIN}`
Then STDOUT should be empty


Scenario: Delete a site by slug
Given a WP multisite install

Expand Down
24 changes: 24 additions & 0 deletions src/Site_Command.php
Expand Up @@ -7,6 +7,7 @@
use WP_CLI\Iterators\Table as TableIterator;
use WP_CLI\Utils;
use WP_CLI\Formatter;
use WP_CLI\Fetchers\User as UserFetcher;

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

if ( isset( $assoc_args['site_user'] ) ) {
$user = ( new UserFetcher() )->get_check( $assoc_args['site_user'] );

if ( $user ) {
$blogs = get_blogs_of_user( $user->ID );

foreach ( $blogs as $blog ) {
$where['blog_id'][] = $blog->userblog_id;
}
}

if ( ! isset( $where['blog_id'] ) || empty( $where['blog_id'] ) ) {
$formatter = new Formatter( $assoc_args, [], 'site' );
$formatter->display_items( [] );
return;
}

$append = 'ORDER BY FIELD( blog_id, ' . implode( ',', array_map( 'intval', $where['blog_id'] ) ) . ' )';
}

$iterator_args = [
'table' => $wpdb->blogs,
'where' => $where,
Expand Down

0 comments on commit 2b5d5d5

Please sign in to comment.