Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve block loading PHP performance #2252

Closed
wants to merge 25 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
76d4e5f
Add task to create block-json.php files from block.json
aristath Jan 31, 2022
4354737
Use block-json.php in register_block_type_from_metadata
aristath Jan 31, 2022
fefeb15
JSHint fix - missing semicolon
aristath Jan 31, 2022
bf10bc9
Use a single file for all core blocks
aristath Feb 1, 2022
6594e1f
Add compiled file
aristath Feb 1, 2022
2aae58c
multi-line comment
aristath Feb 3, 2022
aa12bf2
Add dependency to json2php
aristath Jun 28, 2022
8ec0097
re-compile blocks-json.php
aristath Jun 28, 2022
d4735c0
Exclude blocks-json.php from phpcs
aristath Jun 28, 2022
6ca926a
Update package-lock.json
SergeyBiryukov Sep 19, 2022
3c3f127
Update src/wp-includes/blocks.php
aristath Sep 20, 2022
e566421
Update blocks-json.php
aristath Sep 20, 2022
2365f8c
remove parentheses from include
aristath Sep 21, 2022
e795b35
update blocks-json.php
aristath Sep 21, 2022
9f41dfe
change include to include_once
aristath Sep 21, 2022
d72673e
no need for a $filename var
aristath Sep 21, 2022
2268110
Update json2php to v0.0.5
aristath Sep 21, 2022
5a6743e
Update blocks-json.php
aristath Sep 21, 2022
a30ebe8
update package-lock.json
aristath Sep 21, 2022
bc7a284
remove exception from phpcs.xml.dist
aristath Sep 21, 2022
4de00f6
List dependencies alphabetically
SergeyBiryukov Sep 21, 2022
7f5fc1e
Rename Grunt task for consistency
SergeyBiryukov Sep 21, 2022
4ce978a
Minor documentation edits
SergeyBiryukov Sep 21, 2022
0fa6460
Use str_ends_with()
SergeyBiryukov Sep 21, 2022
9788cfc
Minor documentation edits
SergeyBiryukov Sep 21, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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' );
aristath marked this conversation as resolved.
Show resolved Hide resolved

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 ) + ';'
aristath marked this conversation as resolved.
Show resolved Hide resolved
);
} );

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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you considered using the same paths passed to the register_block_type as keys in the generated file? This way, you could do a simple check:

if ( ! empty( $core_blocks_meta[ $file_or_folder ] ) ) {
    $metadata = $core_blocks_meta[ $file_or_folder ];
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aren't those paths absolute - which means they'd change from site to site? 🤔

Copy link
Member

@gziolo gziolo Sep 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking you could set a variable to use in keys for the generated PHP file:

$dir = ABSPATH . WPINC . '/blocks/';

Maybe, it's too much work in a bit different place 😄

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.