PluginProbe
Elementor Website Builder – more than just a page builder / 3.8.0
Elementor Website Builder – more than just a page builder v3.8.0
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 4.0.7 All 451 releases
elementor / app / modules / import-export / processes / import.php

import.php in Elementor Website Builder – more than just a page builder 3.8.0, at app/modules/import-export/processes/import.php

550 lines 15.1 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\ImportExport\Processes;
4
5 use Elementor\App\Modules\ImportExport\Compatibility\Base_Adapter;
6 use Elementor\App\Modules\ImportExport\Compatibility\Envato;
7 use Elementor\App\Modules\ImportExport\Compatibility\Kit_Library;
8 use Elementor\App\Modules\ImportExport\Utils;
9 use Elementor\Core\Base\Document;
10 use Elementor\Plugin;
11
12 use Elementor\App\Modules\ImportExport\Runners\Import\Elementor_Content;
13 use Elementor\App\Modules\ImportExport\Runners\Import\Import_Runner_Base;
14 use Elementor\App\Modules\ImportExport\Runners\Import\Plugins;
15 use Elementor\App\Modules\ImportExport\Runners\Import\Site_Settings;
16 use Elementor\App\Modules\ImportExport\Runners\Import\Taxonomies;
17 use Elementor\App\Modules\ImportExport\Runners\Import\Templates;
18 use Elementor\App\Modules\ImportExport\Runners\Import\Wp_Content;
19 use Elementor\App\Modules\ImportExport\Module;
20
21 class Import {
22 const MANIFEST_ERROR_KEY = 'manifest-error';
23
24 const SESSION_DOES_NOT_EXITS_ERROR = 'session-does-not-exits-error';
25
26 const ZIP_FILE_ERROR_KEY = 'zip-file-error';
27
28 /**
29 * @var Import_Runner_Base[]
30 */
31 protected $runners = [];
32
33 /**
34 * The session ID of the import process.
35 * This ID is uniquely generated for each import process (by the temp folder which contains the extracted kit files).
36 *
37 * @var string
38 */
39 private $session_id;
40
41 /**
42 * Adapter for the kit compatibility.
43 *
44 * @var Base_Adapter[]
45 */
46 private $adapters;
47
48 /**
49 * Document's elements that imported during the process.
50 *
51 * @var array
52 */
53 private $documents_elements = [];
54
55 /**
56 * Path to the extracted kit files.
57 *
58 * @var string
59 */
60 private $extracted_directory_path;
61
62 /**
63 * Imported kit manifest.
64 *
65 * @var array
66 */
67 private $manifest;
68
69 /**
70 * Imported kit site settings. (e.g: custom_colors, custom_typography, etc.)
71 *
72 * @var array
73 */
74 private $site_settings;
75
76 /**
77 * Selected content types to import.
78 *
79 * @var array
80 */
81 private $settings_include;
82
83 /**
84 * Referer of the import. (e.g: kit-library, local, etc.)
85 *
86 * @var string
87 */
88 private $settings_referrer;
89
90 /**
91 * All the conflict between the exited templates and the kit templates.
92 *
93 * @var array
94 */
95 private $settings_conflicts;
96
97 /**
98 * Selected elementor templates conditions to override.
99 *
100 * @var array
101 */
102 private $settings_selected_override_conditions;
103
104 /**
105 * Selected custom post types to import.
106 *
107 * @var array
108 */
109 private $settings_selected_custom_post_types;
110
111 /**
112 * Selected plugins to import.
113 *
114 * @var array
115 */
116 private $settings_selected_plugins;
117
118 /**
119 * The imported data output.
120 *
121 * @var array
122 */
123 private $imported_data = [];
124
125 /**
126 * The metadata output of the import runners.
127 * Will be saved in the import_session and will be used to revert the import process.
128 *
129 * @var array
130 */
131 private $runners_import_metadata = [];
132
133 /**
134 * @param $path string session_id | zip_file_path
135 * @param $settings array Use to determine which content to import.
136 * (e.g: include, selected_plugins, selected_cpt, selected_override_conditions, etc.)
137 * @throws \Exception
138 */
139 public function __construct( string $path, array $settings = [] ) {
140 if ( is_file( $path ) ) {
141 $this->extracted_directory_path = $this->extract_zip( $path );
142 } else {
143 $elementor_tmp_directory = Plugin::$instance->uploads_manager->get_temp_dir();
144 $path = $elementor_tmp_directory . basename( $path );
145
146 if ( ! is_dir( $path ) ) {
147 throw new \Exception( static::SESSION_DOES_NOT_EXITS_ERROR );
148 }
149
150 $this->extracted_directory_path = $path . '/';
151 }
152
153 $this->session_id = basename( $this->extracted_directory_path );
154 $this->settings_referrer = ! empty( $settings['referrer'] ) ? $settings['referrer'] : 'local';
155 $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null;
156
157 // Using isset and not empty is important since empty array is valid option.
158 $this->settings_selected_override_conditions = $settings['overrideConditions'] ?? null;
159 $this->settings_selected_custom_post_types = $settings['selectedCustomPostTypes'] ?? null;
160 $this->settings_selected_plugins = $settings['plugins'] ?? null;
161
162 $this->manifest = $this->read_manifest_json();
163 $this->site_settings = $this->read_site_settings_json();
164
165 $this->set_default_settings();
166 }
167
168 /**
169 * Register a runner.
170 * Be aware that the runner will be executed in the order of registration, the order is crucial for the import process.
171 *
172 * @param Import_Runner_Base $runner_instance
173 */
174 public function register( Import_Runner_Base $runner_instance ) {
175 $this->runners[ $runner_instance::get_name() ] = $runner_instance;
176 }
177
178 public function register_default_runners() {
179 $this->register( new Site_Settings() );
180 $this->register( new Plugins() );
181 $this->register( new Templates() );
182 $this->register( new Taxonomies() );
183 $this->register( new Elementor_Content() );
184 $this->register( new Wp_Content() );
185 }
186
187 /**
188 * Set default settings for the import.
189 */
190 private function set_default_settings() {
191 if ( ! is_array( $this->get_settings_include() ) ) {
192 $this->settings_include( $this->get_default_settings_include() );
193 }
194
195 if ( ! is_array( $this->get_settings_conflicts() ) ) {
196 $this->settings_conflicts( $this->get_default_settings_conflicts() );
197 }
198
199 if ( ! is_array( $this->get_settings_selected_override_conditions() ) ) {
200 $this->settings_selected_override_conditions( $this->get_default_settings_override_conditions() );
201 }
202
203 if ( ! is_array( $this->get_settings_selected_custom_post_types() ) ) {
204 $this->settings_selected_custom_post_types( $this->get_default_settings_custom_post_types() );
205 }
206
207 if ( ! is_array( $this->get_settings_selected_plugins() ) ) {
208 $this->settings_selected_plugins( $this->get_default_settings_plugins() );
209 }
210 }
211
212 /**
213 * Execute the import process.
214 *
215 * @return array The imported data output.
216 * @throws \Exception
217 */
218 public function run() {
219 $start_time = current_time( 'timestamp' );
220
221 if ( empty( $this->runners ) ) {
222 throw new \Exception( 'Please specify import runners.' );
223 }
224
225 $data = [
226 'session_id' => $this->session_id,
227 'include' => $this->settings_include,
228 'manifest' => $this->manifest,
229 'site_settings' => $this->site_settings,
230 'selected_plugins' => $this->settings_selected_plugins,
231 'extracted_directory_path' => $this->extracted_directory_path,
232 'selected_custom_post_types' => $this->settings_selected_custom_post_types,
233 ];
234
235 add_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10, 2 );
236
237 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
238 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
239
240 foreach ( $this->runners as $runner ) {
241 if ( $runner->should_import( $data ) ) {
242 $import = $runner->import( $data, $this->imported_data );
243 $this->imported_data = array_merge_recursive( $this->imported_data, $import );
244
245 $this->runners_import_metadata[ $runner::get_name() ] = $runner->get_import_session_metadata();
246 }
247 }
248
249 // After the upload complete, set the elementor upload state back to false.
250 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
251
252 remove_filter( 'elementor/document/save/data', [ $this, 'prevent_saving_elements_on_post_creation' ], 10 );
253
254 $this->add_import_session_option( $start_time );
255
256 $this->save_elements_of_imported_posts();
257
258 Plugin::$instance->uploads_manager->remove_file_or_dir( $this->extracted_directory_path );
259 return $this->imported_data;
260 }
261
262 public function get_manifest() {
263 return $this->manifest;
264 }
265
266 public function get_extracted_directory_path() {
267 return $this->extracted_directory_path;
268 }
269
270 public function get_session_id() {
271 return $this->session_id;
272 }
273
274 public function get_adapters() {
275 return $this->adapters;
276 }
277
278 /**
279 * Get settings by key.
280 * Used for backward compatibility.
281 *
282 * @param string $key The key of the setting.
283 */
284 public function get_settings( $key ) {
285 switch ( $key ) {
286 case 'include':
287 return $this->get_settings_include();
288
289 case 'overrideConditions':
290 return $this->get_settings_selected_override_conditions();
291
292 case 'selectedCustomPostTypes':
293 return $this->get_settings_selected_custom_post_types();
294
295 case 'plugins':
296 return $this->get_settings_selected_plugins();
297
298 default:
299 return [];
300 }
301 }
302
303 public function settings_include( array $settings_include ) {
304 $this->settings_include = $settings_include;
305
306 return $this;
307 }
308
309 public function get_settings_include() {
310 return $this->settings_include;
311 }
312
313 public function settings_referrer( $settings_referrer ) {
314 $this->settings_referrer = $settings_referrer;
315
316 return $this;
317 }
318
319 public function get_settings_referrer() {
320 return $this->settings_referrer;
321 }
322
323 public function settings_conflicts( array $settings_conflicts ) {
324 $this->settings_conflicts = $settings_conflicts;
325
326 return $this;
327 }
328
329 public function get_settings_conflicts() {
330 return $this->settings_conflicts;
331 }
332
333 public function settings_selected_override_conditions( array $settings_selected_override_conditions ) {
334 $this->settings_selected_override_conditions = $settings_selected_override_conditions;
335
336 return $this;
337 }
338
339 public function get_settings_selected_override_conditions() {
340 return $this->settings_selected_override_conditions;
341 }
342
343 public function settings_selected_custom_post_types( array $settings_selected_custom_post_types ) {
344 $this->settings_selected_custom_post_types = $settings_selected_custom_post_types;
345
346 return $this;
347 }
348
349 public function get_settings_selected_custom_post_types() {
350 return $this->settings_selected_custom_post_types;
351 }
352
353 public function settings_selected_plugins( array $settings_selected_plugins ) {
354 $this->settings_selected_plugins = $settings_selected_plugins;
355
356 return $this;
357 }
358
359 public function get_settings_selected_plugins() {
360 return $this->settings_selected_plugins;
361 }
362
363 /**
364 * Prevent saving elements on elementor post creation.
365 *
366 * @param array $data
367 * @param Document $document
368 *
369 * @return array
370 */
371 public function prevent_saving_elements_on_post_creation( array $data, Document $document ) {
372 if ( isset( $data['elements'] ) ) {
373 $this->documents_elements[ $document->get_main_id() ] = $data['elements'];
374
375 $data['elements'] = [];
376 }
377
378 return $data;
379 }
380
381 /**
382 * Extract the zip file.
383 *
384 * @param string $zip_path The path to the zip file.
385 * @return string The extracted directory path.
386 */
387 private function extract_zip( $zip_path ) {
388 $extraction_result = Plugin::$instance->uploads_manager->extract_and_validate_zip( $zip_path, [ 'json', 'xml' ] );
389
390 if ( is_wp_error( $extraction_result ) ) {
391 throw new \Error( static::ZIP_FILE_ERROR_KEY );
392 }
393
394 return $extraction_result['extraction_directory'];
395 }
396
397 /**
398 * Get the manifest file from the extracted directory and adapt it if needed.
399 *
400 * @return string The manifest file content.
401 */
402 private function read_manifest_json() {
403 $manifest = Utils::read_json_file( $this->extracted_directory_path . 'manifest' );
404
405 if ( ! $manifest ) {
406 throw new \Error( static::MANIFEST_ERROR_KEY );
407 }
408
409 $this->init_adapters( $manifest );
410
411 foreach ( $this->adapters as $adapter ) {
412 $manifest = $adapter->adapt_manifest( $manifest );
413 }
414
415 return $manifest;
416 }
417
418 /**
419 * Init the adapters and determine which ones to use.
420 *
421 * @param array $manifest_data The manifest file content.
422 */
423 private function init_adapters( array $manifest_data ) {
424 $this->adapters = [];
425
426 /** @var Base_Adapter[] $adapter_types */
427 $adapter_types = [ Envato::class, Kit_Library::class ];
428
429 foreach ( $adapter_types as $adapter_type ) {
430 if ( $adapter_type::is_compatibility_needed( $manifest_data, [ 'referrer' => $this->get_settings_referrer() ] ) ) {
431 $this->adapters[] = new $adapter_type( $this );
432 }
433 }
434 }
435
436 /**
437 * Get the site settings file from the extracted directory and adapt it if needed.
438 *
439 * @return string The site settings file content.
440 */
441 private function read_site_settings_json() {
442 $site_settings = Utils::read_json_file( $this->extracted_directory_path . 'site-settings' );
443
444 foreach ( $this->adapters as $adapter ) {
445 $site_settings = $adapter->adapt_site_settings( $site_settings, $this->manifest, $this->extracted_directory_path );
446 }
447
448 return $site_settings;
449 }
450
451 /**
452 * Get all the custom post types in the kit.
453 *
454 * @return array Custom post types names.
455 */
456 private function get_default_settings_custom_post_types() {
457 if ( empty( $this->manifest['custom-post-type-title'] ) ) {
458 return [];
459 }
460
461 $manifest_post_types = array_keys( $this->manifest['custom-post-type-title'] );
462
463 return array_diff( $manifest_post_types, Utils::get_builtin_wp_post_types() );
464 }
465
466 /**
467 * Get the default settings of elementor templates conditions to override.
468 *
469 * @return array
470 */
471 private function get_default_settings_conflicts() {
472 if ( empty( $this->manifest['templates'] ) ) {
473 return [];
474 }
475
476 return apply_filters( 'elementor/import/get_default_settings_conflicts', [], $this->manifest['templates'] );
477 }
478
479 /**
480 * Get the default settings of elementor templates conditions to override.
481 *
482 * @return array
483 */
484 private function get_default_settings_override_conditions() {
485 if ( empty( $this->settings_conflicts ) ) {
486 return [];
487 }
488
489 return array_keys( $this->settings_conflicts );
490 }
491
492 /**
493 * Get the default settings of the plugins that should be imported.
494 *
495 * @return array
496 */
497 private function get_default_settings_plugins() {
498 return ! empty( $this->manifest['plugins'] ) ? $this->manifest['plugins'] : [];
499 }
500
501 /**
502 * Get the default settings of which content types should be imported.
503 *
504 * @return array
505 */
506 private function get_default_settings_include() {
507 return [ 'templates', 'plugins', 'content', 'settings' ];
508 }
509
510 /**
511 * Get the data that requires updating/replacement when imported.
512 *
513 * @return array{post_ids: array, term_ids: array}
514 */
515 private function get_imported_data_replacements() : array {
516 return [
517 'post_ids' => Utils::map_old_new_post_ids( $this->imported_data ),
518 'term_ids' => Utils::map_old_new_term_ids( $this->imported_data ),
519 ];
520 }
521
522 /**
523 * Save the prevented elements on elementor post creation elements.
524 * Handle the replacement of all the dynamic content of the elements that probably have been changed during the import.
525 */
526 private function save_elements_of_imported_posts() {
527 foreach ( $this->documents_elements as $new_id => $document_elements ) {
528 $document = Plugin::$instance->documents->get( $new_id );
529 $updated_elements = $document->on_import_update_dynamic_content( $document_elements, $this->get_imported_data_replacements() );
530 $document->save( [ 'elements' => $updated_elements ] );
531 }
532 }
533
534 private function add_import_session_option( $start_time ) {
535 $import_sessions = get_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS );
536
537 $import_sessions[ time() ] = [
538 'session_id' => $this->session_id,
539 'kit_name' => $this->manifest['name'],
540 'kit_source' => $this->settings_referrer,
541 'user_id' => get_current_user_id(),
542 'start_timestamp' => $start_time,
543 'end_timestamp' => current_time( 'timestamp' ),
544 'runners' => $this->runners_import_metadata,
545 ];
546
547 update_option( Module::OPTION_KEY_ELEMENTOR_IMPORT_SESSIONS, $import_sessions, false );
548 }
549 }
550