Make WordPress Core

Changeset 56044

Timestamp:
06/26/2023 09:15:21 PM (13 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.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/upgrade.php

    r55988 r56044  
    655655        }
    656656
     657
     658
    657659        /**
    658660         * Fires after a site is fully upgraded.
  • trunk/src/wp-includes/blocks.php

    r56021 r56044  
    187187    }
    188188
     189
     190
     191
     192
     193
     194
     195
     196
     197
     198
     199
     200
     201
     202
    189203    static $wpinc_path_norm = '';
    190204    if ( ! $wpinc_path_norm ) {
     
    196210    if ( $is_core_block && ! wp_should_load_separate_core_block_assets() ) {
    197211        return false;
    198     }
    199 
    200     $style_handle = $metadata[ $field_name ];
    201     if ( is_array( $style_handle ) ) {
    202         if ( empty( $style_handle[ $index ] ) ) {
    203             return false;
    204         }
    205         $style_handle = $style_handle[ $index ];
    206212    }
    207213
     
    247253    }
    248254
    249     $style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
    250     $version      = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
    251     $result       = wp_register_style(
    252         $style_handle,
     255    $version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
     256    $result  = wp_register_style(
     257        $style_handle_name,
    253258        $style_uri,
    254259        array(),
     
    260265
    261266    if ( $has_style_file ) {
    262         wp_style_add_data( $style_handle, 'path', $style_path_norm );
     267        wp_style_add_data( $style_handle, 'path', $style_path_norm );
    263268
    264269        if ( $is_core_block ) {
     
    269274
    270275        if ( is_rtl() && file_exists( $rtl_file ) ) {
    271             wp_style_add_data( $style_handle, 'rtl', 'replace' );
    272             wp_style_add_data( $style_handle, 'suffix', $suffix );
    273             wp_style_add_data( $style_handle, 'path', $rtl_file );
    274         }
    275     }
    276 
    277     return $style_handle;
     276            wp_style_add_data( $style_handle, 'rtl', 'replace' );
     277            wp_style_add_data( $style_handle, 'suffix', $suffix );
     278            wp_style_add_data( $style_handle, 'path', $rtl_file );
     279        }
     280    }
     281
     282    return $style_handle;
    278283}
    279284
     
    321326    static $core_blocks_meta;
    322327    if ( ! $core_blocks_meta ) {
    323         $core_blocks_meta = require_once ABSPATH . WPINC . '/blocks/blocks-json.php';
     328        $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
    324329    }
    325330
  • trunk/src/wp-includes/blocks/index.php

    r55879 r56044  
    1212require BLOCKS_PATH . 'widget-group.php';
    1313require BLOCKS_PATH . 'require-dynamic-blocks.php';
     14
     15
     16
     17
     18
     19
     20
     21
     22
     23
     24
     25
     26
     27
     28
     29
     30
     31
     32
     33
     34
     35
     36
     37
     38
     39
     40
     41
     42
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
     76
     77
     78
     79
     80
     81
     82
     83
     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    14100
    15101/**
  • trunk/tests/phpunit/tests/blocks/register.php

    r56005 r56044  
    398398    public function test_handle_passed_register_block_style_handle() {
    399399        $metadata = array(
     400
    400401            'style' => 'test-style-handle',
    401402        );
     
    407408    public function test_handles_passed_register_block_style_handles() {
    408409        $metadata = array(
     410
    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
     530
     531
     532
     533
     534
     535
     536
     537
     538
     539
     540
     541
     542
     543
     544
     545
    524546    }
    525547
Note: See TracChangeset for help on using the changeset viewer.