Fix: Sync fluid typography size with fluid.max (#789) #796
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fix: Sync fluid typography size with fluid.max
Summary
Fixes a bug where the
sizeproperty in theme.json would retain its old value when editing Custom Fluid Typography in the Site Editor. Thefluid.minandfluid.maxvalues would update correctly, but thesizeproperty (which represents the maximum fluid size fallback) would remain stale.Fixes
Changes
includes/create-theme/theme-fluid-typography.php (new file)
CBT_Theme_Fluid_Typographyclass that syncs thesizeproperty withfluid.maxwhenever fluid typography settings are updated.rest_pre_insert_wp_global_stylesfilter to intercept global styles before they're saved.sizeto matchfluid.maxfor any font size with fluid settings.includes/class-create-block-theme-api.php
theme-fluid-typography.phpfile.tests/test-fluid-typography.php (new file)
Test_Create_Block_Theme_Fluid_Typographytest class with 5 unit tests:test_sync_fluid_typography_sizes()- Verifies fluid sizes are synced while non-fluid remain unchanged.test_sync_global_styles_fluid_typography()- Verifies the REST filter works correctly.test_sync_with_no_typography()- Verifies handling of missing typography.test_sync_with_no_font_sizes()- Verifies handling of missing fontSizes.test_sync_global_styles_with_invalid_json()- Verifies graceful error handling.Rationale
When editing Custom Fluid Typography in the Site Editor (Styles → Typography), WordPress updates the global styles JSON. However, the
sizeproperty (which serves as a fallback for the maximum fluid size) wasn't being updated to match the newfluid.maxvalue. This resulted in inconsistent typography definitions where the fluid values were current but the fallback size was stale.The fix intercepts the global styles save operation and automatically syncs
sizewithfluid.maxfor any font size that has fluid settings defined.Risk/Compatibility
rest_pre_insert_wp_global_styles) which has been available since WordPress 5.9.Test Coverage
fluid.maxvalueBehavior After Fix
Before
{ "fontSizes": [ { "slug": "custom-fluid", "name": "Custom Fluid", "fluid": { "min": "1.5rem", "max": "5.63rem" }, "size": "3rem" } ] }After
{ "fontSizes": [ { "slug": "custom-fluid", "name": "Custom Fluid", "fluid": { "min": "1.5rem", "max": "5.63rem" }, "size": "5.63rem" } ] }