| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Modules\ElementorKitSettings\Utils; |
| 4 |
|
| 5 |
use Elementor\Core\Files\Fonts\Google_Font; |
| 6 |
use Elementor\Plugin; |
| 7 |
|
| 8 |
/** |
| 9 |
* Writes customizer-output color/font/logo assignments into the active |
| 10 |
* Elementor kit document, matching entries by `_id`. Updates BOTH the |
| 11 |
* `system_*` and `custom_*` buckets (`system_colors`/`custom_colors`, |
| 12 |
* `system_typography`/`custom_typography`) — Elementor's own default |
| 13 |
* swatches live in the `system_*` buckets, so a matching `_id` there is |
| 14 |
* updated too. |
| 15 |
* |
| 16 |
* Extracted from modules/settings/REST/Settings.php::update_elementor_settings() |
| 17 |
* — behavior byte-identical. |
| 18 |
*/ |
| 19 |
class KitApplier { |
| 20 |
|
| 21 |
public static function apply( $_settings ) { |
| 22 |
$colors = $_settings['colors'] ?? []; |
| 23 |
$typography = $_settings['typography'] ?? []; |
| 24 |
|
| 25 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 26 |
// `get_active_kit()` never returns falsy: with no valid kit it hands back an |
| 27 |
// EMPTY Kit instance (post id 0), and calling update_settings() on that throws |
| 28 |
// `Exception( 'Invalid post.' )` from Elementor's page settings manager — |
| 29 |
// validating post ID 0. `get_id()` is the check that means "a real kit". |
| 30 |
if ( $kit && $kit->get_id() ) { |
| 31 |
$settings = $kit->get_settings(); |
| 32 |
$settings['site_logo'] = $_settings['logoImage']; |
| 33 |
|
| 34 |
if ( ! empty( $colors ) ) { |
| 35 |
if ( ! empty( $settings['system_colors'] ) ) { |
| 36 |
foreach ( $settings['system_colors'] as $key => $color ) { |
| 37 |
$settings['system_colors'][ $key ]['color'] = $colors[ $color['_id'] ] ?? $color['color']; |
| 38 |
} |
| 39 |
} |
| 40 |
if ( ! empty( $settings['custom_colors'] ) ) { |
| 41 |
foreach ( $settings['custom_colors'] as $key => $color ) { |
| 42 |
$settings['custom_colors'][ $key ]['color'] = $colors[ $color['_id'] ] ?? $color['color']; |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
if ( ! empty( $typography ) ) { |
| 50 |
if ( ! empty( $settings['system_typography'] ) ) { |
| 51 |
foreach ( $settings['system_typography'] as $key => $typo ) { |
| 52 |
if ( ! empty( $typography[ $typo['_id'] ] ) ) { |
| 53 |
$settings['system_typography'][ $key ] = array_merge( $typo, $typography[ $typo['_id'] ] ); |
| 54 |
if ( class_exists( 'Elementor\Core\Files\Fonts\Google_Font' ) && ! empty( $typo['typography_font_family'] ) ) { |
| 55 |
Google_Font::enqueue( $typo['typography_font_family'] ); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
if ( ! empty( $settings['custom_typography'] ) ) { |
| 61 |
foreach ( $settings['custom_typography'] as $key => $typo ) { |
| 62 |
if ( ! empty( $typography[ $typo['_id'] ] ) ) { |
| 63 |
$settings['custom_typography'][ $key ] = array_merge( $typo, $typography[ $typo['_id'] ] ); |
| 64 |
if ( class_exists( 'Elementor\Core\Files\Fonts\Google_Font' ) && ! empty( $typo['typography_font_family'] ) ) { |
| 65 |
Google_Font::enqueue( $typo['typography_font_family'] ); |
| 66 |
} |
| 67 |
} |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
$kit->update_settings( $settings ); |
| 73 |
$settings = $kit->get_settings(); |
| 74 |
|
| 75 |
Plugin::$instance->files_manager->clear_cache(); |
| 76 |
return $settings; |
| 77 |
} |
| 78 |
} |
| 79 |
} |
| 80 |
|