PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / trunk
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! vtrunk
3.8.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 All 112 releases
templately / modules / elementor-kit-settings / Utils / ElementorSettingsMerger.php

ElementorSettingsMerger.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! trunk, at modules/elementor-kit-settings/Utils/ElementorSettingsMerger.php

136 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Templately\Modules\ElementorKitSettings\Utils;
4
5 /**
6 * Merges Elementor global kit settings (colors & typography)
7 * into template element data, resolving __globals__ references.
8 *
9 * Ported from templately-exporter ElementorExporter::merge_global_settings()
10 * and ElementorExporter::replace_global_settings().
11 *
12 * @since 3.x
13 */
14 class ElementorSettingsMerger {
15
16 /**
17 * @var array
18 */
19 private $colors = [];
20
21 /**
22 * @var array
23 */
24 private $fonts = [];
25
26 /**
27 * Merge global kit settings into a template's elements data.
28 *
29 * @param array $elements_data The template content (array of elements).
30 * @param array $settings The kit settings array (system_colors, custom_colors, system_typography, custom_typography).
31 *
32 * @return array Modified elements data with globals resolved.
33 */
34 public static function merge( array $elements_data, array $settings ): array {
35 $instance = new self();
36 $instance->collect_globals( $settings );
37 $instance->replace_global_settings( $elements_data );
38
39 return $elements_data;
40 }
41
42 /**
43 * Collect colors and fonts from kit settings.
44 */
45 private function collect_globals( array $settings ): void {
46 $keys = [ 'system_colors', 'custom_colors', 'system_typography', 'custom_typography' ];
47
48 foreach ( $keys as $key ) {
49 if ( empty( $settings[ $key ] ) ) {
50 continue;
51 }
52
53 // `strpos()`, not `str_contains()` (PHP 8.0+, core-polyfilled only from WP 5.9):
54 // the plugin advertises WordPress 5.0 / PHP 7.2.
55 if ( strpos( $key, 'colors' ) !== false ) {
56 $this->colors = array_merge( $this->colors, $settings[ $key ] );
57 } else {
58 $this->fonts = array_merge( $this->fonts, $settings[ $key ] );
59 }
60 }
61 }
62
63 /**
64 * Walk elements tree and replace __globals__ references with actual values.
65 */
66 private function replace_global_settings( array &$elements_data ): void {
67 foreach ( $elements_data as &$element ) {
68 if ( ! empty( $element['settings'] ) && ! empty( $element['settings']['__globals__'] ) ) {
69 foreach ( $element['settings']['__globals__'] as $key => $setting ) {
70 if ( $this->is_color_setting( $setting ) ) {
71 $this->replace_color( $element['settings'], $key );
72 }
73 if ( $this->is_typography_setting( $setting ) ) {
74 $this->replace_fonts( $element['settings'], $key );
75 }
76 }
77 }
78
79 if ( ! empty( $element['elements'] ) ) {
80 $this->replace_global_settings( $element['elements'] );
81 }
82 }
83 }
84
85 private function replace_color( array &$settings, string $key ): void {
86 $color = $this->get_color(
87 str_replace( 'globals/colors?id=', '', trim( $settings['__globals__'][ $key ] ) )
88 );
89
90 if ( ! empty( $color ) ) {
91 $settings[ $key ] = $color;
92 unset( $settings['__globals__'][ $key ] );
93 }
94 }
95
96 private function replace_fonts( array &$settings, string $key ): void {
97 $font = $this->get_font(
98 str_replace( 'globals/typography?id=', '', trim( $settings['__globals__'][ $key ] ) )
99 );
100
101 if ( ! empty( $font ) ) {
102 unset( $font['_id'], $font['title'] );
103 unset( $settings['__globals__'][ $key ] );
104 $settings = array_merge( $settings, $font );
105 }
106 }
107
108 private function get_color( string $id ): ?string {
109 foreach ( $this->colors as $color ) {
110 if ( isset( $color['_id'] ) && $color['_id'] === $id ) {
111 return $color['color'] ?? null;
112 }
113 }
114
115 return null;
116 }
117
118 private function get_font( string $id ): ?array {
119 foreach ( $this->fonts as $font ) {
120 if ( isset( $font['_id'] ) && $font['_id'] === $id ) {
121 return $font;
122 }
123 }
124
125 return null;
126 }
127
128 private function is_color_setting( string $setting ): bool {
129 return strpos( $setting, 'globals/colors' ) !== false;
130 }
131
132 private function is_typography_setting( string $setting ): bool {
133 return strpos( $setting, 'globals/typography' ) !== false;
134 }
135 }
136