mirror of
git://develop.git.wordpress.org/
synced 2025-01-17 04:48:25 +01:00
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
This commit is contained in:
parent
8cd3fb9983
commit
688b88d5a3
15
Gruntfile.js
15
Gruntfile.js
@ -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'),
|
||||
@ -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',
|
||||
@ -1451,6 +1465,7 @@ module.exports = function(grunt) {
|
||||
grunt.registerTask( 'build:files', [
|
||||
'clean:files',
|
||||
'copy:files',
|
||||
'copy:block-json',
|
||||
'copy:version',
|
||||
] );
|
||||
|
||||
|
15
package-lock.json
generated
15
package-lock.json
generated
@ -4326,6 +4326,14 @@
|
||||
"requires": {
|
||||
"json2php": "^0.0.4",
|
||||
"webpack-sources": "^3.2.2"
|
||||
},
|
||||
"dependencies": {
|
||||
"json2php": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.4.tgz",
|
||||
"integrity": "sha512-hFzejhs28f70sGnutcsRS459MnAsjRVI85RgPAL1KQIZEpjiDitc27CZv4IgOtaR86vrqOVlu9vJNew2XyTH4g==",
|
||||
"dev": true
|
||||
}
|
||||
}
|
||||
},
|
||||
"@wordpress/deprecated": {
|
||||
@ -17763,10 +17771,9 @@
|
||||
"dev": true
|
||||
},
|
||||
"json2php": {
|
||||
"version": "0.0.4",
|
||||
"resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.4.tgz",
|
||||
"integrity": "sha512-hFzejhs28f70sGnutcsRS459MnAsjRVI85RgPAL1KQIZEpjiDitc27CZv4IgOtaR86vrqOVlu9vJNew2XyTH4g==",
|
||||
"dev": true
|
||||
"version": "0.0.5",
|
||||
"resolved": "https://registry.npmjs.org/json2php/-/json2php-0.0.5.tgz",
|
||||
"integrity": "sha512-jWpsGAYlQDKOjJcyq3rYaxcZ+5YMhZIKHKTjdIKJPI9zLSX+yRWHSSwtV8hvIg7YMhbKkgPO669Ve2ZgFK5C7w=="
|
||||
},
|
||||
"json5": {
|
||||
"version": "2.2.1",
|
||||
|
@ -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",
|
||||
|
@ -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;
|
||||
}
|
||||
|
1
src/wp-includes/blocks/blocks-json.php
Normal file
1
src/wp-includes/blocks/blocks-json.php
Normal file
File diff suppressed because one or more lines are too long
Loading…
x
Reference in New Issue
Block a user