Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert r56595 #8004

Open
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Open
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
56 changes: 31 additions & 25 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,33 @@ function get_option( $option, $default_value = false ) {
$passed_default = func_num_args() > 1;

if ( ! wp_installing() ) {
// Prevent non-existent options from triggering multiple queries.
$notoptions = wp_cache_get( 'notoptions', 'options' );

// Prevent non-existent `notoptions` key from triggering multiple key lookups.
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
wp_cache_set( 'notoptions', $notoptions, 'options' );
}

if ( isset( $notoptions[ $option ] ) ) {
/**
* Filters the default value for an option.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 3.4.0
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
*
* @param mixed $default_value The default value to return if the option does not exist
* in the database.
* @param string $option Option name.
* @param bool $passed_default Was `get_option()` passed a default value?
*/
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
}

$alloptions = wp_load_alloptions();

if ( isset( $alloptions[ $option ] ) ) {
Expand All @@ -169,38 +196,17 @@ function get_option( $option, $default_value = false ) {
$value = wp_cache_get( $option, 'options' );

if ( false === $value ) {
// Prevent non-existent options from triggering multiple queries.
$notoptions = wp_cache_get( 'notoptions', 'options' );

// Prevent non-existent `notoptions` key from triggering multiple key lookups.
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
wp_cache_set( 'notoptions', $notoptions, 'options' );
} elseif ( isset( $notoptions[ $option ] ) ) {
/**
* Filters the default value for an option.
*
* The dynamic portion of the hook name, `$option`, refers to the option name.
*
* @since 3.4.0
* @since 4.4.0 The `$option` parameter was added.
* @since 4.7.0 The `$passed_default` parameter was added to distinguish between a `false` value and the default parameter value.
*
* @param mixed $default_value The default value to return if the option does not exist
* in the database.
* @param string $option Option name.
* @param bool $passed_default Was `get_option()` passed a default value?
*/
return apply_filters( "default_option_{$option}", $default_value, $option, $passed_default );
}

$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) );

// Has to be get_row() instead of get_var() because of funkiness with 0, false, null values.
if ( is_object( $row ) ) {
$value = $row->option_value;
wp_cache_add( $option, $value, 'options' );
} else { // Option does not exist, so we must cache its non-existence.
if ( ! is_array( $notoptions ) ) {
$notoptions = array();
}

$notoptions[ $option ] = true;
wp_cache_set( 'notoptions', $notoptions, 'options' );

Expand Down
56 changes: 0 additions & 56 deletions tests/phpunit/tests/option/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,62 +100,6 @@ public function test_get_option_should_call_pre_option_filter() {
$this->assertSame( 1, $filter->get_call_count() );
}

/**
* @ticket 58277
*
* @covers ::get_option
*/
public function test_get_option_notoptions_cache() {
$notoptions = array(
'invalid' => true,
);
wp_cache_set( 'notoptions', $notoptions, 'options' );

$before = get_num_queries();
$value = get_option( 'invalid' );
$after = get_num_queries();

$this->assertSame( 0, $after - $before );
}

/**
* @ticket 58277
*
* @covers ::get_option
*/
public function test_get_option_notoptions_set_cache() {
get_option( 'invalid' );

$before = get_num_queries();
$value = get_option( 'invalid' );
$after = get_num_queries();

$notoptions = wp_cache_get( 'notoptions', 'options' );

$this->assertSame( 0, $after - $before, 'The notoptions cache was not hit on the second call to `get_option()`.' );
$this->assertIsArray( $notoptions, 'The notoptions cache should be set.' );
$this->assertArrayHasKey( 'invalid', $notoptions, 'The "invalid" option should be in the notoptions cache.' );
}

/**
* @ticket 58277
*
* @covers ::get_option
*/
public function test_get_option_notoptions_do_not_load_cache() {
add_option( 'foo', 'bar', '', false );
wp_cache_delete( 'notoptions', 'options' );

$before = get_num_queries();
$value = get_option( 'foo' );
$after = get_num_queries();

$notoptions = wp_cache_get( 'notoptions', 'options' );

$this->assertSame( 0, $after - $before, 'The options cache was not hit on the second call to `get_option()`.' );
$this->assertFalse( $notoptions, 'The notoptions cache should not be set.' );
}

/**
* @covers ::get_option
* @covers ::add_option
Expand Down
Loading