Editor: Improve block loading PHP performance.
This commit improves PHP performance for core blocks by reading a single PHP file with block metadata, instead of reading a JSON file per-block and then decoding from JSON to PHP. Includes: * Adding a new Grunt task to convert `block.json` files to `block-json.php`. * Using the new `block-json.php` file in the `register_block_type_from_metadata()` function. Follow-up to [48141]. Props aristath, gziolo, johnbillion, presstoke, mukesh27, hellofromTonya, petitphp, adamsilverstein, costdev, desrosj, SergeyBiryukov. Fixes #55005. Built from https://develop.svn.wordpress.org/trunk@54276 git-svn-id: http://core.svn.wordpress.org/trunk@53835 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
bb11a85be0
commit
f092e4b013
|
@ -283,15 +283,39 @@ function get_block_metadata_i18n_schema() {
|
|||
* @return WP_Block_Type|false The registered block type on success, or false on failure.
|
||||
*/
|
||||
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
|
||||
$filename = 'block.json';
|
||||
$metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
|
||||
trailingslashit( $file_or_folder ) . $filename :
|
||||
/*
|
||||
* Get an array of metadata from a PHP file.
|
||||
* This improves performance for core blocks as it's only necessary to read a single PHP file
|
||||
* instead of reading a JSON file per-block, and then decoding from JSON to PHP.
|
||||
* Using a static variable ensures that the metadata is only read once per request.
|
||||
*/
|
||||
static $core_blocks_meta;
|
||||
if ( ! $core_blocks_meta ) {
|
||||
$core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php';
|
||||
}
|
||||
|
||||
$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
|
||||
trailingslashit( $file_or_folder ) . 'block.json' :
|
||||
$file_or_folder;
|
||||
|
||||
if ( ! file_exists( $metadata_file ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
|
||||
// Try to get metadata from the static cache for core blocks.
|
||||
$metadata = false;
|
||||
if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) {
|
||||
$core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
|
||||
if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
|
||||
$metadata = $core_blocks_meta[ $core_block_name ];
|
||||
}
|
||||
}
|
||||
|
||||
// If metadata is not found in the static cache, read it from the file.
|
||||
if ( ! $metadata ) {
|
||||
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
|
||||
}
|
||||
|
||||
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
|
||||
return false;
|
||||
}
|
||||
|
|
File diff suppressed because one or more lines are too long
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.1-alpha-54275';
|
||||
$wp_version = '6.1-alpha-54276';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue