Make WordPress Core


Ignore:
Timestamp:
05/11/2021 09:41:48 AM (3 years ago)
Author:
gziolo
Message:

Editor: Enqueue script and style assets only for blocks present on the page

Adds styles for individual core blocks to make it possible to render only styles for those blocks that are rendered on the page (frontend). This is optinal functionality for start that can be controlled with the new separate_core_block_assets filter.

In addition to that, styles can be inlined when path is passed when registering an individual styles. This functionality can be changed with the new styles_inline_size_limit filter. The maximum size of inlined styles in bytes defaults to 20 000.

Props aristath, aduth, westonruter, mcsf.
Fixes #50328, #52620.

File:
1 edited

Legend:

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

    r50761 r50836  
    158158        return false;
    159159    }
     160    $is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC );
     161    if ( $is_core_block && ! should_load_separate_core_block_assets() ) {
     162        return false;
     163    }
     164
     165    // Check whether styles should have a ".min" suffix or not.
     166    $suffix = SCRIPT_DEBUG ? '' : '.min';
     167
    160168    $style_handle = $metadata[ $field_name ];
    161169    $style_path   = remove_block_asset_path_prefix( $metadata[ $field_name ] );
    162     if ( $style_handle === $style_path ) {
     170
     171    if ( $style_handle === $style_path && ! $is_core_block ) {
    163172        return $style_handle;
     173    }
     174
     175    $style_uri = plugins_url( $style_path, $metadata['file'] );
     176    if ( $is_core_block ) {
     177        $style_path = "style$suffix.css";
     178        $style_uri  = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
    164179    }
    165180
     
    167182    $block_dir    = dirname( $metadata['file'] );
    168183    $style_file   = realpath( "$block_dir/$style_path" );
     184    $version      = file_exists( $style_file ) ? filemtime( $style_file ) : false;
    169185    $result       = wp_register_style(
    170186        $style_handle,
    171         plugins_url( $style_path, $metadata['file'] ),
     187        $style_uri,
    172188        array(),
    173         filemtime( $style_file )
     189        $version
    174190    );
    175191    if ( file_exists( str_replace( '.css', '-rtl.css', $style_file ) ) ) {
    176192        wp_style_add_data( $style_handle, 'rtl', 'replace' );
     193    }
     194    if ( file_exists( $style_file ) ) {
     195        wp_style_add_data( $style_handle, 'path', $style_file );
     196    }
     197
     198    $rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_file );
     199    if ( is_rtl() && file_exists( $rtl_file ) ) {
     200        wp_style_add_data( $style_handle, 'path', $rtl_file );
    177201    }
    178202
     
    929953    return true === $block_support || is_array( $block_support );
    930954}
     955
     956/**
     957 * Checks whether separate assets should be loaded for core blocks.
     958 *
     959 * @since 5.8
     960 *
     961 * @return bool
     962 */
     963function should_load_separate_core_block_assets() {
     964    if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
     965        return false;
     966    }
     967    /**
     968     * Determine if separate styles & scripts will be loaded for blocks on-render or not.
     969     *
     970     * @since 5.8.0
     971     *
     972     * @param bool $load_separate_styles Whether separate styles will be loaded or not.
     973     *
     974     * @return bool Whether separate styles will be loaded or not.
     975     */
     976    return apply_filters( 'separate_core_block_assets', false );
     977}
Note: See TracChangeset for help on using the changeset viewer.