PluginProbe
Elementor Website Builder – more than just a page builder / 4.2.0
Elementor Website Builder – more than just a page builder v4.2.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 / includes / template-library / sources / remote.php

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

325 lines 8.5 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\Api;
5 use Elementor\Core\Common\Modules\Connect\Module as ConnectModule;
6 use Elementor\Plugin;
7
8 if ( ! defined( 'ABSPATH' ) ) {
9 exit; // Exit if accessed directly.
10 }
11
12 /**
13 * Elementor template library remote source.
14 *
15 * Elementor template library remote source handler class is responsible for
16 * handling remote templates from Elementor.com servers.
17 *
18 * @since 1.0.0
19 */
20 class Source_Remote extends Source_Base {
21
22 const API_TEMPLATES_URL = 'https://my.elementor.com/api/connect/v1/library/templates';
23
24 const TEMPLATES_DATA_TRANSIENT_KEY_PREFIX = 'elementor_remote_templates_data_';
25
26 /**
27 * Get remote template ID.
28 *
29 * Retrieve the remote template ID.
30 *
31 * @since 1.0.0
32 * @access public
33 *
34 * @return string The remote template ID.
35 */
36 public function get_id() {
37 return 'remote';
38 }
39
40 /**
41 * Get remote template title.
42 *
43 * Retrieve the remote template title.
44 *
45 * @since 1.0.0
46 * @access public
47 *
48 * @return string The remote template title.
49 */
50 public function get_title() {
51 return esc_html__( 'Remote', 'elementor' );
52 }
53
54 /**
55 * Register remote template data.
56 *
57 * Used to register custom template data like a post type, a taxonomy or any
58 * other data.
59 *
60 * @since 1.0.0
61 * @access public
62 */
63 public function register_data() {}
64
65 /**
66 * Get remote templates.
67 *
68 * Retrieve remote templates from Elementor.com servers.
69 *
70 * @since 1.0.0
71 * @access public
72 *
73 * @param array $args Optional. Not used in remote source.
74 *
75 * @return array Remote templates.
76 */
77 public function get_items( $args = [] ) {
78 $force_update = ! empty( $args['force_update'] ) && is_bool( $args['force_update'] );
79
80 $templates_data = $this->get_templates_data( $force_update );
81
82 $templates = [];
83
84 foreach ( $templates_data as $template_data ) {
85 $templates[] = $this->prepare_template( $template_data );
86 }
87
88 return $templates;
89 }
90
91 /**
92 * Get remote template.
93 *
94 * Retrieve a single remote template from Elementor.com servers.
95 *
96 * @since 1.0.0
97 * @access public
98 *
99 * @param int $template_id The template ID.
100 *
101 * @return array Remote template.
102 */
103 public function get_item( $template_id ) {
104 $templates = $this->get_items();
105
106 return $templates[ $template_id ];
107 }
108
109 /**
110 * Save remote template.
111 *
112 * Remote template from Elementor.com servers cannot be saved on the
113 * database as they are retrieved from remote servers.
114 *
115 * @since 1.0.0
116 * @access public
117 *
118 * @param array $template_data Remote template data.
119 *
120 * @return \WP_Error
121 */
122 public function save_item( $template_data ) {
123 return new \WP_Error( 'invalid_request', 'Cannot save template to a remote source' );
124 }
125
126 /**
127 * Update remote template.
128 *
129 * Remote template from Elementor.com servers cannot be updated on the
130 * database as they are retrieved from remote servers.
131 *
132 * @since 1.0.0
133 * @access public
134 *
135 * @param array $new_data New template data.
136 *
137 * @return \WP_Error
138 */
139 public function update_item( $new_data ) {
140 return new \WP_Error( 'invalid_request', 'Cannot update template to a remote source' );
141 }
142
143 /**
144 * Delete remote template.
145 *
146 * Remote template from Elementor.com servers cannot be deleted from the
147 * database as they are retrieved from remote servers.
148 *
149 * @since 1.0.0
150 * @access public
151 *
152 * @param int $template_id The template ID.
153 *
154 * @return \WP_Error
155 */
156 public function delete_template( $template_id ) {
157 return new \WP_Error( 'invalid_request', 'Cannot delete template from a remote source' );
158 }
159
160 /**
161 * Export remote template.
162 *
163 * Remote template from Elementor.com servers cannot be exported from the
164 * database as they are retrieved from remote servers.
165 *
166 * @since 1.0.0
167 * @access public
168 *
169 * @param int $template_id The template ID.
170 *
171 * @return \WP_Error
172 */
173 public function export_template( $template_id ) {
174 return new \WP_Error( 'invalid_request', 'Cannot export template from a remote source' );
175 }
176
177 /**
178 * Get remote template data.
179 *
180 * Retrieve the data of a single remote template from Elementor.com servers.
181 *
182 * @since 1.5.0
183 * @access public
184 *
185 * @param array $args Custom template arguments.
186 * @param string $context Optional. The context. Default is `display`.
187 *
188 * @return array|\WP_Error Remote Template data.
189 */
190 public function get_data( array $args, $context = 'display' ) {
191 $data = Api::get_template_content( $args['template_id'] );
192
193 if ( is_wp_error( $data ) ) {
194 return $data;
195 }
196
197 // Set the Request's state as an Elementor upload request, in order to support unfiltered file uploads.
198 Plugin::$instance->uploads_manager->set_elementor_upload_state( true );
199
200 // BC.
201 $data = (array) $data;
202
203 $data['content'] = $this->replace_elements_ids( $data['content'] );
204 $data['content'] = $this->process_export_import_content( $data['content'], 'on_import' );
205
206 $post_id = $args['editor_post_id'];
207 $document = Plugin::$instance->documents->get( $post_id );
208 if ( $document ) {
209 $data['content'] = $document->get_elements_raw_data( $data['content'], true );
210 }
211
212 // After the upload complete, set the elementor upload state back to false
213 Plugin::$instance->uploads_manager->set_elementor_upload_state( false );
214
215 return $data;
216 }
217
218 /**
219 * Get templates data from a transient or from a remote request.
220 * In any of the following 2 conditions, the remote request will be triggered:
221 * 1. Force update - "$force_update = true" parameter was passed.
222 * 2. The data saved in the transient is empty or not exist.
223 *
224 * @param bool $force_update
225 * @return array
226 */
227 protected function get_templates_data( bool $force_update ): array {
228 $experiments_manager = Plugin::$instance->experiments;
229 $editor_layout_type = $experiments_manager->is_feature_active( 'container' ) ? 'container_flexbox' : '';
230
231 return $this->get_templates( $editor_layout_type );
232 }
233
234 /**
235 * Get the templates from a remote server and set a transient.
236 *
237 * @param string $editor_layout_type
238 * @return array
239 */
240 protected function get_templates( string $editor_layout_type ): array {
241 $templates_data = $this->get_templates_remotely( $editor_layout_type );
242
243 return empty( $templates_data ) ? [] : $templates_data;
244 }
245
246 /**
247 * Fetch templates from the remote server.
248 *
249 * @param string $editor_layout_type
250 * @return array|false
251 */
252 protected function get_templates_remotely( string $editor_layout_type ) {
253 $response = wp_remote_get( static::API_TEMPLATES_URL, [
254 'body' => $this->get_templates_body_args( $editor_layout_type ),
255 ] );
256
257 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
258 return false;
259 }
260
261 $templates_data = json_decode( wp_remote_retrieve_body( $response ), true );
262
263 if ( empty( $templates_data ) || ! is_array( $templates_data ) ) {
264 return [];
265 }
266
267 return $templates_data;
268 }
269
270 /**
271 * Prepare the body arguments for the remote request.
272 *
273 * @param string $editor_layout_type
274 *
275 * @return array
276 */
277 protected function get_templates_body_args( string $editor_layout_type ): array {
278 return [
279 'plugin_version' => ELEMENTOR_VERSION,
280 'editor_layout_type' => $editor_layout_type,
281 ];
282 }
283
284 /**
285 * @since 2.2.0
286 * @access private
287 */
288 protected function prepare_template( array $template_data ) {
289 $favorite_templates = $this->get_user_meta( 'favorites' );
290
291 // BC: Support legacy APIs that don't have access tiers.
292 if ( isset( $template_data['access_tier'] ) ) {
293 $access_tier = $template_data['access_tier'];
294 } else {
295 $access_tier = 0 === $template_data['access_level']
296 ? ConnectModule::ACCESS_TIER_FREE
297 : ConnectModule::ACCESS_TIER_ESSENTIAL;
298 }
299
300 return [
301 'template_id' => $template_data['id'],
302 'source' => $this->get_id(),
303 'type' => $template_data['type'],
304 'subtype' => $template_data['subtype'],
305 'title' => $template_data['title'],
306 'thumbnail' => $template_data['thumbnail'],
307 'date' => $template_data['tmpl_created'],
308 'author' => $template_data['author'],
309 'tags' => json_decode( $template_data['tags'] ),
310 'isPro' => ( '1' === $template_data['is_pro'] ),
311 'accessLevel' => $template_data['access_level'],
312 'accessTier' => $access_tier,
313 'popularityIndex' => (int) $template_data['popularity_index'],
314 'trendIndex' => (int) $template_data['trend_index'],
315 'hasPageSettings' => ( '1' === $template_data['has_page_settings'] ),
316 'url' => $template_data['url'],
317 'favorite' => ! empty( $favorite_templates[ $template_data['id'] ] ),
318 ];
319 }
320
321 public function clear_cache() {
322 delete_transient( static::TEMPLATES_DATA_TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION );
323 }
324 }
325