| 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 |
|