WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

#44956 closed defect (bug) (fixed)

Prevent `update_network_option()` from updating when the old and new values contain identical objects.

Reported by: peterwilsoncc Owned by: peterwilsoncc
Milestone: 5.1 Priority: normal
Severity: normal Version:
Component: Networks and Sites Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

When an object is included in an option, passing an unchanged value to update_network_option will trigger an UPDATE query.

Given the data below, the meta_data will have a different resource ID for the old and new values. $value === $old_value will always evaluate untrue and the database will be updated and the caches cleared.

array(2) {
  ["url"]=>
  string(85) "http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg"
  ["meta_data"]=>
  object(stdClass)#370 (3) {
    ["attachment_id"]=>
    int(292)
    ["height"]=>
    int(708)
    ["width"]=>
    int(1260)
  }
}

Followup from #38903 fixed in [39564].

Attachments (3)

44956.diff (657 bytes) - added by bor0 2 years ago.
44956.2.diff (1.4 KB) - added by bor0 2 years ago.
Unit tests
44956.3.diff (2.5 KB) - added by peterwilsoncc 2 years ago.

Download all attachments as: .zip

Change History (8)

@bor0
2 years ago

#1 @bor0
2 years ago

  • Keywords has-patch added; needs-patch removed

Ccing @SergeyBiryukov for visibility.

#2 @bor0
2 years ago

Code snippet to demonstrate why the proposed patch works:

<?php
$x = array(
        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
        'meta_data' => (object) array(
                'attachment_id' => 292,
                'height'        => 708,
                'width'         => 1260,
        ),
);

$y = array(
        'url'       => 'http://src.wordpress-develop.dev/wp-content/uploads/2016/10/cropped-Blurry-Lights.jpg',
        'meta_data' => (object) array(
                'attachment_id' => 292,
                'height'        => 708,
                'width'         => 1260,
        ),
);

var_dump( $x === $y );
var_dump( serialize( $x ) === serialize( $y ) );

Output:

bor0@boro:~/dev/www/wordpress$ php test.php
bool(false)
bool(true)

#3 @peterwilsoncc
2 years ago

  • Keywords needs-unit-tests added
  • Milestone changed from 5.0 to 5.1

@bor0 Thanks for providing a patch. I've punted this to the 5.1 milestone due to the focus on the new editor for 5.0.

[39564] included a unit test for update_option(). It would be great to include a similar test for update_network_option().

@bor0
2 years ago

Unit tests

#4 @bor0
2 years ago

  • Keywords has-unit-tests added; needs-unit-tests removed

Thanks @peterwilsoncc. I added unit tests for it.

#5 @peterwilsoncc
2 years ago

  • Resolution set to fixed
  • Status changed from assigned to closed

In 44662:

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.

Note: See TracTickets for help on using tickets.