diff --git a/features/user.feature b/features/user.feature index de31288be..3ad9fcdfc 100644 --- a/features/user.feature +++ b/features/user.feature @@ -322,3 +322,44 @@ Feature: Manage WordPress users """ testuser4@example.com """ + + Scenario: Mark/remove a user from spam + Given a WP multisite install + And I run `wp user create bumblebee bbee@example.com --role=author --porcelain` + And save STDOUT as {BBEE_ID} + And I run `wp user create oprime oprime@example.com --role=author --porcelain` + And save STDOUT as {OP_ID} + And I run `wp user get bumblebee` + Then STDOUT should not be empty + And I run `wp user get oprime` + Then STDOUT should not be empty + + When I run `wp user spam {BBEE_ID}` + Then STDOUT should be: + """ + User {BBEE_ID} marked as spam. + Success: Spamed 1 of 1 users. + """ + + When I try the previous command again + Then STDERR should be: + """ + Warning: User {BBEE_ID} already marked as spam. + """ + And STDOUT should be: + """ + Success: User already spamed. + """ + + When I try `wp user spam {OP_ID} 9999` + Then STDOUT should be: + """ + User {OP_ID} marked as spam. + """ + And STDERR should be: + """ + Warning: Invalid user ID, email or login: '9999' + Warning: User 9999 doesn't exist. + Error: Only spamed 1 of 2 users. + """ + And the return code should be 1 diff --git a/src/User_Command.php b/src/User_Command.php index b65563109..312369d2c 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1,5 +1,7 @@ fetcher = new \WP_CLI\Fetchers\User; + $this->sitefetcher = new \WP_CLI\Fetchers\Site; } /** @@ -1017,4 +1020,107 @@ private static function wp_new_user_notification( $user_id, $password ) { } } + /** + * Mark one or more users as spam. + * + * ## OPTIONS + * + * ... + * : One or more IDs of users to mark as spam. + * + * ## EXAMPLES + * + * $ wp user spam 123 + * Success: User 123 marked as spam. + */ + public function spam( $args ) { + $this->update_msuser_status( $args, 'spam', '1' ); + } + + /** + * Mark one or more users as spam. + * + * ## OPTIONS + * + * ... + * : One or more IDs of users to mark as spam. + * + * ## EXAMPLES + * + * $ wp user unspam 123 + * Success: User 123 removed from spam. + */ + public function unspam( $args ) { + $this->update_msuser_status( $args, 'spam', '0' ); + } + + /** + * Common command for updating user data. + */ + private function update_msuser_status( $user_ids, $pref, $value ) { + + // If site is not multisite, then stop execution. + if ( ! is_multisite() ) { + WP_CLI::error( 'This is not a multisite install.' ); + } + + if ( 'spam' === $pref && '1' === $value ) { + $action = 'marked as spam'; + $verb = 'spam'; + } elseif ( 'spam' === $pref && '0' === $value ) { + $action = 'removed from spam'; + $verb = 'unspam'; + } + + $successes = $errors = 0; + $users = $this->fetcher->get_many( $user_ids ); + if ( count( $users ) < count( $user_ids ) ) { + $errors = count( $user_ids ) - count( $users ); + } + + foreach ( $user_ids as $user_id ) { + + $user = get_userdata( $user_id ); + + // If no user found, then show warning. + if ( empty( $user ) ) { + WP_CLI::warning( sprintf( 'User %d doesn\'t exist.', esc_html( $user_id ) ) ); + continue; + } + + // Super admin should not be marked as spam. + if ( is_super_admin( $user->ID ) ) { + WP_CLI::warning( sprintf( 'User cannot be modified. The user %d is a network administrator.', esc_html( $user->ID ) ) ); + continue; + } + + // Skip if user is already marked as spam and show warning. + if ( $value === $user->spam ) { + WP_CLI::warning( "User {$user_id} already {$action}." ); + continue; + } + + // Make that user's blog as spam too. + $blogs = get_blogs_of_user( $user_id, true ); + foreach ( (array) $blogs as $details ) { + $site = $this->sitefetcher->get_check( $details->site_id ); + + // Main blog shouldn't a spam ! + if ( $details->userblog_id != $site->blog_id ) { + update_blog_status( $details->userblog_id, $pref, $value ); + } + } + + // Set status and show message. + update_user_status( $user_id, $pref, $value ); + WP_CLI::log( "User {$user_id} {$action}." ); + $successes++; + } + + if ( ! $this->chained_command ) { + Utils\report_batch_operation_results( 'user', $verb, count( $user_ids ), $successes, $errors ); + } + + } + }