Make WordPress Core


Ignore:
Timestamp:
12/11/2016 09:42:12 PM (8 years ago)
Author:
peterwilsoncc
Message:

Options: Prevent unnecessary SQL updates by update_option.

Previously an option containing an object would trigger an SQL UPDATE on all calls to update_option, even if the old and new values were identical. This was due to the old and new values having differing resource IDs.

This change compares the old and new values as serialized data to remove the resource ID from the comparison.

Props salcode, bradyvercher, peterwilsoncc.
Fixes #38903.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/option/updateOption.php

    r37954 r39564  
    167167
    168168    /**
     169     * @ticket 38903
     170     */
     171    public function test_update_option_array_with_object() {
     172        $array_w_object = array(
     173            'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
     174            'meta_data' => (object) array(
     175                'attachment_id' => 292,
     176                'height'        => 708,
     177                'width'         => 1260,
     178            ),
     179        );
     180
     181        // Add the option, it did not exist before this.
     182        add_option( 'array_w_object', $array_w_object );
     183
     184        $num_queries_pre_update = get_num_queries();
     185
     186        // Update the option using the same array with an object for the value.
     187        $this->assertFalse( update_option( 'array_w_object', $array_w_object ) );
     188
     189        // Check that no new database queries were performed.
     190        $this->assertEquals( $num_queries_pre_update, get_num_queries() );
     191    }
     192
     193    /**
    169194     * `add_filter()` callback for test_should_respect_default_option_filter_when_option_does_not_yet_exist_in_database().
    170195     */
Note: See TracChangeset for help on using the changeset viewer.