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