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
27 changes: 27 additions & 0 deletions features/site.feature
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,33 @@ 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 --site__in={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_in={USER_LOGIN}`
Then STDOUT should be:
"""
{SITE_URL}
"""

When I try `wp site list --site__user_in=invalid_user`
Then the return code should be 1
And STDERR should be:
"""
Error: Could not find a site with the user provided.
"""

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

Expand Down
23 changes: 22 additions & 1 deletion src/Site_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -520,7 +520,10 @@ private function get_network( $network_id ) {
* 'url' isn't an available filter, as it comes from 'home' in wp_options.
*
* [--site__in=<value>]
* : Only list the sites with these blog_id values (comma-separated).
* : Only list 1 the sites with these blog_id values (comma-separated).
marksabbath marked this conversation as resolved.
Show resolved Hide resolved
*
* [--site__user_in=<value>]
marksabbath marked this conversation as resolved.
Show resolved Hide resolved
* : 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 +610,24 @@ public function list_( $args, $assoc_args ) {
$where['site_id'] = $assoc_args['network'];
}

if ( isset( $assoc_args['site__user_in'] ) ) {
$user = get_user_by( 'login', $assoc_args['site__user_in'] );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_in implies the argument supports a CSV of users. Should we accommodate that here?

Also, we should use WP_CLI\Fetchers\User instead of directly calling get_user_by(). It will handle the "invalid user" case for us.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Il'll take a look at WP_CLI\Fetchers\User.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the code to use WP_CLI\Fetchers\User and also updated the name of the flag to --user.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber I needed to change the flag to site_user since it was not properly working with user. I guess it conflicts with some global flag or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed to change the flag to site_user since it was not properly working with user. I guess it conflicts with some global flag or something.

@marksabbath Yep, it does. site_user is a fine alternative.


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

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

if ( ! isset( $where['blog_id'] ) ) {
WP_CLI::error( 'Could not find a site with the user provided.' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should present an empty list here, instead of erroring.

}

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

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