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

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

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