Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add filter site__user_in on wp site list
  • Loading branch information
marksabbath committed Nov 11, 2023
commit 5cfa326af47db102bb2c84e02d3251f9d05aa21f
20 changes: 20 additions & 0 deletions features/site.feature
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,26 @@ 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}
"""

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

Expand Down
18 changes: 17 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).
*
* [--site__user_in=<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 +610,19 @@ 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;
}
$append = 'ORDER BY FIELD( blog_id, ' . implode( ',', array_map( 'intval', $where['blog_id'] ) ) . ' )';
}
}

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