Make WordPress Core

Changeset 56817


Ignore:
Timestamp:
10/10/2023 12:50:19 PM (14 months ago)
Author:
hellofromTonya
Message:

Options, Meta APIs: Check setting group exists before search in unregister_setting().

Checks if the given $option_group exists before searching for the $option_name. Sets the $pos to false, as array_search() returns false if the option name (needle) does not exist.

This changeset fixes 2 different PHP Warning|Notice scenarios:

  1. When the global $new_allowed_options is null, fixes raising Trying to access array offset on value of type null PHP Notice (PHP 7.4) | Warning (on PHP 8).
  1. When the global $new_allowed_options is an array and the setting group key does not exist, fixes raising "Undefined index: unknown_setting_group" PHP Notice (PHP 7) | Warning (on PHP 8).

For both scenarios, the array_search() is skipped and the $pos is set to a default of false, i.e. which is the value returned when array_search() is unsuccessful.

Props xknown, hellofromTonya, nicolefurlan, oglekler, SergeyBiryukov, shailu25.
Fixes #57674.

Location:
trunk
Files:
2 edited

Legend:

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

    r56814 r56817  
    29042904    }
    29052905
    2906     $pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true );
     2906    $pos = false;
     2907    if ( isset( $new_allowed_options[ $option_group ] ) ) {
     2908        $pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true );
     2909    }
    29072910
    29082911    if ( false !== $pos ) {
  • trunk/tests/phpunit/tests/option/registration.php

    r53865 r56817  
    150150        $this->assertFalse( has_filter( 'default_option_test_default', 'filter_default_option' ) );
    151151    }
     152
     153    /**
     154     * The test passes if a Notice | Warning | Error is not raised. Thus. the absence of a Notice | Warning | Error
     155     * is an indicator the fix in the ticket resolves the issue.
     156     *
     157     * @ticket 57674
     158     *
     159     * @covers ::unregister_setting
     160     */
     161    public function test_unregister_invalid_setting_does_not_raise_php_notice_warning_or_error() {
     162        $setting = uniqid();
     163        unregister_setting( $setting, $setting );
     164        $this->assertFalse( has_filter( 'default_option_' . $setting, 'filter_default_option' ) );
     165    }
    152166}
Note: See TracChangeset for help on using the changeset viewer.