PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.6.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.6.0
3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.10 All 111 releases
templately / includes / Core / Importer / Utils / GutenbergSettingsMerger.php

GutenbergSettingsMerger.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.6.0, at includes/Core/Importer/Utils/GutenbergSettingsMerger.php

203 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Core\Importer\Utils;
4
5 /**
6 * Merges Essential Blocks global styles (colors & typography)
7 * into Gutenberg block content, resolving CSS variable references
8 * and global font source references.
9 *
10 * Ported from templately-exporter GutenbergExporter::merge_settings(),
11 * replace_colors(), and replace_fonts().
12 *
13 * @since 3.x
14 */
15 class GutenbergSettingsMerger {
16
17 /**
18 * Global color entries: each has 'var' and 'color' keys.
19 * @var array
20 */
21 private $colors = [];
22
23 /**
24 * Global font entries keyed by font ID / tag name.
25 * @var array
26 */
27 private $fonts = [];
28
29 /**
30 * Font attribute names to copy when resolving a global font.
31 * @var string[]
32 */
33 private $font_attrs = [
34 'fontFamily',
35 'fontWeight',
36 'textTransform',
37 'textDecoration',
38 'fontSize',
39 'fontSizeUnit',
40 'lineHeight',
41 'lineHeightUnit',
42 'letterSpacing',
43 'letterSpacingUnit',
44 ];
45
46 /**
47 * Responsive font attr names (also need TAB/MOB prefixes).
48 * @var string[]
49 */
50 private $responsive_font_attrs = [
51 'fontSize',
52 'fontSizeUnit',
53 'lineHeight',
54 'lineHeightUnit',
55 'letterSpacing',
56 'letterSpacingUnit',
57 ];
58
59 /**
60 * Merge EB global styles into serialized Gutenberg block content.
61 *
62 * @param string $block_content Serialized block content (post_content format).
63 * @param array $settings The EB global styles (keyed by setting type,
64 * values are already decoded arrays matching
65 * what GutenbergExporter::export() writes).
66 *
67 * @return string Modified block content with globals resolved.
68 */
69 public static function merge( string $block_content, array $settings ): string {
70 $instance = new self();
71 $instance->collect_globals( $settings );
72
73 // 1. Replace CSS color variables in the serialized content
74 $block_content = $instance->replace_colors( $block_content );
75
76 // 2. Resolve global font sources in block attrs
77 if ( function_exists( 'parse_blocks' ) && function_exists( 'serialize_blocks' ) ) {
78 $blocks = parse_blocks( $block_content );
79 $instance->replace_fonts( $blocks );
80 $block_content = serialize_blocks( $blocks );
81 }
82
83 return $block_content;
84 }
85
86 /**
87 * Build $colors and $fonts from the settings array.
88 */
89 private function collect_globals( array $settings ): void {
90 $color_keys = [ 'gradient_colors', 'custom_gradient_colors', 'custom_colors', 'global_colors' ];
91
92 foreach ( $color_keys as $key ) {
93 if ( ! empty( $settings[ $key ] ) ) {
94 $this->colors = array_merge( $this->colors, $settings[ $key ] );
95 }
96 }
97
98 if ( ! empty( $settings['global_typography'] ) ) {
99 foreach ( $settings['global_typography'] as $k => $v ) {
100 if ( $k === 'custom' ) {
101 // Custom entries are keyed by their ID
102 foreach ( $v as $k2 => $v2 ) {
103 $this->fonts[ $k2 ] = $v2;
104 }
105 } else {
106 $this->fonts[ $k ] = $v;
107 }
108 }
109 }
110 }
111
112 /**
113 * Replace CSS variable references (e.g. var(--eb-global-...)) with
114 * their actual color values.
115 */
116 private function replace_colors( string $content ): string {
117 foreach ( $this->colors as $color ) {
118 if ( ! empty( $color['var'] ) && ! empty( $color['color'] ) ) {
119 $content = str_replace( 'var(' . $color['var'] . ')', $color['color'], $content );
120 }
121 }
122
123 return $content;
124 }
125
126 /**
127 * Walk parsed blocks recursively and resolve global font sources.
128 */
129 private function replace_fonts( array &$blocks ): void {
130 foreach ( $blocks as &$block ) {
131 if ( ! empty( $block['attrs'] ) ) {
132 foreach ( $block['attrs'] as $attr => $value ) {
133 if ( $this->has_global_fonts( $attr, $value ) ) {
134 $font_id = str_replace( 'global:', '', $block['attrs'][ $attr ] );
135 $tag_name = $block['attrs']['tagName'] ?? '';
136 $block['attrs'] = array_merge(
137 $block['attrs'],
138 $this->prepare_font_attrs( $font_id, $attr, $tag_name )
139 );
140 $block['attrs'][ $attr ] = 'custom';
141 }
142 }
143 }
144
145 if ( ! empty( $block['innerBlocks'] ) ) {
146 $this->replace_fonts( $block['innerBlocks'] );
147 }
148 }
149 }
150
151 /**
152 * Determine if an attribute is a global font source reference.
153 */
154 private function has_global_fonts( string $attr, $value ): bool {
155 return str_contains( $attr, 'FontSource' ) && is_string( $value ) && str_contains( $value, 'global:' );
156 }
157
158 /**
159 * Build the concrete font attribute array for a given global font ID.
160 */
161 private function prepare_font_attrs( string $font_id, string $attr, string $tag_name ): array {
162 $styles = [];
163 $prefix = str_replace( 'FontSource', '', $attr );
164
165 if ( $font_id === 'global' ) {
166 if ( ! empty( $tag_name ) && isset( $this->fonts[ $tag_name ] ) ) {
167 $styles = $this->fonts[ $tag_name ];
168 // If the font family is "Default", fall back to allHeadings
169 if ( isset( $styles['fontFamily'] ) && trim( strtolower( $styles['fontFamily'] ) ) === 'default' ) {
170 $styles['fontFamily'] = $this->fonts['allHeadings']['fontFamily'] ?? $styles['fontFamily'];
171 }
172 } elseif ( isset( $this->fonts['body'] ) ) {
173 $styles = $this->fonts['body'];
174 }
175 } elseif ( isset( $this->fonts[ $font_id ] ) ) {
176 $styles = $this->fonts[ $font_id ];
177 }
178
179 if ( empty( $styles ) ) {
180 return [];
181 }
182
183 $prepared = [];
184
185 foreach ( $this->font_attrs as $font_attr ) {
186 if ( ! empty( $styles[ $font_attr ] ) ) {
187 $prepared[ $prefix . ucfirst( $font_attr ) ] = $styles[ $font_attr ];
188 }
189 }
190
191 foreach ( $this->responsive_font_attrs as $font_attr ) {
192 if ( ! empty( $styles[ 'TAB' . $font_attr ] ) ) {
193 $prepared[ 'TAB' . $prefix . ucfirst( $font_attr ) ] = $styles[ 'TAB' . $font_attr ];
194 }
195 if ( ! empty( $styles[ 'MOB' . $font_attr ] ) ) {
196 $prepared[ 'MOB' . $prefix . ucfirst( $font_attr ) ] = $styles[ 'MOB' . $font_attr ];
197 }
198 }
199
200 return $prepared;
201 }
202 }
203