Skip to content
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

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from 32 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
6bae079
Register block style early.
spacedmonkey Jun 15, 2023
935d846
Move logic
spacedmonkey Jun 15, 2023
2ce21c6
remove logic.
spacedmonkey Jun 15, 2023
8f263c8
Cleaner logic.
spacedmonkey Jun 15, 2023
d03f659
Apply suggestions from code review
spacedmonkey Jun 15, 2023
c324c00
Break into it's own action.
spacedmonkey Jun 15, 2023
e6521a1
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 16, 2023
5dac270
Feedback.
spacedmonkey Jun 17, 2023
989df55
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 17, 2023
a877315
More feedback
spacedmonkey Jun 18, 2023
3a8d4c4
Apply suggestions from code review
spacedmonkey Jun 19, 2023
bb712c3
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 20, 2023
c01d722
Feedback.
spacedmonkey Jun 20, 2023
6567ae6
Reusable.
spacedmonkey Jun 20, 2023
c76122a
Apply suggestions from code review
spacedmonkey Jun 21, 2023
77b44e2
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 21, 2023
ca9791f
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 22, 2023
c3331c5
Try something.
spacedmonkey Jun 22, 2023
f3a4d1d
Use a transinet as a cache
spacedmonkey Jun 22, 2023
d6c0999
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 22, 2023
70c54c5
Update src/wp-includes/blocks/index.php
spacedmonkey Jun 22, 2023
b758aef
More glob.
spacedmonkey Jun 23, 2023
5930b03
Feedback.
spacedmonkey Jun 23, 2023
49b42c1
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 23, 2023
a7a0f0e
Only add new style if supported
spacedmonkey Jun 23, 2023
8c7cc43
Revert wp_common_block_script_and_styles changes.
spacedmonkey Jun 23, 2023
dd2f7a0
Disable cache on `WP_DEBUG`.
spacedmonkey Jun 23, 2023
47d29cd
Feedback.
spacedmonkey Jun 23, 2023
da291a9
Do not add $suffix to glob, so it gets all css files. Even when `SCRI…
spacedmonkey Jun 23, 2023
f83cf18
Update src/wp-includes/blocks/index.php
spacedmonkey Jun 23, 2023
177d91a
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 23, 2023
d3109b8
Revert committed file.
spacedmonkey Jun 23, 2023
838ccf4
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 26, 2023
4ae7d10
Improve performance and add unit tests.
spacedmonkey Jun 26, 2023
b4842ef
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 26, 2023
f2d9152
Apply suggestions from code review
spacedmonkey Jun 26, 2023
d584473
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 26, 2023
27d9cf9
Fix lints.
spacedmonkey Jun 26, 2023
2868761
Fix comment
spacedmonkey Jun 26, 2023
9f95e93
Convert code to use a dataProvider.
spacedmonkey Jun 26, 2023
a15082f
Workaround.
spacedmonkey Jun 26, 2023
6f1bf01
Change transient name.
spacedmonkey Jun 26, 2023
5e0fed6
Use `wp_get_development_mode`.
spacedmonkey Jun 26, 2023
519ba79
Merge branch 'trunk' into fix/realpath-error-5
spacedmonkey Jun 26, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/wp-admin/includes/upgrade.php
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,8 @@ function wp_upgrade() {
update_site_meta( get_current_blog_id(), 'db_last_updated', microtime() );
}

delete_transient( 'wp_core_block_styles' );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
delete_transient( 'wp_core_block_styles' );
delete_transient( 'wp_core_block_css_files' );

Copy link
Member

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?

Suggested change
delete_transient( 'wp_core_block_styles' );
delete_site_transient( 'wp_core_block_css_files' );

Copy link
Member Author

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.


/**
* Fires after a site is fully upgraded.
*
Expand Down
8 changes: 7 additions & 1 deletion src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,11 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$style_handle = $style_handle[ $index ];
}

// If the style handle is already registered, skip re-registering.
if ( wp_style_is( $style_handle, 'registered' ) ) {
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
return $style_handle;
}

$style_path = remove_block_asset_path_prefix( $style_handle );
$is_style_handle = $style_handle === $style_path;
// Allow only passing style handles for core blocks.
Expand Down Expand Up @@ -320,7 +325,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
*/
static $core_blocks_meta;
if ( ! $core_blocks_meta ) {
$core_blocks_meta = require_once ABSPATH . WPINC . '/blocks/blocks-json.php';
$core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';
}

$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
Expand Down Expand Up @@ -368,6 +373,7 @@ function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
if ( ! isset( $metadata['style'] ) ) {
$metadata['style'] = "wp-block-$block_name";
}

spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
spacedmonkey marked this conversation as resolved.
Show resolved Hide resolved
if ( ! isset( $metadata['editorStyle'] ) ) {
$metadata['editorStyle'] = "wp-block-{$block_name}-editor";
}
Expand Down
88 changes: 88 additions & 0 deletions src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be better named:

Suggested change
$transient_name = 'wp_core_block_styles';
$transient_name = 'wp_core_block_css_files';

$files = get_transient( $transient_name );
if ( ! $files ) {
$files = glob( __DIR__ . '/**/**.css' );
set_transient( $transient_name, $files );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like this could be setting a site transient instead:

Suggested change
set_transient( $transient_name, $files );
set_site_transient( $transient_name, $files );

Copy link
Member Author

Choose a reason for hiding this comment

The 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.
Expand Down
Loading