Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add filter site__user_in on wp site list #438

Merged
merged 9 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3483,7 +3483,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 @@ -3498,6 +3498,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 @@ -522,6 +523,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 @@ -607,6 +611,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