Skip to content
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
19 changes: 17 additions & 2 deletions src/wp-includes/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,13 @@ function update_option( $option, $value, $autoload = null ) {

/** This filter is documented in wp-includes/option.php */
if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
return add_option( $option, $value, '', $autoload );
if ( add_option( $option, $value, '', $autoload ) ) {
return true;
}

if ( ! $wpdb->get_var( $wpdb->prepare( "SELECT option_id FROM $wpdb->options WHERE option_name = %s LIMIT 1", $option ) ) ) {
return false;
}
}

$serialized_value = maybe_serialize( $value );
Expand Down Expand Up @@ -1137,8 +1143,17 @@ function add_option( $option, $value = '', $deprecated = '', $autoload = null )
*/
do_action( 'add_option', $option, $value );

$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = VALUES(`option_name`), `option_value` = VALUES(`option_value`), `autoload` = VALUES(`autoload`)", $option, $serialized_value, $autoload ) );
$result = $wpdb->query( $wpdb->prepare( "INSERT INTO `$wpdb->options` (`option_name`, `option_value`, `autoload`) VALUES (%s, %s, %s) ON DUPLICATE KEY UPDATE `option_name` = `option_name`", $option, $serialized_value, $autoload ) );
if ( ! $result ) {
if ( 0 === $result && ! wp_installing() ) {
$notoptions = wp_cache_get( 'notoptions', 'options' );

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

return false;
}

Expand Down
32 changes: 32 additions & 0 deletions tests/phpunit/tests/option/option.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,38 @@ public function test_the_basics() {
$this->assertFalse( get_option( $key2 ) );
}

/**
* @ticket 51486
*
* @covers ::add_option
*/
public function test_add_option_should_not_update_existing_option_when_notoptions_cache_is_stale() {
global $wpdb;

$option = 'ticket_51486';

$this->assertTrue( add_option( $option, 'original', '', false ) );

$notoptions = wp_cache_get( 'notoptions', 'options' );
$notoptions = is_array( $notoptions ) ? $notoptions : array();
$notoptions[ $option ] = true;
wp_cache_set( 'notoptions', $notoptions, 'options' );

$this->assertFalse( add_option( $option, 'changed', '', true ) );

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

$this->assertSame( 'original', $row->option_value );
$this->assertSame( 'off', $row->autoload );

wp_cache_delete( $option, 'options' );
$this->assertSame( 'original', get_option( $option ) );

$notoptions = wp_cache_get( 'notoptions', 'options' );
$this->assertIsArray( $notoptions );
$this->assertArrayNotHasKey( $option, $notoptions );
}

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