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