Skip to content

fix: strip null values from theme.json output - #892

Draft
ishitaj34 wants to merge 1 commit into
WordPress:trunkfrom
ishitaj34:fix/issue-885
Draft

ishitaj34 wants to merge 1 commit into
WordPress:trunkfrom
ishitaj34:fix/issue-885

Conversation

@ishitaj34

Copy link
Copy Markdown

What?

Closes #885

Recursively strips null values from theme data before serializing it to JSON.

Why?

When the user's global styles contain null values, saving changes or exporting the theme writes them directly into theme.json and style variation files.

The theme.json schema rejects null for these properties, and WordPress core ignores them anyway. They create schema validation errors and noisy clutter that theme authors currently have to clean up manually.

How?

In includes/create-theme/resolver_additions.php:

  • Added CBT_Theme_JSON_Resolver::strip_null_values(): A recursive helper method that traverses array data and drops keys containing null values, while preserving valid falsy values (false, 0, "") and empty arrays ([]).
  • Updated CBT_Theme_JSON_Resolver::stringify(): Invokes self::strip_null_values( $data ) before encoding. Because stringify() is the shared funnel for all theme JSON exports (export_theme_data(), variation creation, theme zip export, save changes, and cloned/blank/child themes), this protects all output paths in a centralized place.

In tests/test-theme-utils.php:

  • test_stringify_strips_null_values: Checks that nested null values (such as unlinked border shorthand properties) are dropped when stringifying theme data, while remaining valid properties (like left) are retained.
  • test_strip_null_values_preserves_falsy_values: Checks that non-null falsy values (false, 0, "", and empty arrays []) are preserved and not unintentionally discarded.

Testing Instructions

Unit tests:

  1. Run npm run test:unit:php:setup to start wp-env.
  2. Run npm run test:unit:php:base -- --filter Test_Create_Block_Theme_Utils.
  3. To specifically run the new test cases added:
npm run test:unit:php:base -- --filter 'test_stringify_strips_null_values|test_strip_null_values_preserves_falsy_values'
  1. Verify that all tests pass with 0 failures.

Manual test:

  1. Activate a block theme (e.g. Twenty Twenty-Five) and enable the Create Block Theme plugin.
  2. Go to Site Editor -> Styles -> Blocks -> Quote -> Borders.
  3. Click "Unlink sides", set the left border width to 4px, and save changes in the editor.
  4. Open the sidebar and select export theme as a zip.
  5. Inspect the generated theme.json. Under styles.blocks["core/quote"].border, verify that only left is present and no null fields (color, style, width) exist.

Use of AI Tools

  • AI assistance: Used for implementing recursive stripping, and PR description.
  • Tool(s) used: Claude Sonnet 4.6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strip null values from styles when saving to theme.json

1 participant