| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Admin\API; |
| 4 |
|
| 5 |
use Elementor\Core\Files\Fonts\Google_Font; |
| 6 |
use Elementor\Plugin; |
| 7 |
use WP_REST_Request; |
| 8 |
|
| 9 |
class Settings extends BaseAPI { |
| 10 |
private $endpoint = 'settings'; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
/** |
| 15 |
* @param $request WP_REST_Request for getting all route request in time. |
| 16 |
* |
| 17 |
* @return WP_Error|boolean |
| 18 |
*/ |
| 19 |
public function permission_check( WP_REST_Request $request ) { |
| 20 |
$this->request = $request; |
| 21 |
if(!current_user_can('manage_options')){ |
| 22 |
return false; |
| 23 |
} |
| 24 |
return true; |
| 25 |
} |
| 26 |
|
| 27 |
public function register_routes() { |
| 28 |
$this->post( $this->endpoint . '/update', [$this, 'update_settings'] ); |
| 29 |
} |
| 30 |
|
| 31 |
public function update_settings() { |
| 32 |
$_settings = $this->request->get_param('settings'); |
| 33 |
$platform = $_settings['platform'] ?? ''; |
| 34 |
$siteTitle = $_settings['siteTitle'] ?? ''; |
| 35 |
$siteTagline = $_settings['siteTagline'] ?? ''; |
| 36 |
$customCSS = $_settings['customCSS'] ?? ''; |
| 37 |
|
| 38 |
update_option('blogname', $siteTitle); |
| 39 |
update_option('blogdescription', $siteTagline); |
| 40 |
update_option('templately_custom_css', $customCSS); |
| 41 |
|
| 42 |
$this->update_logo(); |
| 43 |
|
| 44 |
if($platform === 'elementor'){ |
| 45 |
if(class_exists('Elementor\Plugin')){ |
| 46 |
$this->update_elementor_settings($_settings); |
| 47 |
} |
| 48 |
} |
| 49 |
else if($platform === 'gutenberg'){ |
| 50 |
$this->update_gutenberg_settings($_settings); |
| 51 |
} |
| 52 |
|
| 53 |
return $this->success( ['message' => 'Settings updated successfully'] ); |
| 54 |
} |
| 55 |
|
| 56 |
public function update_logo() { |
| 57 |
$_settings = $this->request->get_param('settings'); |
| 58 |
$logo = $_settings['logoImage'] ?? []; |
| 59 |
|
| 60 |
if(!empty($logo['id'])){ |
| 61 |
set_theme_mod( 'custom_logo', $logo['id'] ); |
| 62 |
update_option( 'site_logo', $logo['id'] ); |
| 63 |
|
| 64 |
// Update Elementor kit if Elementor is enabled |
| 65 |
if(class_exists('Elementor\Plugin')){ |
| 66 |
$kit = \Elementor\Plugin::$instance->kits_manager->get_active_kit(); |
| 67 |
if($kit){ |
| 68 |
$settings = $kit->get_settings(); |
| 69 |
$settings['site_logo'] = $logo; |
| 70 |
$kit->update_settings($settings); |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
else{ |
| 75 |
remove_theme_mod( 'custom_logo' ); |
| 76 |
delete_option( 'site_logo' ); |
| 77 |
|
| 78 |
// Remove logo from Elementor kit if Elementor is enabled |
| 79 |
if(class_exists('Elementor\Plugin')){ |
| 80 |
$kit = \Elementor\Plugin::$instance->kits_manager->get_active_kit(); |
| 81 |
if($kit){ |
| 82 |
$settings = $kit->get_settings(); |
| 83 |
unset($settings['site_logo']); |
| 84 |
$kit->update_settings($settings); |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
return true; |
| 90 |
} |
| 91 |
|
| 92 |
private function update_gutenberg_settings($_settings){ |
| 93 |
$colors = $_settings['colors'] ?? []; |
| 94 |
$typography = $_settings['typography'] ?? []; |
| 95 |
$settings = get_option('eb_global_styles', []); |
| 96 |
$settings = is_array($settings) ? $settings: []; |
| 97 |
$settings = array_map(function($item) { return json_decode($item, true); }, $settings); |
| 98 |
|
| 99 |
|
| 100 |
if(!empty($colors)){ |
| 101 |
if (isset($settings['global_colors'])) { |
| 102 |
foreach ($settings['global_colors'] as $key => $color) { |
| 103 |
$settings['global_colors'][$key]['color'] = $colors[$color['var']] ?? $color['color']; |
| 104 |
} |
| 105 |
} |
| 106 |
|
| 107 |
if (isset($settings['custom_colors'])) { |
| 108 |
foreach ($settings['custom_colors'] as $key => $color) { |
| 109 |
$settings['custom_colors'][$key]['color'] = $colors[$color['var']] ?? $color['color']; |
| 110 |
} |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
if(!empty($typography)){ |
| 115 |
$settings["global_typography"] = $typography; |
| 116 |
} |
| 117 |
|
| 118 |
$settings = array_map('json_encode', $settings); |
| 119 |
update_option('eb_global_styles', $settings); |
| 120 |
} |
| 121 |
|
| 122 |
private function update_elementor_settings($_settings){ |
| 123 |
$colors = $_settings['colors'] ?? []; |
| 124 |
$typography = $_settings['typography'] ?? []; |
| 125 |
|
| 126 |
$kit = Plugin::$instance->kits_manager->get_active_kit(); |
| 127 |
if($kit){ |
| 128 |
$settings = $kit->get_settings(); |
| 129 |
$settings['site_logo'] = $_settings['logoImage']; |
| 130 |
|
| 131 |
if(!empty($colors)){ |
| 132 |
if (!empty($settings['system_colors'])) { |
| 133 |
foreach ($settings['system_colors'] as $key => $color) { |
| 134 |
$settings['system_colors'][$key]['color'] = $colors[$color['_id']] ?? $color['color']; |
| 135 |
} |
| 136 |
} |
| 137 |
if (!empty($settings['custom_colors'])) { |
| 138 |
foreach ($settings['custom_colors'] as $key => $color) { |
| 139 |
$settings['custom_colors'][$key]['color'] = $colors[$color['_id']] ?? $color['color']; |
| 140 |
} |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
if(!empty($typography)){ |
| 147 |
if (!empty($settings['system_typography'])) { |
| 148 |
foreach ($settings['system_typography'] as $key => $typo) { |
| 149 |
if(!empty($typography[$typo['_id']])){ |
| 150 |
$settings['system_typography'][$key] = array_merge($typo, $typography[$typo['_id']]); |
| 151 |
if(class_exists('Elementor\Core\Files\Fonts\Google_Font') && !empty($typo['typography_font_family'])){ |
| 152 |
Google_Font::enqueue( $typo['typography_font_family'] ); |
| 153 |
} |
| 154 |
} |
| 155 |
} |
| 156 |
} |
| 157 |
if (!empty($settings['custom_typography'])) { |
| 158 |
foreach ($settings['custom_typography'] as $key => $typo) { |
| 159 |
if(!empty($typography[$typo['_id']])){ |
| 160 |
$settings['custom_typography'][$key] = array_merge($typo, $typography[$typo['_id']]); |
| 161 |
if(class_exists('Elementor\Core\Files\Fonts\Google_Font') && !empty($typo['typography_font_family'])){ |
| 162 |
Google_Font::enqueue( $typo['typography_font_family'] ); |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
$kit->update_settings($settings); |
| 170 |
$settings = $kit->get_settings(); |
| 171 |
|
| 172 |
Plugin::$instance->files_manager->clear_cache(); |
| 173 |
return $settings; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
} |
| 179 |
} |
| 180 |
|