<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[56681] trunk: Options, Meta APIs: Improve logic to avoid unnecessary database writes in `update_option()`.</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/56681">56681</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/56681","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>flixos90</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-09-25 16:23:52 +0000 (Mon, 25 Sep 2023)</dd>
</dl>
<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Options, Meta APIs: Improve logic to avoid unnecessary database writes in `update_option()`.
Prior to this change, a strict comparison between the old and new database value could lead to a false negative, since database values are generally stored as strings. For example, passing an integer to `update_option()` would almost always result in an update given any existing database value for that option would be that number cast to a string.
This changeset adjusts the logic to perform an intentional "loose-y" comparison by casting the values to strings. Extensive coverage previously added in <a href="https://core.trac.wordpress.org/changeset/56648">[56648]</a> provides additional confidence that this does not introduce any backward compatibility issues.
Props mukesh27, costdev, spacedmonkey, joemcgill, flixos90, nacin, atimmer, duck_, boonebgorges.
Fixes <a href="https://core.trac.wordpress.org/ticket/22192">#22192</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesoptionphp">trunk/src/wp-includes/option.php</a></li>
<li><a href="#trunktestsphpunittestsoptionoptionphp">trunk/tests/phpunit/tests/option/option.php</a></li>
</ul>
<h3>Added Paths</h3>
<ul>
<li><a href="#trunktestsphpunittestsoptionisEqualDatabaseValuephp">trunk/tests/phpunit/tests/option/isEqualDatabaseValue.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludesoptionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/option.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/option.php 2023-09-25 15:34:34 UTC (rev 56680)
+++ trunk/src/wp-includes/option.php 2023-09-25 16:23:52 UTC (rev 56681)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -776,21 +776,23 @@
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><span class="cx" style="display: block; padding: 0 10px"> $value = apply_filters( 'pre_update_option', $value, $option, $old_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ /** This filter is documented in wp-includes/option.php */
+ $default_value = apply_filters( "default_option_{$option}", false, $option, false );
+
</ins><span class="cx" style="display: block; padding: 0 10px"> /*
</span><span class="cx" style="display: block; padding: 0 10px"> * If the new and old values are the same, no need to update.
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Unserialized values will be adequate in most cases. If the unserialized
- * data differs, the (maybe) serialized data is checked to avoid
- * unnecessary database calls for otherwise identical object instances.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * An exception applies when no value is set in the database, i.e. the old value is the default.
+ * In that case, the new value should always be added as it may be intentional to store it rather than relying on the default.
</ins><span class="cx" style="display: block; padding: 0 10px"> *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * See https://core.trac.wordpress.org/ticket/38903
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * See https://core.trac.wordpress.org/ticket/38903 and https://core.trac.wordpress.org/ticket/22192.
</ins><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $old_value !== $default_value && _is_equal_database_value( $old_value, $value ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return false;
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- /** This filter is documented in wp-includes/option.php */
- if ( apply_filters( "default_option_{$option}", false, $option, false ) === $old_value ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $old_value === $default_value ) {
+
</ins><span class="cx" style="display: block; padding: 0 10px"> // Default setting for new options is 'yes'.
</span><span class="cx" style="display: block; padding: 0 10px"> if ( null === $autoload ) {
</span><span class="cx" style="display: block; padding: 0 10px"> $autoload = 'yes';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2887,3 +2889,40 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> return $registered[ $option ]['default'];
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+/**
+ * Determines whether two values will be equal when stored in the database.
+ *
+ * @since 6.4.0
+ * @access private
+ *
+ * @param mixed $old_value The old value to compare.
+ * @param mixed $new_value The new value to compare.
+ * @return bool True if the values are equal, false otherwise.
+ */
+function _is_equal_database_value( $old_value, $new_value ) {
+ $values = array(
+ 'old' => $old_value,
+ 'new' => $new_value,
+ );
+
+ foreach ( $values as $_key => &$_value ) {
+ // Cast scalars or null to a string so type discrepancies don't result in cache misses.
+ if ( null === $_value || is_scalar( $_value ) ) {
+ $_value = (string) $_value;
+ }
+ }
+
+ if ( $values['old'] === $values['new'] ) {
+ return true;
+ }
+
+ /*
+ * Unserialized values will be adequate in most cases. If the unserialized
+ * data differs, the (maybe) serialized data is checked to avoid
+ * unnecessary database calls for otherwise identical object instances.
+ *
+ * See https://core.trac.wordpress.org/ticket/38903
+ */
+ return maybe_serialize( $old_value ) === maybe_serialize( $new_value );
+}
</ins></span></pre></div>
<a id="trunktestsphpunittestsoptionisEqualDatabaseValuephp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: trunk/tests/phpunit/tests/option/isEqualDatabaseValue.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/option/isEqualDatabaseValue.php (rev 0)
+++ trunk/tests/phpunit/tests/option/isEqualDatabaseValue.php 2023-09-25 16:23:52 UTC (rev 56681)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,71 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+/**
+ * Tests for _is_equal_database_value().
+ *
+ * @group option
+ *
+ * @covers ::_is_equal_database_value
+ */
+class Tests_Option_IsEqualDatabaseValue extends WP_UnitTestCase {
+
+ /**
+ * @ticket 22192
+ *
+ * @dataProvider data_is_equal_database_value
+ *
+ * @param mixed $old_value The old value to compare.
+ * @param mixed $new_value The new value to compare.
+ * @param bool $expected The expected result.
+ */
+ public function test_is_equal_database_value( $old_value, $new_value, $expected ) {
+ $this->assertSame( $expected, _is_equal_database_value( $old_value, $new_value ) );
+ }
+
+ /**
+ * Data provider.
+ *
+ * @return array
+ */
+ public function data_is_equal_database_value() {
+ return array(
+ // Equal values.
+ array( '123', '123', true ),
+
+ // Not equal values.
+ array( '123', '456', false ),
+
+ // Truthy.
+ array( 1, '1', true ),
+ array( 1.0, '1', true ),
+ array( '1', '1', true ),
+ array( true, '1', true ),
+ array( '1.0', '1', false ),
+ array( ' ', '1', false ),
+ array( array( '0' ), '1', false ),
+ array( new stdClass(), '1', false ),
+ array( 'Howdy, admin!', '1', false ),
+
+ // False-ish values and empty strings.
+ array( 0, '0', true ),
+ array( 0.0, '0', true ),
+ array( '0', '0', true ),
+ array( '', '0', false ),
+ array( false, '0', false ),
+ array( null, '0', false ),
+ array( array(), '0', false ),
+
+ // Object values.
+ array( (object) array( 'foo' => 'bar' ), (object) array( 'foo' => 'bar' ), true ),
+ array( (object) array( 'foo' => 'bar' ), (object) array( 'foo' => 'baz' ), false ),
+ array( (object) array( 'foo' => 'bar' ), serialize( (object) array( 'foo' => 'bar' ) ), false ),
+ array( serialize( (object) array( 'foo' => 'bar' ) ), (object) array( 'foo' => 'bar' ), false ),
+ array( serialize( (object) array( 'foo' => 'bar' ) ), (object) array( 'foo' => 'baz' ), false ),
+
+ // Serialized values.
+ array( array( 'foo' => 'bar' ), serialize( array( 'foo' => 'bar' ) ), false ),
+ array( array( 'foo' => 'bar' ), serialize( array( 'foo' => 'baz' ) ), false ),
+ array( serialize( (object) array( 'foo' => 'bar' ) ), serialize( (object) array( 'foo' => 'bar' ) ), true ),
+ array( serialize( (object) array( 'foo' => 'bar' ) ), serialize( (object) array( 'foo' => 'baz' ) ), false ),
+ );
+ }
+}
</ins><span class="cx" style="display: block; padding: 0 10px">Property changes on: trunk/tests/phpunit/tests/option/isEqualDatabaseValue.php
</span><span class="cx" style="display: block; padding: 0 10px">___________________________________________________________________
</span></span></pre></div>
<a id="svneolstyle"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: svn:eol-style</h4></div>
<ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+native
</ins><span class="cx" style="display: block; padding: 0 10px">\ No newline at end of property
</span><a id="trunktestsphpunittestsoptionoptionphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/option/option.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/option/option.php 2023-09-25 15:34:34 UTC (rev 56680)
+++ trunk/tests/phpunit/tests/option/option.php 2023-09-25 16:23:52 UTC (rev 56681)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -524,4 +524,61 @@
</span><span class="cx" style="display: block; padding: 0 10px"> array( null, array(), true ),
</span><span class="cx" style="display: block; padding: 0 10px"> );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+ /**
+ * Tests that update_option() stores an option that uses
+ * an unfiltered default value of (bool) false.
+ *
+ * @ticket 22192
+ *
+ * @covers ::update_option
+ */
+ public function test_update_option_should_store_option_with_default_value_false() {
+ global $wpdb;
+
+ $option = 'update_option_default_false';
+ update_option( $option, false );
+
+ $actual = $wpdb->query(
+ $wpdb->prepare(
+ "SELECT option_name FROM $wpdb->options WHERE option_name = %s LIMIT 1",
+ $option
+ )
+ );
+
+ $this->assertSame( 1, $actual );
+ }
+
+ /**
+ * Tests that update_option() stores an option that uses
+ * a filtered default value.
+ *
+ * @ticket 22192
+ *
+ * @covers ::update_option
+ */
+ public function test_update_option_should_store_option_with_filtered_default_value() {
+ global $wpdb;
+
+ $option = 'update_option_custom_default';
+ $default_value = 'default-value';
+
+ add_filter(
+ "default_option_{$option}",
+ static function () use ( $default_value ) {
+ return $default_value;
+ }
+ );
+
+ update_option( $option, $default_value );
+
+ $actual = $wpdb->query(
+ $wpdb->prepare(
+ "SELECT option_name FROM $wpdb->options WHERE option_name = %s LIMIT 1",
+ $option
+ )
+ );
+
+ $this->assertSame( 1, $actual );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>