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

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