Make WordPress Core


Ignore:
Timestamp:
06/26/2023 09:15:21 PM (8 months ago)
Author:
spacedmonkey
Message:

Editor: Register core block styles in one place.

Register all core blocks in a new function called register_core_block_style_handles. This mirrors the function wp_default_styles where all core styles are registered in one place. This improves block registration performance, as it avoids expensive file lookups, like realpath in register_block_style_handle. The new function register_core_block_style_handles uses glob to get all css files in the blocks directory. This glob is cached in a transient to save lookups on subsequent requests. The function register_block_style_handle now checks to see if the style handle is already registered before trying to register it again.

Props mukesh27, westonruter, flixos90, joemcgill, spacedmonkey.
Fixes #58528.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/blocks/register.php

    r56005 r56044  
    398398    public function test_handle_passed_register_block_style_handle() {
    399399        $metadata = array(
     400            'name'  => 'test-block',
    400401            'style' => 'test-style-handle',
    401402        );
     
    407408    public function test_handles_passed_register_block_style_handles() {
    408409        $metadata = array(
     410            'name'  => 'test-block',
    409411            'style' => array( 'test-style-handle', 'test-style-handle-2' ),
    410412        );
     
    522524        $this->assertSame( $expected_style_handle, $result );
    523525        $this->assertFalse( wp_styles()->get_data( $expected_style_handle, 'rtl' ) );
     526    }
     527
     528    /**
     529     * @ticket 58528
     530     *
     531     * @covers ::register_block_style_handle
     532     */
     533    public function test_success_register_block_style_handle_exists() {
     534        $expected_style_handle = 'block-theme-example-block-editor-style';
     535        wp_register_style( $expected_style_handle, false );
     536        switch_theme( 'block-theme' );
     537
     538        $metadata = array(
     539            'file'        => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ),
     540            'name'        => 'block-theme/example-block',
     541            'editorStyle' => 'file:./editor-style.css',
     542        );
     543        $result   = register_block_style_handle( $metadata, 'editorStyle' );
     544
     545        $this->assertSame( $expected_style_handle, $result );
    524546    }
    525547
Note: See TracChangeset for help on using the changeset viewer.