Skip to content

Commit 0d2e74a

Browse files
committed
Implement --all in signup delete command
1 parent b266645 commit 0d2e74a

File tree

3 files changed

+98
-17
lines changed

3 files changed

+98
-17
lines changed

README.md

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,8 @@ Errors if the option already exists.
16851685
Should this option be automatically loaded.
16861686
---
16871687
options:
1688+
- 'on'
1689+
- 'off'
16881690
- 'yes'
16891691
- 'no'
16901692
---
@@ -1990,6 +1992,8 @@ wp option update <key> [<value>] [--autoload=<autoload>] [--format=<format>]
19901992
Requires WP 4.2. Should this option be automatically loaded.
19911993
---
19921994
options:
1995+
- 'on'
1996+
- 'off'
19931997
- 'yes'
19941998
- 'no'
19951999
---
@@ -2053,6 +2057,8 @@ wp option set-autoload <key> <autoload>
20532057
Should this option be automatically loaded.
20542058
---
20552059
options:
2060+
- 'on'
2061+
- 'off'
20562062
- 'yes'
20572063
- 'no'
20582064
---
@@ -6316,10 +6322,12 @@ wp user signup
63166322
# Activate signup.
63176323
$ wp user signup activate 2
63186324
Success: Signup 2 activated. Password: bZFSGsfzb9xs
6325+
Success: Activated 1 of 1 signups.
63196326

63206327
# Delete signup.
63216328
$ wp user signup delete 3
63226329
Success: Signup 3 deleted.
6330+
Success: Deleted 1 of 1 signups.
63236331

63246332

63256333

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

63476356

63486357

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

63536362
~~~
6354-
wp user signup delete <signup>...
6363+
wp user signup delete [<signup>...] [--all]
63556364
~~~
63566365

63576366
**OPTIONS**
63586367

6359-
<signup>...
6368+
[<signup>...]
63606369
The signup ID, user login, user email, or activation key of the signup(s) to delete.
63616370

6371+
[--all]
6372+
If set, all signups will be deleted.
6373+
63626374
**EXAMPLES**
63636375

63646376
# Delete signup.
63656377
$ wp user signup delete 3
63666378
Success: Signup 3 deleted.
6379+
Success: Deleted 1 of 1 signups.
63676380

63686381

63696382

