WordPress.org

Make WordPress Core

Changeset 44662


Ignore:
Timestamp:
01/21/2019 04:26:33 AM (8 months ago)
Author:
peterwilsoncc
Message:

Options: Avoid unnecessary DB calls when updating network options.

Adds a maybe_serialize() comparison for the old and new values in update_network_option() to avoid unnecessary database writes when options contain identical objects.

Props bor0.
Fixes #44956.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/option.php

    r44463 r44662  
    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    }
  • trunk/tests/phpunit/tests/option/networkOption.php

    r42834 r44662  
    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}
Note: See TracChangeset for help on using the changeset viewer.