Cache theme colors in static variable

This commit is contained in:
Tetrakern 2024-08-01 19:58:41 +02:00
parent 38b0f4f913
commit 163691b7f1
3 changed files with 15 additions and 15 deletions

View File

@ -56,19 +56,14 @@ if ( class_exists( 'WP_Customize_Control' ) ) {
* Helper to add color theme option
*
* @since 5.12.0
* @global array $fictioneer_colors Default colors read from JSON file.
* @since 5.21.2 - Improved with theme colors helper function.
*
* @param WP_Customize_Manager $manager The customizer instance.
* @param array $args Arguments for the setting and controls.
*/
function fictioneer_add_color_theme_option( $manager, $args ) {
global $fictioneer_colors;
if ( empty( $fictioneer_colors ) && ! is_array( $fictioneer_colors ) ) {
$fictioneer_colors = fictioneer_get_theme_colors_array();
}
$fictioneer_colors = fictioneer_get_theme_colors_array();
$default = $args['default'] ?? $fictioneer_colors[ $args['setting'] ]['hex'] ?? '';
$manager->add_setting(

View File

@ -594,7 +594,7 @@ function fictioneer_get_customizer_css_snippet( $snippet, $filter = null ) {
* Helper to get theme color mod with default fallback
*
* @since 5.12.0
* @global array $fictioneer_colors Default colors read from JSON file.
* @since 5.21.2 - Refactored with theme colors helper function.
*
* @param string $mod The requested theme color.
* @param string|null $default Optional. Default color code.
@ -603,12 +603,7 @@ function fictioneer_get_customizer_css_snippet( $snippet, $filter = null ) {
*/
function fictioneer_get_theme_color( $mod, $default = null ) {
global $fictioneer_colors;
if ( empty( $fictioneer_colors ) && ! is_array( $fictioneer_colors ) ) {
$fictioneer_colors = fictioneer_get_theme_colors_array();
}
$fictioneer_colors = fictioneer_get_theme_colors_array();
$default = $default ?? $fictioneer_colors[ $mod ]['hex'] ?? '#ff6347'; // Tomato
return get_theme_mod( $mod, $default );

View File

@ -1980,6 +1980,13 @@ add_filter( 'elementor/fonts/additional_fonts', 'fictioneer_elementor_add_additi
*/
function fictioneer_get_theme_colors_array() {
static $fictioneer_colors = null;
if ( $fictioneer_colors !== null ) {
return $fictioneer_colors;
}
// Setup
$parent_colors = [];
$child_colors = [];
@ -2007,6 +2014,9 @@ function fictioneer_get_theme_colors_array() {
}
}
// Update static cache
$fictioneer_colors = array_merge($parent_colors, $child_colors);
// Merge and return colors, child overriding parent
return array_merge($parent_colors, $child_colors);
return $fictioneer_colors;
}