Block Editor: Remove repetitive calls to file_get_contents() in block editor settings.

The `get_default_block_editor_settings()` function used to repeatedly get the `default-editor-styles.css` file contents without any implementation to avoid this.

This commit utilizes a static variable to remove repetitive calls made during the same request. In tests ran on the front page of a site using Xdebug & Webgrind, the total `file_get_contents()` invocation count goes down from 181 to 93, and total self cost (the time that the function is responsible for) goes down from 160 ms to 93 ms.

Follow-up to [52042].

Props aristath, mukesh27.
Fixes #56637.

git-svn-id: https://develop.svn.wordpress.org/trunk@54291 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2022-09-23 13:58:19 +00:00
parent 781c00ee0d
commit e33a49b1bd

View File

@ -192,12 +192,17 @@ function get_default_block_editor_settings() {
// These styles are used if the "no theme styles" options is triggered or on
// themes without their own editor styles.
$default_editor_styles_file = ABSPATH . WPINC . '/css/dist/block-editor/default-editor-styles.css';
if ( file_exists( $default_editor_styles_file ) ) {
static $default_editor_styles_file_contents = false;
if ( ! $default_editor_styles_file_contents && file_exists( $default_editor_styles_file ) ) {
$default_editor_styles_file_contents = file_get_contents( $default_editor_styles_file );
}
$default_editor_styles = array();
if ( $default_editor_styles_file_contents ) {
$default_editor_styles = array(
array( 'css' => file_get_contents( $default_editor_styles_file ) ),
array( 'css' => $default_editor_styles_file_contents ),
);
} else {
$default_editor_styles = array();
}
$editor_settings = array(