Make WordPress Core

Changeset 61603


Ignore:
Timestamp:
02/09/2026 05:08:13 PM (2 months ago)
Author:
jorgefilipecosta
Message:

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.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-duotone.php

    r61457 r61603  
    547547     * @since 6.3.0
    548548     *
    549      * @param string $duotone_attr The duotone attribute from a block.
    550      * @return string The slug of the duotone preset or an empty string if no slug is found.
     549     * @param string|string[] $duotone_attr The duotone attribute from a block.
     550     * @return string The slug of the duotone preset or an empty string if no slug is found (including when an array was passed).
    551551     */
    552552    private static function get_slug_from_attribute( $duotone_attr ) {
     553        if ( ! is_string( $duotone_attr ) ) {
     554            return '';
     555        }
     556
    553557        // Uses Branch Reset Groups `(?|…)` to return one capture group.
    554558        preg_match( '/(?|var:preset\|duotone\|(\S+)|var\(--wp--preset--duotone--(\S+)\))/', $duotone_attr, $matches );
     
    567571     *
    568572     * @param string $duotone_attr The duotone attribute from a block.
    569      * @return bool True if the duotone preset present and valid.
     573     * @param string|string[] $duotone_attr The duotone attribute from a block.
    570574     */
    571575    private static function is_preset( $duotone_attr ) {
     576        if ( ! is_string( $duotone_attr ) ) {
     577            return false;
     578        }
     579
    572580        $slug      = self::get_slug_from_attribute( $duotone_attr );
    573581        $filter_id = self::get_filter_id( $slug );
     
    10511059            }
    10521060            // If it has a duotone filter preset, save the block name and the preset slug.
     1061            // Only process if it's a string (preset reference), not an array (custom colors).
     1062            if ( ! is_string( $duotone_attr ) ) {
     1063                continue;
     1064            }
     1065
    10531066            $slug = self::get_slug_from_attribute( $duotone_attr );
    10541067
  • trunk/tests/phpunit/tests/block-supports/duotone.php

    r61008 r61603  
    9494            'css-var-spaces'                  => array( 'var(--wp--preset--duotone--    ', '' ),
    9595            'pipe-slug-spaces'                => array( 'var:preset|duotone|  ', '' ),
     96            'array-of-colors'                 => array( array( '#000000', '#ffffff' ), '' ),
     97            'empty-array'                     => array( array(), '' ),
    9698        );
    9799    }
     
    165167            'css-var-missing-end-parenthesis' => array( 'var(--wp--preset--duotone--blue-orange', false ),
    166168            'invalid'                         => array( 'not a valid attribute', false ),
     169            'array-of-colors'                 => array( array( '#000000', '#ffffff' ), false ),
     170            'empty-array'                     => array( array(), false ),
    167171        );
    168172    }
Note: See TracChangeset for help on using the changeset viewer.