admin-templates
1 year ago
base
2 weeks ago
controls
4 months ago
editor-templates
2 weeks ago
elements
2 weeks ago
interfaces
1 year ago
libraries
1 year ago
managers
2 weeks ago
settings
2 months ago
template-library
1 week ago
widgets
2 weeks ago
api.php
2 weeks ago
autoloader.php
7 months ago
beta-testers.php
3 years ago
compatibility.php
1 year ago
conditions.php
3 years ago
db.php
4 months ago
editor-assets-api.php
2 months ago
embed.php
1 year ago
fonts.php
1 year ago
frontend.php
1 month ago
heartbeat.php
3 years ago
maintenance-mode.php
7 months ago
maintenance.php
1 year ago
plugin.php
1 week ago
preview.php
2 weeks ago
rollback.php
4 months ago
shapes.php
9 months ago
stylesheet.php
8 months ago
tracker.php
6 months ago
user-data.php
7 months ago
user.php
5 months ago
utils.php
2 weeks ago
api.php
371 lines
| 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. (v2 excludes the Library info) |
| 41 | */ |
| 42 | public static $api_info_url = 'https://my.elementor.com/api/v2/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 $api_library_info_url = 'https://my.elementor.com/api/v1/templates/info/'; |
| 57 | |
| 58 | private static function get_info_data( $force_update = false, $additional_status = false ) { |
| 59 | $cache_key = self::TRANSIENT_KEY_PREFIX . ELEMENTOR_VERSION; |
| 60 | |
| 61 | $info_data = get_transient( $cache_key ); |
| 62 | |
| 63 | if ( $force_update || empty( $info_data ) ) { |
| 64 | $timeout = ( $force_update ) ? 25 : 8; |
| 65 | |
| 66 | $body_request = [ |
| 67 | // Which API version is used. |
| 68 | 'api_version' => ELEMENTOR_VERSION, |
| 69 | // Which language to return. |
| 70 | 'site_lang' => get_bloginfo( 'language' ), |
| 71 | ]; |
| 72 | |
| 73 | $site_key = self::get_site_key(); |
| 74 | if ( ! empty( $site_key ) ) { |
| 75 | $body_request['site_key'] = $site_key; |
| 76 | } |
| 77 | |
| 78 | if ( ! empty( $additional_status ) ) { |
| 79 | $body_request['status'] = $additional_status; |
| 80 | $timeout = 3; |
| 81 | } |
| 82 | |
| 83 | $response = wp_remote_get( self::$api_info_url, [ |
| 84 | 'timeout' => $timeout, |
| 85 | 'body' => $body_request, |
| 86 | ] ); |
| 87 | |
| 88 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 89 | set_transient( $cache_key, [ 'last_error' => gmdate( 'c' ) ], 2 * HOUR_IN_SECONDS ); |
| 90 | |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | $info_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 95 | |
| 96 | if ( empty( $info_data ) || ! is_array( $info_data ) ) { |
| 97 | set_transient( $cache_key, [ 'last_error' => gmdate( 'c' ) ], 2 * HOUR_IN_SECONDS ); |
| 98 | |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | if ( isset( $info_data['library'] ) ) { |
| 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( bool $force_update = false ): array { |
| 197 | /** |
| 198 | * Filters the body of the request to get library templates data. |
| 199 | * |
| 200 | * @param-out array $body_request The body of the request. |
| 201 | */ |
| 202 | $body_request = apply_filters( 'elementor/remote/library/templates/request/body', [] ); |
| 203 | |
| 204 | $site_key = self::get_site_key(); |
| 205 | if ( ! empty( $site_key ) ) { |
| 206 | $body_request['site_key'] = $site_key; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Filters the URL to get library templates data. |
| 211 | * |
| 212 | * @param-out string $url The URL to get library templates data. |
| 213 | */ |
| 214 | $url = apply_filters( 'elementor/remote/library/templates/request/url', self::$api_library_info_url ); |
| 215 | |
| 216 | $response = wp_remote_get( $url, [ |
| 217 | 'timeout' => 25, |
| 218 | 'body' => $body_request, |
| 219 | ] ); |
| 220 | |
| 221 | if ( is_wp_error( $response ) || 200 !== (int) wp_remote_retrieve_response_code( $response ) ) { |
| 222 | return []; |
| 223 | } |
| 224 | |
| 225 | $library_data = json_decode( wp_remote_retrieve_body( $response ), true ); |
| 226 | |
| 227 | /** |
| 228 | * Filters the library data to allow 3rd party extending the response data. |
| 229 | * |
| 230 | * @since 3.32.2 |
| 231 | * @param-out array $library_data an array of templates data. |
| 232 | */ |
| 233 | $library_data = apply_filters( 'elementor/remote/library/data', $library_data ); |
| 234 | |
| 235 | if ( empty( $library_data ) || ! is_array( $library_data ) ) { |
| 236 | return []; |
| 237 | } |
| 238 | |
| 239 | // the following update & get are a temporary measure, to allow 3rd party plugins inject more templates: |
| 240 | update_option( self::LIBRARY_OPTION_KEY, $library_data, 'no' ); |
| 241 | |
| 242 | return get_option( self::LIBRARY_OPTION_KEY, $library_data ); |
| 243 | } |
| 244 | |
| 245 | /** |
| 246 | * Get feed data. |
| 247 | * |
| 248 | * Retrieve the feed info data from remote elementor server. |
| 249 | * |
| 250 | * @since 1.9.0 |
| 251 | * @access public |
| 252 | * @static |
| 253 | * |
| 254 | * @param bool $force_update Optional. Whether to force the data update or |
| 255 | * not. Default is false. |
| 256 | * |
| 257 | * @return array Feed data. |
| 258 | */ |
| 259 | public static function get_feed_data( $force_update = false ) { |
| 260 | self::get_info_data( $force_update ); |
| 261 | |
| 262 | $feed = get_option( self::FEED_OPTION_KEY ); |
| 263 | |
| 264 | if ( empty( $feed ) ) { |
| 265 | return []; |
| 266 | } |
| 267 | |
| 268 | return $feed; |
| 269 | } |
| 270 | |
| 271 | public static function get_deactivation_data() { |
| 272 | $data = self::get_info_data( true, 'deactivated' ); |
| 273 | |
| 274 | if ( empty( $data['deactivate_data'] ) ) { |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | return $data['deactivate_data']; |
| 279 | } |
| 280 | |
| 281 | public static function get_uninstalled_data() { |
| 282 | $data = self::get_info_data( true, 'uninstalled' ); |
| 283 | |
| 284 | if ( empty( $data['uninstall_data'] ) ) { |
| 285 | return false; |
| 286 | } |
| 287 | |
| 288 | return $data['uninstall_data']; |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Get template content. |
| 293 | * |
| 294 | * Retrieve the templates content received from a remote server. |
| 295 | * |
| 296 | * @since 1.0.0 |
| 297 | * @access public |
| 298 | * @static |
| 299 | * |
| 300 | * @param int $template_id The template ID. |
| 301 | * |
| 302 | * @return object|\WP_Error The template content. |
| 303 | */ |
| 304 | public static function get_template_content( $template_id ) { |
| 305 | /** @var Library $library */ |
| 306 | $library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' ); |
| 307 | |
| 308 | return $library->get_template_content( $template_id ); |
| 309 | } |
| 310 | |
| 311 | /** |
| 312 | * Send Feedback. |
| 313 | * |
| 314 | * Fires a request to Elementor server with the feedback data. |
| 315 | * |
| 316 | * @since 1.0.0 |
| 317 | * @access public |
| 318 | * @static |
| 319 | * |
| 320 | * @param string $feedback_key Feedback key. |
| 321 | * @param string $feedback_text Feedback text. |
| 322 | * |
| 323 | * @return array The response of the request. |
| 324 | */ |
| 325 | public static function send_feedback( $feedback_key, $feedback_text ) { |
| 326 | return wp_remote_post( self::$api_feedback_url, [ |
| 327 | 'timeout' => 30, |
| 328 | 'body' => [ |
| 329 | 'api_version' => ELEMENTOR_VERSION, |
| 330 | 'site_lang' => get_bloginfo( 'language' ), |
| 331 | 'feedback_key' => $feedback_key, |
| 332 | 'feedback' => $feedback_text, |
| 333 | ], |
| 334 | ] ); |
| 335 | } |
| 336 | |
| 337 | /** |
| 338 | * Ajax reset API data. |
| 339 | * |
| 340 | * Reset Elementor library API data using an ajax call. |
| 341 | * |
| 342 | * @since 1.0.0 |
| 343 | * @access public |
| 344 | * @static |
| 345 | */ |
| 346 | public static function ajax_reset_api_data() { |
| 347 | check_ajax_referer( 'elementor_reset_library', '_nonce' ); |
| 348 | |
| 349 | if ( ! current_user_can( 'manage_options' ) ) { |
| 350 | wp_send_json_error( 'Permission denied' ); |
| 351 | } |
| 352 | |
| 353 | self::get_info_data( true ); |
| 354 | |
| 355 | wp_send_json_success(); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | * Init. |
| 360 | * |
| 361 | * Initialize Elementor API. |
| 362 | * |
| 363 | * @since 1.0.0 |
| 364 | * @access public |
| 365 | * @static |
| 366 | */ |
| 367 | public static function init() { |
| 368 | add_action( 'wp_ajax_elementor_reset_library', [ __CLASS__, 'ajax_reset_api_data' ] ); |
| 369 | } |
| 370 | } |
| 371 |