Advanced_Ads_Modal.php
2 years ago
EDD_SL_Plugin_Updater.php
2 years ago
ad-ajax.php
2 years ago
ad-debug.php
2 years ago
ad-expiration.php
3 years ago
ad-health-notices.php
2 years ago
ad-model.php
2 years ago
ad-select.php
3 years ago
ad.php
2 years ago
ad_ajax_callbacks.php
2 years ago
ad_group.php
2 years ago
ad_placements.php
2 years ago
ad_type_abstract.php
2 years ago
ad_type_content.php
2 years ago
ad_type_dummy.php
2 years ago
ad_type_group.php
2 years ago
ad_type_image.php
2 years ago
ad_type_plain.php
2 years ago
checks.php
2 years ago
class-translation-promo.php
2 years ago
compatibility.php
2 years ago
display-conditions.php
2 years ago
filesystem.php
2 years ago
frontend_checks.php
2 years ago
in-content-injector.php
2 years ago
inline-css.php
2 years ago
plugin.php
2 years ago
upgrades.php
2 years ago
utils.php
3 years ago
visitor-conditions.php
2 years ago
widget.php
2 years ago
EDD_SL_Plugin_Updater.php
645 lines
| 1 | <?php // phpcs:ignoreFile |
| 2 | |
| 3 | // Exit if accessed directly |
| 4 | if ( ! defined( 'ABSPATH' ) ) { |
| 5 | exit; |
| 6 | } |
| 7 | |
| 8 | /** |
| 9 | * Allows plugins to use their own update API. |
| 10 | * |
| 11 | * @author Easy Digital Downloads |
| 12 | * @version 1.9.1 |
| 13 | */ |
| 14 | class ADVADS_SL_Plugin_Updater { |
| 15 | |
| 16 | private $api_url = ''; |
| 17 | private $api_data = array(); |
| 18 | private $plugin_file = ''; |
| 19 | private $name = ''; |
| 20 | private $slug = ''; |
| 21 | private $version = ''; |
| 22 | private $wp_override = false; |
| 23 | private $beta = false; |
| 24 | private $failed_request_cache_key; |
| 25 | |
| 26 | /** |
| 27 | * Class constructor. |
| 28 | * |
| 29 | * @uses plugin_basename() |
| 30 | * @uses hook() |
| 31 | * |
| 32 | * @param string $_api_url The URL pointing to the custom API endpoint. |
| 33 | * @param string $_plugin_file Path to the plugin file. |
| 34 | * @param array $_api_data Optional data to send with API calls. |
| 35 | */ |
| 36 | public function __construct( $_api_url, $_plugin_file, $_api_data = null ) { |
| 37 | |
| 38 | global $edd_plugin_data; |
| 39 | |
| 40 | $this->api_url = trailingslashit( $_api_url ); |
| 41 | $this->api_data = $_api_data; |
| 42 | $this->plugin_file = $_plugin_file; |
| 43 | $this->name = plugin_basename( $_plugin_file ); |
| 44 | $this->slug = basename( $_plugin_file, '.php' ); |
| 45 | $this->version = $_api_data['version']; |
| 46 | $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false; |
| 47 | $this->beta = ! empty( $this->api_data['beta'] ) ? true : false; |
| 48 | $this->failed_request_cache_key = 'edd_sl_failed_http_' . md5( $this->api_url ); |
| 49 | |
| 50 | $edd_plugin_data[ $this->slug ] = $this->api_data; |
| 51 | |
| 52 | /** |
| 53 | * Fires after the $edd_plugin_data is setup. |
| 54 | * |
| 55 | * @since x.x.x |
| 56 | * |
| 57 | * @param array $edd_plugin_data Array of EDD SL plugin data. |
| 58 | */ |
| 59 | do_action( 'post_edd_sl_plugin_updater_setup', $edd_plugin_data ); |
| 60 | |
| 61 | // Set up hooks. |
| 62 | $this->init(); |
| 63 | |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Set up WordPress filters to hook into WP's update process. |
| 68 | * |
| 69 | * @uses add_filter() |
| 70 | * |
| 71 | * @return void |
| 72 | */ |
| 73 | public function init() { |
| 74 | |
| 75 | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) ); |
| 76 | add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
| 77 | add_action( 'after_plugin_row', array( $this, 'show_update_notification' ), 10, 2 ); |
| 78 | add_action( 'admin_init', array( $this, 'show_changelog' ) ); |
| 79 | |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Check for Updates at the defined API endpoint and modify the update array. |
| 84 | * |
| 85 | * This function dives into the update API just when WordPress creates its update array, |
| 86 | * then adds a custom API call and injects the custom plugin data retrieved from the API. |
| 87 | * It is reassembled from parts of the native WordPress plugin update code. |
| 88 | * See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
| 89 | * |
| 90 | * @uses api_request() |
| 91 | * |
| 92 | * @param array $_transient_data Update array build by WordPress. |
| 93 | * @return array Modified update array with custom plugin data. |
| 94 | */ |
| 95 | public function check_update( $_transient_data ) { |
| 96 | |
| 97 | global $pagenow; |
| 98 | |
| 99 | if ( ! is_object( $_transient_data ) ) { |
| 100 | $_transient_data = new stdClass(); |
| 101 | } |
| 102 | |
| 103 | if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) { |
| 104 | return $_transient_data; |
| 105 | } |
| 106 | |
| 107 | $current = $this->get_repo_api_data(); |
| 108 | if ( false !== $current && is_object( $current ) && isset( $current->new_version ) ) { |
| 109 | if ( version_compare( $this->version, $current->new_version, '<' ) ) { |
| 110 | $_transient_data->response[ $this->name ] = $current; |
| 111 | } else { |
| 112 | // Populating the no_update information is required to support auto-updates in WordPress 5.5. |
| 113 | $_transient_data->no_update[ $this->name ] = $current; |
| 114 | } |
| 115 | } |
| 116 | $_transient_data->last_checked = time(); |
| 117 | $_transient_data->checked[ $this->name ] = $this->version; |
| 118 | |
| 119 | return $_transient_data; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Get repo API data from store. |
| 124 | * Save to cache. |
| 125 | * |
| 126 | * @return \stdClass |
| 127 | */ |
| 128 | public function get_repo_api_data() { |
| 129 | $version_info = $this->get_cached_version_info(); |
| 130 | |
| 131 | if ( false === $version_info ) { |
| 132 | $version_info = $this->api_request( |
| 133 | 'plugin_latest_version', |
| 134 | array( |
| 135 | 'slug' => $this->slug, |
| 136 | 'beta' => $this->beta, |
| 137 | ) |
| 138 | ); |
| 139 | if ( ! $version_info ) { |
| 140 | return false; |
| 141 | } |
| 142 | |
| 143 | // This is required for your plugin to support auto-updates in WordPress 5.5. |
| 144 | $version_info->plugin = $this->name; |
| 145 | $version_info->id = $this->name; |
| 146 | |
| 147 | $this->set_version_info_cache( $version_info ); |
| 148 | } |
| 149 | |
| 150 | return $version_info; |
| 151 | } |
| 152 | |
| 153 | /** |
| 154 | * Show the update notification on multisite subsites. |
| 155 | * |
| 156 | * @param string $file |
| 157 | * @param array $plugin |
| 158 | */ |
| 159 | public function show_update_notification( $file, $plugin ) { |
| 160 | |
| 161 | // Return early if in the network admin, or if this is not a multisite install. |
| 162 | if ( is_network_admin() || ! is_multisite() ) { |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | // Allow single site admins to see that an update is available. |
| 167 | if ( ! current_user_can( 'activate_plugins' ) ) { |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | if ( $this->name !== $file ) { |
| 172 | return; |
| 173 | } |
| 174 | |
| 175 | // Do not print any message if update does not exist. |
| 176 | $update_cache = get_site_transient( 'update_plugins' ); |
| 177 | |
| 178 | if ( ! isset( $update_cache->response[ $this->name ] ) ) { |
| 179 | if ( ! is_object( $update_cache ) ) { |
| 180 | $update_cache = new stdClass(); |
| 181 | } |
| 182 | $update_cache->response[ $this->name ] = $this->get_repo_api_data(); |
| 183 | } |
| 184 | |
| 185 | // Return early if this plugin isn't in the transient->response or if the site is running the current or newer version of the plugin. |
| 186 | if ( empty( $update_cache->response[ $this->name ] ) || version_compare( $this->version, $update_cache->response[ $this->name ]->new_version, '>=' ) ) { |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | printf( |
| 191 | '<tr class="plugin-update-tr %3$s" id="%1$s-update" data-slug="%1$s" data-plugin="%2$s">', |
| 192 | $this->slug, |
| 193 | $file, |
| 194 | in_array( $this->name, $this->get_active_plugins(), true ) ? 'active' : 'inactive' |
| 195 | ); |
| 196 | |
| 197 | echo '<td colspan="3" class="plugin-update colspanchange">'; |
| 198 | echo '<div class="update-message notice inline notice-warning notice-alt"><p>'; |
| 199 | |
| 200 | $changelog_link = ''; |
| 201 | if ( ! empty( $update_cache->response[ $this->name ]->sections->changelog ) ) { |
| 202 | $changelog_link = add_query_arg( |
| 203 | array( |
| 204 | 'edd_sl_action' => 'view_plugin_changelog', |
| 205 | 'plugin' => urlencode( $this->name ), |
| 206 | 'slug' => urlencode( $this->slug ), |
| 207 | 'TB_iframe' => 'true', |
| 208 | 'width' => 77, |
| 209 | 'height' => 911, |
| 210 | ), |
| 211 | self_admin_url( 'index.php' ) |
| 212 | ); |
| 213 | } |
| 214 | $update_link = add_query_arg( |
| 215 | array( |
| 216 | 'action' => 'upgrade-plugin', |
| 217 | 'plugin' => urlencode( $this->name ), |
| 218 | ), |
| 219 | self_admin_url( 'update.php' ) |
| 220 | ); |
| 221 | |
| 222 | printf( |
| 223 | /* translators: the plugin name. */ |
| 224 | esc_html__( 'There is a new version of %1$s available.', 'advanced-ads' ), |
| 225 | esc_html( $plugin['Name'] ) |
| 226 | ); |
| 227 | |
| 228 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 229 | echo ' '; |
| 230 | esc_html_e( 'Contact your network administrator to install the update.', 'advanced-ads' ); |
| 231 | } elseif ( empty( $update_cache->response[ $this->name ]->package ) && ! empty( $changelog_link ) ) { |
| 232 | echo ' '; |
| 233 | printf( |
| 234 | /* translators: 1. opening anchor tag, do not translate 2. the new plugin version 3. closing anchor tag, do not translate. */ |
| 235 | __( '%1$sView version %2$s details%3$s.', 'advanced-ads' ), |
| 236 | '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">', |
| 237 | esc_html( $update_cache->response[ $this->name ]->new_version ), |
| 238 | '</a>' |
| 239 | ); |
| 240 | } elseif ( ! empty( $changelog_link ) ) { |
| 241 | echo ' '; |
| 242 | printf( |
| 243 | /* translators: 1: link tag 2: Version 3: link tag close 4: link tag 5: link tag close */ |
| 244 | __( '%1$s View version %2$s details%3$s or %4$supdate now%5$s.', 'advanced-ads' ), |
| 245 | '<a target="_blank" class="thickbox open-plugin-details-modal" href="' . esc_url( $changelog_link ) . '">', |
| 246 | esc_html( $update_cache->response[ $this->name ]->new_version ), |
| 247 | '</a>', |
| 248 | '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">', |
| 249 | '</a>' |
| 250 | ); |
| 251 | } else { |
| 252 | printf( |
| 253 | ' %1$s%2$s%3$s', |
| 254 | '<a target="_blank" class="update-link" href="' . esc_url( wp_nonce_url( $update_link, 'upgrade-plugin_' . $file ) ) . '">', |
| 255 | esc_html__( 'Update now.', 'advanced-ads' ), |
| 256 | '</a>' |
| 257 | ); |
| 258 | } |
| 259 | |
| 260 | do_action( "in_plugin_update_message-{$file}", $plugin, $plugin ); |
| 261 | |
| 262 | echo '</p></div></td></tr>'; |
| 263 | } |
| 264 | |
| 265 | /** |
| 266 | * Gets the plugins active in a multisite network. |
| 267 | * |
| 268 | * @return array |
| 269 | */ |
| 270 | private function get_active_plugins() { |
| 271 | $active_plugins = (array) get_option( 'active_plugins' ); |
| 272 | $active_network_plugins = (array) get_site_option( 'active_sitewide_plugins' ); |
| 273 | |
| 274 | return array_merge( $active_plugins, array_keys( $active_network_plugins ) ); |
| 275 | } |
| 276 | |
| 277 | /** |
| 278 | * Updates information on the "View version x.x details" page with custom data. |
| 279 | * |
| 280 | * @uses api_request() |
| 281 | * |
| 282 | * @param mixed $_data |
| 283 | * @param string $_action |
| 284 | * @param object $_args |
| 285 | * @return object $_data |
| 286 | */ |
| 287 | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 288 | |
| 289 | if ( 'plugin_information' !== $_action ) { |
| 290 | |
| 291 | return $_data; |
| 292 | |
| 293 | } |
| 294 | |
| 295 | if ( ! isset( $_args->slug ) || ( $_args->slug !== $this->slug ) ) { |
| 296 | |
| 297 | return $_data; |
| 298 | |
| 299 | } |
| 300 | |
| 301 | $to_send = array( |
| 302 | 'slug' => $this->slug, |
| 303 | 'is_ssl' => is_ssl(), |
| 304 | 'fields' => array( |
| 305 | 'banners' => array(), |
| 306 | 'reviews' => false, |
| 307 | 'icons' => array(), |
| 308 | ), |
| 309 | ); |
| 310 | |
| 311 | // Get the transient where we store the api request for this plugin for 24 hours |
| 312 | $edd_api_request_transient = $this->get_cached_version_info(); |
| 313 | |
| 314 | //If we have no transient-saved value, run the API, set a fresh transient with the API value, and return that value too right now. |
| 315 | if ( empty( $edd_api_request_transient ) ) { |
| 316 | |
| 317 | $api_response = $this->api_request( 'plugin_information', $to_send ); |
| 318 | |
| 319 | // Expires in 3 hours |
| 320 | $this->set_version_info_cache( $api_response ); |
| 321 | |
| 322 | if ( false !== $api_response ) { |
| 323 | $_data = $api_response; |
| 324 | } |
| 325 | } else { |
| 326 | $_data = $edd_api_request_transient; |
| 327 | } |
| 328 | |
| 329 | // Convert sections into an associative array, since we're getting an object, but Core expects an array. |
| 330 | if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) { |
| 331 | $_data->sections = $this->convert_object_to_array( $_data->sections ); |
| 332 | } |
| 333 | |
| 334 | // Convert banners into an associative array, since we're getting an object, but Core expects an array. |
| 335 | if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) { |
| 336 | $_data->banners = $this->convert_object_to_array( $_data->banners ); |
| 337 | } |
| 338 | |
| 339 | // Convert icons into an associative array, since we're getting an object, but Core expects an array. |
| 340 | if ( isset( $_data->icons ) && ! is_array( $_data->icons ) ) { |
| 341 | $_data->icons = $this->convert_object_to_array( $_data->icons ); |
| 342 | } |
| 343 | |
| 344 | // Convert contributors into an associative array, since we're getting an object, but Core expects an array. |
| 345 | if ( isset( $_data->contributors ) && ! is_array( $_data->contributors ) ) { |
| 346 | $_data->contributors = $this->convert_object_to_array( $_data->contributors ); |
| 347 | } |
| 348 | |
| 349 | if ( ! isset( $_data->plugin ) ) { |
| 350 | $_data->plugin = $this->name; |
| 351 | } |
| 352 | |
| 353 | return $_data; |
| 354 | } |
| 355 | |
| 356 | /** |
| 357 | * Convert some objects to arrays when injecting data into the update API |
| 358 | * |
| 359 | * Some data like sections, banners, and icons are expected to be an associative array, however due to the JSON |
| 360 | * decoding, they are objects. This method allows us to pass in the object and return an associative array. |
| 361 | * |
| 362 | * @since 3.6.5 |
| 363 | * |
| 364 | * @param stdClass $data |
| 365 | * |
| 366 | * @return array |
| 367 | */ |
| 368 | private function convert_object_to_array( $data ) { |
| 369 | if ( ! is_array( $data ) && ! is_object( $data ) ) { |
| 370 | return array(); |
| 371 | } |
| 372 | $new_data = array(); |
| 373 | foreach ( $data as $key => $value ) { |
| 374 | $new_data[ $key ] = is_object( $value ) ? $this->convert_object_to_array( $value ) : $value; |
| 375 | } |
| 376 | |
| 377 | return $new_data; |
| 378 | } |
| 379 | |
| 380 | /** |
| 381 | * Disable SSL verification in order to prevent download update failures |
| 382 | * |
| 383 | * @param array $args |
| 384 | * @param string $url |
| 385 | * @return object $array |
| 386 | */ |
| 387 | public function http_request_args( $args, $url ) { |
| 388 | |
| 389 | if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
| 390 | $args['sslverify'] = $this->verify_ssl(); |
| 391 | } |
| 392 | return $args; |
| 393 | |
| 394 | } |
| 395 | |
| 396 | /** |
| 397 | * Calls the API and, if successfull, returns the object delivered by the API. |
| 398 | * |
| 399 | * @uses get_bloginfo() |
| 400 | * @uses wp_remote_post() |
| 401 | * @uses is_wp_error() |
| 402 | * |
| 403 | * @param string $_action The requested action. |
| 404 | * @param array $_data Parameters for the API action. |
| 405 | * @return false|object|void |
| 406 | */ |
| 407 | private function api_request( $_action, $_data ) { |
| 408 | $data = array_merge( $this->api_data, $_data ); |
| 409 | |
| 410 | if ( $data['slug'] !== $this->slug ) { |
| 411 | return; |
| 412 | } |
| 413 | |
| 414 | // Don't allow a plugin to ping itself |
| 415 | if ( trailingslashit( home_url() ) === $this->api_url ) { |
| 416 | return false; |
| 417 | } |
| 418 | |
| 419 | if ( $this->request_recently_failed() ) { |
| 420 | return false; |
| 421 | } |
| 422 | |
| 423 | return $this->get_version_from_remote(); |
| 424 | } |
| 425 | |
| 426 | /** |
| 427 | * Determines if a request has recently failed. |
| 428 | * |
| 429 | * @since 1.9.1 |
| 430 | * |
| 431 | * @return bool |
| 432 | */ |
| 433 | private function request_recently_failed() { |
| 434 | $failed_request_details = get_option( $this->failed_request_cache_key ); |
| 435 | |
| 436 | // Request has never failed. |
| 437 | if ( empty( $failed_request_details ) || ! is_numeric( $failed_request_details ) ) { |
| 438 | return false; |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Request previously failed, but the timeout has expired. |
| 443 | * This means we're allowed to try again. |
| 444 | */ |
| 445 | if ( time() > $failed_request_details ) { |
| 446 | delete_option( $this->failed_request_cache_key ); |
| 447 | |
| 448 | return false; |
| 449 | } |
| 450 | |
| 451 | return true; |
| 452 | } |
| 453 | |
| 454 | /** |
| 455 | * Logs a failed HTTP request for this API URL. |
| 456 | * We set a timestamp for 1 hour from now. This prevents future API requests from being |
| 457 | * made to this domain for 1 hour. Once the timestamp is in the past, API requests |
| 458 | * will be allowed again. This way if the site is down for some reason we don't bombard |
| 459 | * it with failed API requests. |
| 460 | * |
| 461 | * @see EDD_SL_Plugin_Updater::request_recently_failed |
| 462 | * |
| 463 | * @since 1.9.1 |
| 464 | */ |
| 465 | private function log_failed_request() { |
| 466 | update_option( $this->failed_request_cache_key, strtotime( '+1 hour' ) ); |
| 467 | } |
| 468 | |
| 469 | /** |
| 470 | * If available, show the changelog for sites in a multisite install. |
| 471 | */ |
| 472 | public function show_changelog() { |
| 473 | |
| 474 | if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' !== $_REQUEST['edd_sl_action'] ) { |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | if ( empty( $_REQUEST['plugin'] ) ) { |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | if ( empty( $_REQUEST['slug'] ) || $this->slug !== $_REQUEST['slug'] ) { |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | if ( ! current_user_can( 'update_plugins' ) ) { |
| 487 | wp_die( esc_html__( 'You do not have permission to install plugin updates', 'advanced-ads' ), esc_html__( 'Error', 'advanced-ads' ), array( 'response' => 403 ) ); |
| 488 | } |
| 489 | |
| 490 | $version_info = $this->get_repo_api_data(); |
| 491 | if ( isset( $version_info->sections ) ) { |
| 492 | $sections = $this->convert_object_to_array( $version_info->sections ); |
| 493 | if ( ! empty( $sections['changelog'] ) ) { |
| 494 | echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $sections['changelog'] ) . '</div>'; |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | exit; |
| 499 | } |
| 500 | |
| 501 | /** |
| 502 | * Gets the current version information from the remote site. |
| 503 | * |
| 504 | * @return array|false |
| 505 | */ |
| 506 | private function get_version_from_remote() { |
| 507 | $api_params = array( |
| 508 | 'edd_action' => 'get_version', |
| 509 | 'license' => ! empty( $this->api_data['license'] ) ? $this->api_data['license'] : '', |
| 510 | 'item_name' => isset( $this->api_data['item_name'] ) ? $this->api_data['item_name'] : false, |
| 511 | 'item_id' => isset( $this->api_data['item_id'] ) ? $this->api_data['item_id'] : false, |
| 512 | 'version' => isset( $this->api_data['version'] ) ? $this->api_data['version'] : false, |
| 513 | 'slug' => $this->slug, |
| 514 | 'author' => $this->api_data['author'], |
| 515 | 'url' => home_url(), |
| 516 | 'beta' => $this->beta, |
| 517 | 'php_version' => phpversion(), |
| 518 | 'wp_version' => get_bloginfo( 'version' ), |
| 519 | ); |
| 520 | |
| 521 | /** |
| 522 | * Filters the parameters sent in the API request. |
| 523 | * |
| 524 | * @param array $api_params The array of data sent in the request. |
| 525 | * @param array $this->api_data The array of data set up in the class constructor. |
| 526 | * @param string $this->plugin_file The full path and filename of the file. |
| 527 | */ |
| 528 | $api_params = apply_filters( 'edd_sl_plugin_updater_api_params', $api_params, $this->api_data, $this->plugin_file ); |
| 529 | |
| 530 | $request = wp_remote_post( |
| 531 | $this->api_url, |
| 532 | array( |
| 533 | 'timeout' => 15, |
| 534 | 'sslverify' => $this->verify_ssl(), |
| 535 | 'body' => $api_params, |
| 536 | ) |
| 537 | ); |
| 538 | |
| 539 | if ( is_wp_error( $request ) || ( 200 !== wp_remote_retrieve_response_code( $request ) ) ) { |
| 540 | $this->log_failed_request(); |
| 541 | |
| 542 | return false; |
| 543 | } |
| 544 | |
| 545 | $request = json_decode( wp_remote_retrieve_body( $request ) ); |
| 546 | |
| 547 | if ( $request && isset( $request->sections ) ) { |
| 548 | $request->sections = maybe_unserialize( $request->sections ); |
| 549 | } else { |
| 550 | $request = false; |
| 551 | } |
| 552 | |
| 553 | if ( $request && isset( $request->banners ) ) { |
| 554 | $request->banners = maybe_unserialize( $request->banners ); |
| 555 | } |
| 556 | |
| 557 | if ( $request && isset( $request->icons ) ) { |
| 558 | $request->icons = maybe_unserialize( $request->icons ); |
| 559 | } |
| 560 | |
| 561 | if ( ! empty( $request->sections ) ) { |
| 562 | foreach ( $request->sections as $key => $section ) { |
| 563 | $request->$key = (array) $section; |
| 564 | } |
| 565 | } |
| 566 | |
| 567 | return $request; |
| 568 | } |
| 569 | |
| 570 | /** |
| 571 | * Get the version info from the cache, if it exists. |
| 572 | * |
| 573 | * @param string $cache_key |
| 574 | * @return object |
| 575 | */ |
| 576 | public function get_cached_version_info( $cache_key = '' ) { |
| 577 | |
| 578 | if ( empty( $cache_key ) ) { |
| 579 | $cache_key = $this->get_cache_key(); |
| 580 | } |
| 581 | |
| 582 | $cache = get_option( $cache_key ); |
| 583 | |
| 584 | // Cache is expired |
| 585 | if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { |
| 586 | return false; |
| 587 | } |
| 588 | |
| 589 | // We need to turn the icons into an array, thanks to WP Core forcing these into an object at some point. |
| 590 | $cache['value'] = json_decode( $cache['value'] ); |
| 591 | if ( ! empty( $cache['value']->icons ) ) { |
| 592 | $cache['value']->icons = (array) $cache['value']->icons; |
| 593 | } |
| 594 | |
| 595 | return $cache['value']; |
| 596 | |
| 597 | } |
| 598 | |
| 599 | /** |
| 600 | * Adds the plugin version information to the database. |
| 601 | * |
| 602 | * @param string $value |
| 603 | * @param string $cache_key |
| 604 | */ |
| 605 | public function set_version_info_cache( $value = '', $cache_key = '' ) { |
| 606 | |
| 607 | if ( empty( $cache_key ) ) { |
| 608 | $cache_key = $this->get_cache_key(); |
| 609 | } |
| 610 | |
| 611 | $data = array( |
| 612 | 'timeout' => strtotime( '+3 hours', time() ), |
| 613 | 'value' => wp_json_encode( $value ), |
| 614 | ); |
| 615 | |
| 616 | update_option( $cache_key, $data, 'no' ); |
| 617 | |
| 618 | // Delete the duplicate option |
| 619 | delete_option( 'edd_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ) ); |
| 620 | } |
| 621 | |
| 622 | /** |
| 623 | * Returns if the SSL of the store should be verified. |
| 624 | * |
| 625 | * @since 1.6.13 |
| 626 | * @return bool |
| 627 | */ |
| 628 | private function verify_ssl() { |
| 629 | return (bool) apply_filters( 'edd_sl_api_request_verify_ssl', true, $this ); |
| 630 | } |
| 631 | |
| 632 | /** |
| 633 | * Gets the unique key (option name) for a plugin. |
| 634 | * |
| 635 | * @since 1.9.0 |
| 636 | * @return string |
| 637 | */ |
| 638 | private function get_cache_key() { |
| 639 | $string = $this->slug . $this->api_data['license'] . $this->beta; |
| 640 | |
| 641 | return 'edd_sl_' . md5( serialize( $string ) ); |
| 642 | } |
| 643 | |
| 644 | } |
| 645 |