PluginProbe
Elementor Website Builder – more than just a page builder / 3.32.0-dev2
Elementor Website Builder – more than just a page builder v3.32.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 / api.php

api.php in Elementor Website Builder – more than just a page builder 3.32.0-dev2, at includes/api.php

351 lines 8.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Elementor;
3
4 use Elementor\Core\Common\Modules\Connect\Apps\Library;
5
6 if ( ! defined( 'ABSPATH' ) ) {
7 exit; // Exit if accessed directly.
8 }
9
10 /**
11 * Elementor API.
12 *
13 * Elementor API handler class is responsible for communicating with Elementor
14 * remote servers retrieving templates data and to send uninstall feedback.
15 *
16 * @since 1.0.0
17 */
18 class Api {
19
20 /**
21 * Elementor feed option key.
22 */
23 const FEED_OPTION_KEY = 'elementor_remote_info_feed_data';
24
25 const TRANSIENT_KEY_PREFIX = 'elementor_remote_info_api_data_';
26
27 /**
28 * API info URL.
29 *
30 * Holds the URL of the info API.
31 *
32 * @access public
33 * @static
34 *
35 * @var string API info URL. (v2 excludes the Library info)
36 */
37 public static $api_info_url = 'https://my.elementor.com/api/v2/info/';
38
39 /**
40 * API feedback URL.
41 *
42 * Holds the URL of the feedback API.
43 *
44 * @access private
45 * @static
46 *
47 * @var string API feedback URL.
48 */
49 private static $api_feedback_url = 'https://my.elementor.com/api/v1/feedback/';
50
51 private static $api_library_info_url = 'https://my.elementor.com/api/v1/templates/info/';
52
53 private static function get_info_data( $force_update = false, $additinal_status = false ) {
54 $cache_key = self::TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION;
55
56 $info_data = get_transient( $cache_key );
57
58 if ( $force_update || empty( $info_data ) ) {
59 $timeout = ( $force_update ) ? 25 : 8;
60
61 $body_request = [
62 // Which API version is used.
63 'api_version' => ELEMENTOR_VERSION,
64 // Which language to return.
65 'site_lang' => get_bloginfo( 'language' ),
66 ];
67
68 $site_key = self::get_site_key();
69 if ( ! empty( $site_key ) ) {
70 $body_request['site_key'] = $site_key;
71 }
72
73 if ( ! empty( $additinal_status ) ) {
74 $body_request['status'] = $additinal_status;
75 $timeout = 3;
76 }
77
78 $response = wp_remote_get( self::$api_info_url, [
79 'timeout' => $timeout,
80 'body' => $body_request,
81 ] );
82
83 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
84 set_transient( $cache_key, [], 2 * HOUR_IN_SECONDS );
85
86 return false;
87 }
88
89 $info_data = json_decode( wp_remote_retrieve_body( $response ), true );
90
91 if ( empty( $info_data ) || ! is_array( $info_data ) ) {
92 set_transient( $cache_key, [], 2 * HOUR_IN_SECONDS );
93
94 return false;
95 }
96
97 if ( isset( $info_data['library'] ) ) {
98 unset( $info_data['library'] );
99 }
100
101 if ( isset( $info_data['feed'] ) ) {
102 update_option( self::FEED_OPTION_KEY, $info_data['feed'], 'no' );
103
104 unset( $info_data['feed'] );
105 }
106
107 set_transient( $cache_key, $info_data, 12 * HOUR_IN_SECONDS );
108 }
109
110 return $info_data;
111 }
112
113 public static function get_site_key() {
114 if ( null === Plugin::$instance->common ) {
115 return get_option( Library::OPTION_CONNECT_SITE_KEY );
116 }
117
118 /** @var Library $library */
119 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
120
121 if ( ! $library || ! method_exists( $library, 'get_site_key' ) ) {
122 return false;
123 }
124
125 return $library->get_site_key();
126 }
127
128 /**
129 * Get upgrade notice.
130 *
131 * Retrieve the upgrade notice if one exists, or false otherwise.
132 *
133 * @since 1.0.0
134 * @access public
135 * @static
136 *
137 * @return array|false Upgrade notice, or false none exist.
138 */
139 public static function get_upgrade_notice() {
140 $data = self::get_info_data();
141
142 if ( empty( $data['upgrade_notice'] ) ) {
143 return false;
144 }
145
146 return $data['upgrade_notice'];
147 }
148
149 public static function get_admin_notice() {
150 $data = self::get_info_data();
151 if ( empty( $data['admin_notice'] ) ) {
152 return false;
153 }
154 return $data['admin_notice'];
155 }
156
157 public static function get_canary_deployment_info( $force = false ) {
158 $data = self::get_info_data( $force );
159
160 if ( empty( $data['canary_deployment'] ) ) {
161 return false;
162 }
163
164 return $data['canary_deployment'];
165 }
166
167 public static function get_promotion_widgets() {
168 $data = self::get_info_data();
169
170 if ( ! isset( $data['pro_widgets'] ) ) {
171 $data['pro_widgets'] = [];
172 }
173
174 return $data['pro_widgets'];
175 }
176
177 /**
178 * Get templates data.
179 *
180 * Retrieve the templates data from a remote server.
181 *
182 * @since 2.0.0
183 * @access public
184 * @static
185 *
186 * @param bool $force_update Optional. Whether to force the data update or
187 * not. Default is false.
188 *
189 * @return array The templates' data.
190 */
191 public static function get_library_data( bool $force_update = false ): array {
192 /**
193 * Filters the body of the request to get library templates data.
194 *
195 * @param-out array $body_request The body of the request.
196 */
197 $body_request = apply_filters( 'elementor/remote/library/templates/request/body', [] );
198
199 $site_key = self::get_site_key();
200 if ( ! empty( $site_key ) ) {
201 $body_request['site_key'] = $site_key;
202 }
203
204 /**
205 * Filters the URL to get library templates data.
206 *
207 * @param-out string $url The URL to get library templates data.
208 */
209 $url = apply_filters( 'elementor/remote/library/templates/request/url', self::$api_library_info_url );
210
211 $response = wp_remote_get( $url, [
212 'timeout' => 25,
213 'body' => $body_request,
214 ] );
215
216 if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) {
217 return [];
218 }
219
220 $library_data = json_decode( wp_remote_retrieve_body( $response ), true );
221
222 return empty( $library_data ) ? [] : $library_data;
223 }
224
225 /**
226 * Get feed data.
227 *
228 * Retrieve the feed info data from remote elementor server.
229 *
230 * @since 1.9.0
231 * @access public
232 * @static
233 *
234 * @param bool $force_update Optional. Whether to force the data update or
235 * not. Default is false.
236 *
237 * @return array Feed data.
238 */
239 public static function get_feed_data( $force_update = false ) {
240 self::get_info_data( $force_update );
241
242 $feed = get_option( self::FEED_OPTION_KEY );
243
244 if ( empty( $feed ) ) {
245 return [];
246 }
247
248 return $feed;
249 }
250
251 public static function get_deactivation_data() {
252 $data = self::get_info_data( true, 'deactivated' );
253
254 if ( empty( $data['deactivate_data'] ) ) {
255 return false;
256 }
257
258 return $data['deactivate_data'];
259 }
260
261 public static function get_uninstalled_data() {
262 $data = self::get_info_data( true, 'uninstalled' );
263
264 if ( empty( $data['uninstall_data'] ) ) {
265 return false;
266 }
267
268 return $data['uninstall_data'];
269 }
270
271 /**
272 * Get template content.
273 *
274 * Retrieve the templates content received from a remote server.
275 *
276 * @since 1.0.0
277 * @access public
278 * @static
279 *
280 * @param int $template_id The template ID.
281 *
282 * @return object|\WP_Error The template content.
283 */
284 public static function get_template_content( $template_id ) {
285 /** @var Library $library */
286 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
287
288 return $library->get_template_content( $template_id );
289 }
290
291 /**
292 * Send Feedback.
293 *
294 * Fires a request to Elementor server with the feedback data.
295 *
296 * @since 1.0.0
297 * @access public
298 * @static
299 *
300 * @param string $feedback_key Feedback key.
301 * @param string $feedback_text Feedback text.
302 *
303 * @return array The response of the request.
304 */
305 public static function send_feedback( $feedback_key, $feedback_text ) {
306 return wp_remote_post( self::$api_feedback_url, [
307 'timeout' => 30,
308 'body' => [
309 'api_version' => ELEMENTOR_VERSION,
310 'site_lang' => get_bloginfo( 'language' ),
311 'feedback_key' => $feedback_key,
312 'feedback' => $feedback_text,
313 ],
314 ] );
315 }
316
317 /**
318 * Ajax reset API data.
319 *
320 * Reset Elementor library API data using an ajax call.
321 *
322 * @since 1.0.0
323 * @access public
324 * @static
325 */
326 public static function ajax_reset_api_data() {
327 check_ajax_referer( 'elementor_reset_library', '_nonce' );
328
329 if ( ! current_user_can( 'manage_options' ) ) {
330 wp_send_json_error( 'Permission denied' );
331 }
332
333 self::get_info_data( true );
334
335 wp_send_json_success();
336 }
337
338 /**
339 * Init.
340 *
341 * Initialize Elementor API.
342 *
343 * @since 1.0.0
344 * @access public
345 * @static
346 */
347 public static function init() {
348 add_action( 'wp_ajax_elementor_reset_library', [ __CLASS__, 'ajax_reset_api_data' ] );
349 }
350 }
351