mirror of
https://github.com/gosticks/wordpress-develop.git
synced 2026-04-04 20:54:29 +00:00
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. git-svn-id: https://develop.svn.wordpress.org/trunk@50836 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
@@ -157,24 +157,48 @@ function register_block_style_handle( $metadata, $field_name ) {
|
||||
if ( empty( $metadata[ $field_name ] ) ) {
|
||||
return false;
|
||||
}
|
||||
$is_core_block = isset( $metadata['file'] ) && 0 === strpos( $metadata['file'], ABSPATH . WPINC );
|
||||
if ( $is_core_block && ! should_load_separate_core_block_assets() ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check whether styles should have a ".min" suffix or not.
|
||||
$suffix = SCRIPT_DEBUG ? '' : '.min';
|
||||
|
||||
$style_handle = $metadata[ $field_name ];
|
||||
$style_path = remove_block_asset_path_prefix( $metadata[ $field_name ] );
|
||||
if ( $style_handle === $style_path ) {
|
||||
|
||||
if ( $style_handle === $style_path && ! $is_core_block ) {
|
||||
return $style_handle;
|
||||
}
|
||||
|
||||
$style_uri = plugins_url( $style_path, $metadata['file'] );
|
||||
if ( $is_core_block ) {
|
||||
$style_path = "style$suffix.css";
|
||||
$style_uri = includes_url( 'blocks/' . str_replace( 'core/', '', $metadata['name'] ) . "/style$suffix.css" );
|
||||
}
|
||||
|
||||
$style_handle = generate_block_asset_handle( $metadata['name'], $field_name );
|
||||
$block_dir = dirname( $metadata['file'] );
|
||||
$style_file = realpath( "$block_dir/$style_path" );
|
||||
$version = file_exists( $style_file ) ? filemtime( $style_file ) : false;
|
||||
$result = wp_register_style(
|
||||
$style_handle,
|
||||
plugins_url( $style_path, $metadata['file'] ),
|
||||
$style_uri,
|
||||
array(),
|
||||
filemtime( $style_file )
|
||||
$version
|
||||
);
|
||||
if ( file_exists( str_replace( '.css', '-rtl.css', $style_file ) ) ) {
|
||||
wp_style_add_data( $style_handle, 'rtl', 'replace' );
|
||||
}
|
||||
if ( file_exists( $style_file ) ) {
|
||||
wp_style_add_data( $style_handle, 'path', $style_file );
|
||||
}
|
||||
|
||||
$rtl_file = str_replace( "$suffix.css", "-rtl$suffix.css", $style_file );
|
||||
if ( is_rtl() && file_exists( $rtl_file ) ) {
|
||||
wp_style_add_data( $style_handle, 'path', $rtl_file );
|
||||
}
|
||||
|
||||
return $result ? $style_handle : false;
|
||||
}
|
||||
@@ -928,3 +952,26 @@ function block_has_support( $block_type, $feature, $default = false ) {
|
||||
|
||||
return true === $block_support || is_array( $block_support );
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks whether separate assets should be loaded for core blocks.
|
||||
*
|
||||
* @since 5.8
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
function should_load_separate_core_block_assets() {
|
||||
if ( is_admin() || is_feed() || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ) {
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* Determine if separate styles & scripts will be loaded for blocks on-render or not.
|
||||
*
|
||||
* @since 5.8.0
|
||||
*
|
||||
* @param bool $load_separate_styles Whether separate styles will be loaded or not.
|
||||
*
|
||||
* @return bool Whether separate styles will be loaded or not.
|
||||
*/
|
||||
return apply_filters( 'separate_core_block_assets', false );
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user