Skip to content

Commit 688b88d

Browse files
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. git-svn-id: https://develop.svn.wordpress.org/trunk@54276 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 8cd3fb9 commit 688b88d

File tree

5 files changed

+56
-8
lines changed

5 files changed

+56
-8
lines changed

Gruntfile.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
/* globals Set */
44
var webpackConfig = require( './webpack.config' );
55
var installChanged = require( 'install-changed' );
6+
var json2php = require( 'json2php' );
67

78
module.exports = function(grunt) {
89
var path = require('path'),
@@ -1403,6 +1404,19 @@ module.exports = function(grunt) {
14031404
}
14041405
} );
14051406

1407+
grunt.registerTask( 'copy:block-json', 'Copies block.json file contents to block-json.php.', function() {
1408+
var blocks = {};
1409+
grunt.file.recurse( SOURCE_DIR + 'wp-includes/blocks', function( abspath, rootdir, subdir, filename ) {
1410+
if ( /^block\.json$/.test( filename ) ) {
1411+
blocks[ subdir ] = grunt.file.readJSON( abspath );
1412+
}
1413+
} );
1414+
grunt.file.write(
1415+
SOURCE_DIR + 'wp-includes/blocks/blocks-json.php',
1416+
'<?php return ' + json2php( blocks ) + ';'
1417+
);
1418+
} );
1419+
14061420
grunt.registerTask( 'copy:js', [
14071421
'copy:npm-packages',
14081422
'copy:vendor-js',
@@ -1451,6 +1465,7 @@ module.exports = function(grunt) {
14511465
grunt.registerTask( 'build:files', [
14521466
'clean:files',
14531467
'copy:files',
1468+
'copy:block-json',
14541469
'copy:version',
14551470
] );
14561471

package-lock.json

Lines changed: 11 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@
143143
"jquery-color": "2.2.0",
144144
"jquery-form": "4.3.0",
145145
"jquery-hoverintent": "1.10.2",
146+
"json2php": "^0.0.5",
146147
"lodash": "4.17.21",
147148
"masonry-layout": "4.2.2",
148149
"moment": "2.29.4",

src/wp-includes/blocks.php

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -283,15 +283,39 @@ function get_block_metadata_i18n_schema() {
283283
* @return WP_Block_Type|false The registered block type on success, or false on failure.
284284
*/
285285
function register_block_type_from_metadata( $file_or_folder, $args = array() ) {
286-
$filename = 'block.json';
287-
$metadata_file = ( substr( $file_or_folder, -strlen( $filename ) ) !== $filename ) ?
288-
trailingslashit( $file_or_folder ) . $filename :
286+
/*
287+
* Get an array of metadata from a PHP file.
288+
* This improves performance for core blocks as it's only necessary to read a single PHP file
289+
* instead of reading a JSON file per-block, and then decoding from JSON to PHP.
290+
* Using a static variable ensures that the metadata is only read once per request.
291+
*/
292+
static $core_blocks_meta;
293+
if ( ! $core_blocks_meta ) {
294+
$core_blocks_meta = include_once ABSPATH . WPINC . '/blocks/blocks-json.php';
295+
}
296+
297+
$metadata_file = ( ! str_ends_with( $file_or_folder, 'block.json' ) ) ?
298+
trailingslashit( $file_or_folder ) . 'block.json' :
289299
$file_or_folder;
300+
290301
if ( ! file_exists( $metadata_file ) ) {
291302
return false;
292303
}
293304

294-
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
305+
// Try to get metadata from the static cache for core blocks.
306+
$metadata = false;
307+
if ( str_starts_with( $file_or_folder, ABSPATH . WPINC ) ) {
308+
$core_block_name = str_replace( ABSPATH . WPINC . '/blocks/', '', $file_or_folder );
309+
if ( ! empty( $core_blocks_meta[ $core_block_name ] ) ) {
310+
$metadata = $core_blocks_meta[ $core_block_name ];
311+
}
312+
}
313+
314+
// If metadata is not found in the static cache, read it from the file.
315+
if ( ! $metadata ) {
316+
$metadata = wp_json_file_decode( $metadata_file, array( 'associative' => true ) );
317+
}
318+
295319
if ( ! is_array( $metadata ) || empty( $metadata['name'] ) ) {
296320
return false;
297321
}

src/wp-includes/blocks/blocks-json.php

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)