Opened 8 weeks ago
Last modified 8 weeks ago
#66183 new defect (bug)
wp_get_global_settings() and wp_get_global_styles() return the entire array when the requested path does not exist
| Reported by: | devansh2002 | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | Editor | Version: | |
| Severity: | normal | Keywords: | has-patch has-unit-tests |
| Cc: | Focuses: |
Description
When a $path is passed to wp_get_global_settings() or wp_get_global_styles() and that path does not exist, both functions return the entire settings/styles array instead of an empty value.
This makes it impossible for callers to distinguish "this setting is not set" from "here is every setting", so defensive code has to sniff for expected keys and guess.
Originally reported in Gutenberg: https://github.com/WordPress/gutenberg/issues/39657
Steps to reproduce
<?php add_action( 'init', function () { var_dump( wp_get_global_settings( array( 'layout', 'contentSize' ) ) ); // string, e.g. '645px' var_dump( wp_get_global_settings( array( 'foo', 'bar' ) ) ); // entire settings array (12 top-level keys) var_dump( wp_get_global_styles( array( 'foo', 'bar' ) ) ); // entire styles array }, 9999 );
Expected
null when the requested path does not exist.
Actual
The full settings (or styles) array.
Cause
In src/wp-includes/global-styles-and-settings.php:
return _wp_array_get( $settings, $path, $settings ); // wp_get_global_settings() return _wp_array_get( $styles, $path, $styles ); // wp_get_global_styles()
The third argument of _wp_array_get() is the default returned when the path is not found, and here it is the whole array.
Proposed fix
Return null for a missing path (i.e. drop the third argument / pass null), while keeping the existing behaviour of returning everything when $path is empty:
if ( empty( $path ) ) { return $settings; } return _wp_array_get( $settings, $path, null );
Why null rather than an empty array:
- It is
_wp_array_get()'s own default for a missing path. - It distinguishes "not set" from a setting whose value legitimately is an empty array.
- It makes the
?? $fallbackidiom work for callers.
Backward compatibility
This is a behaviour change. Callers of both functions should be audited for any that pass a path that may be absent. In practice, a caller currently receiving the full array for a missing path is already getting an unusable value, so impact should be minimal. A dev note may be warranted.
Also needs unit tests covering: existing path, missing path, and empty path (returns full array).
Change History (1)
This ticket was mentioned in PR #13752 on WordPress/wordpress-develop by @devansh2002.
8 weeks ago
#1
- Keywords has-patch has-unit-tests added
When a
$pathis passed towp_get_global_settings()orwp_get_global_styles()and that path does not exist, both functions return the entire settings/styles array. This happens because the full array is passed as the fallback to_wp_array_get(). Callers cannot tell "not set" apart from "all settings"This PR:
nullfrom both functions when the requested path does not exist. An empty$pathstill returns everything, as before.block_core_image_get_lightbox_settings()in the Image block. It relied on the old behaviour to fall back to the top-levellightboxsetting, and now also falls back when the block-level lookup returnsnull. It works under both the old and new behaviour. A matching change is needed in Gutenberg (packages/block-library/src/image/index.php), since that file is synced from there.Other core callers that pass a path (
button.php,block-supports/states.php,block-supports/block-visibility.php, and thewp_get_global_styles( array( 'color' ) )call inimage.php) already handlenull.Trac ticket: https://core.trac.wordpress.org/ticket/66183
Use of AI Tools
AI assistance: Yes
Tool(s): Claude
Model(s): Opus 5.5
Used for: Initial code skeleton and test suggestions; final implementation and tests were reviewed and edited by me.