PluginProbe
Elementor Website Builder – more than just a page builder / 4.0.0-dev3
Elementor Website Builder – more than just a page builder v4.0.0-dev3
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 4.0.0-dev3, at app/modules/import-export-customization/runners/import/site-settings.php

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