Make WordPress Core

Opened 22 months ago

Closed 14 months ago

Last modified 14 months ago

#57674 closed defect (bug) (fixed)

unregister_setting() raises a PHP warning on unknown settings

Reported by: xknown's profile xknown Owned by: hellofromtonya's profile hellofromTonya
Milestone: 6.4 Priority: normal
Severity: normal Version:
Component: Options, Meta APIs Keywords: php81 has-patch has-unit-tests commit
Focuses: php-compatibility Cc:

Description

Starting from PHP 8.x, attempting to unregister an undefined setting results in PHP warnings: Warning: Trying to access array offset on value of type null.

unregister_setting( 'somethingrandom', 'somethingrandom' );

Change History (14)

This ticket was mentioned in PR #4035 on WordPress/wordpress-develop by xknown.


22 months ago
#1

  • Keywords has-patch has-unit-tests added

#2 @hellofromTonya
16 months ago

  • Focuses php-compatibility added

I'll mark this ticket as a php-compatibility focus to include it in the PHP 8.1 compat exclusion list.

#3 @SergeyBiryukov
16 months ago

  • Milestone changed from Awaiting Review to 6.4

Good catch, we already check if $wp_registered_settings[ $option_name ] is set there, so I think it makes sense to do the same for $new_allowed_options[ $option_group ].

#4 @oglekler
15 months ago

  • Keywords needs-testing added

This ticket was mentioned in Slack in #core-test by nicolefurlan. View the logs.


14 months ago

#6 @nicolefurlan
14 months ago

I dropped this one in #core-test to hopefully get a test report so that we can include it in the 6.4 release.

#7 @nicolefurlan
14 months ago

Test Report

This report validates that the indicated patch addresses the issue.

Patch tested: https://github.com/WordPress/wordpress-develop/pull/4035

Environment

  • OS: macOS Ventura 13.4.1
  • PHP: 8.2.11
  • WordPress: trunk

Actual Results

  • ✅ Issue resolved with patch.
  • ✅ Unit tests pass.

Additional Notes

  • I used WP CLI via npm run env:cli to test this and verified that when executing unregister_setting( 'somethingrandom', 'somethingrandom' ); on the CLI, the Notice: Trying to access array offset on value of type null in /var/www/src/wp-includes/option.php on line 2821 notice appears without the patch, and disappears after applying the patch.
  • I ran phpunit on registration.php via npm run test:php --debug tests/phpunit/tests/option/registration.php and verified that all unit tests pass in registration.php.

#8 @shailu25
14 months ago

Test Report

Patch Tested: https://github.com/WordPress/wordpress-develop/pull/4035

Environment:

WordPress - 6.4-beta2
OS - Windows
Browser - Chrome
Theme: Twenty Nineteen
PHP - 8.1.17
Active Plugin - None

Actual Results:

  • ✅ After Applying Patch PHP warnings is Resolved. Patch is Working as Expected.

Screenshots :

Before Patch: https://prnt.sc/2fFQlHqLDN3X
After Patch : https://prnt.sc/hjkRvmewxIgl

Last edited 14 months ago by shailu25 (previous) (diff)

#9 @nicolefurlan
14 months ago

We have a couple of passing test reports here. Does this seem ready for commit? (cc @hellofromTonya, @SergeyBiryukov)

#10 @hellofromTonya
14 months ago

  • Keywords needs-testing removed
  • Owner set to hellofromTonya
  • Status changed from new to reviewing

Thanks for the ping (and reminder) @nicolefurlan. The source code fix LGTM and is ready for commit. I'm not sure about the test, but am reviewing.

I'll set myself as owner for the review and commit.

#11 @hellofromTonya
14 months ago

  • Keywords commit added

The patch https://github.com/WordPress/wordpress-develop/pull/4035 is ready for commit. Will prep the commit in time for 6.4 Beta 3.

#12 @hellofromTonya
14 months ago

The patch 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).

See both scenarios in action https://3v4l.org/oeFUJ.

#13 @hellofromTonya
14 months ago

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

In 56817:

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.

Note: See TracTickets for help on using tickets.