features/signup.feature

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,37 @@ Feature: Manage signups in a multisite installation
156156
"""
157157

158158
When I run `wp user signup delete bobuser@example.com johnuser@example.com`
159-
Then STDOUT should be:
159+
Then STDOUT should contain:
160160
"""
161-
Success: Signup 1 deleted.
162-
Success: Signup 2 deleted.
161+
Success: Deleted 2 of 2 signups.
163162
"""
164163

165164
When I try `wp user signup get bobuser`
166165
Then STDERR should be:
167166
"""
168167
Error: Invalid signup ID, email, login, or activation key: 'bobuser'
169168
"""
169+
170+
Scenario: Delete all signups
171+
Given a WP multisite install
172+
And I run `wp eval 'wpmu_signup_user( "bobuser", "[email protected]" );'`
173+
And I run `wp eval 'wpmu_signup_user( "johnuser", "[email protected]" );'`
174+
175+
When I try `wp user signup delete`
176+
Then STDERR should be:
177+
"""
178+
Error: You need to specify either one or more signups or provide the --all flag.
179+
"""
180+
181+
When I run `wp user signup delete --all`
182+
Then STDOUT should contain:
183+
"""
184+
Success: Deleted all signups.
185+
"""
186+
187+
When I run `wp user signup list --format=count`
188+
Then STDOUT should be:
189+
"""
190+
0
191+
"""
192+

src/Signup_Command.php

Lines changed: 57 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,12 @@
2121
* # Activate signup.
2222
* $ wp user signup activate 2
2323
* Success: Signup 2 activated. Password: bZFSGsfzb9xs
24+
* Success: Activated 1 of 1 signups.
2425
*
2526
* # Delete signup.
2627
* $ wp user signup delete 3
2728
* Success: Signup 3 deleted.
29+
* Success: Deleted 1 of 1 signups.
2830
*
2931
* @package wp-cli
3032
*/
@@ -210,6 +212,7 @@ public function get( $args, $assoc_args ) {
210212
* # Activate signup.
211213
* $ wp user signup activate 2
212214
* Success: Signup 2 activated. Password: bZFSGsfzb9xs
215+
* Success: Activated 1 of 1 signups.
213216
*
214217
* @package wp-cli
215218
*/
@@ -239,41 +242,83 @@ public function activate( $args, $assoc_args ) {
239242
*
240243
* ## OPTIONS
241244
*
242-
* <signup>...
245+
* [<signup>...]
243246
* : The signup ID, user login, user email, or activation key of the signup(s) to delete.
244247
*
248+
* [--all]
249+
* : If set, all signups will be deleted.
250+
*
245251
* ## EXAMPLES
246252
*
247253
* # Delete signup.
248254
* $ wp user signup delete 3
249255
* Success: Signup 3 deleted.
256+
* Success: Deleted 1 of 1 signups.
250257
*
251258
* @package wp-cli
252259
*/
253260
public function delete( $args, $assoc_args ) {
261+
$count = count( $args );
262+
263+
$all = Utils\get_flag_value( $assoc_args, 'all', false );
264+
265+
if ( ( 0 < $count && true === $all ) || ( 0 === $count && true !== $all ) ) {
266+
WP_CLI::error( 'You need to specify either one or more signups or provide the --all flag.' );
267+
}
268+
269+
if ( true === $all ) {
270+
if ( ! $this->delete_all_signups() ) {
271+
WP_CLI::error( 'Error deleting signups.' );
272+
}
273+
274+
WP_CLI::success( 'Deleted all signups.' );
275+
WP_CLI::halt( 0 );
276+
}
277+
254278
$signups = $this->fetcher->get_many( $args );
255279

256-
parent::_delete( $signups, $assoc_args, [ $this, 'delete_callback' ] );
280+
$successes = 0;
281+
$errors = 0;
282+
283+
foreach ( $signups as $signup ) {
284+
if ( $this->delete_signup( $signup ) ) {
285+
WP_CLI::success( "Signup {$signup->signup_id} deleted." );
286+
++$successes;
287+
} else {
288+
WP_CLI::error( "Failed deleting signup {$signup->signup_id}." );
289+
++$errors;
290+
}
291+
}
292+
293+
Utils\report_batch_operation_results( 'signup', 'delete', $count, $successes, $errors );
257294
}
258295

259296
/**
260-
* Callback used to delete a signup.
297+
* Deletes signup.
261298
*
262-
* @param $signup
263-
* @param $assoc_args
264-
* @return array
299+
* @param stdClasss $signup
300+
* @return bool True if success; otherwise false.
265301
*/
266-
protected function delete_callback( $signup, $assoc_args ) {
302+
private function delete_signup( $signup ) {
267303
global $wpdb;
268304

269305
$signup_id = $signup->signup_id;
270306

271307
$result = $wpdb->delete( $wpdb->signups, array( 'signup_id' => $signup_id ), array( '%d' ) );
272308

273-
if ( $result ) {
274-
return [ 'success', "Signup {$signup_id} deleted." ];
275-
} else {
276-
return [ 'error', "Failed deleting signup {$signup_id}." ];
277-
}
309+
return $result ? true : false;
310+
}
311+
312+
/**
313+
* Deletes all signup.
314+
*
315+
* @return bool True if success; otherwise false.
316+
*/
317+
private function delete_all_signups() {
318+
global $wpdb;
319+
320+
$results = $wpdb->query( 'DELETE FROM ' . $wpdb->signups );
321+
322+
return $results ? true : false;
278323
}
279324
}

0 commit comments

Comments
 (0)