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

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

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