PluginProbe
Elementor Website Builder – more than just a page builder / 3.26.3
Elementor Website Builder – more than just a page builder v3.26.3
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.26.3, at includes/api.php

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