Spacing presets: Add check for 0 spacing steps#43105
Conversation
…ice about invalid setting
andrewserong
left a comment
There was a problem hiding this comment.
Good catch @glendaviesnz, this is working nicely for me!
Before, the following PHP Notice is output:
After, there is no notice and the presets aren't output. Setting to a value greater than 0 correctly still outputs the spacing sizes.
Not at all a blocker, but I was wondering if it's worth changing the check from 0 === $spacing_scale['steps'] to empty( $spacing_scale['steps'] ) so that any falsy value for steps would result in skipping the output without triggering an error? (E.g. 0, false, null, etc)
The theory is that if a theme author doesn't want the core scale they explicitly set |
That sounds reasonable to me, particularly because |
| $spacing_scale = _wp_array_get( $this->theme_json, array( 'settings', 'spacing', 'spacingScale' ), array() ); | ||
|
|
||
| // If theme authors want to prevent the generation of the core spacing scale they can set their theme.json spacingScale.steps to 0. | ||
| if ( 0 === $spacing_scale['steps'] ) { |
There was a problem hiding this comment.
Oh, actually just another thought. This probably doesn't matter in practice because the base theme.json contains the 'steps' key, but if we keep the 0 check, should this line and the > 0 line below occur after the ! isset( $spacing_scale['steps'] ) check further down, to (theoretically) avoid an undefined index notice?
There was a problem hiding this comment.
yeh, makes more sense to check for invalid values first and then check for 0, have switched the order
|
Just retested and this is testing nicely for me, thanks for the update 👍 Again, not at all a blocker (do feel free to merge this PR as-is), but is there a reason we don't also allow opting out at the root feature level? Whichever way we recommend themes do it, it'd be good to update https://developer.wordpress.org/themes/advanced-topics/theme-json/#enabling-and-disabling-settings with the desired approach prior to 6.1. If / when we document it, we can add |
no explicit reason @andrewserong , just hasn't been considered to-date - have added a note to explore this to the tracking issue |
|
Nice one, thanks @glendaviesnz! |

What?
Adds an early return from
set_spacing_sizesif spacing.spaceScale.steps === 0.Why?
Theme authors should be able to set this to 0 to prevent the generation of the core default spacing presets, and currently doing so causing an
invalid validerror notice to be thrown.How?
If pacing.spaceScale.steps === 0 return from method before spaceScale settings are validated
Testing Instructions
theme.jsonfile settings section:--wp--preset--spacing--*CSS properties defined in the page source and that there is no PHP notice at the top of the page.Screenshots or screencast
Before:

After:
