Make WordPress Core

Ticket #44956: 44956.3.diff

File 44956.3.diff, 2.5 KB (added by peterwilsoncc, 7 years ago)
  • src/wp-includes/option.php

    diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php
    index 1c0d88225e..21a09bbd51 100644
    a b function update_network_option( $network_id, $option, $value ) { 
    15831583         */
    15841584        $value = apply_filters( "pre_update_site_option_{$option}", $value, $old_value, $option, $network_id );
    15851585
    1586         if ( $value === $old_value ) {
     1586        /*
     1587         * If the new and old values are the same, no need to update.
     1588         *
     1589         * Unserialized values will be adequate in most cases. If the unserialized
     1590         * data differs, the (maybe) serialized data is checked to avoid
     1591         * unnecessary database calls for otherwise identical object instances.
     1592         *
     1593         * See https://core.trac.wordpress.org/ticket/44956
     1594         */
     1595        if ( $value === $old_value || maybe_serialize( $value ) === maybe_serialize( $old_value ) ) {
    15871596                return false;
    15881597        }
    15891598
  • tests/phpunit/tests/option/networkOption.php

    diff --git a/tests/phpunit/tests/option/networkOption.php b/tests/phpunit/tests/option/networkOption.php
    index 637ff5732a..244ea43898 100644
    a b class Tests_Option_NetworkOption extends WP_UnitTestCase { 
    169169
    170170                $this->assertSame( array( 'this_does_not_exist' => true ), $cache );
    171171        }
     172
     173        /**
     174         * Ensure updating network options containing an object do not result in unneeded database calls.
     175         *
     176         * @ticket 44956
     177         */
     178        public function test_update_network_option_array_with_object() {
     179                $array_w_object = array(
     180                        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
     181                        'meta_data' => (object) array(
     182                                'attachment_id' => 292,
     183                                'height'        => 708,
     184                                'width'         => 1260,
     185                        ),
     186                );
     187
     188                $array_w_object_2 = array(
     189                        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
     190                        'meta_data' => (object) array(
     191                                'attachment_id' => 292,
     192                                'height'        => 708,
     193                                'width'         => 1260,
     194                        ),
     195                );
     196
     197                // Add the option, it did not exist before this.
     198                add_network_option( null, 'array_w_object', $array_w_object );
     199
     200                $num_queries_pre_update = get_num_queries();
     201
     202                // Update the option using the same array with an object for the value.
     203                $this->assertFalse( update_network_option( null, 'array_w_object', $array_w_object_2 ) );
     204
     205                // Check that no new database queries were performed.
     206                $this->assertEquals( $num_queries_pre_update, get_num_queries() );
     207        }
    172208}