Skip to content

Commit 39d420e

Browse files
Add wp option get-autoload and wp option set-autoload (#382)
* Add `wp option get-autoload` and `wp option set-autoload` * Add a step for running the same command again * Clean up PHPCS issues * Use the constant instead for WP 3.7 compat * Remove unnecessary confusion from comment
1 parent cccd002 commit 39d420e

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Feature: Get 'autoload' value for an option
2+
3+
Scenario: Option doesn't exist
4+
Given a WP install
5+
6+
When I try `wp option get-autoload foo`
7+
Then STDERR should be:
8+
"""
9+
Error: Could not get 'foo' option. Does it exist?
10+
"""
11+
12+
Scenario: Displays 'autoload' value
13+
Given a WP install
14+
15+
When I run `wp option add foo bar`
16+
Then STDOUT should contain:
17+
"""
18+
Success:
19+
"""
20+
21+
When I run `wp option get-autoload foo`
22+
Then STDOUT should be:
23+
"""
24+
yes
25+
"""
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
Feature: Set 'autoload' value for an option
2+
3+
Scenario: Option doesn't exist
4+
Given a WP install
5+
6+
When I try `wp option set-autoload foo yes`
7+
Then STDERR should be:
8+
"""
9+
Error: Could not get 'foo' option. Does it exist?
10+
"""
11+
12+
Scenario: Invalid 'autoload' value provided
13+
Given a WP install
14+
15+
When I run `wp option add foo bar`
16+
Then STDOUT should contain:
17+
"""
18+
Success:
19+
"""
20+
21+
When I try `wp option set-autoload foo invalid`
22+
Then STDERR should be:
23+
"""
24+
Error: Invalid value specified for positional arg.
25+
"""
26+
27+
Scenario: Successfully updates autoload value
28+
Given a WP install
29+
30+
When I run `wp option add foo bar`
31+
Then STDOUT should contain:
32+
"""
33+
Success:
34+
"""
35+
36+
When I run `wp option get-autoload foo`
37+
Then STDOUT should be:
38+
"""
39+
yes
40+
"""
41+
42+
When I run `wp option set-autoload foo no`
43+
Then STDOUT should be:
44+
"""
45+
Success: Updated autoload value for 'foo' option.
46+
"""
47+
48+
When I run the previous command again
49+
Then STDOUT should be:
50+
"""
51+
Success: Autoload value passed for 'foo' option is unchanged.
52+
"""
53+
54+
When I run `wp option get-autoload foo`
55+
Then STDOUT should be:
56+
"""
57+
no
58+
"""

src/Option_Command.php

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,100 @@ public function update( $args, $assoc_args ) {
436436
}
437437
}
438438

439+
/**
440+
* Gets the 'autoload' value for an option.
441+
*
442+
* ## OPTIONS
443+
*
444+
* <key>
445+
* : The name of the option to get 'autoload' of.
446+
*
447+
* @subcommand get-autoload
448+
*/
449+
public function get_autoload( $args ) {
450+
global $wpdb;
451+
452+
list( $option ) = $args;
453+
454+
$existing = $wpdb->get_row(
455+
$wpdb->prepare(
456+
"SELECT autoload FROM $wpdb->options WHERE option_name=%s",
457+
$option
458+
)
459+
);
460+
if ( ! $existing ) {
461+
WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );
462+
463+
}
464+
WP_CLI::log( $existing->autoload );
465+
}
466+
467+
/**
468+
* Sets the 'autoload' value for an option.
469+
*
470+
* ## OPTIONS
471+
*
472+
* <key>
473+
* : The name of the option to set 'autoload' for.
474+
*
475+
* <autoload>
476+
* : Should this option be automatically loaded.
477+
* ---
478+
* options:
479+
* - 'yes'
480+
* - 'no'
481+
* ---
482+
*
483+
* @subcommand set-autoload
484+
*/
485+
public function set_autoload( $args ) {
486+
global $wpdb;
487+
488+
list( $option, $autoload ) = $args;
489+
490+
$previous = $wpdb->get_row(
491+
$wpdb->prepare(
492+
"SELECT autoload, option_value FROM $wpdb->options WHERE option_name=%s",
493+
$option
494+
)
495+
);
496+
if ( ! $previous ) {
497+
WP_CLI::error( "Could not get '{$option}' option. Does it exist?" );
498+
499+
}
500+
501+
if ( $previous->autoload === $autoload ) {
502+
WP_CLI::success( "Autoload value passed for '{$option}' option is unchanged." );
503+
return;
504+
}
505+
506+
$wpdb->update(
507+
$wpdb->options,
508+
array( 'autoload' => $autoload ),
509+
array( 'option_name' => $option )
510+
);
511+
512+
// Recreate cache refreshing from update_option().
513+
$notoptions = wp_cache_get( 'notoptions', 'options' );
514+
515+
if ( is_array( $notoptions ) && isset( $notoptions[ $option ] ) ) {
516+
unset( $notoptions[ $option ] );
517+
wp_cache_set( 'notoptions', $notoptions, 'options' );
518+
}
519+
520+
if ( ! defined( 'WP_INSTALLING' ) ) {
521+
$alloptions = wp_load_alloptions( true );
522+
if ( isset( $alloptions[ $option ] ) ) {
523+
$alloptions[ $option ] = $previous->option_value;
524+
wp_cache_set( 'alloptions', $alloptions, 'options' );
525+
} else {
526+
wp_cache_set( $option, $previous->option_value, 'options' );
527+
}
528+
}
529+
530+
WP_CLI::success( "Updated autoload value for '{$option}' option." );
531+
}
532+
439533
/**
440534
* Deletes an option.
441535
*

0 commit comments

Comments
 (0)