balmet.com

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

api.php (6563B)


      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 		if ( ! isset( $data['pro_widgets'] ) ) {
    161 			$data['pro_widgets'] = [];
    162 		}
    163 
    164 		return $data['pro_widgets'];
    165 	}
    166 
    167 	/**
    168 	 * Get templates data.
    169 	 *
    170 	 * Retrieve the templates data from a remote server.
    171 	 *
    172 	 * @since 2.0.0
    173 	 * @access public
    174 	 * @static
    175 	 *
    176 	 * @param bool $force_update Optional. Whether to force the data update or
    177 	 *                                     not. Default is false.
    178 	 *
    179 	 * @return array The templates data.
    180 	 */
    181 	public static function get_library_data( $force_update = false ) {
    182 		self::get_info_data( $force_update );
    183 
    184 		$library_data = get_option( self::LIBRARY_OPTION_KEY );
    185 
    186 		if ( empty( $library_data ) ) {
    187 			return [];
    188 		}
    189 
    190 		return $library_data;
    191 	}
    192 
    193 	/**
    194 	 * Get feed data.
    195 	 *
    196 	 * Retrieve the feed info data from remote elementor server.
    197 	 *
    198 	 * @since 1.9.0
    199 	 * @access public
    200 	 * @static
    201 	 *
    202 	 * @param bool $force_update Optional. Whether to force the data update or
    203 	 *                                     not. Default is false.
    204 	 *
    205 	 * @return array Feed data.
    206 	 */
    207 	public static function get_feed_data( $force_update = false ) {
    208 		self::get_info_data( $force_update );
    209 
    210 		$feed = get_option( self::FEED_OPTION_KEY );
    211 
    212 		if ( empty( $feed ) ) {
    213 			return [];
    214 		}
    215 
    216 		return $feed;
    217 	}
    218 
    219 	/**
    220 	 * Get template content.
    221 	 *
    222 	 * Retrieve the templates content received from a remote server.
    223 	 *
    224 	 * @since 1.0.0
    225 	 * @access public
    226 	 * @static
    227 	 *
    228 	 * @param int $template_id The template ID.
    229 	 *
    230 	 * @return object|\WP_Error The template content.
    231 	 */
    232 	public static function get_template_content( $template_id ) {
    233 		/** @var Library $library */
    234 		$library = Plugin::$instance->common->get_component( 'connect' )->get_app( 'library' );
    235 
    236 		return $library->get_template_content( $template_id );
    237 	}
    238 
    239 	/**
    240 	 * Send Feedback.
    241 	 *
    242 	 * Fires a request to Elementor server with the feedback data.
    243 	 *
    244 	 * @since 1.0.0
    245 	 * @access public
    246 	 * @static
    247 	 *
    248 	 * @param string $feedback_key  Feedback key.
    249 	 * @param string $feedback_text Feedback text.
    250 	 *
    251 	 * @return array The response of the request.
    252 	 */
    253 	public static function send_feedback( $feedback_key, $feedback_text ) {
    254 		return wp_remote_post( self::$api_feedback_url, [
    255 			'timeout' => 30,
    256 			'body' => [
    257 				'api_version' => ELEMENTOR_VERSION,
    258 				'site_lang' => get_bloginfo( 'language' ),
    259 				'feedback_key' => $feedback_key,
    260 				'feedback' => $feedback_text,
    261 			],
    262 		] );
    263 	}
    264 
    265 	/**
    266 	 * Ajax reset API data.
    267 	 *
    268 	 * Reset Elementor library API data using an ajax call.
    269 	 *
    270 	 * @since 1.0.0
    271 	 * @access public
    272 	 * @static
    273 	 */
    274 	public static function ajax_reset_api_data() {
    275 		check_ajax_referer( 'elementor_reset_library', '_nonce' );
    276 
    277 		self::get_info_data( true );
    278 
    279 		wp_send_json_success();
    280 	}
    281 
    282 	/**
    283 	 * Init.
    284 	 *
    285 	 * Initialize Elementor API.
    286 	 *
    287 	 * @since 1.0.0
    288 	 * @access public
    289 	 * @static
    290 	 */
    291 	public static function init() {
    292 		add_action( 'wp_ajax_elementor_reset_library', [ __CLASS__, 'ajax_reset_api_data' ] );
    293 	}
    294 }