Make WordPress Core


Ignore:
Timestamp:
06/26/2023 09:15:21 PM (15 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/src/wp-includes/blocks/index.php

    r55879 r56044  
    1212require BLOCKS_PATH . 'widget-group.php';
    1313require BLOCKS_PATH . 'require-dynamic-blocks.php';
     14
     15/**
     16 * Registers core block style handles.
     17 *
     18 * While {@see register_block_style_handle()} is typically used for that, the way it is
     19 * implemented is inefficient for core block styles. Registering those style handles here
     20 * avoids unnecessary logic and filesystem lookups in the other function.
     21 *
     22 * @since 6.3.0
     23 */
     24function register_core_block_style_handles() {
     25    if ( ! wp_should_load_separate_core_block_assets() ) {
     26        return;
     27    }
     28
     29    static $core_blocks_meta;
     30    if ( ! $core_blocks_meta ) {
     31        $core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
     32    }
     33
     34    $includes_url  = includes_url();
     35    $includes_path = ABSPATH . WPINC . '/';
     36    $suffix        = wp_scripts_get_suffix();
     37    $wp_styles     = wp_styles();
     38    $style_fields  = array(
     39        'style'       => 'style',
     40        'editorStyle' => 'editor',
     41    );
     42
     43    /*
     44     * Ignore transient cache when the development mode is set to 'core'. Why? To avoid interfering with
     45     * the core developer's workflow.
     46     */
     47    if ( 'core' !== wp_get_development_mode() ) {
     48        $transient_name = 'wp_core_block_css_files';
     49        $files          = get_transient( $transient_name );
     50        if ( ! $files ) {
     51            $files = glob( __DIR__ . '/**/**.css' );
     52            set_transient( $transient_name, $files );
     53        }
     54    } else {
     55        $files = glob( __DIR__ . '/**/**.css' );
     56    }
     57
     58    foreach ( $core_blocks_meta as $name => $schema ) {
     59        /** This filter is documented in wp-includes/blocks.php */
     60        $schema = apply_filters( 'block_type_metadata', $schema );
     61
     62        // Backfill these properties similar to `register_block_type_from_metadata()`.
     63        if ( ! isset( $schema['style'] ) ) {
     64            $schema['style'] = "wp-block-{$name}";
     65        }
     66        if ( ! isset( $schema['editorStyle'] ) ) {
     67            $schema['editorStyle'] = "wp-block-{$name}-editor";
     68        }
     69
     70        foreach ( $style_fields as $style_field => $filename ) {
     71            $style_handle = $schema[ $style_field ];
     72            if ( is_array( $style_handle ) ) {
     73                continue;
     74            }
     75
     76            $style_path = "blocks/{$name}/{$filename}{$suffix}.css";
     77            $path       = $includes_path . $style_path;
     78
     79            if ( ! in_array( $path, $files, true ) ) {
     80                $wp_styles->add(
     81                    $style_handle,
     82                    false
     83                );
     84                continue;
     85            }
     86
     87            $wp_styles->add( $style_handle, $includes_url . $style_path );
     88            $wp_styles->add_data( $style_handle, 'path', $path );
     89
     90            $rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path );
     91            if ( is_rtl() && in_array( $rtl_file, $files, true ) ) {
     92                $wp_styles->add_data( $style_handle, 'rtl', 'replace' );
     93                $wp_styles->add_data( $style_handle, 'suffix', $suffix );
     94                $wp_styles->add_data( $style_handle, 'path', $rtl_file );
     95            }
     96        }
     97    }
     98}
     99add_action( 'init', 'register_core_block_style_handles', 9 );
    14100
    15101/**
Note: See TracChangeset for help on using the changeset viewer.