PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.5
Elementor Website Builder – more than just a page builder v3.32.5
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-customization / processes / export.php

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

369 lines 10.2 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\Processes;
4
5 use Elementor\App\Modules\ImportExportCustomization\Module;
6 use Elementor\App\Modules\ImportExportCustomization\Utils;
7 use Elementor\Core\Utils\Str;
8 use Elementor\Plugin;
9
10 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Elementor_Content;
11 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Export_Runner_Base;
12 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Plugins;
13 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Site_Settings;
14 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Taxonomies;
15 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Templates;
16 use Elementor\App\Modules\ImportExportCustomization\Runners\Export\Wp_Content;
17
18 class Export {
19 const ZIP_ARCHIVE_MODULE_MISSING = 'zip-archive-module-is-missing';
20
21 /**
22 * @var Export_Runner_Base[]
23 */
24 protected $runners = [];
25
26 /**
27 * Selected content types to export.
28 *
29 * @var array
30 */
31 private $settings_include;
32
33 /**
34 * The kit information. (e.g: title, description)
35 *
36 * @var array $export_data
37 */
38 private $settings_kit_info;
39
40 /**
41 * Customization settings for selective export.
42 *
43 * @var array
44 */
45 private $settings_customization;
46
47 /**
48 * Selected plugins to export.
49 * Contains the plugins essential data for export. (e.g: name, path, version, etc.)
50 *
51 * @var array
52 */
53 private $settings_selected_plugins;
54
55 /**
56 * Selected custom post types to export.
57 *
58 * @var array
59 */
60 private $settings_selected_custom_post_types;
61
62 /**
63 * The output data of the export process.
64 * Will be written into the manifest.json file.
65 *
66 * @var array
67 */
68 private $manifest_data;
69
70 /**
71 * The zip archive object.
72 *
73 * @var \ZipArchive
74 */
75 private $zip;
76
77 public function __construct( $settings = [] ) {
78 $this->settings_include = ! empty( $settings['include'] ) ? $settings['include'] : null;
79 $this->settings_kit_info = ! empty( $settings['kitInfo'] ) ? $settings['kitInfo'] : null;
80 $this->settings_customization = isset( $settings['customization'] ) ? $settings['customization'] : null;
81 $this->settings_selected_plugins = isset( $settings['plugins'] ) ? $settings['plugins'] : null;
82 $this->settings_selected_custom_post_types = isset( $settings['customization']['content']['customPostTypes'] ) ? $settings['customization']['content']['customPostTypes'] : null;
83 }
84
85 /**
86 * Register a runner.
87 *
88 * @param Export_Runner_Base $runner_instance
89 */
90 public function register( Export_Runner_Base $runner_instance ) {
91 $this->runners[ $runner_instance::get_name() ] = $runner_instance;
92 }
93
94 public function register_default_runners() {
95 $this->register( new Site_Settings() );
96 $this->register( new Plugins() );
97 $this->register( new Templates() );
98 $this->register( new Taxonomies() );
99 $this->register( new Elementor_Content() );
100 $this->register( new Wp_Content() );
101 }
102
103 /**
104 * Execute the export process.
105 *
106 * @return array The export data output.
107 *
108 * @throws \Exception If no export runners have been specified.
109 */
110 public function run() {
111 if ( empty( $this->runners ) ) {
112 throw new \Exception( 'Couldn’t execute the export process because no export runners have been specified. Try again by specifying export runners.' );
113 }
114
115 $this->set_default_settings();
116
117 $this->init_zip_archive();
118 $this->init_manifest_data();
119
120 $data = [
121 'include' => $this->settings_include,
122 'customization' => $this->settings_customization,
123 'selected_plugins' => $this->settings_selected_plugins,
124 'selected_custom_post_types' => $this->settings_selected_custom_post_types,
125 ];
126
127 foreach ( $this->runners as $runner ) {
128 if ( $runner->should_export( $data ) ) {
129 $export_result = $runner->export( $data );
130 $this->handle_export_result( $export_result );
131 }
132 }
133
134 $this->add_json_file( 'manifest', $this->manifest_data );
135
136 $zip_file_name = $this->zip->filename;
137 $this->zip->close();
138
139 return [
140 'manifest' => $this->manifest_data,
141 'file_name' => $zip_file_name,
142 ];
143 }
144
145 /**
146 * Set default settings for the export.
147 */
148 private function set_default_settings() {
149 if ( ! is_array( $this->get_settings_include() ) ) {
150 $this->settings_include( $this->get_default_settings_include() );
151 }
152
153 if ( ! is_array( $this->get_settings_kit_info() ) ) {
154 $this->settings_kit_info( $this->get_default_settings_kit_info() );
155 }
156
157 if ( ! is_array( $this->get_settings_selected_custom_post_types() ) && in_array( 'content', $this->settings_include, true ) ) {
158 $this->settings_selected_custom_post_types( $this->get_default_settings_custom_post_types() );
159 }
160
161 if ( ! is_array( $this->get_settings_selected_plugins() ) && in_array( 'plugins', $this->settings_include, true ) ) {
162 $this->settings_selected_plugins( $this->get_default_settings_selected_plugins() );
163 }
164
165 if ( ! is_array( $this->get_settings_customization() ) ) {
166 $this->settings_customization( $this->get_default_settings_customization() );
167 }
168 }
169
170 public function settings_include( $include ) {
171 $this->settings_include = $include;
172 }
173
174 public function get_settings_include() {
175 return $this->settings_include;
176 }
177
178 private function settings_kit_info( $kit_info ) {
179 $this->settings_kit_info = $kit_info;
180 }
181
182 private function get_settings_kit_info() {
183 return $this->settings_kit_info;
184 }
185
186 public function settings_customization( $customization ) {
187 $this->settings_customization = $customization;
188 }
189
190 public function get_settings_customization() {
191 return $this->settings_customization;
192 }
193
194 public function settings_selected_custom_post_types( $selected_custom_post_types ) {
195 $this->settings_selected_custom_post_types = $selected_custom_post_types;
196 }
197
198 public function get_settings_selected_custom_post_types() {
199 return $this->settings_selected_custom_post_types;
200 }
201
202 public function settings_selected_plugins( $plugins ) {
203 $this->settings_selected_plugins = $plugins;
204 }
205
206 public function get_settings_selected_plugins() {
207 return $this->settings_selected_plugins;
208 }
209
210 /**
211 * Get the default settings of which content types should be exported.
212 *
213 * @return array
214 */
215 private function get_default_settings_include() {
216 return [ 'templates', 'content', 'settings', 'plugins' ];
217 }
218
219 /**
220 * Get the default settings of the kit info.
221 *
222 * @return array
223 */
224 private function get_default_settings_kit_info() {
225 return [
226 'title' => 'kit',
227 'description' => '',
228 ];
229 }
230
231 /**
232 * Get the default settings of the plugins that should be exported.
233 *
234 * @return array{name: string, plugin:string, pluginUri: string, version: string}
235 */
236 private function get_default_settings_selected_plugins() {
237 $installed_plugins = Plugin::$instance->wp->get_plugins();
238
239 $result = [];
240 foreach ( $installed_plugins->all() as $key => $item ) {
241 $plugin_key = str_replace( '.php', '', $key ); // Inconsistency between get_plugins() and WP Rest API's key format.
242
243 $result[ $plugin_key ] = [
244 'name' => $item['Name'],
245 'plugin' => $plugin_key,
246 'pluginUri' => $item['PluginURI'],
247 'version' => $item['Version'],
248 ];
249 }
250 return $result;
251 }
252
253 private function get_default_settings_customization() {
254 return [
255 'settings' => null,
256 'templates' => null,
257 'content' => null,
258 'plugins' => null,
259 ];
260 }
261
262 /**
263 * Get the default settings of all the custom post types that should be exported.
264 * Should be all the custom post types that are not built in to WordPress and not part of Elementor.
265 *
266 * @return array
267 */
268 private function get_default_settings_custom_post_types() {
269 return Utils::get_registered_cpt_names();
270 }
271
272 /**
273 * Init the zip archive.
274 */
275 private function init_zip_archive() {
276 if ( ! class_exists( '\ZipArchive' ) ) {
277 throw new \Error( static::ZIP_ARCHIVE_MODULE_MISSING ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
278 }
279
280 $zip = new \ZipArchive();
281
282 $temp_dir = Plugin::$instance->uploads_manager->create_unique_dir();
283
284 $zip_file_name = $temp_dir . sanitize_title( $this->settings_kit_info['title'] ) . '.zip';
285
286 $zip->open( $zip_file_name, \ZipArchive::CREATE | \ZipArchive::OVERWRITE );
287
288 $this->zip = $zip;
289 }
290
291 /**
292 * Init the manifest data and add some basic info to it.
293 */
294 private function init_manifest_data() {
295 $kit_post = Plugin::$instance->kits_manager->get_active_kit()->get_post();
296
297 $manifest_data = [
298 'name' => sanitize_title( $this->settings_kit_info['title'] ),
299 'title' => $this->settings_kit_info['title'],
300 'description' => $this->settings_kit_info['description'],
301 'author' => get_the_author_meta( 'display_name', $kit_post->post_author ),
302 'version' => Module::FORMAT_VERSION,
303 'elementor_version' => ELEMENTOR_VERSION,
304 'created' => gmdate( 'Y-m-d H:i:s' ),
305 'thumbnail' => get_the_post_thumbnail_url( $kit_post ),
306 'site' => get_site_url(),
307 ];
308
309 $this->manifest_data = $manifest_data;
310 }
311
312 /**
313 * Handle the export process output.
314 * Add the manifest data from the runner to the manifest.json file.
315 * Create files according to the files array that should be exported by the runner.
316 *
317 * @param array $export_result
318 */
319 private function handle_export_result( $export_result ) {
320 foreach ( $export_result['manifest'] as $data ) {
321 $this->manifest_data += $data;
322 }
323
324 if ( isset( $export_result['files']['path'] ) ) {
325 $export_result['files'] = [ $export_result['files'] ];
326 }
327
328 foreach ( $export_result['files'] as $file ) {
329 $file_extension = pathinfo( $file['path'], PATHINFO_EXTENSION );
330 if ( empty( $file_extension ) ) {
331 $this->add_json_file(
332 $file['path'],
333 $file['data']
334 );
335 } else {
336 $this->add_file(
337 $file['path'],
338 $file['data']
339 );
340 }
341 }
342 }
343
344 /**
345 * Add json file to the zip archive.
346 *
347 * @param string $path The relative path to the file.
348 * @param array $content The content of the file.
349 * @param int $json_flags
350 */
351 private function add_json_file( $path, array $content, $json_flags = 0 ) {
352 if ( ! Str::ends_with( $path, '.json' ) ) {
353 $path .= '.json';
354 }
355
356 $this->add_file( $path, wp_json_encode( $content, $json_flags ) );
357 }
358
359 /**
360 * Add file to the zip archive.
361 *
362 * @param string $file
363 * @param string $content The content of the file.
364 */
365 private function add_file( $file, $content ) {
366 $this->zip->addFromString( $file, $content );
367 }
368 }
369