Cache WP_Theme_JSON instance

This is a slight improvement to avoid building WP_Theme_JSON instance repeatedly.
[MAILPOET-5741]
This commit is contained in:
Rostislav Wolny
2024-02-22 13:28:02 +01:00
committed by Rostislav Wolný
parent b724ef4ac2
commit e5d11f99d4

View File

@@ -2,18 +2,27 @@
namespace MailPoet\EmailEditor\Engine; namespace MailPoet\EmailEditor\Engine;
use WP_Theme_JSON;
use WP_Theme_JSON_Resolver;
/** /**
* E-mail editor works with own theme.json which defines settings for the editor and styles for the e-mail. * E-mail editor works with own theme.json which defines settings for the editor and styles for the e-mail.
* This class is responsible for accessing data defined by the theme.json. * This class is responsible for accessing data defined by the theme.json.
*/ */
class ThemeController { class ThemeController {
public function getTheme(): \WP_Theme_JSON { private WP_Theme_JSON $themeJson;
$coreThemeData = \WP_Theme_JSON_Resolver::get_core_data();
public function getTheme(): WP_Theme_JSON {
if (isset($this->themeJson)) {
return $this->themeJson;
}
$coreThemeData = WP_Theme_JSON_Resolver::get_core_data();
$themeJson = (string)file_get_contents(dirname(__FILE__) . '/theme.json'); $themeJson = (string)file_get_contents(dirname(__FILE__) . '/theme.json');
$themeJson = json_decode($themeJson, true); $themeJson = json_decode($themeJson, true);
/** @var array $themeJson */ /** @var array $themeJson */
$coreThemeData->merge(new \WP_Theme_JSON($themeJson, 'default')); $coreThemeData->merge(new WP_Theme_JSON($themeJson, 'default'));
return apply_filters('mailpoet_email_editor_theme_json', $coreThemeData); $this->themeJson = apply_filters('mailpoet_email_editor_theme_json', $coreThemeData);
return $this->themeJson;
} }
public function getStylesheetForRendering(): string { public function getStylesheetForRendering(): string {