From 50555e99cf5c94f821d281b6ff6739fecbb61f1e Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 00:13:11 +0530 Subject: [PATCH 01/13] Add command for user marked as a spam Command: - wp user spam 123 --- src/User_Command.php | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/User_Command.php b/src/User_Command.php index b65563109..c4ab79e9d 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1017,4 +1017,44 @@ private static function wp_new_user_notification( $user_id, $password ) { } } + /** + * Mark as spam one or more users. + * + * ## 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, $assoc_args ) { + global $wpdb; + $user_ids = $args; + + foreach ( $user_ids as $user_id ) { + + $add_spam = $wpdb->update( + $wpdb->users, + array( + 'spam' => 1, + ), + array( + 'ID' => $user_id, + ), + array( '%d' ), + array( '%d' ) + ); + + if ( ! is_wp_error( $add_spam ) ) { + WP_CLI::success( "User $user_id marked as spam." ); + } else { + WP_CLI::error( "There was an error, probably that user doesn't exist." ); + } + } + + } + } From e6ad447ea88c52bae20b02759b78c9ff7c59f6cc Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 01:06:22 +0530 Subject: [PATCH 02/13] Add featured test for setting user as spam --- features/user.feature | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/features/user.feature b/features/user.feature index de31288be..5143098cb 100644 --- a/features/user.feature +++ b/features/user.feature @@ -322,3 +322,18 @@ Feature: Manage WordPress users """ testuser4@example.com """ + + Scenario: Set user as spam + Given a WP install + + When I run `wp user create bobjones bob@example.com --role=author --porcelain` + And save STDOUT as {BOB_ID} + + When I run `wp user get bobjones` + Then STDOUT should not be empty + + When I run `wp user spam {BOB_ID}` + Then STDOUT should be: + """ + Success: User {BOB_ID} marked as spam. + """ From 0c695f4df262e11b9cfd53eb296c795d148ad70b Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 01:33:04 +0530 Subject: [PATCH 03/13] Add some validations while reporting user as spam Validations: - If user is super-user, then don't set as spam - Show warning if user is already reported as spam --- src/User_Command.php | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/src/User_Command.php b/src/User_Command.php index c4ab79e9d..8e11b6eca 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1036,6 +1036,19 @@ public function spam( $args, $assoc_args ) { foreach ( $user_ids as $user_id ) { + // Do not mark super user as spam. + if ( is_super_admin( $user_id ) ) { + WP_CLI::warning( "Sorry!! Super-user can not be set as spam." ); + continue; + } + + // Show warning if user is already marked as spam. + if ( $this->is_user_as_sapm( $user_id ) ) { + WP_CLI::warning( "User {$user_id} already marked as spam." ); + continue; + } + + // Set user as spam. $add_spam = $wpdb->update( $wpdb->users, array( @@ -1052,9 +1065,27 @@ public function spam( $args, $assoc_args ) { WP_CLI::success( "User $user_id marked as spam." ); } else { WP_CLI::error( "There was an error, probably that user doesn't exist." ); + continue; } } } + /** + * Check if user is already set as spam or not. + * + * @param int $user_id User ID. + * @return bool Return true if user is already reported as spam. + */ + public function is_user_as_sapm( $user_id = 0 ) { + if ( 0 === $user_id ) { + WP_CLI::error( "There was an error, probably that user doesn't exist." ); + } + + global $wpdb; + $is_user_spam = $wpdb->get_var( $wpdb->prepare( "SELECT `spam` FROM {$wpdb->users} WHERE `ID` = %d LIMIT 0, 1", $user_id ) ); + + return ( '0' !== $is_user_spam ) ? true : false; + } + } From 8ca485b0c27811eee23ff642e3ebc3a4bc416d0a Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 13:59:37 +0530 Subject: [PATCH 04/13] Use 'update_user_status' instead of sql queries --- src/User_Command.php | 65 ++++++++++++++++++-------------------------- 1 file changed, 26 insertions(+), 39 deletions(-) diff --git a/src/User_Command.php b/src/User_Command.php index 8e11b6eca..c74db5473 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1018,7 +1018,7 @@ private static function wp_new_user_notification( $user_id, $password ) { } /** - * Mark as spam one or more users. + * Mark one or more users as spam. * * ## OPTIONS * @@ -1036,56 +1036,43 @@ public function spam( $args, $assoc_args ) { foreach ( $user_ids as $user_id ) { - // Do not mark super user as spam. - if ( is_super_admin( $user_id ) ) { - WP_CLI::warning( "Sorry!! Super-user can not be set as spam." ); + $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; } - // Show warning if user is already marked as spam. - if ( $this->is_user_as_sapm( $user_id ) ) { - WP_CLI::warning( "User {$user_id} already marked as spam." ); + // 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; } - // Set user as spam. - $add_spam = $wpdb->update( - $wpdb->users, - array( - 'spam' => 1, - ), - array( - 'ID' => $user_id, - ), - array( '%d' ), - array( '%d' ) - ); - - if ( ! is_wp_error( $add_spam ) ) { - WP_CLI::success( "User $user_id marked as spam." ); - } else { - WP_CLI::error( "There was an error, probably that user doesn't exist." ); + // Skip if user is already marked as spam and show warning. + if ( '1' === $user->spam ) { + WP_CLI::warning( "User {$user_id} already marked as spam." ); continue; } - } - } + // Make that user's blog as spam too. + $blogs = get_blogs_of_user( $user_id, true ); + foreach ( (array) $blogs as $details ) { - /** - * Check if user is already set as spam or not. - * - * @param int $user_id User ID. - * @return bool Return true if user is already reported as spam. - */ - public function is_user_as_sapm( $user_id = 0 ) { - if ( 0 === $user_id ) { - WP_CLI::error( "There was an error, probably that user doesn't exist." ); - } + // Main blog shouldn't a spam ! + if ( $details->userblog_id != get_network()->site_id ) { + update_blog_status( $details->userblog_id, 'spam', '1' ); + } + } - global $wpdb; - $is_user_spam = $wpdb->get_var( $wpdb->prepare( "SELECT `spam` FROM {$wpdb->users} WHERE `ID` = %d LIMIT 0, 1", $user_id ) ); + // Set status and show message. + if ( update_user_status( $user_id, 'spam', '1' ) ) { + WP_CLI::success( "User {$user_id} marked as spam." ); + } + + } - return ( '0' !== $is_user_spam ) ? true : false; } } From 9244ffa31c0131e99297690d940915bb739298e5 Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 14:07:30 +0530 Subject: [PATCH 05/13] Update feature test for user spam command --- features/user.feature | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/features/user.feature b/features/user.feature index 5143098cb..3bc7d33e0 100644 --- a/features/user.feature +++ b/features/user.feature @@ -326,14 +326,20 @@ Feature: Manage WordPress users Scenario: Set user as spam Given a WP install - When I run `wp user create bobjones bob@example.com --role=author --porcelain` - And save STDOUT as {BOB_ID} + When I run `wp user create bumblebee bbee@example.com --role=author --porcelain` + And save STDOUT as {BBEE_ID} - When I run `wp user get bobjones` + When I run `wp user get bumblebee` Then STDOUT should not be empty - When I run `wp user spam {BOB_ID}` + When I run `wp user spam {BBEE_ID}` + Then STDOUT should be: + """ + Success: User {BBEE_ID} marked as spam. + """ + + When I try the previous command again Then STDOUT should be: """ - Success: User {BOB_ID} marked as spam. + Warning: User {BBEE_ID} already marked as spam. """ From 79cb3892964fe7e73f6598fd1a700be37531d647 Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 14:56:02 +0530 Subject: [PATCH 06/13] Update feature test as spam command is for multisite --- features/user.feature | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/features/user.feature b/features/user.feature index 3bc7d33e0..d2256b534 100644 --- a/features/user.feature +++ b/features/user.feature @@ -324,7 +324,7 @@ Feature: Manage WordPress users """ Scenario: Set user as spam - Given a WP install + Given a WP multisite install When I run `wp user create bumblebee bbee@example.com --role=author --porcelain` And save STDOUT as {BBEE_ID} @@ -337,9 +337,3 @@ Feature: Manage WordPress users """ Success: User {BBEE_ID} marked as spam. """ - - When I try the previous command again - Then STDOUT should be: - """ - Warning: User {BBEE_ID} already marked as spam. - """ From 4c0e3f9629e4365e1ebfe33b780902f1e59a1e2f Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 15:13:35 +0530 Subject: [PATCH 07/13] Prevent command for single site and string improvements --- src/User_Command.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/User_Command.php b/src/User_Command.php index c74db5473..3b8127cdd 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1034,19 +1034,24 @@ public function spam( $args, $assoc_args ) { global $wpdb; $user_ids = $args; + // If site is not multisite, then stop execution. + if ( ! is_multisite() ) { + WP_CLI::error( 'Sorry! this command is for multisite only.' ); + } + 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 ) ) ); + 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 ) ) ); + WP_CLI::warning( sprintf( 'User cannot be modified. The user %d is a network administrator.', esc_html( $user->ID ) ) ); continue; } From 6f23d56889a320a3a4384c801bf7a5ddb0da3140 Mon Sep 17 00:00:00 2001 From: bunty Date: Sun, 3 Sep 2017 18:19:19 +0530 Subject: [PATCH 08/13] Add test for already spammed user --- features/user.feature | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/features/user.feature b/features/user.feature index d2256b534..18c61eb0d 100644 --- a/features/user.feature +++ b/features/user.feature @@ -325,11 +325,9 @@ Feature: Manage WordPress users Scenario: Set user as spam Given a WP multisite install - - When I run `wp user create bumblebee bbee@example.com --role=author --porcelain` + And I run `wp user create bumblebee bbee@example.com --role=author --porcelain` And save STDOUT as {BBEE_ID} - - When I run `wp user get bumblebee` + And I run `wp user get bumblebee` Then STDOUT should not be empty When I run `wp user spam {BBEE_ID}` @@ -337,3 +335,9 @@ Feature: Manage WordPress users """ Success: User {BBEE_ID} marked as spam. """ + + When I try the previous command again + Then STDERR should be: + """ + Warning: User {BBEE_ID} already marked as spam. + """ From d5146e011434d598539e722f8345d5521678bcc2 Mon Sep 17 00:00:00 2001 From: bunty Date: Tue, 5 Sep 2017 01:34:25 +0530 Subject: [PATCH 09/13] Used fetcher for getting network info --- src/User_Command.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/User_Command.php b/src/User_Command.php index 3b8127cdd..0e5d925d8 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -42,6 +42,7 @@ class User_Command extends \WP_CLI\CommandWithDBObject { public function __construct() { $this->fetcher = new \WP_CLI\Fetchers\User; + $this->sitefetcher = new \WP_CLI\Fetchers\Site; } /** @@ -1064,9 +1065,10 @@ public function spam( $args, $assoc_args ) { // 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 != get_network()->site_id ) { + if ( $details->userblog_id != $site->blog_id ) { update_blog_status( $details->userblog_id, 'spam', '1' ); } } From 1f16271fe1fb99767e3cf3a2f4aee0ed12054771 Mon Sep 17 00:00:00 2001 From: bunty Date: Thu, 7 Sep 2017 00:03:47 +0530 Subject: [PATCH 10/13] Add unspam user command - made a common function for updating user data --- src/User_Command.php | 46 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/src/User_Command.php b/src/User_Command.php index 0e5d925d8..286122829 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1031,15 +1031,43 @@ private static function wp_new_user_notification( $user_id, $password ) { * $ wp user spam 123 * Success: User 123 marked as spam. */ - public function spam( $args, $assoc_args ) { - global $wpdb; - $user_ids = $args; + 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( 'Sorry! this command is for multisite only.' ); } + if ( 'spam' === $pref && '1' === $value ) { + $action = 'marked as spam'; + } elseif ( 'spam' === $pref && '0' === $value ) { + $action = 'removed from spam'; + } + foreach ( $user_ids as $user_id ) { $user = get_userdata( $user_id ); @@ -1057,8 +1085,8 @@ public function spam( $args, $assoc_args ) { } // Skip if user is already marked as spam and show warning. - if ( '1' === $user->spam ) { - WP_CLI::warning( "User {$user_id} already marked as spam." ); + if ( $value === $user->spam ) { + WP_CLI::warning( "User {$user_id} already {$action}." ); continue; } @@ -1069,15 +1097,13 @@ public function spam( $args, $assoc_args ) { // Main blog shouldn't a spam ! if ( $details->userblog_id != $site->blog_id ) { - update_blog_status( $details->userblog_id, 'spam', '1' ); + update_blog_status( $details->userblog_id, $pref, $value ); } } // Set status and show message. - if ( update_user_status( $user_id, 'spam', '1' ) ) { - WP_CLI::success( "User {$user_id} marked as spam." ); - } - + update_user_status( $user_id, $pref, $value ); + WP_CLI::success( "User {$user_id} {$action}." ); } } From 9494c99c26a5d76e6f60d581c0327a7e3f24b56f Mon Sep 17 00:00:00 2001 From: bunty Date: Thu, 7 Sep 2017 02:16:46 +0530 Subject: [PATCH 11/13] Add status code for result --- features/user.feature | 11 +++++++++-- src/User_Command.php | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/features/user.feature b/features/user.feature index 18c61eb0d..98557265f 100644 --- a/features/user.feature +++ b/features/user.feature @@ -327,17 +327,24 @@ Feature: Manage WordPress users 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}` + When I run `wp user spam {BBEE_ID} {OP_ID}` Then STDOUT should be: """ - Success: User {BBEE_ID} marked as spam. + User {BBEE_ID} marked as spam. + User {OP_ID} marked as spam. + Success: Spamed 2 of 2 users. """ When I try the previous command again Then STDERR should be: """ Warning: User {BBEE_ID} already marked as spam. + Warning: User {OP_ID} already marked as spam. """ diff --git a/src/User_Command.php b/src/User_Command.php index 286122829..0a6f34ae6 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1,5 +1,7 @@ fetcher->get_many( $user_ids ); + if ( count( $users ) < count( $user_ids ) ) { + $errors = count( $user_ids ) - count( $users ); } foreach ( $user_ids as $user_id ) { @@ -1103,7 +1113,12 @@ private function update_msuser_status( $user_ids, $pref, $value ) { // Set status and show message. update_user_status( $user_id, $pref, $value ); - WP_CLI::success( "User {$user_id} {$action}." ); + WP_CLI::log( "User {$user_id} {$action}." ); + $successes++; + } + + if ( ! $this->chained_command ) { + Utils\report_batch_operation_results( 'user', $verb, count( $user_ids ), $successes, $errors ); } } From dab921be7b7a96ab50ced2df67aee5c51ea81c76 Mon Sep 17 00:00:00 2001 From: bunty Date: Fri, 8 Sep 2017 08:31:26 +0530 Subject: [PATCH 12/13] Add test for non-exist user --- features/user.feature | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/features/user.feature b/features/user.feature index 98557265f..3ad9fcdfc 100644 --- a/features/user.feature +++ b/features/user.feature @@ -323,7 +323,7 @@ Feature: Manage WordPress users testuser4@example.com """ - Scenario: Set user as spam + 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} @@ -334,17 +334,32 @@ Feature: Manage WordPress users And I run `wp user get oprime` Then STDOUT should not be empty - When I run `wp user spam {BBEE_ID} {OP_ID}` + When I run `wp user spam {BBEE_ID}` Then STDOUT should be: """ User {BBEE_ID} marked as spam. - User {OP_ID} marked as spam. - Success: Spamed 2 of 2 users. + 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. - Warning: User {OP_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 From 331509b03fc1b18bf84b959987215d9c875ca7b7 Mon Sep 17 00:00:00 2001 From: bunty Date: Fri, 8 Sep 2017 19:58:17 +0530 Subject: [PATCH 13/13] Change error message --- src/User_Command.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/User_Command.php b/src/User_Command.php index 0a6f34ae6..312369d2c 100644 --- a/src/User_Command.php +++ b/src/User_Command.php @@ -1061,7 +1061,7 @@ private function update_msuser_status( $user_ids, $pref, $value ) { // If site is not multisite, then stop execution. if ( ! is_multisite() ) { - WP_CLI::error( 'Sorry! this command is for multisite only.' ); + WP_CLI::error( 'This is not a multisite install.' ); } if ( 'spam' === $pref && '1' === $value ) {