Make WordPress Core

Changeset 56788


Ignore:
Timestamp:
10/05/2023 04:13:38 PM (17 months ago)
Author:
flixos90
Message:

Options, Meta APIs: Fix minor compatibility issue with update_option() change.

When calling update_option() with value false on a non-existent option, prior to [56681] the function would have returned false and not stored the value in the database, since the given value was the same as the default.

The aforementioned changeset broke that promise with good intention, however this particular change was a backward compatibility break and therefore is resolved here.

Props mukesh27, costdev.
Fixes #22192.

Location:
trunk
Files:
2 edited

Legend:

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

    r56717 r56788  
    806806     * See https://core.trac.wordpress.org/ticket/38903 and https://core.trac.wordpress.org/ticket/22192.
    807807     */
    808     if ( $raw_old_value !== $default_value && _is_equal_database_value( $raw_old_value, $value ) ) {
     808    if (
     809        $value === $raw_old_value ||
     810        (
     811            $raw_old_value !== $default_value &&
     812            _is_equal_database_value( $raw_old_value, $value )
     813        )
     814    ) {
    809815        return false;
    810816    }
  • trunk/tests/phpunit/tests/option/option.php

    r56762 r56788  
    640640            '(float) 0.0'        => array( 0.0, 0.0 ),
    641641            'empty array'        => array( array(), array() ),
     642            'false'              => array( false, false ),
    642643
    643644            /*
    644              * false and null are not included in these datasets
    645              * because false is the default value, which triggers
     645             * null is not included in these datasets because
     646             * false is the default value, which triggers
    646647             * a call to add_option().
    647648             *
     
    680681    public function data_stored_as_empty_string() {
    681682        return array(
    682             'false'        => array( false ),
    683683            'empty string' => array( '' ),
    684684            'null'         => array( null ),
     
    706706        );
    707707
    708         $this->assertTrue( update_option( $option, $default_value ), 'update_option() should have returned true.' );
     708        /*
     709         * For a non existing option with the unfiltered default of false, passing false here wouldn't work.
     710         * Because the default is different than false here though, passing false is expected to result in
     711         * a database update.
     712         */
     713        $this->assertTrue( update_option( $option, false ), 'update_option() should have returned true.' );
    709714
    710715        $actual = $wpdb->get_row(
     
    717722        $this->assertIsObject( $actual, 'The option was not added to the database.' );
    718723        $this->assertObjectHasProperty( 'option_value', $actual, 'The "option_value" property was not included.' );
    719         $this->assertSame( $default_value, $actual->option_value, 'The value was not stored as an empty string.' );
     724        $this->assertSame( '', $actual->option_value, 'The new value was not stored in the database.' );
    720725    }
    721726
Note: See TracChangeset for help on using the changeset viewer.