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 all 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_css_files' );

/**
* Fires after a site is fully upgraded.
*
Expand Down
41 changes: 23 additions & 18 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,20 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
return false;
}

$style_handle = $metadata[ $field_name ];
if ( is_array( $style_handle ) ) {
if ( empty( $style_handle[ $index ] ) ) {
return false;
}
$style_handle = $style_handle[ $index ];
}

$style_handle_name = generate_block_asset_handle( $metadata['name'], $field_name, $index );
// If the style handle is already registered, skip re-registering.
if ( wp_style_is( $style_handle_name, 'registered' ) ) {
return $style_handle_name;
}

static $wpinc_path_norm = '';
if ( ! $wpinc_path_norm ) {
$wpinc_path_norm = wp_normalize_path( realpath( ABSPATH . WPINC ) );
Expand All @@ -197,14 +211,6 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
return false;
}

$style_handle = $metadata[ $field_name ];
if ( is_array( $style_handle ) ) {
if ( empty( $style_handle[ $index ] ) ) {
return false;
}
$style_handle = $style_handle[ $index ];
}

$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 @@ -246,10 +252,9 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
$style_uri = false;
}

$style_handle = generate_block_asset_handle( $metadata['name'], $field_name, $index );
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$result = wp_register_style(
$style_handle,
$version = ! $is_core_block && isset( $metadata['version'] ) ? $metadata['version'] : false;
$result = wp_register_style(
$style_handle_name,
$style_uri,
array(),
$version
Expand All @@ -259,7 +264,7 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
}

if ( $has_style_file ) {
wp_style_add_data( $style_handle, 'path', $style_path_norm );
wp_style_add_data( $style_handle_name, 'path', $style_path_norm );

if ( $is_core_block ) {
$rtl_file = str_replace( "{$suffix}.css", "-rtl{$suffix}.css", $style_path_norm );
Expand All @@ -268,13 +273,13 @@ function register_block_style_handle( $metadata, $field_name, $index = 0 ) {
}

if ( is_rtl() && file_exists( $rtl_file ) ) {
wp_style_add_data( $style_handle, 'rtl', 'replace' );
wp_style_add_data( $style_handle, 'suffix', $suffix );
wp_style_add_data( $style_handle, 'path', $rtl_file );
wp_style_add_data( $style_handle_name, 'rtl', 'replace' );
wp_style_add_data( $style_handle_name, 'suffix', $suffix );
wp_style_add_data( $style_handle_name, 'path', $rtl_file );
}
}

return $style_handle;
return $style_handle_name;
}

/**
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
86 changes: 86 additions & 0 deletions src/wp-includes/blocks/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,92 @@
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 the development mode is set to 'core'. Why? To avoid interfering with
* the core developer's workflow.
*/
if ( 'core' !== wp_get_development_mode() ) {
$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}";
}
if ( ! isset( $schema['editorStyle'] ) ) {
$schema['editorStyle'] = "wp-block-{$name}-editor";
}

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
22 changes: 22 additions & 0 deletions tests/phpunit/tests/blocks/register.php
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,7 @@ public function data_register_block_style_handle_uses_correct_core_stylesheet()
*/
public function test_handle_passed_register_block_style_handle() {
$metadata = array(
'name' => 'test-block',
'style' => 'test-style-handle',
);
$result = register_block_style_handle( $metadata, 'style' );
Expand All @@ -406,6 +407,7 @@ public function test_handle_passed_register_block_style_handle() {

public function test_handles_passed_register_block_style_handles() {
$metadata = array(
'name' => 'test-block',
'style' => array( 'test-style-handle', 'test-style-handle-2' ),
);

Expand Down Expand Up @@ -523,6 +525,26 @@ public function test_success_register_block_style_handle_in_theme() {
$this->assertFalse( wp_styles()->get_data( $expected_style_handle, 'rtl' ) );
}

/**
* @ticket 58528
*
* @covers ::register_block_style_handle
*/
public function test_success_register_block_style_handle_exists() {
$expected_style_handle = 'block-theme-example-block-editor-style';
wp_register_style( $expected_style_handle, false );
switch_theme( 'block-theme' );

$metadata = array(
'file' => wp_normalize_path( get_theme_file_path( 'blocks/example-block/block.json' ) ),
'name' => 'block-theme/example-block',
'editorStyle' => 'file:./editor-style.css',
);
$result = register_block_style_handle( $metadata, 'editorStyle' );

$this->assertSame( $expected_style_handle, $result );
}

/**
* Tests that the function returns false when the `block.json` is not found
* in the WordPress core.
Expand Down
124 changes: 124 additions & 0 deletions tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
<?php

/**
* Tests for block style handles.
*
* @package WordPress
* @subpackage Blocks
*
* @since 6.3.0
*
* @group blocks
*
* @covers ::register_core_block_style_handles
*/
class Tests_Blocks_registerCoreBlockStyleHandles extends WP_UnitTestCase {

/**
* @var WP_Styles
*/
private $old_wp_styles;

/**
* @var string
*/
private $includes_url;

const STYLE_FIELDS = array(
'style' => 'style',
'editorStyle' => 'editor',
);

public function set_up() {
parent::set_up();

$this->old_wp_styles = $GLOBALS['wp_styles'];

$this->includes_url = includes_url();

remove_action( 'wp_default_styles', 'wp_default_styles' );

if ( empty( $GLOBALS['wp_styles'] ) ) {
$GLOBALS['wp_styles'] = null;
}
}

public function tear_down() {
$GLOBALS['wp_styles'] = $this->old_wp_styles;

add_action( 'wp_default_styles', 'wp_default_styles' );

parent::tear_down();
}

/**
* @ticket 58528
*
* @dataProvider data_block_data
*/
public function test_wp_should_load_separate_core_block_assets_false( $name, $schema ) {
register_core_block_style_handles();

foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
$style_handle = $schema[ $style_field ];
if ( is_array( $style_handle ) ) {
continue;
}

$this->assertArrayNotHasKey( $style_handle, $GLOBALS['wp_styles']->registered, 'The key should not exist, as this style should not be registered' );
}
}


/**
* @ticket 58528
*
* @dataProvider data_block_data
*/
public function test_wp_should_load_separate_core_block_assets_true( $name, $schema ) {
add_filter( 'should_load_separate_core_block_assets', '__return_true' );
register_core_block_style_handles();

$wp_styles = $GLOBALS['wp_styles'];

foreach ( self::STYLE_FIELDS as $style_field => $filename ) {
$style_handle = $schema[ $style_field ];
if ( is_array( $style_handle ) ) {
continue;
}

$this->assertArrayHasKey( $style_handle, $wp_styles->registered, 'The key should exist, as this style should be registered' );
if ( false === $wp_styles->registered[ $style_handle ]->src ) {
$this->assertEmpty( $wp_styles->registered[ $style_handle ]->extra, 'If source is false, not style path should be set' );
} else {
$this->assertStringContainsString( $this->includes_url, $wp_styles->registered[ $style_handle ]->src, 'Source of style should contain the includes url' );
$this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra, 'The path of the style should exist' );
$this->assertArrayHasKey( 'path', $wp_styles->registered[ $style_handle ]->extra, 'The path key of the style should exist in extra array' );
$this->assertNotEmpty( $wp_styles->registered[ $style_handle ]->extra['path'], 'The path key of the style should not be empty' );
}
}
}


public function data_block_data() {
$core_blocks_meta = require ABSPATH . WPINC . '/blocks/blocks-json.php';

// Remove this blocks for now, as they are registered elsewhere.
unset( $core_blocks_meta['archives'] );
unset( $core_blocks_meta['widget-group'] );

$data = array();
foreach ( $core_blocks_meta as $name => $schema ) {
if ( ! isset( $schema['style'] ) ) {
$schema['style'] = "wp-block-$name";
}
if ( ! isset( $schema['editorStyle'] ) ) {
$schema['editorStyle'] = "wp-block-{$name}-editor";
}

$data[ $name ] = array( $name, $schema );
}

return $data;
}
}
Loading