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
25 changes: 25 additions & 0 deletions features/option-get-autoload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Feature: Get 'autoload' value for an option

Scenario: Option doesn't exist
Given a WP install

When I try `wp option get-autoload foo`
Then STDERR should be:
"""
Error: Could not get 'foo' option. Does it exist?
"""

Scenario: Displays 'autoload' value
Given a WP install

When I run `wp option add foo bar`
Then STDOUT should contain:
"""
Success:
"""

When I run `wp option get-autoload foo`
Then STDOUT should be:
"""
yes
"""
58 changes: 58 additions & 0 deletions features/option-set-autoload.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
Feature: Set 'autoload' value for an option

Scenario: Option doesn't exist
Given a WP install

When I try `wp option set-autoload foo yes`
Then STDERR should be:
"""
Error: Could not get 'foo' option. Does it exist?
"""

Scenario: Invalid 'autoload' value provided
Given a WP install

When I run `wp option add foo bar`
Then STDOUT should contain:
"""
Success:
"""

When I try `wp option set-autoload foo invalid`
Then STDERR should be:
"""
Error: Invalid value specified for positional arg.
"""

Scenario: Successfully updates autoload value
Given a WP install

When I run `wp option add foo bar`
Then STDOUT should contain:
"""
Success:
"""

When I run `wp option get-autoload foo`
Then STDOUT should be:
"""
yes
"""

When I run `wp option set-autoload foo no`
Then STDOUT should be:
"""
Success: Updated autoload value for 'foo' option.
"""

When I run the previous command again
Then STDOUT should be:
"""
Success: Autoload value passed for 'foo' option is unchanged.
"""

When I run `wp option get-autoload foo`
Then STDOUT should be:
"""
no
"""
94 changes: 94 additions & 0 deletions src/Option_Command.php
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,100 @@ public function update( $args, $assoc_args ) {
}
}

/**
* Gets the 'autoload' value for an option.
*
* ## OPTIONS
*
* <key>
* : The name of the option to get 'autoload' of.
*
* @subcommand get-autoload
*/
public function get_autoload( $args ) {
global $wpdb;

list( $option ) = $args;

$existing = $wpdb->get_row(
$wpdb->prepare(
"SELECT autoload FROM $wpdb->options WHERE option_name=%s",
$option
)
);
if ( ! $existing ) {
WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );

}
WP_CLI::log( $existing->autoload );
}

/**
* Sets the 'autoload' value for an option.
*
* ## OPTIONS
*
* <key>
* : The name of the option to set 'autoload' for.
*
* <autoload>
* : Should this option be automatically loaded.
* ---
* options:
* - 'yes'
* - 'no'
Comment on lines +479 to +480
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This means the API is:

wp option set-autoload my_option no

That seems to be forced by the DB value and is neither developer-speak (set autoload true) nor natural language (disable autoload).

I'm don't love that API, but I have trouble coming up with something better.

What do think about:

wp option autoload get my_option
wp option autoload enable my_option
wp option autoload disable my_option

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm don't love that API, but I have trouble coming up with something better.

What do think about:

@schlessera Good points. I don't love your proposed approach either because it seems unnecessarily complex to nest the commands further.

I went with wp option set-autoload my_option no because wp option add accepts --autoload=yes/--autoload=no.

* ---
*
* @subcommand set-autoload
*/
public function set_autoload( $args ) {
global $wpdb;

list( $option, $autoload ) = $args;

$previous = $wpdb->get_row(
$wpdb->prepare(
"SELECT autoload, option_value FROM $wpdb->options WHERE option_name=%s",
$option
)
);
if ( ! $previous ) {
WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );

}

if ( $previous->autoload === $autoload ) {
WP_CLI::success( "Autoload value passed for '{$option}' option is unchanged." );
return;
}

$wpdb->update(
$wpdb->options,
array( 'autoload' => $autoload ),
array( 'option_name' => $option )
);

// Recreate cache refreshing from update_option().
$notoptions = wp_cache_get( 'notoptions', 'options' );

if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
unset( $notoptions[ $option ] );
wp_cache_set( 'notoptions', $notoptions, 'options' );
}

if ( ! defined( 'WP_INSTALLING' ) ) {
$alloptions = wp_load_alloptions( true );
if ( isset( $alloptions[ $option ] ) ) {
$alloptions[ $option ] = $previous->option_value;
wp_cache_set( 'alloptions', $alloptions, 'options' );
} else {
wp_cache_set( $option, $previous->option_value, 'options' );
}
}

WP_CLI::success( "Updated autoload value for '{$option}' option." );
}

/**
* Deletes an option.
*
Expand Down