-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Register block style early. #4621
Changes from 32 commits
6bae079
935d846
2ce21c6
8f263c8
d03f659
c324c00
e6521a1
5dac270
989df55
a877315
3a8d4c4
bb712c3
c01d722
6567ae6
c76122a
77b44e2
ca9791f
c3331c5
f3a4d1d
d6c0999
70c54c5
b758aef
5930b03
49b42c1
a7a0f0e
8c7cc43
dd2f7a0
47d29cd
da291a9
f83cf18
177d91a
d3109b8
838ccf4
4ae7d10
b4842ef
f2d9152
d584473
27d9cf9
2868761
9f95e93
a15082f
6f1bf01
5e0fed6
519ba79
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,94 @@ | |||||
require BLOCKS_PATH . 'widget-group.php'; | ||||||
require BLOCKS_PATH . 'require-dynamic-blocks.php'; | ||||||
|
||||||
/** | ||||||
* Registers core block style handles. | ||||||
* | ||||||
* While {@see register_block_style_handle()} is typically used for that, the way it is | ||||||
* implemented is inefficient for core block styles. Registering those style handles here | ||||||
* avoids unnecessary logic and filesystem lookups in the other function. | ||||||
* | ||||||
* @since 6.3.0 | ||||||
*/ | ||||||
function register_core_block_style_handles() { | ||||||
if ( ! wp_should_load_separate_core_block_assets() ) { | ||||||
return; | ||||||
} | ||||||
|
||||||
static $core_blocks_meta; | ||||||
if ( ! $core_blocks_meta ) { | ||||||
$core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php'; | ||||||
} | ||||||
|
||||||
$includes_url = includes_url(); | ||||||
$includes_path = ABSPATH . WPINC . '/'; | ||||||
$suffix = wp_scripts_get_suffix(); | ||||||
$wp_styles = wp_styles(); | ||||||
$style_fields = array( | ||||||
'style' => 'style', | ||||||
'editorStyle' => 'editor', | ||||||
); | ||||||
|
||||||
/* | ||||||
* Ignore transient cache when `WP_DEBUG` is enabled. Why? To avoid interfering with | ||||||
* the core developer's workflow. | ||||||
* | ||||||
* @todo Replace `WP_DEBUG` once an "in development mode" check is available in Core. | ||||||
*/ | ||||||
if ( ! WP_DEBUG ) { | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
$transient_name = 'wp_core_block_styles'; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would this be better named:
Suggested change
|
||||||
$files = get_transient( $transient_name ); | ||||||
if ( ! $files ) { | ||||||
$files = glob( __DIR__ . '/**/**.css' ); | ||||||
set_transient( $transient_name, $files ); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems like this could be setting a site transient instead:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||
} | ||||||
} else { | ||||||
$files = glob( __DIR__ . '/**/**.css' ); | ||||||
} | ||||||
|
||||||
foreach ( $core_blocks_meta as $name => $schema ) { | ||||||
/** This filter is documented in wp-includes/blocks.php */ | ||||||
$schema = apply_filters( 'block_type_metadata', $schema ); | ||||||
|
||||||
// Backfill these properties similar to `register_block_type_from_metadata()`. | ||||||
if ( ! isset( $schema['style'] ) ) { | ||||||
$schema['style'] = "wp-block-$name"; | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
} | ||||||
if ( ! isset( $schema['editorStyle'] ) ) { | ||||||
$schema['editorStyle'] = "wp-block-{$name}-editor"; | ||||||
} | ||||||
spacedmonkey marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
foreach ( $style_fields as $style_field => $filename ) { | ||||||
$style_handle = $schema[ $style_field ]; | ||||||
if ( is_array( $style_handle ) ) { | ||||||
continue; | ||||||
} | ||||||
|
||||||
$style_path = "blocks/{$name}/{$filename}{$suffix}.css"; | ||||||
$path = $includes_path . $style_path; | ||||||
|
||||||
if ( ! in_array( $path, $files, true ) ) { | ||||||
$wp_styles->add( | ||||||
$style_handle, | ||||||
false | ||||||
); | ||||||
continue; | ||||||
} | ||||||
|
||||||
$wp_styles->add( $style_handle, $includes_url . $style_path ); | ||||||
$wp_styles->add_data( $style_handle, 'path', $path ); | ||||||
|
||||||
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $path ); | ||||||
if ( is_rtl() && in_array( $rtl_file, $files, true ) ) { | ||||||
$wp_styles->add_data( $style_handle, 'rtl', 'replace' ); | ||||||
$wp_styles->add_data( $style_handle, 'suffix', $suffix ); | ||||||
$wp_styles->add_data( $style_handle, 'path', $rtl_file ); | ||||||
} | ||||||
} | ||||||
} | ||||||
} | ||||||
add_action( 'init', 'register_core_block_style_handles', 9 ); | ||||||
|
||||||
/** | ||||||
* Registers core block types using metadata files. | ||||||
* Dynamic core blocks are registered separately. | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, per below, wouldn't site transients be more appropriate?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@westonruter Using network options ( tranisents ) has a performance impact. I think network options are not autoloaded. So it result in a database query, slow things down. Maybe we could work this out later.