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 / wp-cli.php

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

286 lines 8.0 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;
4
5 use Elementor\Core\Utils\Collection;
6 use Elementor\Core\Utils\Plugins_Manager;
7 use Elementor\Plugin;
8 use Elementor\App\Modules\KitLibrary\Connect\Kit_Library;
9
10 if ( ! defined( 'ABSPATH' ) ) {
11 exit; // Exit if accessed directly
12 }
13
14 class Wp_Cli extends \WP_CLI_Command {
15
16 const AVAILABLE_SETTINGS = [ 'include', 'overrideConditions', 'selectedCustomPostTypes', 'plugins' ];
17
18 /**
19 * Export a Kit
20 *
21 * [--include]
22 * Which type of content to include. Possible values are 'content', 'templates', 'site-settings'.
23 * if this parameter won't be specified, All data types will be included.
24 *
25 * ## EXAMPLES
26 *
27 * 1. wp elementor kit export path/to/export-file-name.zip
28 * - This will export all site data to the specified file name.
29 *
30 * 2. wp elementor kit export path/to/export-file-name.zip --include=kit-settings,content
31 * - This will export only site settings and content.
32 *
33 * @param array $args
34 * @param array $assoc_args
35 */
36 public function export( $args, $assoc_args ) {
37 if ( empty( $args[0] ) ) {
38 \WP_CLI::error( 'Please specify a file name' );
39 }
40
41 \WP_CLI::line( 'Kit export started.' );
42
43 $export_settings = [];
44 foreach ( $assoc_args as $key => $value ) {
45 if ( ! in_array( $key, static::AVAILABLE_SETTINGS, true ) ) {
46 continue;
47 }
48
49 $export_settings[ $key ] = explode( ',', $value );
50 }
51
52 try {
53 /**
54 * Running the export process through the import-export module so the export property in the module will be available to use.
55 *
56 * @type Module $import_export_module
57 */
58 $import_export_module = Plugin::$instance->app->get_component( 'import-export' );
59 $result = $import_export_module->export_kit( $export_settings );
60
61 rename( $result['file_name'], $args[0] );
62 } catch ( \Error $error ) {
63 \WP_CLI::error( $error->getMessage() );
64 }
65
66 \WP_CLI::success( 'Kit exported successfully.' );
67 }
68
69 /**
70 * Import a Kit
71 *
72 * [--include]
73 * Which type of content to include. Possible values are 'content', 'templates', 'site-settings'.
74 * if this parameter won't be specified, All data types will be included.
75 *
76 * [--overrideConditions]
77 * Templates ids to override conditions for.
78 *
79 * [--sourceType]
80 * Which source type is used in the current session. Available values are 'local', 'remote', 'library'.
81 * The default value is 'local'
82 *
83 * ## EXAMPLES
84 *
85 * 1. wp elementor kit import path/to/elementor-kit.zip
86 * - This will import the whole kit file content.
87 *
88 * 2. wp elementor kit import path/to/elementor-kit.zip --include=site-settings,content
89 * - This will import only site settings and content.
90 *
91 * 3. wp elementor kit import path/to/elementor-kit.zip --overrideConditions=3478,4520
92 * - This will import all content and will override conditions for the given template ids.
93 *
94 * 4. wp elementor kit import path/to/elementor-kit.zip --unfilteredFilesUpload=enable
95 * - This will allow the import process to import unfiltered files.
96 *
97 * @param array $args
98 * @param array $assoc_args
99 */
100 public function import( array $args, array $assoc_args ) {
101 if ( ! current_user_can( 'administrator' ) ) {
102 \WP_CLI::error( 'You must run this command as an admin user' );
103 }
104
105 if ( empty( $args[0] ) ) {
106 \WP_CLI::error( 'Please specify a file to import' );
107 }
108
109 \WP_CLI::line( 'Kit import started' );
110
111 $assoc_args = wp_parse_args( $assoc_args, [
112 'sourceType' => 'local',
113 ] );
114
115 $url = null;
116 $file_path = $args[0];
117 $import_settings = [];
118 $import_settings['referrer'] = Module::REFERRER_LOCAL;
119
120 switch ( $assoc_args['sourceType'] ) {
121 case 'library':
122 $url = $this->get_url_from_library( $file_path );
123 $zip_path = $this->create_temp_file_from_url( $url );
124 $import_settings['referrer'] = Module::REFERRER_KIT_LIBRARY;
125 break;
126
127 case 'remote':
128 $zip_path = $this->create_temp_file_from_url( $file_path );
129 break;
130
131 case 'local':
132 $zip_path = $file_path;
133 break;
134
135 default:
136 \WP_CLI::error( 'Unknown source type.' );
137 break;
138 }
139
140 if ( 'enable' === $assoc_args['unfilteredFilesUpload'] ) {
141 Plugin::$instance->uploads_manager->enable_unfiltered_files_upload();
142 }
143
144 foreach ( $assoc_args as $key => $value ) {
145 if ( ! in_array( $key, static::AVAILABLE_SETTINGS, true ) ) {
146 continue;
147 }
148
149 $import_settings[ $key ] = explode( ',', $value );
150 }
151
152 try {
153 \WP_CLI::line( 'Importing data...' );
154
155 /**
156 * Running the import process through the import-export module so the import property in the module will be available to use.
157 *
158 * @type Module $import_export_module
159 */
160 $import_export_module = Plugin::$instance->app->get_component( 'import-export' );
161
162 if ( ! $import_export_module ) {
163 \WP_CLI::error( 'Import Export module is not available.' );
164 }
165
166 $import = $import_export_module->import_kit( $zip_path, $import_settings );
167
168 $manifest_data = $import_export_module->import->get_manifest();
169
170 /**
171 * Import Export Manifest Data
172 *
173 * Allows 3rd parties to read and edit the kit's manifest before it is used.
174 *
175 * @since 3.7.0
176 *
177 * @param array $manifest_data The Kit's Manifest data
178 */
179 $manifest_data = apply_filters( 'elementor/import-export/wp-cli/manifest_data', $manifest_data );
180
181 \WP_CLI::line( 'Removing temp files...' );
182
183 // The file was created from remote or library request, it also should be removed.
184 if ( $url ) {
185 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $zip_path ) );
186 }
187
188 \WP_CLI::success( 'Kit imported successfully' );
189 } catch ( \Error $error ) {
190 if ( $url ) {
191 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $zip_path ) );
192 }
193
194 \WP_CLI::error( $error->getMessage() );
195 }
196 }
197
198 /**
199 * Revert last imported kit.
200 */
201 public function revert() {
202 \WP_CLI::line( 'Kit revert started.' );
203
204 try {
205 /**
206 * Running the revert process through the import-export module so the revert property in the module will be available to use.
207 *
208 * @type Module $import_export_module
209 */
210 $import_export_module = Plugin::$instance->app->get_component( 'import-export' );
211 $import_export_module->revert_last_imported_kit();
212
213 } catch ( \Error $error ) {
214 \WP_CLI::error( $error->getMessage() );
215 }
216
217 \WP_CLI::success( 'Kit reverted successfully.' );
218 }
219
220 /**
221 * Helper to get kit url by the kit id
222 * TODO: Maybe extract it.
223 *
224 * @param $kit_id
225 *
226 * @return string
227 */
228 private function get_url_from_library( $kit_id ) {
229 /** @var Kit_Library $app */
230 $app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'kit-library' );
231
232 if ( ! $app ) {
233 \WP_CLI::error( 'Kit library app not found' );
234 }
235
236 $response = $app->download_link( $kit_id );
237
238 if ( is_wp_error( $response ) ) {
239 \WP_CLI::error( "Library Response: {$response->get_error_message()}" );
240 }
241
242 return $response->download_link;
243 }
244
245 /**
246 * Helper to get kit zip file path by the kit url
247 * TODO: Maybe extract it.
248 *
249 * @param $url
250 *
251 * @return string
252 */
253 private function create_temp_file_from_url( $url ) {
254 \WP_CLI::line( 'Extracting zip archive...' );
255 $response = wp_remote_get( $url );
256
257 if ( is_wp_error( $response ) ) {
258 \WP_CLI::error( "Download file url: {$response->get_error_message()}" );
259 }
260
261 if ( 200 !== $response['response']['code'] ) {
262 \WP_CLI::error( "Download file url: {$response['response']['message']}" );
263 }
264
265 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
266 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
267
268 $file = Plugin::$instance->uploads_manager->create_temp_file( $response['body'], 'kit.zip' );
269
270 // After the upload complete, set the elementor upload state back to false.
271 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
272
273 return $file;
274 }
275
276 /**
277 * Handle the import process of plugins.
278 *
279 * Returns a string contains the successfully installed and activated plugins.
280 *
281 * @param array $plugins
282 * @return string
283 */
284
285 }
286