PluginProbe
Elementor Website Builder – more than just a page builder / 3.16.0-dev2
Elementor Website Builder – more than just a page builder v3.16.0-dev2
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.16.0-dev2, at app/modules/import-export/wp-cli.php

282 lines 7.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;
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 Plugin::$instance->logger->get_logger()->error( $error->getMessage(), [
191 'meta' => [
192 'trace' => $error->getTraceAsString(),
193 ],
194 ] );
195
196 if ( $url ) {
197 Plugin::$instance->uploads_manager->remove_file_or_dir( dirname( $zip_path ) );
198 }
199
200 \WP_CLI::error( $error->getMessage() );
201 }
202 }
203
204 /**
205 * Revert last imported kit.
206 */
207 public function revert() {
208 \WP_CLI::line( 'Kit revert started.' );
209
210 try {
211 /**
212 * Running the revert process through the import-export module so the revert property in the module will be available to use.
213 *
214 * @type Module $import_export_module
215 */
216 $import_export_module = Plugin::$instance->app->get_component( 'import-export' );
217 $import_export_module->revert_last_imported_kit();
218
219 } catch ( \Error $error ) {
220 \WP_CLI::error( $error->getMessage() );
221 }
222
223 \WP_CLI::success( 'Kit reverted successfully.' );
224 }
225
226 /**
227 * Helper to get kit url by the kit id
228 * TODO: Maybe extract it.
229 *
230 * @param $kit_id
231 *
232 * @return string
233 */
234 private function get_url_from_library( $kit_id ) {
235 /** @var Kit_Library $app */
236 $app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'kit-library' );
237
238 if ( ! $app ) {
239 \WP_CLI::error( 'Kit library app not found' );
240 }
241
242 $response = $app->download_link( $kit_id );
243
244 if ( is_wp_error( $response ) ) {
245 \WP_CLI::error( "Library Response: {$response->get_error_message()}" );
246 }
247
248 return $response->download_link;
249 }
250
251 /**
252 * Helper to get kit zip file path by the kit url
253 * TODO: Maybe extract it.
254 *
255 * @param $url
256 *
257 * @return string
258 */
259 private function create_temp_file_from_url( $url ) {
260 \WP_CLI::line( 'Extracting zip archive...' );
261 $response = wp_remote_get( $url );
262
263 if ( is_wp_error( $response ) ) {
264 \WP_CLI::error( "Download file url: {$response->get_error_message()}" );
265 }
266
267 if ( 200 !== $response['response']['code'] ) {
268 \WP_CLI::error( "Download file url: {$response['response']['message']}" );
269 }
270
271 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
272 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
273
274 $file = Plugin::$instance->uploads_manager->create_temp_file( $response['body'], 'kit.zip' );
275
276 // After the upload complete, set the elementor upload state back to false.
277 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
278
279 return $file;
280 }
281 }
282