Make WordPress Core


Ignore:
Timestamp:
07/13/2023 11:32:19 AM (17 months ago)
Author:
spacedmonkey
Message:

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/block-supports/duotone.php

    r56101 r56226  
    2828            'attrs'     => array( 'style' => array( 'color' => array( 'duotone' => 'var:preset|duotone|blue-orange' ) ) ),
    2929        );
     30        $wp_block      = new WP_Block( $block );
    3031        $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
    3132        $expected      = '<figure class="wp-block-image size-full wp-duotone-blue-orange"><img src="/my-image.jpg" /></figure>';
    32         $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
     33        $this->assertSame( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
    3334    }
    3435
     
    4546            'attrs'     => array( 'style' => array( 'color' => array( 'duotone' => 'unset' ) ) ),
    4647        );
     48        $wp_block      = new WP_Block( $block );
    4749        $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
    4850        $expected      = '/<figure class="wp-block-image size-full wp-duotone-unset-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
    49         $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
     51        $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
    5052    }
    5153
     
    6062            'attrs'     => array( 'style' => array( 'color' => array( 'duotone' => array( '#FFFFFF', '#000000' ) ) ) ),
    6163        );
     64        $wp_block      = new WP_Block( $block );
    6265        $block_content = '<figure class="wp-block-image size-full"><img src="/my-image.jpg" /></figure>';
    6366        $expected      = '/<figure class="wp-block-image size-full wp-duotone-ffffff-000000-\d+"><img src="\\/my-image.jpg" \\/><\\/figure>/';
    64         $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block ) );
     67        $this->assertMatchesRegularExpression( $expected, WP_Duotone::render_duotone_support( $block_content, $block, $wp_block ) );
    6568    }
    6669
Note: See TracChangeset for help on using the changeset viewer.