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

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