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

291 lines 6.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 /**
31 * API info URL.
32 *
33 * Holds the URL of the info API.
34 *
35 * @access public
36 * @static
37 *
38 * @var string API info URL.
39 */
40 public static $api_info_url = 'https://my.elementor.com/api/v1/info/';
41
42 /**
43 * API feedback URL.
44 *
45 * Holds the URL of the feedback API.
46 *
47 * @access private
48 * @static
49 *
50 * @var string API feedback URL.
51 */
52 private static $api_feedback_url = 'https://my.elementor.com/api/v1/feedback/';
53
54 /**
55 * Get info data.
56 *
57 * This function notifies the user of upgrade notices, new templates and contributors.
58 *
59 * @since 2.0.0
60 * @access private
61 * @static
62 *
63 * @param bool $force_update Optional. Whether to force the data retrieval or
64 * not. Default is false.
65 *
66 * @return array|false Info data, or false.
67 */
68 private static function get_info_data( $force_update = false ) {
69 $cache_key = 'elementor_remote_info_api_data_' . ELEMENTOR_VERSION;
70
71 $info_data = get_transient( $cache_key );
72
73 if ( $force_update || false === $info_data ) {
74 $timeout = ( $force_update ) ? 25 : 8;
75
76 $response = wp_remote_get( self::$api_info_url, [
77 'timeout' => $timeout,
78 'body' => [
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
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 /**
119 * Get upgrade notice.
120 *
121 * Retrieve the upgrade notice if one exists, or false otherwise.
122 *
123 * @since 1.0.0
124 * @access public
125 * @static
126 *
127 * @return array|false Upgrade notice, or false none exist.
128 */
129 public static function get_upgrade_notice() {
130 $data = self::get_info_data();
131
132 if ( empty( $data['upgrade_notice'] ) ) {
133 return false;
134 }
135
136 return $data['upgrade_notice'];
137 }
138
139 public static function get_admin_notice() {
140 $data = self::get_info_data();
141 if ( empty( $data['admin_notice'] ) ) {
142 return false;
143 }
144 return $data['admin_notice'];
145 }
146
147 public static function get_canary_deployment_info( $force = false ) {
148 $data = self::get_info_data( $force );
149
150 if ( empty( $data['canary_deployment'] ) ) {
151 return false;
152 }
153
154 return $data['canary_deployment'];
155 }
156
157 public static function get_promotion_widgets() {
158 $data = self::get_info_data();
159
160 return $data['pro_widgets'];
161 }
162
163 /**
164 * Get templates data.
165 *
166 * Retrieve the templates data from a remote server.
167 *
168 * @since 2.0.0
169 * @access public
170 * @static
171 *
172 * @param bool $force_update Optional. Whether to force the data update or
173 * not. Default is false.
174 *
175 * @return array The templates data.
176 */
177 public static function get_library_data( $force_update = false ) {
178 self::get_info_data( $force_update );
179
180 $library_data = get_option( self::LIBRARY_OPTION_KEY );
181
182 if ( empty( $library_data ) ) {
183 return [];
184 }
185
186 return $library_data;
187 }
188
189 /**
190 * Get feed data.
191 *
192 * Retrieve the feed info data from remote elementor server.
193 *
194 * @since 1.9.0
195 * @access public
196 * @static
197 *
198 * @param bool $force_update Optional. Whether to force the data update or
199 * not. Default is false.
200 *
201 * @return array Feed data.
202 */
203 public static function get_feed_data( $force_update = false ) {
204 self::get_info_data( $force_update );
205
206 $feed = get_option( self::FEED_OPTION_KEY );
207
208 if ( empty( $feed ) ) {
209 return [];
210 }
211
212 return $feed;
213 }
214
215 /**
216 * Get template content.
217 *
218 * Retrieve the templates content received from a remote server.
219 *
220 * @since 1.0.0
221 * @access public
222 * @static
223 *
224 * @param int $template_id The template ID.
225 *
226 * @return object|\WP_Error The template content.
227 */
228 public static function get_template_content( $template_id ) {
229 /** @var Library $library */
230 $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
231
232 return $library->get_template_content( $template_id );
233 }
234
235 /**
236 * Send Feedback.
237 *
238 * Fires a request to Elementor server with the feedback data.
239 *
240 * @since 1.0.0
241 * @access public
242 * @static
243 *
244 * @param string $feedback_key Feedback key.
245 * @param string $feedback_text Feedback text.
246 *
247 * @return array The response of the request.
248 */
249 public static function send_feedback( $feedback_key, $feedback_text ) {
250 return wp_remote_post( self::$api_feedback_url, [
251 'timeout' => 30,
252 'body' => [
253 'api_version' => ELEMENTOR_VERSION,
254 'site_lang' => get_bloginfo( 'language' ),
255 'feedback_key' => $feedback_key,
256 'feedback' => $feedback_text,
257 ],
258 ] );
259 }
260
261 /**
262 * Ajax reset API data.
263 *
264 * Reset Elementor library API data using an ajax call.
265 *
266 * @since 1.0.0
267 * @access public
268 * @static
269 */
270 public static function ajax_reset_api_data() {
271 check_ajax_referer( 'elementor_reset_library', '_nonce' );
272
273 self::get_info_data( true );
274
275 wp_send_json_success();
276 }
277
278 /**
279 * Init.
280 *
281 * Initialize Elementor API.
282 *
283 * @since 1.0.0
284 * @access public
285 * @static
286 */
287 public static function init() {
288 add_action( 'wp_ajax_elementor_reset_library', [ __CLASS__, 'ajax_reset_api_data' ] );
289 }
290 }
291