PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.0-dev4
Elementor Website Builder – more than just a page builder v3.26.0-dev4
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 3.26.0-dev4, at includes/template-library/sources/remote.php

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