diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index c813695030..4df0b81458 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -2903,7 +2903,10 @@ function unregister_setting( $option_group, $option_name, $deprecated = '' ) { $option_group = 'reading'; } - $pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true ); + $pos = false; + if ( isset( $new_allowed_options[ $option_group ] ) ) { + $pos = array_search( $option_name, (array) $new_allowed_options[ $option_group ], true ); + } if ( false !== $pos ) { unset( $new_allowed_options[ $option_group ][ $pos ] ); diff --git a/tests/phpunit/tests/option/registration.php b/tests/phpunit/tests/option/registration.php index 04991e4a33..9b0e418c91 100644 --- a/tests/phpunit/tests/option/registration.php +++ b/tests/phpunit/tests/option/registration.php @@ -149,4 +149,18 @@ class Tests_Option_Registration extends WP_UnitTestCase { $this->assertFalse( has_filter( 'default_option_test_default', 'filter_default_option' ) ); } + + /** + * The test passes if a Notice | Warning | Error is not raised. Thus. the absence of a Notice | Warning | Error + * is an indicator the fix in the ticket resolves the issue. + * + * @ticket 57674 + * + * @covers ::unregister_setting + */ + public function test_unregister_invalid_setting_does_not_raise_php_notice_warning_or_error() { + $setting = uniqid(); + unregister_setting( $setting, $setting ); + $this->assertFalse( has_filter( 'default_option_' . $setting, 'filter_default_option' ) ); + } }