diff --git a/README.md b/README.md index 4408c743..e3965afd 100644 --- a/README.md +++ b/README.md @@ -3542,7 +3542,7 @@ WP_CLI::add_hook( 'after_invoke:site empty', function(){ Lists all sites in a multisite installation. ~~~ -wp site list [--network=] [--=] [--site__in=] [--field=] [--fields=] [--format=] +wp site list [--network=] [--=] [--site__in=] [--site_user=] [--field=] [--fields=] [--format=] ~~~ **OPTIONS** @@ -3557,6 +3557,9 @@ wp site list [--network=] [--=] [--site__in=] [--field= [--site__in=] Only list the sites with these blog_id values (comma-separated). + [--site_user=] + Only list the sites with this user. + [--field=] Prints the value of a single field for each site. diff --git a/features/site.feature b/features/site.feature index 2d44f286..1c85ec18 100644 --- a/features/site.feature +++ b/features/site.feature @@ -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 diff --git a/src/Site_Command.php b/src/Site_Command.php index 4379b7a2..247d09df 100644 --- a/src/Site_Command.php +++ b/src/Site_Command.php @@ -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. @@ -526,6 +527,9 @@ private function get_network( $network_id ) { * [--site__in=] * : Only list the sites with these blog_id values (comma-separated). * + * [--site_user=] + * : Only list the sites with this user. + * * [--field=] * : Prints the value of a single field for each site. * @@ -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,