Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 19 additions & 4 deletions features/option.feature
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ Feature: Manage WordPress options
When I run `wp option delete str_opt`
Then STDOUT should not be empty

When I run `wp option add option_one "ONE"`
And I run `wp option add option_two "TWO"`
Then STDOUT should not be empty

When I try `wp option delete option_one option_two option_three`
Then STDOUT should be:
"""
Success: Deleted 'option_one' option.
Success: Deleted 'option_two' option.
"""
And STDERR should be:
"""
Warning: Could not delete 'option_three' option. Does it exist?
"""

When I run `wp option list`
Then STDOUT should not contain:
"""
Expand Down Expand Up @@ -227,31 +242,31 @@ Feature: Manage WordPress options

When I try `wp option list --search='auto_opt' --autoload`
Then STDOUT should not be empty
And STDERR should be:
And STDERR should be:
"""
Warning: --autoload parameter needs a value
"""
And the return code should be 0

When I try `wp option list --search='auto_opt' --autoload=no`
Then STDOUT should be empty
And STDERR should be:
And STDERR should be:
"""
Error: Value of '--autoload' should be on or off.
"""
And the return code should be 1

When I try `wp option add str_opt_foo 'bar' --autoload`
Then STDOUT should not be empty
And STDERR should be:
And STDERR should be:
"""
Warning: --autoload parameter needs a value
"""
And the return code should be 0

When I try `wp option add str_opt_foo 'bar' --autoload=off`
Then STDOUT should be empty
And STDERR should contain:
And STDERR should contain:
"""
Error: Parameter errors:
"""
Expand Down
38 changes: 22 additions & 16 deletions src/Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function get( $args, $assoc_args ) {
$value = get_option( $key );

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

WP_CLI::print_value( $value, $assoc_args );
Expand Down Expand Up @@ -132,9 +132,9 @@ public function add( $args, $assoc_args ) {
}

if ( !add_option( $key, $value, '', $autoload ) ) {
WP_CLI::error( "Could not add option '$key'. Does it already exist?" );
WP_CLI::error( "Could not add option '{$key}'. Does it already exist?" );
} else {
WP_CLI::success( "Added '$key' option." );
WP_CLI::success( "Added '{$key}' option." );
}
}

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

if ( $value === $old_value && is_null( $autoload ) ) {
WP_CLI::success( "Value passed for '$key' option is unchanged." );
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
} else {
if ( update_option( $key, $value, $autoload ) ) {
WP_CLI::success( "Updated '$key' option." );
WP_CLI::success( "Updated '{$key}' option." );
} else {
WP_CLI::error( "Could not update option '$key'." );
WP_CLI::error( "Could not update option '{$key}'." );
}
}
}
Expand All @@ -435,22 +435,28 @@ public function update( $args, $assoc_args ) {
*
* ## OPTIONS
*
* <key>
* <key>...
* : Key for the option.
*
* ## EXAMPLES
*
* # Delete an option.
* $ wp option delete my_option
* Success: Deleted 'my_option' option.
*
* # Delete multiple options.
* $ wp option delete option_one option_two option_three
* Success: Deleted 'option_one' option.
* Success: Deleted 'option_two' option.
* Warning: Could not delete 'option_three' option. Does it exist?
*/
public function delete( $args ) {
list( $key ) = $args;

if ( !delete_option( $key ) ) {
WP_CLI::error( "Could not delete '$key' option. Does it exist?" );
} else {
WP_CLI::success( "Deleted '$key' option." );
foreach ( $args as $arg ) {
if ( ! delete_option( $arg ) ) {
WP_CLI::warning( "Could not delete '{$arg}' option. Does it exist?" );
} else {
WP_CLI::success( "Deleted '{$arg}' option." );
}
}
}

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

if ( $patched_value === $old_value ) {
WP_CLI::success( "Value passed for '$key' option is unchanged." );
WP_CLI::success( "Value passed for '{$key}' option is unchanged." );
} else {
if ( update_option( $key, $patched_value ) ) {
WP_CLI::success( "Updated '$key' option." );
WP_CLI::success( "Updated '{$key}' option." );
} else {
WP_CLI::error( "Could not update option '$key'." );
WP_CLI::error( "Could not update option '{$key}'." );
}
}
}
Expand Down