Make WordPress Core

Opened 4 weeks ago

Last modified 4 weeks ago

#66285 new defect (bug)

Duotone presets with slugs that are not kebab-case are not applied on the front end

Reported by: maheshbohara Owned by:
Priority: normal Milestone: Awaiting Review
Component: Editor Version: 6.3
Severity: normal Keywords: has-patch has-unit-tests
Cc: Focuses:

Description

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 registers presets under a kebab-cased filter ID (get_all_global_styles_presets() uses _wp_to_kebab_case( $preset['slug'] )), 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.

The block editor saves the reference with the raw slug (var:preset|duotone|blueOrange) and resolves the colors itself, which is why the filter shows correctly in the editor.

Steps to reproduce

  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 and apply the "Blue Orange" duotone filter. It shows in the editor.
  3. Save and view the post on the front end.

Actual result

The image has no filter. The preset is not recognised, so the attribute is treated as a raw CSS value:

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

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

The declaration is invalid CSS and no SVG filter is printed.

The same preset applied through global styles (styles.blocks.core/image.filter.duotone) fails differently. Since [63641] (#66121) the reference is kebab-cased, so the block CSS and the SVG filter use blue-orange, but the custom property is still defined with the raw slug, so the reference 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); }

Expected result

The filter is applied on the front end, with the class name, SVG filter ID, custom property and block CSS all using the kebab-cased slug (blue-orange).

Proposed fix

Convert the slug to kebab-case in WP_Duotone::get_slug_from_attribute() and in WP_Duotone::get_global_styles_presets(), so every part uses the same form the presets are registered under. Slugs that are already kebab-case are unchanged.

The block attribute path has behaved this way since the class was introduced in 6.3. I have a patch with unit tests and will open a PR shortly.

Attachments (2)

66285-before-fix.png​ (20.1 KB ) - added by anntrauf 4 weeks ago.
Before Fix – Duotone filter is not applied on the frontend.
66285-after-fix.png​ (15.7 KB ) - added by anntrauf 4 weeks ago.
After Fix – Blue Orange duotone filter is successfully applied on the frontend after manually applying the proposed PHP fix from PR #14188.

Download all attachments as: .zip

Change History (4)

This ticket was mentioned in ​PR #14188 on ​WordPress/wordpress-develop by ​@maheshbohara.


4 weeks ago
#1

  • Keywords has-patch has-unit-tests added

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" ] }
       ] } }
    
  1. Add an Image block to a post, apply the "Blue Orange" duotone filter and save.
  2. 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.

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.

#2 @anntrauf
4 weeks ago

Test Report – Ticket #66285

I tested this issue locally using the Twenty Twenty-Five block theme.

Steps to Reproduce:

  1. Added a custom duotone preset with the camelCase slug blueOrange in theme.json.
  2. Created a post and added an Image block.
  3. Applied the Blue Orange duotone filter.
  4. Published the post and checked the frontend.

Before Fix:

  • The duotone filter worked correctly in the block editor.
  • The filter was not applied on the frontend.

After Fix:

  • Manually applied the two PHP changes proposed in PR #14188.
  • Refreshed the frontend and verified the same image.
  • The Blue Orange duotone filter now works correctly in both the editor and frontend.

Test Environment:

  • Theme: Twenty Twenty-Five
  • Browser: Google Chrome
  • OS: Windows
  • Testing: Manual

Result: PASS — The reported issue was reproduced, and the proposed PHP fix successfully resolved it in my local environment.

@anntrauf
4 weeks ago

Before Fix – Duotone filter is not applied on the frontend.

@anntrauf
4 weeks ago

After Fix – Blue Orange duotone filter is successfully applied on the frontend after manually applying the proposed PHP fix from PR #14188.

Note: See TracTickets for help on using tickets.