Skip to content

Commit 5447cd3

Browse files
authored
Merge pull request #247 from Sidsector9/feat/220
Allow deleting multiple options at once
2 parents 65181e5 + ffb67c9 commit 5447cd3

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

features/option.feature

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ Feature: Manage WordPress options
5757
When I run `wp option delete str_opt`
5858
Then STDOUT should not be empty
5959

60+
When I run `wp option add option_one "ONE"`
61+
And I run `wp option add option_two "TWO"`
62+
Then STDOUT should not be empty
63+
64+
When I try `wp option delete option_one option_two option_three`
65+
Then STDOUT should be:
66+
"""
67+
Success: Deleted 'option_one' option.
68+
Success: Deleted 'option_two' option.
69+
"""
70+
And STDERR should be:
71+
"""
72+
Warning: Could not delete 'option_three' option. Does it exist?
73+
"""
74+
6075
When I run `wp option list`
6176
Then STDOUT should not contain:
6277
"""
@@ -227,31 +242,31 @@ Feature: Manage WordPress options
227242
228243
When I try `wp option list --search='auto_opt' --autoload`
229244
Then STDOUT should not be empty
230-
And STDERR should be:
245+
And STDERR should be:
231246
"""
232247
Warning: --autoload parameter needs a value
233248
"""
234249
And the return code should be 0
235250
236251
When I try `wp option list --search='auto_opt' --autoload=no`
237252
Then STDOUT should be empty
238-
And STDERR should be:
253+
And STDERR should be:
239254
"""
240255
Error: Value of '--autoload' should be on or off.
241256
"""
242257
And the return code should be 1
243258
244259
When I try `wp option add str_opt_foo 'bar' --autoload`
245260
Then STDOUT should not be empty
246-
And STDERR should be:
261+
And STDERR should be:
247262
"""
248263
Warning: --autoload parameter needs a value
249264
"""
250265
And the return code should be 0
251266
252267
When I try `wp option add str_opt_foo 'bar' --autoload=off`
253268
Then STDOUT should be empty
254-
And STDERR should contain:
269+
And STDERR should contain:
255270
"""
256271
Error: Parameter errors:
257272
"""

src/Option_Command.php

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public function get( $args, $assoc_args ) {
7777
$value = get_option( $key );
7878

7979
if ( false === $value ) {
80-
WP_CLI::error( "Could not get '$key' option. Does it exist?" );
80+
WP_CLI::error( "Could not get '{$key}' option. Does it exist?" );
8181
}
8282

8383
WP_CLI::print_value( $value, $assoc_args );
@@ -132,9 +132,9 @@ public function add( $args, $assoc_args ) {
132132
}
133133

134134
if ( !add_option( $key, $value, '', $autoload ) ) {
135-
WP_CLI::error( "Could not add option '$key'. Does it already exist?" );
135+
WP_CLI::error( "Could not add option '{$key}'. Does it already exist?" );
136136
} else {
137-
WP_CLI::success( "Added '$key' option." );
137+
WP_CLI::success( "Added '{$key}' option." );
138138
}
139139
}
140140

@@ -420,12 +420,12 @@ public function update( $args, $assoc_args ) {
420420
$old_value = sanitize_option( $key, get_option( $key ) );
421421

422422
if ( $value === $old_value && is_null( $autoload ) ) {
423-
WP_CLI::success( "Value passed for '$key' option is unchanged." );
423+
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
424424
} else {
425425
if ( update_option( $key, $value, $autoload ) ) {
426-
WP_CLI::success( "Updated '$key' option." );
426+
WP_CLI::success( "Updated '{$key}' option." );
427427
} else {
428-
WP_CLI::error( "Could not update option '$key'." );
428+
WP_CLI::error( "Could not update option '{$key}'." );
429429
}
430430
}
431431
}
@@ -435,22 +435,28 @@ public function update( $args, $assoc_args ) {
435435
*
436436
* ## OPTIONS
437437
*
438-
* <key>
438+
* <key>...
439439
* : Key for the option.
440440
*
441441
* ## EXAMPLES
442442
*
443443
* # Delete an option.
444444
* $ wp option delete my_option
445445
* Success: Deleted 'my_option' option.
446+
*
447+
* # Delete multiple options.
448+
* $ wp option delete option_one option_two option_three
449+
* Success: Deleted 'option_one' option.
450+
* Success: Deleted 'option_two' option.
451+
* Warning: Could not delete 'option_three' option. Does it exist?
446452
*/
447453
public function delete( $args ) {
448-
list( $key ) = $args;
449-
450-
if ( !delete_option( $key ) ) {
451-
WP_CLI::error( "Could not delete '$key' option. Does it exist?" );
452-
} else {
453-
WP_CLI::success( "Deleted '$key' option." );
454+
foreach ( $args as $arg ) {
455+
if ( ! delete_option( $arg ) ) {
456+
WP_CLI::warning( "Could not delete '{$arg}' option. Does it exist?" );
457+
} else {
458+
WP_CLI::success( "Deleted '{$arg}' option." );
459+
}
454460
}
455461
}
456462

@@ -571,12 +577,12 @@ public function patch( $args, $assoc_args ) {
571577
$patched_value = sanitize_option( $key, $traverser->value() );
572578

573579
if ( $patched_value === $old_value ) {
574-
WP_CLI::success( "Value passed for '$key' option is unchanged." );
580+
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
575581
} else {
576582
if ( update_option( $key, $patched_value ) ) {
577-
WP_CLI::success( "Updated '$key' option." );
583+
WP_CLI::success( "Updated '{$key}' option." );
578584
} else {
579-
WP_CLI::error( "Could not update option '$key'." );
585+
WP_CLI::error( "Could not update option '{$key}'." );
580586
}
581587
}
582588
}

0 commit comments

Comments
 (0)