Skip to content

Editor: Apply duotone presets with slugs that are not kebab-case on the front end - #14188

Open
maheshbohara wants to merge 1 commit into
WordPress:trunkfrom
maheshbohara:66285-duotone-non-kebab-case-slugs
Open

maheshbohara wants to merge 1 commit into
WordPress:trunkfrom
maheshbohara:66285-duotone-non-kebab-case-slugs

Conversation

@maheshbohara

Copy link
Copy Markdown

A duotone preset whose slug is not already kebab-case (for example blueOrange or duotone1) works in the block editor but is not applied on the front end.

WP_Duotone::get_all_global_styles_presets() registers presets under a kebab-cased filter ID, but two other places use the raw slug:

  • get_slug_from_attribute() returns the slug exactly as written in the block attribute, so the lookup in is_preset() fails.
  • get_global_styles_presets() names the CSS custom property after the raw slug.

This PR converts the slug to kebab-case in both, so the class name, SVG filter ID, CSS custom property and block CSS all use the same form. Slugs that are already kebab-case are unchanged.

Before

Preset blueOrange applied to an Image block (var:preset|duotone|blueOrange): the preset is not recognised, the attribute is treated as a raw CSS value, and no SVG filter is printed.

<figure class="wp-block-image wp-duotone-varpresetduotoneblueorange-1">

.wp-duotone-varpresetduotoneblueorange-1.wp-block-image img { filter: var:preset|duotone|blueOrange; }

The same preset applied through global styles: since [63641] the reference is kebab-cased, but the custom property is still defined with the raw slug, so it never resolves.

:root { --wp--preset--duotone--blueOrange: url(#wp-duotone-blue-orange); }
.wp-duotone-blue-orange.wp-block-image img { filter: var(--wp--preset--duotone--blue-orange); }

After

<figure class="wp-block-image wp-duotone-blue-orange">

:root { --wp--preset--duotone--blue-orange: url(#wp-duotone-blue-orange); }
.wp-duotone-blue-orange.wp-block-image img { filter: var(--wp--preset--duotone--blue-orange); }

and <filter id="wp-duotone-blue-orange"> is printed in the footer.

Testing

  1. In a block theme's theme.json, add a duotone preset with a camelCase slug:
    "settings": { "color": { "duotone": [
    	{ "slug": "blueOrange", "name": "Blue Orange", "colors": [ "#0000ff", "#ffa500" ] }
    ] } }
  2. Add an Image block to a post, apply the "Blue Orange" duotone filter and save.
  3. View the post on the front end. Before this change the image has no filter; after it the filter is applied.

Unit tests:

npm run test:php -- tests/phpunit/tests/block-supports/duotone.php

The new test_render_duotone_support_preset_with_non_kebab_case_slug covers a camelCase slug referenced both ways, a slug with a trailing number, and a kebab-case slug as a control. New cases were also added to the get_slug_from_attribute and is_preset data providers.

One existing expectation changed: a reference whose slug is only . now yields an empty slug instead of ., because kebab-casing removes it. It was not a valid preset before either.

Note

lib/class-wp-duotone-gutenberg.php in the Gutenberg plugin has the same two lines and would need the same change.

Trac ticket: https://core.trac.wordpress.org/ticket/66285

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code
Model(s): Claude Opus
Used for: Investigating and reproducing the bug, and drafting the patch, unit tests and this description. I reviewed the changes and ran the tests locally.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

…he front end.

`WP_Duotone` registers presets under a kebab-cased filter ID, but used the raw slug when reading the preset from a block attribute and when naming the CSS custom property. A preset with a slug such as `blueOrange` was therefore not recognized when applied to a block, and its custom property did not match the kebab-cased reference when applied through global styles.

This converts the slug to kebab-case in `WP_Duotone::get_slug_from_attribute()` and `WP_Duotone::get_global_styles_presets()`, so the class name, SVG filter ID, CSS custom property, and block CSS all use the same form. Slugs that are already kebab-case are unchanged.

Follow-up to [56101], [63641].

See #66285.
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

Core Committers: Use this line as a base for the props when committing in SVN:

Props maheshbohara.

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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