balmet.com

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

Updater.php (6022B)


      1 <?php
      2 namespace ReduxAppsero;
      3 
      4 /**
      5  * Appsero Updater
      6  *
      7  * This class will show new updates project
      8  */
      9 class Updater {
     10 
     11 	/**
     12 	 * Appsero\Client
     13 	 *
     14 	 * @var object
     15 	 */
     16 	protected $client;
     17 
     18 	/**
     19 	 * Initialize the class
     20 	 *
     21 	 * @param Appsero\Client
     22 	 */
     23 	public function __construct( Client $client ) {
     24 
     25 		$this->client    = $client;
     26 		$this->cache_key = 'appsero_' . md5( $this->client->slug ) . '_version_info';
     27 
     28 		// Run hooks.
     29 		if ( $this->client->type == 'plugin' ) {
     30 			$this->run_plugin_hooks();
     31 		} elseif ( $this->client->type == 'theme' ) {
     32 			$this->run_theme_hooks();
     33 		}
     34 	}
     35 
     36 	/**
     37 	 * Set up WordPress filter to hooks to get update.
     38 	 *
     39 	 * @return void
     40 	 */
     41 	public function run_plugin_hooks() {
     42 		add_filter( 'pre_set_site_transient_update_plugins', array( $this, 'check_plugin_update' ) );
     43 		add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
     44 	}
     45 
     46 	/**
     47 	 * Set up WordPress filter to hooks to get update.
     48 	 *
     49 	 * @return void
     50 	 */
     51 	public function run_theme_hooks() {
     52 		add_filter( 'pre_set_site_transient_update_themes', array( $this, 'check_theme_update' ) );
     53 	}
     54 
     55 	/**
     56 	 * Check for Update for this specific project
     57 	 */
     58 	public function check_plugin_update( $transient_data ) {
     59 		global $pagenow;
     60 
     61 		if ( ! is_object( $transient_data ) ) {
     62 			$transient_data = new \stdClass();
     63 		}
     64 
     65 		if ( 'plugins.php' == $pagenow && is_multisite() ) {
     66 			return $transient_data;
     67 		}
     68 
     69 		if ( ! empty( $transient_data->response ) && ! empty( $transient_data->response[ $this->client->basename ] ) ) {
     70 			return $transient_data;
     71 		}
     72 
     73 		$version_info = $this->get_cached_version_info();
     74 
     75 		if ( false === $version_info ) {
     76 			$version_info = $this->get_project_latest_version();
     77 			$this->set_cached_version_info( $version_info );
     78 		}
     79 
     80 		if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
     81 
     82 			if ( version_compare( $this->client->project_version, $version_info->new_version, '<' ) ) {
     83 				unset( $version_info->sections );
     84 				$transient_data->response[ $this->client->basename ] = $version_info;
     85 			}
     86 
     87 			$transient_data->last_checked                       = time();
     88 			$transient_data->checked[ $this->client->basename ] = $this->client->project_version;
     89 		}
     90 
     91 		return $transient_data;
     92 	}
     93 
     94 	/**
     95 	 * Get version info from database
     96 	 *
     97 	 * @return Object or Boolean
     98 	 */
     99 	private function get_cached_version_info() {
    100 
    101 		$value = get_transient( $this->cache_key );
    102 
    103 		if ( ! $value && ! isset( $value->name ) ) {
    104 			return false; // Cache is expired
    105 		}
    106 
    107 		// We need to turn the icons into an array
    108 		if ( isset( $value->icons ) ) {
    109 			$value->icons = (array) $value->icons;
    110 		}
    111 
    112 		// We need to turn the banners into an array
    113 		if ( isset( $value->banners ) ) {
    114 			$value->banners = (array) $value->banners;
    115 		}
    116 
    117 		if ( isset( $value->sections ) ) {
    118 			$value->sections = (array) $value->sections;
    119 		}
    120 
    121 		return $value;
    122 	}
    123 
    124 	/**
    125 	 * Set version info to database
    126 	 */
    127 	private function set_cached_version_info( $value ) {
    128 		if ( ! $value ) {
    129 			return;
    130 		}
    131 
    132 		set_transient( $this->cache_key, $value, 3 * HOUR_IN_SECONDS );
    133 	}
    134 
    135 	/**
    136 	 * Get plugin info from Appsero
    137 	 */
    138 	private function get_project_latest_version() {
    139 
    140 		$license_option_key = 'appsero_' . md5( $this->client->slug ) . '_manage_license';
    141 		$license            = get_option( $license_option_key, null );
    142 
    143 		$params = array(
    144 			'version'     => $this->client->project_version,
    145 			'name'        => $this->client->name,
    146 			'slug'        => $this->client->slug,
    147 			'basename'    => $this->client->basename,
    148 			'license_key' => ! empty( $license ) && isset( $license['key'] ) ? $license['key'] : '',
    149 		);
    150 
    151 		$route = 'update/' . $this->client->hash . '/check';
    152 
    153 		$response = $this->client->send_request( $params, $route, true );
    154 
    155 		if ( is_wp_error( $response ) ) {
    156 			return false;
    157 		}
    158 
    159 		$response = json_decode( wp_remote_retrieve_body( $response ) );
    160 
    161 		if ( ! isset( $response->slug ) ) {
    162 			return false;
    163 		}
    164 
    165 		if ( isset( $response->icons ) ) {
    166 			$response->icons = (array) $response->icons;
    167 		}
    168 
    169 		if ( isset( $response->banners ) ) {
    170 			$response->banners = (array) $response->banners;
    171 		}
    172 
    173 		if ( isset( $response->sections ) ) {
    174 			$response->sections = (array) $response->sections;
    175 		}
    176 
    177 		return $response;
    178 	}
    179 
    180 	/**
    181 	 * Updates information on the "View version x.x details" page with custom data.
    182 	 *
    183 	 * @param mixed  $data
    184 	 * @param string $action
    185 	 * @param object $args
    186 	 *
    187 	 * @return object $data
    188 	 */
    189 	public function plugins_api_filter( $data, $action = '', $args = null ) {
    190 
    191 		if ( $action != 'plugin_information' ) {
    192 			return $data;
    193 		}
    194 
    195 		if ( ! isset( $args->slug ) || ( $args->slug != $this->client->slug ) ) {
    196 			return $data;
    197 		}
    198 
    199 		$version_info = $this->get_cached_version_info();
    200 
    201 		if ( false === $version_info ) {
    202 			$version_info = $this->get_project_latest_version();
    203 			$this->set_cached_version_info( $version_info );
    204 		}
    205 
    206 		return $version_info;
    207 	}
    208 
    209 	/**
    210 	 * Check theme upate
    211 	 */
    212 	public function check_theme_update( $transient_data ) {
    213 		global $pagenow;
    214 
    215 		if ( ! is_object( $transient_data ) ) {
    216 			$transient_data = new \stdClass();
    217 		}
    218 
    219 		if ( 'themes.php' == $pagenow && is_multisite() ) {
    220 			return $transient_data;
    221 		}
    222 
    223 		if ( ! empty( $transient_data->response ) && ! empty( $transient_data->response[ $this->client->slug ] ) ) {
    224 			return $transient_data;
    225 		}
    226 
    227 		$version_info = $this->get_cached_version_info();
    228 
    229 		if ( false === $version_info ) {
    230 			$version_info = $this->get_project_latest_version();
    231 			$this->set_cached_version_info( $version_info );
    232 		}
    233 
    234 		if ( false !== $version_info && is_object( $version_info ) && isset( $version_info->new_version ) ) {
    235 
    236 			if ( version_compare( $this->client->project_version, $version_info->new_version, '<' ) ) {
    237 				$transient_data->response[ $this->client->slug ] = (array) $version_info;
    238 			}
    239 
    240 			$transient_data->last_checked                   = time();
    241 			$transient_data->checked[ $this->client->slug ] = $this->client->project_version;
    242 		}
    243 
    244 		return $transient_data;
    245 	}
    246 
    247 }