PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev1
Elementor Website Builder – more than just a page builder v3.32.0-dev1
4.3.0-beta3 4.3.0-beta2 4.3.0-beta1 4.2.4 4.2.3 4.2.2 4.2.1 4.2.0 4.1.5 4.2.0-beta2 4.2.0-dev2 4.2.0-beta1 4.1.4 4.1.3 4.1.2 4.1.1 4.1.0 4.1.0-beta3 4.1.0-dev3 4.0.9 4.1.0-beta2 4.1.0-dev2 4.0.8 4.1.0-beta1 4.1.0-dev1 All 452 releases
elementor / app / modules / import-export-customization / runners / import / site-settings.php

site-settings.php in Elementor Website Builder – more than just a page builder 3.32.0-dev1, at app/modules/import-export-customization/runners/import/site-settings.php

412 lines 11.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Elementor\App\Modules\ImportExportCustomization\Runners\Import;
4
5 use Elementor\Plugin;
6 use Elementor\Core\Settings\Page\Manager as PageManager;
7 use Elementor\App\Modules\ImportExportCustomization\Utils;
8 use Elementor\Core\Experiments\Manager as ExperimentsManager;
9
10 class Site_Settings extends Import_Runner_Base {
11
12 const ALLOWED_SETTINGS = [
13 'theme',
14 'globalColors',
15 'globalFonts',
16 'themeStyleSettings',
17 'generalSettings',
18 'experiments',
19 ];
20
21 /**
22 * @var int
23 */
24 private $previous_kit_id;
25
26 /**
27 * @var int
28 */
29 private $active_kit_id;
30
31 /**
32 * @var int
33 */
34 private $imported_kit_id;
35
36 /**
37 * @var string|null
38 */
39 private ?string $installed_theme = null;
40
41 /**
42 * @var string|null
43 */
44 private ?string $activated_theme = null;
45
46 /**
47 * @var array|null
48 */
49 private ?array $previous_active_theme = null;
50
51 /**
52 * @var array
53 */
54 private $previous_experiments = [];
55
56 /**
57 * @var array
58 */
59 private $imported_experiments = [];
60
61 public function get_theme_upgrader(): \Theme_Upgrader {
62 if ( ! class_exists( '\Theme_Upgrader' ) ) {
63 require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
64 }
65
66 if ( ! class_exists( '\WP_Ajax_Upgrader_Skin' ) ) {
67 require_once ABSPATH . 'wp-admin/includes/class-wp-ajax-upgrader-skin.php';
68 }
69
70 return new \Theme_Upgrader( new \WP_Ajax_Upgrader_Skin() );
71 }
72
73 public static function get_name(): string {
74 return 'site-settings';
75 }
76
77 public function should_import( array $data ) {
78 return (
79 isset( $data['include'] ) &&
80 in_array( 'settings', $data['include'], true ) &&
81 ! empty( $data['site_settings']['settings'] )
82 );
83 }
84
85 public function import( array $data, array $imported_data ) {
86 $customization = $data['customization']['settings'] ?? null;
87
88 if ( $customization ) {
89 return $this->import_with_customization( $data, $imported_data, $customization );
90 }
91
92 return $this->import_with_manifest( $data, $imported_data );
93 }
94
95 private function import_with_customization( array $data, array $imported_data, array $customization ) {
96 $result = apply_filters( 'elementor/import-export-customization/import/site-settings/customization', null, $data, $imported_data, $customization, $this );
97
98 if ( is_array( $result ) ) {
99 return $result;
100 }
101
102 return $this->import_with_manifest( $data, $imported_data, ! empty( $customization['theme'] ) );
103 }
104
105 private function import_with_manifest( array $data, array $imported_data, $include_theme = true ) {
106 $new_site_settings = $data['site_settings']['settings'];
107 $title = $data['manifest']['title'] ?? 'Imported Kit';
108 $manifest_settings = $data['manifest']['site-settings'] ?? [];
109
110 $active_kit = Plugin::$instance->kits_manager->get_active_kit();
111
112 $this->active_kit_id = (int) $active_kit->get_id();
113 $this->previous_kit_id = (int) Plugin::$instance->kits_manager->get_previous_id();
114
115 $result = [];
116
117 $old_settings = $active_kit->get_meta( PageManager::META_KEY );
118
119 if ( ! $old_settings ) {
120 $old_settings = [];
121 }
122
123 $new_site_settings = $this->filter_settings_by_manifest( $new_site_settings, $manifest_settings );
124
125 if ( ( $manifest_settings['globalColors'] ?? false ) && ! empty( $old_settings['custom_colors'] ) && ! empty( $new_site_settings['custom_colors'] ) ) {
126 $new_site_settings['custom_colors'] = array_merge( $old_settings['custom_colors'], $new_site_settings['custom_colors'] );
127 }
128
129 if ( ( $manifest_settings['globalFonts'] ?? false ) && ! empty( $old_settings['custom_typography'] ) && ! empty( $new_site_settings['custom_typography'] ) ) {
130 $new_site_settings['custom_typography'] = array_merge( $old_settings['custom_typography'], $new_site_settings['custom_typography'] );
131 }
132
133 if ( ( $manifest_settings['generalSettings'] ?? false ) && ! empty( $new_site_settings['space_between_widgets'] ) ) {
134 $new_site_settings['space_between_widgets'] = Utils::update_space_between_widgets_values( $new_site_settings['space_between_widgets'] );
135 }
136
137 $new_site_settings = array_replace_recursive( $old_settings, $new_site_settings );
138
139 $new_kit = Plugin::$instance->kits_manager->create_new_kit( $title, $new_site_settings );
140
141 $this->imported_kit_id = (int) $new_kit;
142
143 $result['site-settings']['imported_kit_id'] = $this->imported_kit_id;
144
145 foreach ( $new_site_settings as $key => $value ) {
146 $result['site-settings'][ $key ] = $value;
147 }
148
149 if ( ( $manifest_settings['theme'] ?? false ) && $include_theme ) {
150 $import_theme_result = $this->import_theme( $data );
151
152 if ( ! empty( $import_theme_result ) ) {
153 $result['theme'] = $import_theme_result;
154 }
155 }
156
157 if ( $manifest_settings['experiments'] ?? false ) {
158 $this->import_experiments( $data );
159
160 if ( ! empty( $this->imported_experiments ) ) {
161 $result['experiments'] = $this->imported_experiments;
162 }
163 }
164
165 return $result;
166 }
167
168 private function filter_settings_by_manifest( array $settings, array $manifest_settings ): array {
169 foreach ( self::ALLOWED_SETTINGS as $setting_key ) {
170 if ( ! ( $manifest_settings[ $setting_key ] ?? false ) ) {
171 $settings = $this->remove_setting_by_key( $settings, $setting_key );
172 }
173 }
174
175 return $settings;
176 }
177
178 private function remove_setting_by_key( array $settings, string $setting_key ): array {
179 switch ( $setting_key ) {
180 case 'globalColors':
181 $settings = $this->remove_global_colors( $settings );
182 break;
183 case 'globalFonts':
184 $settings = $this->remove_global_fonts( $settings );
185 break;
186 case 'themeStyleSettings':
187 $settings = $this->remove_theme_style( $settings );
188 break;
189 case 'generalSettings':
190 $settings = $this->remove_other_settings( $settings );
191 break;
192 }
193
194 return $settings;
195 }
196
197 private function remove_global_colors( array $settings ): array {
198 $color_keys = [ 'system_colors', 'custom_colors' ];
199
200 foreach ( $color_keys as $key ) {
201 if ( isset( $settings[ $key ] ) ) {
202 unset( $settings[ $key ] );
203 }
204 }
205
206 return $settings;
207 }
208
209 private function remove_global_fonts( array $settings ): array {
210 $typography_keys = [ 'system_typography', 'custom_typography', 'default_generic_fonts' ];
211
212 foreach ( $typography_keys as $key ) {
213 if ( isset( $settings[ $key ] ) ) {
214 unset( $settings[ $key ] );
215 }
216 }
217
218 return $settings;
219 }
220
221 private function remove_theme_style( array $settings ): array {
222 $theme_style_patterns = [
223 '/^body_/',
224 '/^h[1-6]_/',
225 '/^button_/',
226 '/^link_/',
227 '/^form_field_/',
228 ];
229
230 foreach ( $settings as $key => $value ) {
231 foreach ( $theme_style_patterns as $pattern ) {
232 if ( preg_match( $pattern, $key ) ) {
233 unset( $settings[ $key ] );
234 break;
235 }
236 }
237 }
238
239 return $settings;
240 }
241
242 private function remove_other_settings( array $settings ): array {
243 $settings_keys = [
244 'template',
245 'container_width',
246 'container_padding',
247 'space_between_widgets',
248 'viewport_md',
249 'viewport_lg',
250 'page_title_selector',
251 'activeItemIndex',
252 ];
253
254 foreach ( $settings_keys as $key ) {
255 if ( isset( $settings[ $key ] ) ) {
256 unset( $settings[ $key ] );
257 }
258 }
259
260 return $settings;
261 }
262
263 protected function install_theme( $slug, $version ) {
264 $download_url = "https://downloads.wordpress.org/theme/{$slug}.{$version}.zip";
265
266 return $this->get_theme_upgrader()->install( $download_url );
267 }
268
269 protected function activate_theme( $slug ) {
270 switch_theme( $slug );
271 }
272
273 public function import_theme( array $data ) {
274 if ( empty( $data['site_settings']['theme'] ) ) {
275 return null;
276 }
277
278 if ( ! function_exists( 'wp_get_theme' ) ) {
279 require_once ABSPATH . 'wp-admin/includes/theme.php';
280 }
281
282 $theme = $data['site_settings']['theme'];
283 $theme_slug = $theme['slug'];
284 $theme_name = $theme['name'];
285
286 $current_theme = wp_get_theme();
287 $this->previous_active_theme = [];
288 $this->previous_active_theme['slug'] = $current_theme->get_stylesheet();
289 $this->previous_active_theme['version'] = $current_theme->get( 'Version' );
290
291 if ( $current_theme->get_stylesheet() === $theme_slug ) {
292 $result['succeed'][ $theme_slug ] = sprintf(
293 /* translators: %s: Theme name. */
294 __( 'Theme: %s is already used', 'elementor' ),
295 $theme_name
296 );
297 return $result;
298 }
299
300 try {
301 if ( wp_get_theme( $theme_slug )->exists() ) {
302 $this->activate_theme( $theme_slug );
303 $this->activated_theme = $theme_slug;
304 $result['succeed'][ $theme_slug ] = sprintf(
305 /* translators: %s: Theme name. */
306 __( 'Theme: %s has already been installed and activated', 'elementor' ),
307 $theme_name
308 );
309 return $result;
310 }
311
312 $import = $this->install_theme( $theme_slug, $theme['version'] );
313
314 if ( is_wp_error( $import ) ) {
315 $result['failed'][ $theme_slug ] = sprintf(
316 /* translators: %s: Theme name. */
317 __( 'Failed to install theme: %s', 'elementor' ),
318 $theme_name
319 );
320 return $result;
321 }
322
323 $result['succeed'][ $theme_slug ] = sprintf(
324 /* translators: %s: Theme name. */
325 __( 'Theme: %s has been successfully installed', 'elementor' ),
326 $theme_name
327 );
328 $this->installed_theme = $theme_slug;
329 $this->activate_theme( $theme_slug );
330 } catch ( \Exception $error ) {
331 $result['failed'][ $theme_slug ] = $error->getMessage();
332 }
333
334 return $result;
335 }
336
337 public function import_experiments( array $data ) {
338 if ( empty( $data['site_settings']['experiments'] ) ) {
339 return null;
340 }
341
342 $experiments_data = $data['site_settings']['experiments'];
343 $experiments_manager = Plugin::$instance->experiments;
344 $current_features = $experiments_manager->get_features();
345
346 $this->save_previous_experiments_state( $current_features );
347
348 foreach ( $experiments_data as $feature_name => $feature_data ) {
349 if ( ! isset( $current_features[ $feature_name ] ) ) {
350 continue;
351 }
352
353 $current_feature = $current_features[ $feature_name ];
354
355 $current_feature_state = $current_feature['state'];
356 $new_state = $feature_data['state'];
357
358 if ( $current_feature_state === $new_state ) {
359 continue;
360 }
361
362 if ( ! in_array( $new_state, [ ExperimentsManager::STATE_DEFAULT, ExperimentsManager::STATE_ACTIVE, ExperimentsManager::STATE_ACTIVE ], true ) ) {
363 continue;
364 }
365
366 $option_key = $experiments_manager->get_feature_option_key( $feature_name );
367
368 if ( 'default' === $new_state ) {
369 delete_option( $option_key );
370 } else {
371 update_option( $option_key, $new_state );
372 }
373
374 $this->imported_experiments[ $feature_name ] = $feature_data;
375 }
376 }
377
378 private function save_previous_experiments_state( array $current_features ) {
379 $experiments_manager = Plugin::$instance->experiments;
380
381 foreach ( $current_features as $feature_name => $feature ) {
382 if ( ! $feature['mutable'] ) {
383 continue;
384 }
385
386 $option_key = $experiments_manager->get_feature_option_key( $feature_name );
387 $saved_state = get_option( $option_key );
388
389 $this->previous_experiments[ $feature_name ] = [
390 'name' => $feature_name,
391 'title' => $feature['title'],
392 'state' => empty( $saved_state ) ? 'default' : $saved_state,
393 'default' => $feature['default'],
394 'release_status' => $feature['release_status'],
395 ];
396 }
397 }
398
399 public function get_import_session_metadata(): array {
400 return [
401 'previous_kit_id' => $this->previous_kit_id,
402 'active_kit_id' => $this->active_kit_id,
403 'imported_kit_id' => $this->imported_kit_id,
404 'installed_theme' => $this->installed_theme,
405 'activated_theme' => $this->activated_theme,
406 'previous_active_theme' => $this->previous_active_theme,
407 'previous_experiments' => $this->previous_experiments,
408 'imported_experiments' => $this->imported_experiments,
409 ];
410 }
411 }
412