Make WordPress Core

07/13/2023 11:32:19 AM (12 months ago)

Editor: Lazily load Duotone settings only when needed.

Introduced in [56101] the WP_Duotone class, hooks into the wp_loaded action to load duotone style data from global styles. Hooking in early in the bootstrap process caused a number of problems. This hook, triggered an error on installing, as this lookup for global styles, would result in a global post trying to be created, even before the table existed. Additionally, this implementation caused a severe performance regression, as duotone styling data was loaded unnecessarily for requests that did not require such data, such as REST API calls or actions within the wp-admin interface.

In this change, refactor the WP_Duotone to lazily load the global styles and theme.json data, only when a block that supports duotone is encountered. The method render_duotone_support was change to take a third parameter to reuse the existing WP_Block_Type object passed to the filter, to save it being looked up again. The code has also got improved type checking and the use of the util function block_has_support. Furthermore, the code's readability has been improved, along with enhancements to the documentation blocks.

Props Chouby, spacedmonkey, SergeyBiryukov, swissspidy, costdev, joemcgill, flixos90, mukesh27, nazmul111, ajlende, isabel_brison.
Fixes #58673.

1 edited


  • trunk/tests/phpunit/tests/blocks/supportedStyles.php

    r55457 r56226  
    696696        $this->register_block_type( 'core/example', $block_type_settings );
    698         $block = array(
     698        $block    = array(
    699699            'blockName'    => 'core/example',
    700700            'attrs'        => array(),
    703703            'innerHTML'    => array(),
    704704        );
     705        $wp_block = new WP_Block( $block );
    706707        // Custom error handler's see Warnings even if they are suppressed by the @ symbol.
    715716        // HTML5 elements like <time> are not supported by the DOMDocument parser used by the block supports feature.
    716717        // This specific example is emitted by the "Display post date" setting in the latest-posts block.
    717         apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block );
     718        apply_filters( 'render_block', '<div><time datetime="2020-06-18T04:01:43+10:00" class="wp-block-latest-posts__post-date">June 18, 2020</time></div>', $block, $wp_block );
    719720        restore_error_handler();
Note: See TracChangeset for help on using the changeset viewer.