| 1: | <?php |
| 2: | |
| 3: | |
| 4: | |
| 5: | |
| 6: | |
| 7: | |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | |
| 15: | |
| 16: | |
| 17: | |
| 18: | if ( ! defined( 'ABSPATH' ) ) { |
| 19: | exit; |
| 20: | } |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | |
| 27: | |
| 28: | |
| 29: | class EPL_SL_Plugin_Updater { |
| 30: | |
| 31: | |
| 32: | |
| 33: | |
| 34: | |
| 35: | private $api_url = ''; |
| 36: | |
| 37: | |
| 38: | |
| 39: | |
| 40: | |
| 41: | |
| 42: | private $api_data = array(); |
| 43: | |
| 44: | |
| 45: | |
| 46: | |
| 47: | |
| 48: | |
| 49: | private $name = ''; |
| 50: | |
| 51: | |
| 52: | |
| 53: | |
| 54: | |
| 55: | |
| 56: | private $slug = ''; |
| 57: | |
| 58: | |
| 59: | |
| 60: | |
| 61: | |
| 62: | |
| 63: | private $version = ''; |
| 64: | |
| 65: | |
| 66: | |
| 67: | |
| 68: | |
| 69: | |
| 70: | |
| 71: | private $wp_override = false; |
| 72: | |
| 73: | |
| 74: | |
| 75: | |
| 76: | |
| 77: | |
| 78: | private $cache_key = ''; |
| 79: | |
| 80: | |
| 81: | |
| 82: | |
| 83: | |
| 84: | |
| 85: | private $beta = false; |
| 86: | |
| 87: | |
| 88: | |
| 89: | |
| 90: | |
| 91: | |
| 92: | |
| 93: | |
| 94: | |
| 95: | |
| 96: | |
| 97: | public function __construct( $_api_url, $_plugin_file, $_api_data = null ) { |
| 98: | |
| 99: | global $epl_plugin_data; |
| 100: | |
| 101: | $this->api_url = trailingslashit( $_api_url ); |
| 102: | $this->api_data = $_api_data; |
| 103: | $this->name = plugin_basename( $_plugin_file ); |
| 104: | $this->slug = basename( $_plugin_file, '.php' ); |
| 105: | $this->version = $_api_data['version']; |
| 106: | $this->wp_override = isset( $_api_data['wp_override'] ) ? (bool) $_api_data['wp_override'] : false; |
| 107: | $this->beta = ! empty( $this->api_data['beta'] ) ? true : false; |
| 108: | $this->cache_key = md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ); |
| 109: | |
| 110: | $epl_plugin_data[ $this->slug ] = $this->api_data; |
| 111: | |
| 112: | |
| 113: | $this->init(); |
| 114: | } |
| 115: | |
| 116: | |
| 117: | |
| 118: | |
| 119: | |
| 120: | |
| 121: | |
| 122: | |
| 123: | public function init() { |
| 124: | |
| 125: | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 ); |
| 126: | add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
| 127: | remove_action( 'after_plugin_row_' . $this->name, 'wp_plugin_update_row', 10 ); |
| 128: | add_action( 'after_plugin_row_' . $this->name, array( $this, 'show_update_notification' ), 10, 2 ); |
| 129: | add_action( 'admin_init', array( $this, 'show_changelog' ) ); |
| 130: | } |
| 131: | |
| 132: | |
| 133: | |
| 134: | |
| 135: | |
| 136: | |
| 137: | |
| 138: | |
| 139: | |
| 140: | |
| 141: | |
| 142: | |
| 143: | |
| 144: | |
| 145: | public function check_update( $_transient_data ) { |
| 146: | |
| 147: | global $pagenow; |
| 148: | |
| 149: | if ( ! is_object( $_transient_data ) ) { |
| 150: | $_transient_data = new stdClass(); |
| 151: | } |
| 152: | |
| 153: | if ( 'plugins.php' === $pagenow && is_multisite() ) { |
| 154: | return $_transient_data; |
| 155: | } |
| 156: | |
| 157: | if ( ! empty( $_transient_data->response ) && ! empty( $_transient_data->response[ $this->name ] ) && false === $this->wp_override ) { |
| 158: | return $_transient_data; |
| 159: | } |
| 160: | |
| 161: | $version_info = $this->get_cached_version_info(); |
| 162: | |
| 163: | if ( false === $version_info ) { |
| 164: | $version_info = $this->api_request( |
| 165: | 'plugin_latest_version', |
| 166: | array( |
| 167: | 'slug' => $this->slug, |
| 168: | 'beta' => $this->beta, |
| 169: | ) |
| 170: | ); |
| 171: | |
| 172: | $this->set_version_info_cache( $version_info ); |
| 173: | } |
| 174: | |
| 175: | if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) { |
| 176: | |
| 177: | if ( version_compare( $this->version, $version_info->new_version, '<' ) ) { |
| 178: | |
| 179: | $_transient_data->response[ $this->name ] = $version_info; |
| 180: | |
| 181: | } |
| 182: | $_transient_data->last_checked = time(); |
| 183: | $_transient_data->checked[ $this->name ] = $this->version; |
| 184: | |
| 185: | } |
| 186: | |
| 187: | return $_transient_data; |
| 188: | } |
| 189: | |
| 190: | |
| 191: | |
| 192: | |
| 193: | |
| 194: | |
| 195: | |
| 196: | public function show_update_notification( $file, $plugin ) { |
| 197: | |
| 198: | if ( is_network_admin() ) { |
| 199: | return; |
| 200: | } |
| 201: | |
| 202: | if ( ! current_user_can( 'update_plugins' ) ) { |
| 203: | return; |
| 204: | } |
| 205: | |
| 206: | if ( ! is_multisite() ) { |
| 207: | return; |
| 208: | } |
| 209: | |
| 210: | if ( $this->name !== $file ) { |
| 211: | return; |
| 212: | } |
| 213: | |
| 214: | |
| 215: | remove_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ), 10 ); |
| 216: | |
| 217: | $update_cache = get_site_transient( 'update_plugins' ); |
| 218: | |
| 219: | $update_cache = is_object( $update_cache ) ? $update_cache : new stdClass(); |
| 220: | |
| 221: | if ( empty( $update_cache->response ) || empty( $update_cache->response[ $this->name ] ) ) { |
| 222: | |
| 223: | $version_info = $this->get_cached_version_info(); |
| 224: | |
| 225: | if ( false === $version_info ) { |
| 226: | |
| 227: | $version_info = $this->api_request( |
| 228: | 'plugin_latest_version', |
| 229: | array( |
| 230: | 'slug' => $this->slug, |
| 231: | 'beta' => $this->beta, |
| 232: | ) |
| 233: | ); |
| 234: | |
| 235: | $this->set_version_info_cache( $version_info ); |
| 236: | } |
| 237: | |
| 238: | if ( ! is_object( $version_info ) ) { |
| 239: | return; |
| 240: | } |
| 241: | |
| 242: | if ( version_compare( $this->version, $version_info->new_version, '<' ) ) { |
| 243: | |
| 244: | $update_cache->response[ $this->name ] = $version_info; |
| 245: | } |
| 246: | |
| 247: | $update_cache->last_checked = time(); |
| 248: | $update_cache->checked[ $this->name ] = $this->version; |
| 249: | |
| 250: | set_site_transient( 'update_plugins', $update_cache ); |
| 251: | |
| 252: | } else { |
| 253: | |
| 254: | $version_info = $update_cache->response[ $this->name ]; |
| 255: | |
| 256: | } |
| 257: | |
| 258: | |
| 259: | add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_update' ) ); |
| 260: | |
| 261: | if ( ! empty( $update_cache->response[ $this->name ] ) && version_compare( $this->version, $version_info->new_version, '<' ) ) { |
| 262: | |
| 263: | |
| 264: | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
| 265: | echo '<tr class="plugin-update-tr" id="' . esc_attr( $this->slug ) . '-update" data-slug="' . esc_attr( $this->slug ) . '" data-plugin="' . esc_attr( $this->slug ) . '/' . esc_url( $file ) . '">'; |
| 266: | echo '<td colspan="3" class="plugin-update colspanchange">'; |
| 267: | echo '<div class="update-message notice inline notice-warning notice-alt">'; |
| 268: | |
| 269: | $changelog_link = self_admin_url( 'index.php?edd_sl_action=view_plugin_changelog&plugin=' . $this->name . '&slug=' . $this->slug . '&TB_iframe=true&width=772&height=911' ); |
| 270: | |
| 271: | if ( empty( $version_info->download_link ) ) { |
| 272: | |
| 273: | printf( |
| 274: | wp_kses_post( __( 'There is a new version of %1$s available. %2$s View version %3$s details %4$s.', 'easy-property-listings' ) ), |
| 275: | esc_html( $version_info->name ), |
| 276: | '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">', |
| 277: | esc_html( $version_info->new_version ), |
| 278: | '</a>' |
| 279: | ); |
| 280: | } else { |
| 281: | |
| 282: | printf( |
| 283: | wp_kses_post( __( 'There is a new version of %1$s available. %2$s View version %3$s details %4$s or %5$s update now %6$s.', 'easy-property-listings' ) ), |
| 284: | esc_html( $version_info->name ), |
| 285: | '<a target="_blank" class="thickbox" href="' . esc_url( $changelog_link ) . '">', |
| 286: | esc_html( $version_info->new_version ), |
| 287: | '</a>', |
| 288: | '<a href="' . esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . $this->name, 'upgrade-plugin_' . $this->name ) ) . '">', |
| 289: | '</a>' |
| 290: | ); |
| 291: | } |
| 292: | |
| 293: | do_action( "in_plugin_update_message-{$file}", $plugin, $version_info ); |
| 294: | |
| 295: | echo '</div></td></tr>'; |
| 296: | } |
| 297: | } |
| 298: | |
| 299: | |
| 300: | |
| 301: | |
| 302: | |
| 303: | |
| 304: | |
| 305: | |
| 306: | |
| 307: | |
| 308: | |
| 309: | |
| 310: | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 311: | |
| 312: | if ( 'plugin_information' !== $_action ) { |
| 313: | |
| 314: | return $_data; |
| 315: | |
| 316: | } |
| 317: | |
| 318: | if ( ! isset( $_args->slug ) || ( $_args->slug !== $this->slug ) ) { |
| 319: | |
| 320: | return $_data; |
| 321: | |
| 322: | } |
| 323: | |
| 324: | $to_send = array( |
| 325: | 'slug' => $this->slug, |
| 326: | 'is_ssl' => is_ssl(), |
| 327: | 'fields' => array( |
| 328: | 'banners' => array(), |
| 329: | 'reviews' => false, |
| 330: | ), |
| 331: | ); |
| 332: | |
| 333: | $cache_key = 'epl_api_request_' . md5( serialize( $this->slug . $this->api_data['license'] . $this->beta ) ); |
| 334: | |
| 335: | |
| 336: | $epl_api_request_transient = $this->get_cached_version_info( $cache_key ); |
| 337: | |
| 338: | |
| 339: | if ( empty( $epl_api_request_transient ) ) { |
| 340: | |
| 341: | $api_response = $this->api_request( 'plugin_information', $to_send ); |
| 342: | |
| 343: | $this->set_version_info_cache( $api_response, $cache_key ); |
| 344: | if ( false !== $api_response ) { |
| 345: | $_data = $api_response; |
| 346: | } |
| 347: | } else { |
| 348: | |
| 349: | $_data = $epl_api_request_transient; |
| 350: | } |
| 351: | |
| 352: | |
| 353: | if ( isset( $_data->sections ) && ! is_array( $_data->sections ) ) { |
| 354: | |
| 355: | $new_sections = array(); |
| 356: | foreach ( $_data->sections as $key => $value ) { |
| 357: | $new_sections[ $key ] = $value; |
| 358: | } |
| 359: | $_data->sections = $new_sections; |
| 360: | } |
| 361: | |
| 362: | |
| 363: | if ( isset( $_data->banners ) && ! is_array( $_data->banners ) ) { |
| 364: | |
| 365: | $new_banners = array(); |
| 366: | foreach ( $_data->banners as $key => $value ) { |
| 367: | $new_banners[ $key ] = $value; |
| 368: | } |
| 369: | $_data->banners = $new_banners; |
| 370: | } |
| 371: | |
| 372: | return $_data; |
| 373: | } |
| 374: | |
| 375: | |
| 376: | |
| 377: | |
| 378: | |
| 379: | |
| 380: | |
| 381: | |
| 382: | |
| 383: | |
| 384: | public function http_request_args( $args, $url ) { |
| 385: | |
| 386: | if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
| 387: | $args['sslverify'] = false; |
| 388: | } |
| 389: | return $args; |
| 390: | } |
| 391: | |
| 392: | |
| 393: | |
| 394: | |
| 395: | |
| 396: | |
| 397: | |
| 398: | |
| 399: | |
| 400: | |
| 401: | |
| 402: | |
| 403: | private function api_request( $_action, $_data ) { |
| 404: | |
| 405: | global $wp_version; |
| 406: | |
| 407: | $data = array_merge( $this->api_data, $_data ); |
| 408: | |
| 409: | if ( $data['slug'] !== $this->slug ) { |
| 410: | return; |
| 411: | } |
| 412: | |
| 413: | if ( trailingslashit( home_url() ) === $this->api_url ) { |
| 414: | return false; |
| 415: | } |
| 416: | |
| 417: | $api_params = array( |
| 418: | 'edd_action' => 'get_version', |
| 419: | 'license' => ! empty( $data['license'] ) ? $data['license'] : '', |
| 420: | 'item_name' => isset( $data['item_name'] ) ? $data['item_name'] : false, |
| 421: | 'item_id' => isset( $data['item_id'] ) ? $data['item_id'] : false, |
| 422: | 'version' => isset( $data['version'] ) ? $data['version'] : false, |
| 423: | 'slug' => $data['slug'], |
| 424: | 'author' => $data['author'], |
| 425: | 'url' => home_url(), |
| 426: | 'beta' => ! empty( $data['beta'] ), |
| 427: | ); |
| 428: | |
| 429: | $verify_ssl = $this->verify_ssl(); |
| 430: | |
| 431: | $request = wp_remote_post( |
| 432: | $this->api_url, |
| 433: | array( |
| 434: | 'timeout' => 15, |
| 435: | 'sslverify' => $verify_ssl, |
| 436: | 'body' => $api_params, |
| 437: | ) |
| 438: | ); |
| 439: | |
| 440: | if ( ! is_wp_error( $request ) ) { |
| 441: | $request = json_decode( wp_remote_retrieve_body( $request ) ); |
| 442: | } |
| 443: | |
| 444: | if ( $request && isset( $request->sections ) ) { |
| 445: | $request->sections = maybe_unserialize( $request->sections ); |
| 446: | } else { |
| 447: | $request = false; |
| 448: | } |
| 449: | |
| 450: | if ( $request && isset( $request->banners ) ) { |
| 451: | |
| 452: | $request->banners = maybe_unserialize( $request->banners ); |
| 453: | } |
| 454: | |
| 455: | if ( ! empty( $request->sections ) ) { |
| 456: | foreach ( $request->sections as $key => $section ) { |
| 457: | $request->$key = (array) $section; |
| 458: | } |
| 459: | } |
| 460: | |
| 461: | return $request; |
| 462: | } |
| 463: | |
| 464: | |
| 465: | |
| 466: | |
| 467: | public function show_changelog() { |
| 468: | |
| 469: | global $epl_plugin_data; |
| 470: | |
| 471: | if ( empty( $_REQUEST['edd_sl_action'] ) || 'view_plugin_changelog' !== $_REQUEST['edd_sl_action'] ) { |
| 472: | return; |
| 473: | } |
| 474: | |
| 475: | if ( empty( $_REQUEST['plugin'] ) ) { |
| 476: | return; |
| 477: | } |
| 478: | |
| 479: | if ( empty( $_REQUEST['slug'] ) ) { |
| 480: | return; |
| 481: | } |
| 482: | |
| 483: | if ( ! current_user_can( 'update_plugins' ) ) { |
| 484: | wp_die( esc_html__( 'You do not have permission to install plugin updates', 'easy-property-listings' ), esc_html__( 'Error', 'easy-property-listings' ), array( 'response' => 403 ) ); |
| 485: | } |
| 486: | |
| 487: | $data = $epl_plugin_data[ sanitize_text_field( wp_unslash( $_REQUEST['slug'] ) ) ]; |
| 488: | $beta = ! empty( $data['beta'] ) ? true : false; |
| 489: | $cache_key = md5( 'epl_plugin_' . sanitize_key( $_REQUEST['plugin'] ) . '_' . $beta . '_version_info' ); |
| 490: | $version_info = $this->get_cached_version_info( $cache_key ); |
| 491: | |
| 492: | if ( false === $version_info ) { |
| 493: | |
| 494: | $api_params = array( |
| 495: | 'edd_action' => 'get_version', |
| 496: | 'item_name' => isset( $data['item_name'] ) ? sanitize_text_field( $data['item_name'] ) : false, |
| 497: | 'item_id' => isset( $data['item_id'] ) ? sanitize_text_field( $data['item_id'] ) : false, |
| 498: | 'slug' => sanitize_text_field( wp_unslash( $_REQUEST['slug'] ) ), |
| 499: | 'author' => sanitize_text_field( $data['author'] ), |
| 500: | 'url' => home_url(), |
| 501: | 'beta' => ! empty( $data['beta'] ), |
| 502: | ); |
| 503: | |
| 504: | $verify_ssl = $this->verify_ssl(); |
| 505: | |
| 506: | $request = wp_remote_post( |
| 507: | $this->api_url, |
| 508: | array( |
| 509: | 'timeout' => 15, |
| 510: | 'sslverify' => $verify_ssl, |
| 511: | 'body' => $api_params, |
| 512: | ) |
| 513: | ); |
| 514: | |
| 515: | if ( ! is_wp_error( $request ) ) { |
| 516: | $version_info = json_decode( wp_remote_retrieve_body( $request ) ); |
| 517: | } |
| 518: | |
| 519: | if ( ! empty( $version_info ) && isset( $version_info->sections ) ) { |
| 520: | $version_info->sections = maybe_unserialize( $version_info->sections ); |
| 521: | } else { |
| 522: | $version_info = false; |
| 523: | } |
| 524: | |
| 525: | if ( ! empty( $version_info ) ) { |
| 526: | foreach ( $version_info->sections as $key => $section ) { |
| 527: | $version_info->$key = (array) $section; |
| 528: | } |
| 529: | } |
| 530: | |
| 531: | $this->set_version_info_cache( $version_info, $cache_key ); |
| 532: | |
| 533: | } |
| 534: | |
| 535: | if ( ! empty( $version_info ) && isset( $version_info->sections['changelog'] ) ) { |
| 536: | echo '<div style="background:#fff;padding:10px;">' . wp_kses_post( $version_info->sections['changelog'] ) . '</div>'; |
| 537: | } |
| 538: | |
| 539: | exit; |
| 540: | } |
| 541: | |
| 542: | |
| 543: | |
| 544: | |
| 545: | |
| 546: | |
| 547: | |
| 548: | |
| 549: | public function get_cached_version_info( $cache_key = '' ) { |
| 550: | |
| 551: | if ( empty( $cache_key ) ) { |
| 552: | $cache_key = $this->cache_key; |
| 553: | } |
| 554: | $cache = get_option( $cache_key ); |
| 555: | if ( empty( $cache['timeout'] ) || time() > $cache['timeout'] ) { |
| 556: | return false; |
| 557: | } |
| 558: | return json_decode( $cache['value'] ); |
| 559: | } |
| 560: | |
| 561: | |
| 562: | |
| 563: | |
| 564: | |
| 565: | |
| 566: | |
| 567: | public function set_version_info_cache( $value = '', $cache_key = '' ) { |
| 568: | |
| 569: | if ( empty( $cache_key ) ) { |
| 570: | $cache_key = $this->cache_key; |
| 571: | } |
| 572: | $data = array( |
| 573: | 'timeout' => strtotime( '+3 hours', time() ), |
| 574: | 'value' => wp_json_encode( $value ), |
| 575: | ); |
| 576: | update_option( $cache_key, $data, 'no' ); |
| 577: | } |
| 578: | |
| 579: | |
| 580: | |
| 581: | |
| 582: | |
| 583: | |
| 584: | |
| 585: | private function verify_ssl() { |
| 586: | return (bool) apply_filters( 'epl_sl_api_request_verify_ssl', true, $this ); |
| 587: | } |
| 588: | } |
| 589: | |