Make WordPress Core

Opened 5 weeks ago

Closed 4 weeks ago

#64612 closed defect (bug) (fixed)

WP_Duotone: Fatal error when duotone attribute is an array instead of a string

Reported by: jorgefilipecosta's profile jorgefilipecosta Owned by: jorgefilipecosta's profile jorgefilipecosta
Milestone: 7.0 Priority: normal
Severity: normal Version: trunk
Component: Editor Keywords: has-patch has-unit-tests
Focuses: Cc:

Description

A fatal error occurs in WP_Duotone::get_slug_from_attribute() when the $duotone_attr parameter is an array (custom colors) instead of a string (preset reference).

Description

When a theme's theme.json defines duotone with an array of custom colors rather than a preset reference string, a fatal error is thrown:

Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in wp-includes/class-wp-duotone.php

The duotone attribute can be:

  1. A preset reference string: var:preset|duotone|blue-orange or var(--wp--preset--duotone--blue-orange)
  2. An array of custom colors: ['#000000', '#ffffff']
  3. A CSS string: unset

The current code assumes the attribute is always a string when checking for presets, which causes a fatal error when an array of custom colors is passed.

Steps to Reproduce

  1. Create a theme with a theme.json that uses an array value for duotone:
    {
      "styles": {
        "blocks": {
          "core/image": {
            "filter": {
              "duotone": ["#000000", "#ffffff"]
            }
          }
        }
      }
    }
    
  2. Activate the theme
  3. Load any page - fatal error occurs

Expected Results

No fatal error. Custom duotone colors should be processed correctly.

Actual Results

Fatal error is thrown due to preg_match() receiving an array instead of a string.

Solution

Add type checks to ensure $duotone_attr is a string before attempting to parse it:

  1. get_slug_from_attribute() - Returns empty string for non-string input
  2. is_preset() - Returns false for non-string input
  3. get_all_global_style_block_names() - Skips non-string duotone attributes

Change History (3)

This ticket was mentioned in PR #10883 on WordPress/wordpress-develop by @jorgefilipecosta.


5 weeks ago
#1

## Trac ticket

https://core.trac.wordpress.org/ticket/64612

## Description

This PR backports the fix from Gutenberg PR #75283 to prevent a fatal error when the duotone attribute in theme.json is an array (custom colors) instead of a string (preset reference).

## Problem

A fatal error occurs in WP_Duotone::get_slug_from_attribute() when $duotone_attr is an array:

Fatal error: Uncaught TypeError: preg_match(): Argument #2 ($subject) must be of type string, array given in wp-includes/class-wp-duotone.php

This happens when a theme's theme.json defines duotone with an array of colors (custom colors) rather than a preset reference string.

## Solution

Add type checks to ensure $duotone_attr is a string before attempting to parse it:

  1. get_slug_from_attribute() - Returns empty string for non-string input
  2. is_preset() - Returns false for non-string input
  3. get_all_global_style_block_names() - Skips non-string duotone attributes

## Testing

  1. Apply a theme with a theme.json that has an array value for duotone (custom colors)
  2. Verify no fatal error occurs
  3. Run PHPUnit tests: phpunit tests/phpunit/tests/block-supports/duotone.php

Props xavier-lc for the Gutenberg PR.

#2 @SergeyBiryukov
4 weeks ago

  • Milestone changed from Awaiting Review to 7.0

#3 @jorgefilipecosta
4 weeks ago

  • Owner set to jorgefilipecosta
  • Resolution set to fixed
  • Status changed from new to closed

In 61603:

Block Supports: Prevent fatal error in WP_Duotone when the duotone attribute is an array.

Adds type checks to get_slug_from_attribute(), is_preset(), and get_all_global_style_block_names() to handle cases where the duotone attribute is an array of custom colors instead of a preset reference string.
This prevents an error when preg_match() receives an array instead of a string.

Props jorgefilipecosta, westonruter, xavilc.
Fixes #64612.

Note: See TracTickets for help on using tickets.