PluginProbe
Elementor Website Builder – more than just a page builder / 3.28.0-dev2
Elementor Website Builder – more than just a page builder v3.28.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 / includes / template-library / sources / cloud.php

cloud.php in Elementor Website Builder – more than just a page builder 3.28.0-dev2, at includes/template-library/sources/cloud.php

257 lines 7.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor\TemplateLibrary;
3
4 use Elementor\Core\Utils\Exceptions;
5 use Elementor\Modules\CloudLibrary\Connect\Cloud_Library;
6 use Elementor\Plugin;
7 use Elementor\DB;
8
9 if ( ! defined( 'ABSPATH' ) ) {
10 exit; // Exit if accessed directly.
11 }
12
13 class Source_Cloud extends Source_Base {
14 const FOLDER_RESOURCE_TYPE = 'FOLDER';
15 const TEMPLATE_RESOURCE_TYPE = 'TEMPLATE';
16
17 protected function get_app(): Cloud_Library {
18 $cloud_library_app = Plugin::$instance->common->get_component( 'connect' )->get_app( 'cloud-library' );
19
20 if ( ! $cloud_library_app ) {
21 $error_message = esc_html__( 'Cloud-Library is not instantiated.', 'elementor' );
22
23 throw new \Exception( $error_message, Exceptions::FORBIDDEN ); // phpcs:ignore WordPress.Security.EscapeOutput.ExceptionNotEscaped
24 }
25
26 return $cloud_library_app;
27 }
28
29 public function get_id(): string {
30 return 'cloud';
31 }
32
33 public function get_title(): string {
34 return esc_html__( 'Cloud Library', 'elementor' );
35 }
36
37 public function register_data() {}
38
39 public function get_items( $args = [] ) {
40 return $this->get_app()->get_resources( $args );
41 }
42
43 public function get_item_children( array $args = [] ) {
44 return $this->get_app()->get_resources( [ 'parentId' => $args['template_id'] ] );
45 }
46
47 public function get_item( $id ) {
48 return $this->get_app()->get_resource( [ 'id' => $id ] );
49 }
50
51 public function get_data( array $args ) {
52 $data = $this->get_app()->get_resource( [ 'id' => $args['template_id'] ] );
53
54 if ( is_wp_error( $data ) || empty( $data['content'] ) ) {
55 return $data;
56 }
57
58 $decoded_data = json_decode( $data['content'], true );
59 $data['content'] = $decoded_data['content'];
60
61 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
62
63 $data['content'] = $this->replace_elements_ids( $data['content'] );
64 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
65
66 if ( ! empty( $args['editor_post_id'] ) ) {
67 $post_id = $args['editor_post_id'];
68 $document = Plugin::$instance->documents->get( $post_id );
69 if ( $document ) {
70 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
71 }
72 }
73
74 if ( ! empty( $args['with_page_settings'] ) ) {
75 $data['page_settings'] = $decoded_data['page_settings'];
76 }
77
78 // After the upload complete, set the elementor upload state back to false
79 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
80
81 return $data;
82 }
83
84 public function delete_template( $template_id ) {
85 return $this->get_app()->delete_resource( $template_id );
86 }
87
88 public function save_item( $template_data ): int {
89 $app = $this->get_app();
90
91 $resource_data = [
92 'title' => $template_data['title'] ?? esc_html__( '(no title)', 'elementor' ),
93 'type' => self::TEMPLATE_RESOURCE_TYPE,
94 'templateType' => $template_data['type'],
95 'parentId' => $template_data['parentId'] ?? null,
96 'content' => wp_json_encode( $template_data['content'] ),
97 ];
98
99 $response = $app->post_resource( $resource_data );
100
101 return (int) $response['id'];
102 }
103
104 public function save_folder( array $folder_data = [] ) {
105 $app = $this->get_app();
106
107 $resource_data = [
108 'title' => $folder_data['title'] ?? esc_html__( 'New Folder', 'elementor' ),
109 'type' => self::FOLDER_RESOURCE_TYPE,
110 'templateType' => 'folder',
111 'parentId' => null,
112 ];
113
114 $response = $app->post_resource( $resource_data );
115
116 return (int) $response['id'];
117 }
118
119 public function update_item( $template_data ) {
120 return $this->get_app()->update_resource( $template_data );
121 }
122
123 public function search_templates( array $args = [] ) {
124 return $this->get_app()->get_resources( $args );
125 }
126
127 public function export_template( $id ) {
128 $data = $this->get_app()->get_resource( [ 'id' => $id ] );
129
130 if ( is_wp_error( $data ) ) {
131 return new \WP_Error( 'export_template_error', 'An error has occured' );
132 }
133
134 if ( static::TEMPLATE_RESOURCE_TYPE === $data['type'] ) {
135 $this->handle_export_file( $data );
136 }
137
138 if ( static::FOLDER_RESOURCE_TYPE === $data['type'] ) {
139 $this->handle_export_folder( $id );
140 }
141 }
142
143 protected function handle_export_file( array $data ): void {
144 $file_data = $this->prepare_template_export( $data );
145
146 if ( is_wp_error( $file_data ) ) {
147 return;
148 }
149
150 $this->send_file_headers( $file_data['name'], strlen( $file_data['content'] ) );
151
152 $this->serve_file( $file_data['content'] );
153 }
154
155 protected function handle_export_folder( int $folder_id ) {
156 $data = $this->get_item_children( [ 'template_id' => $folder_id ] );
157
158 if ( empty( $data['templates'] ) ) {
159 throw new \Exception( 'Folder does not have any templates to export' );
160 }
161
162 $template_ids = array_map( fn( $template ) => $template['template_id'], $data['templates'] );
163
164 $this->export_multiple_templates( $template_ids );
165 }
166
167 private function prepare_template_export( $data ) {
168 if ( empty( $data['content'] ) ) {
169 throw new \Exception( 'Template data not found' );
170 }
171
172 $data['content'] = json_decode( $data['content'], true );
173
174 if ( empty( $data['content']['content'] ) ) {
175 throw new \Exception( 'The template is empty' );
176 }
177
178 $export_data = [
179 'content' => $data['content']['content'],
180 'page_settings' => $data['content']['page_settings'] ?? [],
181 'version' => DB::DB_VERSION,
182 'title' => $data['title'],
183 'type' => $data['templateType'],
184 ];
185
186 return [
187 'name' => 'elementor-' . $data['id'] . '-' . gmdate( 'Y-m-d' ) . '.json',
188 'content' => wp_json_encode( $export_data ),
189 ];
190 }
191
192 public function export_multiple_templates( array $template_ids ) {
193 $files = [];
194 $temp_path = Plugin::$instance->uploads_manager->create_unique_dir();
195
196 foreach ( $template_ids as $template_id ) {
197 $files[] = $this->get_file_item( $template_id, $temp_path );
198 }
199
200 if ( empty( $files ) ) {
201 throw new \Exception( 'There are no files to export (probably all the requested templates are empty).' );
202 }
203
204 list( $zip_archive_filename, $zip_complete_path ) = $this->handle_zip_file( $temp_path, $files );
205
206 $this->send_file_headers( $zip_archive_filename, $this->filesize( $zip_complete_path ) );
207
208 $this->serve_zip( $zip_complete_path );
209
210 Plugin::$instance->uploads_manager->remove_file_or_dir( $temp_path );
211 }
212
213 protected function handle_zip_file( string $temp_path, array $files ): array {
214 if ( ! class_exists( 'ZipArchive' ) ) {
215 throw new \Error( 'ZipArchive module missing' );
216 }
217
218 $zip_archive_filename = 'elementor-templates-' . gmdate( 'Y-m-d' ) . '.zip';
219
220 $zip_archive = new \ZipArchive();
221
222 $zip_complete_path = $temp_path . '/' . $zip_archive_filename;
223
224 $zip_archive->open( $zip_complete_path, \ZipArchive::CREATE );
225
226 foreach ( $files as $file ) {
227 $zip_archive->addFile( $file['path'], $file['name'] );
228 }
229
230 $zip_archive->close();
231
232 return [ $zip_archive_filename, $zip_complete_path ];
233 }
234
235 private function get_file_item( $template_id, string $temp_path ) {
236 $data = $this->get_app()->get_resource( [ 'id' => $template_id ] );
237 $file_data = $this->prepare_template_export( $data );
238
239 if ( is_wp_error( $file_data ) ) {
240 return;
241 }
242
243 $complete_path = $temp_path . $file_data['name'];
244
245 $put_contents = file_put_contents( $complete_path, $file_data['content'] );
246
247 if ( ! $put_contents ) {
248 throw new \Exception( sprintf( 'Cannot create file "%s".', esc_html( $file_data['name'] ) ) );
249 }
250
251 return [
252 'path' => $complete_path,
253 'name' => $file_data['name'],
254 ];
255 }
256 }
257