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

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