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 / api.php

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

319 lines 7.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 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 /** @var \Elementor\Core\Common\Modules\Connect\Apps\Library $library */
129 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
130
131 if ( ! $library || ! method_exists( $library, 'get_site_key' ) ) {
132 return false;
133 }
134
135 return $library->get_site_key();
136 }
137
138 /**
139 * Get upgrade notice.
140 *
141 * Retrieve the upgrade notice if one exists, or false otherwise.
142 *
143 * @since 1.0.0
144 * @access public
145 * @static
146 *
147 * @return array|false Upgrade notice, or false none exist.
148 */
149 public static function get_upgrade_notice() {
150 $data = self::get_info_data();
151
152 if ( empty( $data['upgrade_notice'] ) ) {
153 return false;
154 }
155
156 return $data['upgrade_notice'];
157 }
158
159 public static function get_admin_notice() {
160 $data = self::get_info_data();
161 if ( empty( $data['admin_notice'] ) ) {
162 return false;
163 }
164 return $data['admin_notice'];
165 }
166
167 public static function get_canary_deployment_info( $force = false ) {
168 $data = self::get_info_data( $force );
169
170 if ( empty( $data['canary_deployment'] ) ) {
171 return false;
172 }
173
174 return $data['canary_deployment'];
175 }
176
177 public static function get_promotion_widgets() {
178 $data = self::get_info_data();
179
180 if ( ! isset( $data['pro_widgets'] ) ) {
181 $data['pro_widgets'] = [];
182 }
183
184 return $data['pro_widgets'];
185 }
186
187 /**
188 * Get templates data.
189 *
190 * Retrieve the templates data from a remote server.
191 *
192 * @since 2.0.0
193 * @access public
194 * @static
195 *
196 * @param bool $force_update Optional. Whether to force the data update or
197 * not. Default is false.
198 *
199 * @return array The templates data.
200 */
201 public static function get_library_data( $force_update = false ) {
202 self::get_info_data( $force_update );
203
204 $library_data = get_option( self::LIBRARY_OPTION_KEY );
205
206 if ( empty( $library_data ) ) {
207 return [];
208 }
209
210 return $library_data;
211 }
212
213 /**
214 * Get feed data.
215 *
216 * Retrieve the feed info data from remote elementor server.
217 *
218 * @since 1.9.0
219 * @access public
220 * @static
221 *
222 * @param bool $force_update Optional. Whether to force the data update or
223 * not. Default is false.
224 *
225 * @return array Feed data.
226 */
227 public static function get_feed_data( $force_update = false ) {
228 self::get_info_data( $force_update );
229
230 $feed = get_option( self::FEED_OPTION_KEY );
231
232 if ( empty( $feed ) ) {
233 return [];
234 }
235
236 return $feed;
237 }
238
239 /**
240 * Get template content.
241 *
242 * Retrieve the templates content received from a remote server.
243 *
244 * @since 1.0.0
245 * @access public
246 * @static
247 *
248 * @param int $template_id The template ID.
249 *
250 * @return object|\WP_Error The template content.
251 */
252 public static function get_template_content( $template_id ) {
253 /** @var Library $library */
254 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
255
256 return $library->get_template_content( $template_id );
257 }
258
259 /**
260 * Send Feedback.
261 *
262 * Fires a request to Elementor server with the feedback data.
263 *
264 * @since 1.0.0
265 * @access public
266 * @static
267 *
268 * @param string $feedback_key Feedback key.
269 * @param string $feedback_text Feedback text.
270 *
271 * @return array The response of the request.
272 */
273 public static function send_feedback( $feedback_key, $feedback_text ) {
274 return wp_remote_post( self::$api_feedback_url, [
275 'timeout' => 30,
276 'body' => [
277 'api_version' => ELEMENTOR_VERSION,
278 'site_lang' => get_bloginfo( 'language' ),
279 'feedback_key' => $feedback_key,
280 'feedback' => $feedback_text,
281 ],
282 ] );
283 }
284
285 /**
286 * Ajax reset API data.
287 *
288 * Reset Elementor library API data using an ajax call.
289 *
290 * @since 1.0.0
291 * @access public
292 * @static
293 */
294 public static function ajax_reset_api_data() {
295 check_ajax_referer( 'elementor_reset_library', '_nonce' );
296
297 if ( ! current_user_can( 'manage_options' ) ) {
298 wp_send_json_error( 'Permission denied' );
299 }
300
301 self::get_info_data( true );
302
303 wp_send_json_success();
304 }
305
306 /**
307 * Init.
308 *
309 * Initialize Elementor API.
310 *
311 * @since 1.0.0
312 * @access public
313 * @static
314 */
315 public static function init() {
316 add_action( 'wp_ajax_elementor_reset_library', [ __CLASS__, 'ajax_reset_api_data' ] );
317 }
318 }
319