1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459:
<?php
class Inbound_Updater {
static $api_key;
static $domain;
static $api_url;
static $path;
static $file;
static $name;
static $slug;
static $response;
static $info;
static $current_version;
public function __construct( $_api_url, $_plugin_path , $_plugin_file , $_current_version) {
self::$api_key = Inbound_API_Wrapper::get_api_key();
self::$domain = site_url();
self::$api_url = $_api_url;
self::$path = $_plugin_path ;
self::$file = $_plugin_file ;
self::$name = plugin_basename( $_plugin_file );
self::$slug = basename( $_plugin_file, '.php' );
self::$current_version = $_current_version ;
self::load_hooks();
}
public static function load_hooks() {
add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__, 'check_update' ) );
add_filter( 'plugins_api', array( __CLASS__, 'plugins_api_filter' ), 10, 3 );
add_action( 'after_plugin_row_' . self::$name, array( __CLASS__, 'show_update_notification' ), 10, 2 );
add_action( 'wp_ajax_update-plugin', array( __CLASS__ , 'run_update' ) , 1 );
add_filter( 'http_request_args', array( __CLASS__ , 'allow_download_url' ) , 10, 1 );
}
public static function check_update( $_transient_data ) {
global $pagenow;
if( ! is_object( $_transient_data ) ) {
$_transient_data = new stdClass;
}
if( 'plugins.php' == $pagenow && is_multisite() ) {
return $_transient_data;
}
if ( empty( $_transient_data->response ) || empty( $_transient_data->response[ self::$name ] ) ) {
self::api_request();
if ( false !== self::$info && is_object( self::$info ) && isset( self::$info->new_version ) ) {
if( version_compare( self::$current_version, self::$info->new_version, '<' ) ) {
$_transient_data->response[ self::$name ] = self::$info;
}
$_transient_data->last_checked = time();
$_transient_data->checked[ self::$name ] = self::$current_version;
}
}
return $_transient_data;
}
public static function api_request() {
global $inbound_settings;
self::$response = wp_remote_get( self::$api_url , array ('timeout' => 10 ) );
if ( is_wp_error(self::$response) || empty(self::$response['body']) ) {
return;
}
self::$info = json_decode( self::$response['body'] );
if (!is_object(self::$info)) {
self::$info = false;
return false;
}
if (isset(self::$info->error) && self::$info->error) {
self::$info = false;
return;
}
self::$info->slug = self::$slug;
self::$info->plugin = self::$name;
self::$info->last_updated = '';
self::$info->sections = (array) self::$info->sections;
}
public static function show_update_notification( $file , $plugin ) {
if( ! current_user_can( 'update_plugins' ) ) {
return;
}
if ( self::$name != $file || !is_multisite() ) {
return;
}
remove_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__ , 'check_update' ), 10 );
$update_cache = get_site_transient( 'update_plugins' );
if ( ! is_object( $update_cache ) || empty( $update_cache->response ) || empty( $update_cache->response[ self::$name ] ) ) {
$cache_key = md5( 'inbound_plugin_' .sanitize_key( self::$name ) . '_version_info' );
self::$info = get_transient( $cache_key );
if( false === self::$info ) {
self::api_request();
set_transient( $cache_key, self::$info, 3600 );
}
if( ! is_object( self::$info ) ) {
return;
}
if( version_compare( self::$current_version, self::$info->new_version, '<' ) ) {
$update_cache->response[ self::$name ] = self::$info;
}
$update_cache->last_checked = time();
$update_cache->checked[ self::$name ] = self::$current_version;
set_site_transient( 'update_plugins', $update_cache );
} else {
self::$info = $update_cache->response[ self::$name ];
}
add_filter( 'pre_set_site_transient_update_plugins', array( __CLASS__ , 'check_update' ) );
if ( ! empty( $update_cache->response[ self::$name ] ) && version_compare( self::$current_version, self::$info->new_version, '<' ) ) {
$wp_list_table = _get_list_table( 'WP_Plugins_List_Table' );
echo '<tr class="plugin-update-tr"><td colspan="' . $wp_list_table->get_column_count() . '" class="plugin-update colspanchange"><div class="update-message">';
printf(
__( 'There is a new version of %1$s available. <a target="_blank" class="thickbox" href="%2$s">View version %3$s details</a> or <a href="%4$s">update now</a>.', 'inbound-pro' ),
esc_html( self::$info->name ),
esc_url( $changelog_link ),
esc_html( self::$info->new_version ),
esc_url( wp_nonce_url( self_admin_url( 'update.php?action=upgrade-plugin&plugin=' ) . self::$name, 'upgrade-plugin_' . self::$name ) )
);
echo '</div></td></tr>';
}
}
public static function plugins_api_filter( $_data, $_action = '', $_args = null ) {
if ( $_action != 'plugin_information' ) {
return $_data;
}
if ( ! isset( $_args->slug ) || ( $_args->slug != self::$slug ) ) {
return $_data;
}
self::api_request();
if ( false !== self::$info ) {
$_data = self::$info;
}
return $_data;
}
public static function run_update() {
if( empty( $_REQUEST['action'] ) || 'update-plugin' != $_REQUEST['action'] ) {
return;
}
if( self::$slug != $_REQUEST['slug']) {
return;
}
$plugins = get_site_transient( 'update_plugins' );
if( ! current_user_can( 'update_plugins' ) ) {
self::ajax_throw_fail();
}
if (!isset($plugins->response[ $_REQUEST['plugin']] )) {
self::ajax_throw_fail();
}
self::$info = $plugins->response[ $_REQUEST['plugin'] ];
self::$response = wp_remote_get(
self::$info->package ,
array(
'timeout' => 500,
'redirection' => 5,
'decompress' => false
)
);
if (!empty(self::$response['body'])) {
self::delete_plugin_folder( self::$path );
self::install_new_plugin();
self::ajax_throw_success();
} else {
self::ajax_throw_fail();
}
}
public static function ajax_throw_success() {
$message = array(
'update'=>"plugin",
'plugin'=> $_REQUEST['plugin'],
'slug' => $_REQUEST['slug'],
'oldVersion'=> 'Version ' . self::$current_version,
'newVersion' => 'Version ' . self::$info->new_version
);
return wp_send_json_success($message);
}
public static function ajax_throw_fail() {
$message = array(
'update'=>"plugin",
'plugin'=> $_REQUEST['plugin'],
'slug' => $_REQUEST['slug'],
'oldVersion'=> 'Version ' . self::$current_version,
'newVersion' => 'Version ' . self::$info->new_version
);
return wp_send_json_error(json_encode($message));
}
public static function delete_plugin_folder($dirPath) {
if (is_dir($dirPath)) {
$objects = scandir($dirPath);
foreach ($objects as $object) {
if ($object != "." && $object !="..") {
if (filetype($dirPath . DIRECTORY_SEPARATOR . $object) == "dir") {
self::delete_plugin_folder($dirPath . DIRECTORY_SEPARATOR . $object);
} else {
unlink($dirPath . DIRECTORY_SEPARATOR . $object);
}
}
}
reset($objects);
@rmdir($dirPath);
}
}
public static function install_new_plugin() {
include_once( ABSPATH . '/wp-admin/includes/class-pclzip.php');
$temp_file = tempnam('/tmp', 'TEMPPLUGIN' );
$handle = fopen($temp_file, "w");
fwrite($handle, self::$response['body']);
fclose($handle);
$archive = new PclZip($temp_file);
if ( $_REQUEST['slug'] == 'inbound-pro' ) {
if (strstr(self::$info->package , 'inbound-pro.zip' )) {
$result = $archive->extract( PCLZIP_OPT_REMOVE_PATH, 'inbound-pro' , PCLZIP_OPT_PATH, self::$path , PCLZIP_OPT_REPLACE_NEWER );
} else {
$result = $archive->extract( PCLZIP_OPT_REMOVE_PATH, '_inbound-now' , PCLZIP_OPT_PATH, self::$path , PCLZIP_OPT_REPLACE_NEWER );
}
} else {
$result = $archive->extract( PCLZIP_OPT_PATH, self::$path , PCLZIP_OPT_REPLACE_NEWER );
}
if ($result == 0) {
die("Error : ".$archive->errorInfo(true));
}
unlink($temp_file);
}
public static function allow_download_url( $args ) {
$args['reject_unsafe_urls'] = false;
return $args;
}
}
class Inbound_Pro_Automatic_Updates {
static $api_key;
static $domain;
static $api_url;
static $response;
public function __construct() {
self::load_hooks();
}
public static function load_hooks() {
add_action( 'admin_init' , array( __CLASS__ , 'load_static_vars' ) , 1 );
add_action( 'admin_init' , array( __CLASS__ , 'setup_uploader' ) );
}
public static function load_static_vars() {
self::$api_key = Inbound_API_Wrapper::get_api_key();
self::$domain = site_url();
self::$api_url = add_query_arg( array( 'api-key' => self::$api_key , 'site' => self::$domain ), Inbound_API_Wrapper::get_pro_info_endpoint() );
}
public static function setup_uploader() {
new Inbound_Updater( self::$api_url , INBOUND_PRO_PATH , INBOUND_PRO_FILE , INBOUND_PRO_CURRENT_VERSION );
}
}
new Inbound_Pro_Automatic_Updates;