<!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>[56814] trunk: Options, Meta APIs: Prevent unnecessary database updates caused by strict comparisons in update_network_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/56814">56814</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/56814","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>spacedmonkey</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2023-10-10 09:35:09 +0000 (Tue, 10 Oct 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: Prevent unnecessary database updates caused by strict comparisons in update_network_option.
Previously, in the update_network_option function, strict comparisons between old and new values could erroneously trigger updates in cases where there was no actual change in values. Building upon the groundwork laid in <a href="https://core.trac.wordpress.org/ticket/22192">#22192</a>, this commit introduces the use of the _is_equal_database_value function to accurately compare values before initiating any database updates. This change ensures consistency between the behaviors of `update_option` and `update_network_option`.
Furthermore, we have incorporated similar workarounds that were previously implemented in <a href="https://core.trac.wordpress.org/changeset/56717">[56717]</a>. These changes ensure that the raw version of network option values is considered in the comparisons, enhancing the overall reliability of the update process.
Props mukesh27, flixos90, joemcgill, costdev, spacedmonkey.
Fixes <a href="https://core.trac.wordpress.org/ticket/59360">#59360</a>.</pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludesoptionphp">trunk/src/wp-includes/option.php</a></li>
<li><a href="#trunktestsphpunittestsoptionnetworkOptionphp">trunk/tests/phpunit/tests/option/networkOption.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-10-10 09:01:25 UTC (rev 56813)
+++ trunk/src/wp-includes/option.php 2023-10-10 09:35:09 UTC (rev 56814)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2125,7 +2125,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> wp_protect_special_option( $option );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $old_value = get_network_option( $network_id, $option, false );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $old_value = get_network_option( $network_id, $option );
</ins><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="cx" style="display: block; padding: 0 10px"> * Filters a specific network option before its value is updated.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2145,19 +2145,79 @@
</span><span class="cx" style="display: block; padding: 0 10px"> $value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
</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">+ * To get the actual raw old value from the database, any existing pre filters need to be temporarily disabled.
+ * Immediately after getting the raw value, they are reinstated.
+ * The raw value is only used to determine whether a value is present in the database. It is not used anywhere
+ * else, and is not passed to any of the hooks either.
+ */
+ global $wp_filter;
+
+ /** This filter is documented in wp-includes/option.php */
+ $default_value = apply_filters( "default_site_option_{$option}", false, $option, $network_id );
+
+ $has_site_filter = has_filter( "pre_site_option_{$option}" );
+ $has_option_filter = has_filter( "pre_option_{$option}" );
+ if ( $has_site_filter || $has_option_filter ) {
+ if ( $has_site_filter ) {
+ $old_ms_filters = $wp_filter[ "pre_site_option_{$option}" ];
+ unset( $wp_filter[ "pre_site_option_{$option}" ] );
+ }
+
+ if ( $has_option_filter ) {
+ $old_single_site_filters = $wp_filter[ "pre_option_{$option}" ];
+ unset( $wp_filter[ "pre_option_{$option}" ] );
+ }
+
+ if ( is_multisite() ) {
+ $raw_old_value = get_network_option( $network_id, $option );
+ } else {
+ $raw_old_value = get_option( $option, $default_value );
+ }
+
+ if ( $has_site_filter ) {
+ $wp_filter[ "pre_site_option_{$option}" ] = $old_ms_filters;
+ }
+ if ( $has_option_filter ) {
+ $wp_filter[ "pre_option_{$option}" ] = $old_single_site_filters;
+ }
+ } else {
+ $raw_old_value = $old_value;
+ }
+
+ if ( ! is_multisite() ) {
+ /** This filter is documented in wp-includes/option.php */
+ $default_value = apply_filters( "default_option_{$option}", $default_value, $option, true );
+ }
+
+ /*
</ins><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/44956
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * See https://core.trac.wordpress.org/ticket/44956 and https://core.trac.wordpress.org/ticket/22192 and https://core.trac.wordpress.org/ticket/59360
</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 (
+ $value === $raw_old_value ||
+ (
+ false !== $raw_old_value &&
+ /*
+ * Single site stores values in the `option_value` field, which cannot be set to NULL.
+ * This means a PHP `null` value will be cast to an empty string, which can be considered
+ * equal to values such as an empty string, or false when cast to string.
+ *
+ * However, Multisite stores values in the `meta_value` field, which can be set to NULL.
+ * As NULL is unique in the database, skip checking an old or new value of NULL
+ * against any other value.
+ */
+ ( ! is_multisite() || ! ( null === $raw_old_value || null === $value ) ) &&
+ _is_equal_database_value( $raw_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">- if ( false === $old_value ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( $default_value === $raw_old_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> return add_network_option( $network_id, $option, $value );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span></span></pre></div>
<a id="trunktestsphpunittestsoptionnetworkOptionphp"></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/networkOption.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/option/networkOption.php 2023-10-10 09:01:25 UTC (rev 56813)
+++ trunk/tests/phpunit/tests/option/networkOption.php 2023-10-10 09:35:09 UTC (rev 56814)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -399,7 +399,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * @param mixed $old_value The old value.
</span><span class="cx" style="display: block; padding: 0 10px"> * @param mixed $new_value The new value to try to set.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_not_update_some_values_from_cache( $old_value, $new_value ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_network_option_should_not_update_some_values_from_cache( $old_value, $new_value ) {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_network_option( null, 'foo', $old_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $num_queries = get_num_queries();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -407,9 +407,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> // Comparison will happen against value cached during add_option() above.
</span><span class="cx" style="display: block; padding: 0 10px"> $updated = update_network_option( null, 'foo', $new_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected_queries = $old_value === $new_value || ! is_multisite() ? 0 : 1;
- $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -441,14 +439,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $updated = update_network_option( null, 'foo', $new_value );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- // Mimic the behavior of the database by casting the old value to string.
- if ( is_scalar( $old_value ) ) {
- $old_value = (string) $old_value;
- }
-
- $expected_queries = $old_value === $new_value || ! is_multisite() ? 1 : 2;
- $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( 1, get_num_queries() - $num_queries, 'One additional query should have run to update the value.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -480,9 +471,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> * Strictly equal old and new values will cause an early return
</span><span class="cx" style="display: block; padding: 0 10px"> * with no additional queries.
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- $expected_queries = $old_value === $new_value || ! is_multisite() ? 0 : 1;
- $this->assertSame( $expected_queries, get_num_queries() - $num_queries, "The number of queries should have increased by $expected_queries." );
-
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $this->assertSame( $num_queries, get_num_queries(), 'No additional queries should have run.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> $this->assertFalse( $updated, 'update_network_option() should have returned false.' );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -574,7 +563,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_network_option
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_handle_a_null_new_value_from_cache() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_network_option_should_handle_a_null_new_value_from_cache() {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_network_option( null, 'foo', '' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $num_queries = get_num_queries();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -606,7 +595,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_network_option
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_handle_a_null_new_value_from_db() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_network_option_should_handle_a_null_new_value_from_db() {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_network_option( null, 'foo', '' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> $num_queries = get_num_queries();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -642,7 +631,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> *
</span><span class="cx" style="display: block; padding: 0 10px"> * @covers ::update_network_option
</span><span class="cx" style="display: block; padding: 0 10px"> */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- public function test_update_option_should_handle_a_null_new_value_from_refreshed_cache() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function test_update_network_option_should_handle_a_null_new_value_from_refreshed_cache() {
</ins><span class="cx" style="display: block; padding: 0 10px"> add_network_option( null, 'foo', '' );
</span><span class="cx" style="display: block; padding: 0 10px">
</span><span class="cx" style="display: block; padding: 0 10px"> // Delete and refresh cache from DB.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -699,4 +688,166 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 'null' => array( null ),
</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 a non-existent option is added even when its pre filter returns a value.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_with_pre_filter_adds_missing_option() {
+ $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';
+
+ // Force a return value of integer 0.
+ add_filter( $hook_name, '__return_zero' );
+
+ /*
+ * This should succeed, since the 'foo' option does not exist in the database.
+ * The default value is false, so it differs from 0.
+ */
+ $this->assertTrue( update_network_option( null, 'foo', 0 ) );
+ }
+
+ /**
+ * Tests that an existing option is updated even when its pre filter returns the same value.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_with_pre_filter_updates_option_with_different_value() {
+ $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';
+
+ // Add the option with a value of 1 to the database.
+ update_network_option( null, 'foo', 1 );
+
+ // Force a return value of integer 0.
+ add_filter( $hook_name, '__return_zero' );
+
+ /*
+ * This should succeed, since the 'foo' option has a value of 1 in the database.
+ * Therefore it differs from 0 and should be updated.
+ */
+ $this->assertTrue( update_network_option( null, 'foo', 0 ) );
+ }
+
+ /**
+ * Tests that calling update_network_option() does not permanently remove pre filters.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_maintains_pre_filters() {
+ $hook_name = is_multisite() ? 'pre_site_option_foo' : 'pre_option_foo';
+
+ add_filter( $hook_name, '__return_zero' );
+ update_network_option( null, 'foo', 0 );
+
+ // Assert that the filter is still present.
+ $this->assertSame( 10, has_filter( $hook_name, '__return_zero' ) );
+ }
+
+ /**
+ * Tests that update_network_option() conditionally applies
+ * 'pre_site_option_{$option}' and 'pre_option_{$option}' filters.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_should_conditionally_apply_pre_site_option_and_pre_option_filters() {
+ $option = 'foo';
+ $site_hook = new MockAction();
+ $option_hook = new MockAction();
+
+ add_filter( "pre_site_option_{$option}", array( $site_hook, 'filter' ) );
+ add_filter( "pre_option_{$option}", array( $option_hook, 'filter' ) );
+
+ update_network_option( null, $option, 'false' );
+
+ $this->assertSame( 1, $site_hook->get_call_count(), "'pre_site_option_{$option}' filters occurred an unexpected number of times." );
+ $this->assertSame( is_multisite() ? 0 : 1, $option_hook->get_call_count(), "'pre_option_{$option}' filters occurred an unexpected number of times." );
+ }
+
+ /**
+ * Tests that update_network_option() conditionally applies
+ * 'default_site_{$option}' and 'default_option_{$option}' filters.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_should_conditionally_apply_site_and_option_default_value_filters() {
+ $option = 'foo';
+ $site_hook = new MockAction();
+ $option_hook = new MockAction();
+
+ add_filter( "default_site_option_{$option}", array( $site_hook, 'filter' ) );
+ add_filter( "default_option_{$option}", array( $option_hook, 'filter' ) );
+
+ update_network_option( null, $option, 'false' );
+
+ $this->assertSame( 2, $site_hook->get_call_count(), "'default_site_option_{$option}' filters occurred an unexpected number of times." );
+ $this->assertSame( is_multisite() ? 0 : 2, $option_hook->get_call_count(), "'default_option_{$option}' filters occurred an unexpected number of times." );
+ }
+
+ /**
+ * Tests that update_network_option() adds a non-existent option that uses a filtered default value.
+ *
+ * @ticket 59360
+ *
+ * @covers ::update_network_option
+ */
+ public function test_update_network_option_should_add_option_with_filtered_default_value() {
+ global $wpdb;
+
+ $option = 'foo';
+ $default_site_value = 'default-site-value';
+ $default_option_value = 'default-option-value';
+
+ add_filter(
+ "default_site_option_{$option}",
+ static function () use ( $default_site_value ) {
+ return $default_site_value;
+ }
+ );
+
+ add_filter(
+ "default_option_{$option}",
+ static function () use ( $default_option_value ) {
+ return $default_option_value;
+ }
+ );
+
+ /*
+ * For a non existing option with the unfiltered default of false, passing false here wouldn't work.
+ * Because the default is different than false here though, passing false is expected to result in
+ * a database update.
+ */
+ $this->assertTrue( update_network_option( null, $option, false ), 'update_network_option() should have returned true.' );
+
+ if ( is_multisite() ) {
+ $actual = $wpdb->get_row(
+ $wpdb->prepare(
+ "SELECT meta_value FROM $wpdb->sitemeta WHERE meta_key = %s LIMIT 1",
+ $option
+ )
+ );
+ } else {
+ $actual = $wpdb->get_row(
+ $wpdb->prepare(
+ "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1",
+ $option
+ )
+ );
+ }
+
+ $value_field = is_multisite() ? 'meta_value' : 'option_value';
+
+ $this->assertIsObject( $actual, 'The option was not added to the database.' );
+ $this->assertObjectHasProperty( $value_field, $actual, "The '$value_field' property was not included." );
+ $this->assertSame( '', $actual->$value_field, 'The new value was not stored in the database.' );
+ }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>
</body>
</html>