mirrored from git://develop.git.wordpress.org/
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
1 parent
c1a3975
commit d8409a2
Showing
5 changed files
with
257 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
124 changes: 124 additions & 0 deletions
124
tests/phpunit/tests/blocks/registerCoreBlockStyleHandles.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |