Editor: Register core block styles in one place.

Register all core blocks in a new function called `register_core_block_style_handles`. This mirrors the function `wp_default_styles` where all core styles are registered in one place. This improves block registration performance, as it avoids expensive file lookups, like realpath in `register_block_style_handle`. The new function `register_core_block_style_handles` uses `glob` to get all css files in the blocks directory. This glob is cached in a transient to save lookups on subsequent requests. The function `register_block_style_handle` now checks to see if the style handle is already registered before trying to register it again. 

Props mukesh27, westonruter, flixos90, joemcgill, spacedmonkey.
Fixes #58528.

git-svn-id: https://develop.svn.wordpress.org/trunk@56044 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonny Harris
2023-06-26 21:15:21 +00:00
parent c1a3975367
commit d8409a205a
5 changed files with 257 additions and 18 deletions
+22
View File
@@ -397,6 +397,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
*/
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' );
@@ -406,6 +407,7 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
public function test_handles_passed_register_block_style_handles() {
$metadata = array(
'name' => 'test-block',
'style' => array( 'test-style-handle', 'test-style-handle-2' ),
);
@@ -523,6 +525,26 @@ class Tests_Blocks_Register extends WP_UnitTestCase {
$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.
@@ -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;
}
}