Skip to content

Style variations: Add $schema when saving variations - #889

Closed
KhushalSainS wants to merge 1 commit into
WordPress:trunkfrom
KhushalSainS:fix/style-variation-schema-886
Closed

KhushalSainS wants to merge 1 commit into
WordPress:trunkfrom
KhushalSainS:fix/style-variation-schema-886

Conversation

@KhushalSainS

@KhushalSainS KhushalSainS commented Oct 16, 2026 •

Copy link
Copy Markdown

What?

Make style variations declare $schema, matching theme.json and core default-theme variations, and unify the two code paths that write theme.json-shaped data.

Why?

Style variations created with "Create Theme Variation" were built from WP_Theme_JSON::get_data() plus a title, so they never declared $schema, while theme.json did. Without $schema, editors cannot validate or autocomplete the file and theme validators warn about it, and it is inconsistent with every core default theme, which declares $schema in each variation (e.g. Twenty Twenty-Five styles/01-evening.json).

The root cause is that two independent code paths write theme.json-shaped data and only one of them added the schema. Unifying them removes the drift instead of duplicating the logic.

How?

  • Extract CBT_Theme_JSON_Resolver::get_schema() to resolve the schema URL: the theme's declared $schema, then the parent theme's for child themes, then the version-based URL.
  • Extract CBT_Theme_JSON_Resolver::add_schema_to_data() to order $schema, version, and title first.
  • Delegate export_theme_data() to the shared helpers so the export and variation paths cannot drift.
  • Route CBT_Theme_JSON::add_theme_json_variation_to_local() through add_schema_to_data() before writing.
  • Fix a latent key bug: the declared schema was read with array_key_exists( 'schema', ... ) instead of '$schema', so the theme's declared schema was effectively never used.
  • Add tests/test-theme-json.php covering the ordering and asserting the variation's $schema matches theme.json.

Note: fixing the key bug means exported theme.json now honors the theme's declared $schema instead of always falling back to the version-based URL. If you prefer theme.json output to stay byte-for-byte identical, the conservative variant keeps the old fallback and only applies the ordering helper.

Testing Instructions

  1. Install dependencies with npm ci and composer install.
  2. Start the test environment with npx wp-env start.
  3. Run npm run test:unit:php.
  4. Confirm Test_Create_Block_Theme_JSON passes, verifying that add_schema_to_data() orders $schema, version, and title first and that a created variation's $schema equals the one written to theme.json.
  5. Run composer lint.
  6. Manual check: in the Site Editor open Create Block Theme, create a theme variation, and confirm styles/<slug>.json starts with $schema then version.

Use of AI Tools

AI assistance: Yes
Tool: OpenAI Codex

Style variations created via "Save as style variation" are written from WP_Theme_JSON::get_data() plus a title, so they never declare $schema. theme.json does, via CBT_Theme_JSON_Resolver::export_theme_data().

Extract the schema resolution and $schema/version ordering into CBT_Theme_JSON_Resolver::get_schema() and add_schema_to_data(), and route both export_theme_data() and add_theme_json_variation_to_local() through them. This also fixes a latent bug where the declared $schema was read under the wrong key ('schema' instead of '$schema').
@KhushalSainS
KhushalSainS deleted the fix/style-variation-schema-886 branch October 16, 2026 15:14
@KhushalSainS
KhushalSainS restored the fix/style-variation-schema-886 branch October 16, 2026 15:16
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.

1 participant