Skip to content

Commit

Permalink
Implement --all in signup delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
ernilambar committed Apr 26, 2024
1 parent b266645 commit 0d2e74a
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 17 deletions.
17 changes: 15 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1685,6 +1685,8 @@ Errors if the option already exists.
Should this option be automatically loaded.
---
options:
- 'on'
- 'off'
- 'yes'
- 'no'
---
Expand Down Expand Up @@ -1990,6 +1992,8 @@ wp option update <key> [<value>] [--autoload=<autoload>] [--format=<format>]
Requires WP 4.2. Should this option be automatically loaded.
---
options:
- 'on'
- 'off'
- 'yes'
- 'no'
---
Expand Down Expand Up @@ -2053,6 +2057,8 @@ wp option set-autoload <key> <autoload>
Should this option be automatically loaded.
---
options:
- 'on'
- 'off'
- 'yes'
- 'no'
---
Expand Down Expand Up @@ -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.



Expand All @@ -6343,6 +6351,7 @@ wp user signup activate <signup>...
# Activate signup.
$ wp user signup activate 2
Success: Signup 2 activated. Password: bZFSGsfzb9xs
Success: Activated 1 of 1 signups.



Expand All @@ -6351,19 +6360,23 @@ wp user signup activate <signup>...
Deletes one or more signups.

~~~
wp user signup delete <signup>...
wp user signup delete [<signup>...] [--all]
~~~

**OPTIONS**

<signup>...
[<signup>...]
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.



Expand Down
29 changes: 26 additions & 3 deletions features/signup.feature
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,37 @@ 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`
Then STDERR should be:
"""
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", "[email protected]" );'`
And I run `wp eval 'wpmu_signup_user( "johnuser", "[email protected]" );'`

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
"""

69 changes: 57 additions & 12 deletions src/Signup_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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
*/
Expand Down Expand Up @@ -239,41 +242,83 @@ public function activate( $args, $assoc_args ) {
*
* ## OPTIONS
*
* <signup>...
* [<signup>...]
* : 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;
}
}

0 comments on commit 0d2e74a

Please sign in to comment.