diff --git a/README.md b/README.md index b5a882ce..fa007184 100644 --- a/README.md +++ b/README.md @@ -1685,6 +1685,8 @@ Errors if the option already exists. Should this option be automatically loaded. --- options: + - 'on' + - 'off' - 'yes' - 'no' --- @@ -1990,6 +1992,8 @@ wp option update [] [--autoload=] [--format=] Requires WP 4.2. Should this option be automatically loaded. --- options: + - 'on' + - 'off' - 'yes' - 'no' --- @@ -2053,6 +2057,8 @@ wp option set-autoload Should this option be automatically loaded. --- options: + - 'on' + - 'off' - 'yes' - 'no' --- @@ -6316,10 +6322,12 @@ wp user signup # Activate signup. $ wp user signup activate 2 Success: Signup 2 activated. Password: bZFSGsfzb9xs + Success: Activated 1 of 1 signups. # Delete signup. $ wp user signup delete 3 Success: Signup 3 deleted. + Success: Deleted 1 of 1 signups. @@ -6343,6 +6351,7 @@ wp user signup activate ... # Activate signup. $ wp user signup activate 2 Success: Signup 2 activated. Password: bZFSGsfzb9xs + Success: Activated 1 of 1 signups. @@ -6351,19 +6360,23 @@ wp user signup activate ... Deletes one or more signups. ~~~ -wp user signup delete ... +wp user signup delete [...] [--all] ~~~ **OPTIONS** - ... + [...] The signup ID, user login, user email, or activation key of the signup(s) to delete. + [--all] + If set, all signups will be deleted. + **EXAMPLES** # Delete signup. $ wp user signup delete 3 Success: Signup 3 deleted. + Success: Deleted 1 of 1 signups. diff --git a/features/signup.feature b/features/signup.feature index 966615e0..eaa5da1b 100644 --- a/features/signup.feature +++ b/features/signup.feature @@ -156,10 +156,9 @@ Feature: Manage signups in a multisite installation """ When I run `wp user signup delete bobuser@example.com johnuser@example.com` - Then STDOUT should be: + Then STDOUT should contain: """ - Success: Signup 1 deleted. - Success: Signup 2 deleted. + Success: Deleted 2 of 2 signups. """ When I try `wp user signup get bobuser` @@ -167,3 +166,27 @@ Feature: Manage signups in a multisite installation """ Error: Invalid signup ID, email, login, or activation key: 'bobuser' """ + + Scenario: Delete all signups + Given a WP multisite install + And I run `wp eval 'wpmu_signup_user( "bobuser", "bobuser@example.com" );'` + And I run `wp eval 'wpmu_signup_user( "johnuser", "johnuser@example.com" );'` + + When I try `wp user signup delete` + Then STDERR should be: + """ + Error: You need to specify either one or more signups or provide the --all flag. + """ + + When I run `wp user signup delete --all` + Then STDOUT should contain: + """ + Success: Deleted all signups. + """ + + When I run `wp user signup list --format=count` + Then STDOUT should be: + """ + 0 + """ + diff --git a/src/Signup_Command.php b/src/Signup_Command.php index b7838fde..89c8f023 100644 --- a/src/Signup_Command.php +++ b/src/Signup_Command.php @@ -21,10 +21,12 @@ * # Activate signup. * $ wp user signup activate 2 * Success: Signup 2 activated. Password: bZFSGsfzb9xs + * Success: Activated 1 of 1 signups. * * # Delete signup. * $ wp user signup delete 3 * Success: Signup 3 deleted. + * Success: Deleted 1 of 1 signups. * * @package wp-cli */ @@ -210,6 +212,7 @@ public function get( $args, $assoc_args ) { * # Activate signup. * $ wp user signup activate 2 * Success: Signup 2 activated. Password: bZFSGsfzb9xs + * Success: Activated 1 of 1 signups. * * @package wp-cli */ @@ -239,41 +242,83 @@ public function activate( $args, $assoc_args ) { * * ## OPTIONS * - * ... + * [...] * : The signup ID, user login, user email, or activation key of the signup(s) to delete. * + * [--all] + * : If set, all signups will be deleted. + * * ## EXAMPLES * * # Delete signup. * $ wp user signup delete 3 * Success: Signup 3 deleted. + * Success: Deleted 1 of 1 signups. * * @package wp-cli */ public function delete( $args, $assoc_args ) { + $count = count( $args ); + + $all = Utils\get_flag_value( $assoc_args, 'all', false ); + + if ( ( 0 < $count && true === $all ) || ( 0 === $count && true !== $all ) ) { + WP_CLI::error( 'You need to specify either one or more signups or provide the --all flag.' ); + } + + if ( true === $all ) { + if ( ! $this->delete_all_signups() ) { + WP_CLI::error( 'Error deleting signups.' ); + } + + WP_CLI::success( 'Deleted all signups.' ); + WP_CLI::halt( 0 ); + } + $signups = $this->fetcher->get_many( $args ); - parent::_delete( $signups, $assoc_args, [ $this, 'delete_callback' ] ); + $successes = 0; + $errors = 0; + + foreach ( $signups as $signup ) { + if ( $this->delete_signup( $signup ) ) { + WP_CLI::success( "Signup {$signup->signup_id} deleted." ); + ++$successes; + } else { + WP_CLI::error( "Failed deleting signup {$signup->signup_id}." ); + ++$errors; + } + } + + Utils\report_batch_operation_results( 'signup', 'delete', $count, $successes, $errors ); } /** - * Callback used to delete a signup. + * Deletes signup. * - * @param $signup - * @param $assoc_args - * @return array + * @param stdClasss $signup + * @return bool True if success; otherwise false. */ - protected function delete_callback( $signup, $assoc_args ) { + private function delete_signup( $signup ) { global $wpdb; $signup_id = $signup->signup_id; $result = $wpdb->delete( $wpdb->signups, array( 'signup_id' => $signup_id ), array( '%d' ) ); - if ( $result ) { - return [ 'success', "Signup {$signup_id} deleted." ]; - } else { - return [ 'error', "Failed deleting signup {$signup_id}." ]; - } + return $result ? true : false; + } + + /** + * Deletes all signup. + * + * @return bool True if success; otherwise false. + */ + private function delete_all_signups() { + global $wpdb; + + $results = $wpdb->query( 'DELETE FROM ' . $wpdb->signups ); + + return $results ? true : false; } }