diff --git a/class.jetpack-options.php b/class.jetpack-options.php index c8199b2eef63a..9afe9c920fbd9 100644 --- a/class.jetpack-options.php +++ b/class.jetpack-options.php @@ -368,9 +368,17 @@ static function update_raw_option( $name, $value, $autoload = false ) { global $wpdb; $autoload_value = $autoload ? 'yes' : 'no'; + $old_value = $wpdb->get_var( + $wpdb->prepare( + "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", + $name + ) + ); + if ( $old_value === $value ) { + return false; + } + $serialized_value = maybe_serialize( $value ); - // try updating, if no update then insert - // TODO: try to deal with the fact that unchanged values can return updated_num = 0 // below we used "insert ignore" to at least suppress the resulting error $updated_num = $wpdb->query( $wpdb->prepare( @@ -380,6 +388,7 @@ static function update_raw_option( $name, $value, $autoload = false ) { ) ); + // Try inserting the option if the value doesn't exits. if ( ! $updated_num ) { $updated_num = $wpdb->query( $wpdb->prepare( @@ -389,7 +398,7 @@ static function update_raw_option( $name, $value, $autoload = false ) { ) ); } - return $updated_num; + return (bool) $updated_num; } /** diff --git a/tests/php/test_class.jetpack-options.php b/tests/php/test_class.jetpack-options.php index 1f65fb49d35f7..0241c70915746 100644 --- a/tests/php/test_class.jetpack-options.php +++ b/tests/php/test_class.jetpack-options.php @@ -109,4 +109,14 @@ function test_raw_option_delete_will_bypass_wp_cache_and_filters() { remove_action( 'added_option', array( $this, 'cache_option' ), 10, 2 ); remove_filter( 'option_test_option', array( $this, 'get_test_option_from_cache' ) ); } + + function test_raw_option_update_with_duplicate_value_returns_false() { + Jetpack_Options::delete_raw_option( 'test_option_2' ); + + Jetpack_Options::update_raw_option( 'test_option_2', 'blue' ); + $this->assertEquals( 'blue', Jetpack_Options::get_raw_option( 'test_option_2' ) ); + + $this->assertFalse( Jetpack_Options::update_raw_option( 'test_option_2', 'blue' ) ); + $this->assertTrue( Jetpack_Options::update_raw_option( 'test_option_2', 'yellow' ) ); + } } \ No newline at end of file