Skip to content

Commit

Permalink
Editor: Improve block loading PHP performance.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
SergeyBiryukov committed Sep 21, 2022
1 parent 8cd3fb9 commit 688b88d
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/* globals Set */
var webpackConfig = require( './webpack.config' );
var installChanged = require( 'install-changed' );
var json2php = require( 'json2php' );

module.exports = function(grunt) {
var path = require('path'),
Expand Down Expand Up @@ -1403,6 +1404,19 @@ module.exports = function(grunt) {
}
} );

grunt.registerTask( 'copy:block-json', 'Copies block.json file contents to block-json.php.', function() {
var blocks = {};
grunt.file.recurse( SOURCE_DIR + 'wp-includes/blocks', function( abspath, rootdir, subdir, filename ) {
if ( /^block\.json$/.test( filename ) ) {
blocks[ subdir ] = grunt.file.readJSON( abspath );
}
} );
grunt.file.write(
SOURCE_DIR + 'wp-includes/blocks/blocks-json.php',
'<?php return ' + json2php( blocks ) + ';'
);
} );

grunt.registerTask( 'copy:js', [
'copy:npm-packages',
'copy:vendor-js',
Expand Down Expand Up @@ -1451,6 +1465,7 @@ module.exports = function(grunt) {
grunt.registerTask( 'build:files', [
'clean:files',
'copy:files',
'copy:block-json',
'copy:version',
] );

Expand Down
15 changes: 11 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@
"jquery-color": "2.2.0",
"jquery-form": "4.3.0",
"jquery-hoverintent": "1.10.2",
"json2php": "^0.0.5",
"lodash": "4.17.21",
"masonry-layout": "4.2.2",
"moment": "2.29.4",
Expand Down
32 changes: 28 additions & 4 deletions src/wp-includes/blocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions src/wp-includes/blocks/blocks-json.php

Large diffs are not rendered by default.

0 comments on commit 688b88d

Please sign in to comment.