class-auxels-plugin-check-update.php
5 years ago
class-auxels-system-check.php
5 years ago
class-auxin-cli-commands.php
5 years ago
class-auxin-license-activation.php
6 years ago
class-auxin-notices.php
6 years ago
class-auxin-upgrader-ajax-handlers.php
7 years ago
class-auxin-upgrader-http-api.php
5 years ago
class-auxin-upgrader-plugin.php
5 years ago
class-auxin-upgrader-prepare.php
7 years ago
class-auxin-upgrader-theme.php
7 years ago
class-auxin-upgrader-prepare.php
462 lines
| 1 | <?php |
| 2 | |
| 3 | class Auxin_Upgrader_Prepare { |
| 4 | |
| 5 | /** |
| 6 | * Instance of this class. |
| 7 | * |
| 8 | * @var object |
| 9 | */ |
| 10 | protected static $instance = null; |
| 11 | protected $upgrade_types = array( |
| 12 | 'plugins' => 'update_plugins', |
| 13 | 'themes' => 'update_themes' |
| 14 | ); |
| 15 | protected $api = 'http://api.averta.net/envato/items/'; |
| 16 | |
| 17 | function __construct(){ |
| 18 | add_filter( 'site_transient_update_plugins', array( $this, 'disable_update_plugins' ) ); |
| 19 | add_filter( 'site_transient_update_themes', array( $this, 'disable_update_themes' ) ); |
| 20 | |
| 21 | add_action( 'load-plugins.php', array( $this, 'update_plugins' ) ); |
| 22 | add_action( 'load-update.php', array( $this, 'update_plugins' ) ); |
| 23 | add_action( 'load-update-core.php', array( $this, 'update_plugins' ) ); |
| 24 | add_action( 'wp_update_plugins', array( $this, 'update_plugins' ) ); |
| 25 | |
| 26 | add_action( 'load-themes.php', array( $this, 'update_themes' ) ); |
| 27 | add_action( 'load-update.php', array( $this, 'update_themes' ) ); |
| 28 | add_action( 'load-update-core.php', array( $this, 'update_themes' ) ); |
| 29 | add_action( 'wp_update_themes', array( $this, 'update_themes' ) ); |
| 30 | |
| 31 | add_action( 'admin_init', array( $this, 'maybe_update_list' ) ); |
| 32 | } |
| 33 | |
| 34 | /** |
| 35 | * Remove auxin plugins from wp auto update |
| 36 | * |
| 37 | * @return object |
| 38 | */ |
| 39 | public function disable_update_plugins( $transient ) { |
| 40 | // Pass plugins list with their slug e.g. array( 'auxin-elements' ) |
| 41 | $plugins = apply_filters( 'auxin_disable_plugins_updates', array() ); |
| 42 | if ( isset($transient) && is_object($transient) && ! empty( $plugins ) ) { |
| 43 | foreach ( $plugins as $key => $plugin ) { |
| 44 | $pluginPath = $plugin . '/' . $plugin . '.php'; |
| 45 | if ( isset( $transient->response[$pluginPath] ) ) { |
| 46 | unset( $transient->response[$pluginPath] ); |
| 47 | } |
| 48 | } |
| 49 | } |
| 50 | return $transient; |
| 51 | } |
| 52 | |
| 53 | /** |
| 54 | * Remove auxin themes from wp auto update |
| 55 | * |
| 56 | * @return object |
| 57 | */ |
| 58 | public function disable_update_themes( $transient ) { |
| 59 | // Pass themes list with their slug e.g. array( 'phlox' ) |
| 60 | $themes = apply_filters( 'auxin_disable_themes_updates', array() ); |
| 61 | if ( isset($transient) && is_object($transient) && ! empty( $themes ) ) { |
| 62 | foreach ( $themes as $theme ) { |
| 63 | if ( isset( $transient->response[$theme] ) ) { |
| 64 | unset( $transient->response[$theme] ); |
| 65 | } |
| 66 | } |
| 67 | } |
| 68 | return $transient; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Check theme versions against the latest versions hosted on WordPress.org. & Averta API |
| 73 | * |
| 74 | * @return void |
| 75 | */ |
| 76 | public function update_themes(){ |
| 77 | if ( wp_installing() ) { |
| 78 | return; |
| 79 | } |
| 80 | |
| 81 | $get_themes = $this->get_themes(); |
| 82 | |
| 83 | $last_update = auxin_get_transient( 'auxin_update_themes' ); |
| 84 | if ( ! is_object($last_update) ) { |
| 85 | $last_update = new stdClass; |
| 86 | } |
| 87 | |
| 88 | $new_option = new stdClass; |
| 89 | $new_option->last_checked = time(); |
| 90 | |
| 91 | $doing_cron = wp_doing_cron(); |
| 92 | |
| 93 | // Check for update on a different schedule, depending on the page. |
| 94 | switch ( current_filter() ) { |
| 95 | case 'upgrader_process_complete' : |
| 96 | $timeout = 0; |
| 97 | break; |
| 98 | case 'load-update-core.php' : |
| 99 | $timeout = MINUTE_IN_SECONDS; |
| 100 | break; |
| 101 | case 'load-themes.php' : |
| 102 | case 'load-update.php' : |
| 103 | $timeout = HOUR_IN_SECONDS; |
| 104 | break; |
| 105 | default : |
| 106 | if ( $doing_cron ) { |
| 107 | $timeout = 2 * HOUR_IN_SECONDS; |
| 108 | } else { |
| 109 | $timeout = 12 * HOUR_IN_SECONDS; |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | $time_not_changed = isset( $last_update->last_checked ) && $timeout > ( time() - $last_update->last_checked ); |
| 114 | |
| 115 | if ( $time_not_changed ) { |
| 116 | $theme_changed = false; |
| 117 | foreach ( $get_themes as $slug => $data ) { |
| 118 | $new_option->checked[ $slug ] = $data->get( 'Version' ); |
| 119 | |
| 120 | if ( !isset( $last_update->checked[ $slug ] ) || strval($last_update->checked[ $slug ]) !== strval($data->get( 'Version' )) ){ |
| 121 | $theme_changed = true; |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | if ( isset ( $last_update->response ) && is_array( $last_update->response ) ) { |
| 126 | foreach ( $last_update->response as $slug => $update_details ) { |
| 127 | if ( ! isset($checked[ $slug ]) ) { |
| 128 | $theme_changed = true; |
| 129 | break; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Bail if we've checked recently and if nothing has changed |
| 135 | if ( ! $theme_changed ) { |
| 136 | return; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | // Update last_checked for current to prevent multiple blocking requests if request hangs |
| 141 | $last_update->last_checked = time(); |
| 142 | auxin_set_transient( 'auxin_update_themes', $last_update ); |
| 143 | |
| 144 | // Include plugins install |
| 145 | if ( ! function_exists( 'themes_api' ) ){ |
| 146 | require_once ABSPATH . 'wp-admin/includes/theme.php'; |
| 147 | } |
| 148 | |
| 149 | foreach ( $get_themes as $slug => $data ) { |
| 150 | |
| 151 | if( $data->isOfficial ) { |
| 152 | $response = themes_api( 'theme_information', array( |
| 153 | 'slug' => sanitize_key( $slug ), |
| 154 | 'fields' => array( |
| 155 | 'sections' => false, |
| 156 | ), |
| 157 | ) ); |
| 158 | if ( ! is_wp_error( $response ) ) { |
| 159 | if( version_compare( $response->version, $data->get( 'Version' ), '>' ) ){ |
| 160 | $new_option->response[ $data->get_stylesheet() ] = array( |
| 161 | 'slug' => esc_sql($slug), |
| 162 | 'version' => esc_sql($response->version), |
| 163 | 'package' => esc_url($response->download_link) |
| 164 | ); |
| 165 | } |
| 166 | } |
| 167 | } else { |
| 168 | // Get version number of our api |
| 169 | $new_version = $this->remote_get( array( |
| 170 | 'cat' => 'version-check', |
| 171 | 'action' => 'final', |
| 172 | 'item-name' => sanitize_key( $slug ) |
| 173 | ) ); |
| 174 | |
| 175 | if( ! empty( $new_version ) && version_compare( $new_version, $data->get( 'Version' ), '>' ) ){ |
| 176 | $new_option->response[ $data->get_stylesheet() ] = array( |
| 177 | 'slug' => esc_sql($slug), |
| 178 | 'version' => esc_sql($new_version), |
| 179 | 'package' => 'AUXIN_GET_DOWNLOAD_URL' |
| 180 | ); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | } |
| 185 | |
| 186 | auxin_set_transient( 'auxin_update_themes', $new_option ); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Check plugin versions against the latest versions hosted on WordPress.org. & Averta API |
| 191 | * |
| 192 | * @return void |
| 193 | */ |
| 194 | public function update_plugins(){ |
| 195 | if ( wp_installing() ) { |
| 196 | return; |
| 197 | } |
| 198 | |
| 199 | $get_plugins = $this->get_plugins(); |
| 200 | |
| 201 | $current = auxin_get_transient( 'auxin_update_plugins' ); |
| 202 | if ( ! is_object($current) ) { |
| 203 | $current = new stdClass; |
| 204 | } |
| 205 | |
| 206 | $new_option = new stdClass; |
| 207 | $new_option->last_checked = time(); |
| 208 | |
| 209 | $doing_cron = wp_doing_cron(); |
| 210 | // Check for update on a different schedule, depending on the page. |
| 211 | switch ( current_filter() ) { |
| 212 | case 'upgrader_process_complete' : |
| 213 | $timeout = 0; |
| 214 | break; |
| 215 | case 'load-update-core.php' : |
| 216 | $timeout = MINUTE_IN_SECONDS; |
| 217 | break; |
| 218 | case 'load-plugins.php' : |
| 219 | case 'load-update.php' : |
| 220 | $timeout = HOUR_IN_SECONDS; |
| 221 | break; |
| 222 | default : |
| 223 | if ( $doing_cron ) { |
| 224 | $timeout = 2 * HOUR_IN_SECONDS; |
| 225 | } else { |
| 226 | $timeout = 12 * HOUR_IN_SECONDS; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | $time_not_changed = isset( $current->last_checked ) && $timeout > ( time() - $current->last_checked ); |
| 231 | if ( $time_not_changed ) { |
| 232 | $plugin_changed = false; |
| 233 | foreach ( $get_plugins as $path => $data ) { |
| 234 | $new_option->checked[ $path ] = $data['Version']; |
| 235 | |
| 236 | if ( !isset( $current->checked[ $path ] ) || strval($current->checked[ $path ]) !== strval($data['Version']) ){ |
| 237 | $plugin_changed = true; |
| 238 | } |
| 239 | } |
| 240 | |
| 241 | if ( isset ( $current->response ) && is_array( $current->response) ) { |
| 242 | foreach ( $current->response as $plugin_file => $update_details ) { |
| 243 | if ( ! isset($get_plugins[ $plugin_file ]) ) { |
| 244 | $plugin_changed = true; |
| 245 | break; |
| 246 | } |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | // Bail if we've checked recently and if nothing has changed |
| 251 | if ( ! $plugin_changed ) { |
| 252 | return; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | // Update last_checked for current to prevent multiple blocking requests if request hangs |
| 257 | $current->last_checked = time(); |
| 258 | auxin_set_transient( 'auxin_update_plugins', $current ); |
| 259 | |
| 260 | // Include plugins install |
| 261 | if ( ! function_exists( 'plugins_api' ) ){ |
| 262 | include_once( ABSPATH . 'wp-admin/includes/plugin-install.php' ); |
| 263 | } |
| 264 | |
| 265 | foreach ( $get_plugins as $path => $data ) { |
| 266 | $slug = dirname( $path ); |
| 267 | |
| 268 | if( $data['isOfficial'] ) { |
| 269 | $response = plugins_api( 'plugin_information', array( |
| 270 | 'slug' => sanitize_key( $slug ), |
| 271 | 'fields' => array( |
| 272 | 'sections' => false, |
| 273 | ), |
| 274 | ) ); |
| 275 | if ( ! is_wp_error( $response ) ) { |
| 276 | if( version_compare( $response->version, $data['Version'], '>' ) ){ |
| 277 | $new_option->response[ $path ] = array( |
| 278 | 'slug' => esc_sql($slug), |
| 279 | 'version' => esc_sql($response->version), |
| 280 | 'package' => esc_url($response->download_link) |
| 281 | ); |
| 282 | } |
| 283 | } |
| 284 | } else { |
| 285 | // Get version number of our api |
| 286 | $new_version = $this->remote_get( array( |
| 287 | 'cat' => 'version-check', |
| 288 | 'action' => 'final', |
| 289 | 'item-name' => sanitize_key( $slug ) |
| 290 | ) ); |
| 291 | |
| 292 | if( ! empty( $new_version ) && version_compare( $new_version, $data['Version'], '>' ) ){ |
| 293 | $new_option->response[ $path ] = array( |
| 294 | 'slug' => esc_sql($slug), |
| 295 | 'version' => esc_sql($new_version), |
| 296 | 'package' => 'AUXIN_GET_DOWNLOAD_URL' |
| 297 | ); |
| 298 | } |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | auxin_set_transient( 'auxin_update_plugins', $new_option ); |
| 303 | } |
| 304 | |
| 305 | /** |
| 306 | * Get averta group plugins list |
| 307 | * |
| 308 | * @return array |
| 309 | */ |
| 310 | public function get_plugins(){ |
| 311 | // If running blog-side, bail unless we've not checked in the last 12 hours |
| 312 | if ( ! function_exists( 'get_plugins' ) ){ |
| 313 | require_once( ABSPATH . 'wp-admin/includes/plugin.php' ); |
| 314 | } |
| 315 | $plugins_list = get_plugins(); |
| 316 | $regex_pattern = apply_filters( 'auxin_averta_plugins_regex', '(auxin|phlox)' ); |
| 317 | foreach ( $plugins_list as $path => $args ) { |
| 318 | if( ! preg_match( $regex_pattern, $path ) ) { |
| 319 | unset( $plugins_list[ $path ] ); |
| 320 | continue; |
| 321 | } |
| 322 | if( $this->is_official( 'plugins', $path ) ){ |
| 323 | $plugins_list[ $path ]['isOfficial'] = true; |
| 324 | } else { |
| 325 | if( defined('THEME_PRO' ) && THEME_PRO && auxin_is_activated() ) { |
| 326 | $plugins_list[ $path ]['isOfficial'] = false; |
| 327 | } else { |
| 328 | unset( $plugins_list[ $path ] ); |
| 329 | continue; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | return apply_filters( 'auxin_get_plugins_list', $plugins_list ); |
| 334 | } |
| 335 | |
| 336 | /** |
| 337 | * Get averta group themes list |
| 338 | * |
| 339 | * @return array |
| 340 | */ |
| 341 | public function get_themes(){ |
| 342 | $themes_list = wp_get_themes(); |
| 343 | $regex_pattern = apply_filters( 'auxin_averta_themes_regex', '(auxin|phlox)' ); |
| 344 | foreach ( $themes_list as $path => $args ) { |
| 345 | if( ! preg_match( $regex_pattern, $path ) ) { |
| 346 | unset( $themes_list[ $path ] ); |
| 347 | continue; |
| 348 | } |
| 349 | if( $this->is_official( 'themes', $path ) ){ |
| 350 | $themes_list[ $path ]->isOfficial = true; |
| 351 | } else { |
| 352 | if( defined('THEME_PRO' ) && THEME_PRO && auxin_is_activated() ) { |
| 353 | $themes_list[ $path ]->isOfficial = false; |
| 354 | } else { |
| 355 | unset( $themes_list[ $path ] ); |
| 356 | continue; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | return apply_filters( 'auxin_get_themes_list', $themes_list ); |
| 361 | } |
| 362 | |
| 363 | /** |
| 364 | * Check official plugins |
| 365 | * |
| 366 | * @param string $type |
| 367 | * @param string $slug |
| 368 | * @return boolean |
| 369 | */ |
| 370 | private function is_official( $type, $slug ){ |
| 371 | switch ( $type ) { |
| 372 | case 'themes': |
| 373 | $official = apply_filters( 'auxin_official_themes', array() ); |
| 374 | if ( in_array ( $slug, $official ) ) { |
| 375 | return true; |
| 376 | } |
| 377 | break; |
| 378 | |
| 379 | case 'plugins': |
| 380 | $official = apply_filters( 'auxin_official_plugins', array() ); |
| 381 | // Convert path to slug |
| 382 | if( strpos( $slug, '.php' ) ) { |
| 383 | $slug = dirname( $slug ); |
| 384 | } |
| 385 | if( in_array ( $slug, $official ) ) { |
| 386 | return true; |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | default: |
| 391 | return false; |
| 392 | break; |
| 393 | } |
| 394 | |
| 395 | return false; |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * General remote get function |
| 400 | * |
| 401 | * @param array $request_args |
| 402 | * @return void |
| 403 | */ |
| 404 | public function remote_get( $request_args ){ |
| 405 | $url = add_query_arg( |
| 406 | $request_args, |
| 407 | $this->api |
| 408 | ); |
| 409 | |
| 410 | $request = wp_remote_get( $url ); |
| 411 | |
| 412 | if ( is_wp_error( $request ) ) { |
| 413 | return false; |
| 414 | } |
| 415 | |
| 416 | return wp_remote_retrieve_body( $request ); |
| 417 | } |
| 418 | |
| 419 | /** |
| 420 | * Check the last time upgrades were run before checking plugin versions. |
| 421 | * |
| 422 | * @return void |
| 423 | */ |
| 424 | public function maybe_update_list() { |
| 425 | |
| 426 | foreach ( $this->upgrade_types as $key => $type ) { |
| 427 | // Set transient key name |
| 428 | $transient_key = 'auxin_' . $type; |
| 429 | // This will remove update data transient |
| 430 | if( isset( $_GET['force-check'] ) && $_GET['force-check'] == 1 ){ |
| 431 | auxin_delete_transient( $transient_key ); |
| 432 | } else { |
| 433 | // Otherwise check for last time upgrades |
| 434 | $current = auxin_get_transient( $transient_key ); |
| 435 | if ( isset( $current->last_checked ) && 12 * HOUR_IN_SECONDS > ( time() - $current->last_checked ) ){ |
| 436 | continue; |
| 437 | } |
| 438 | } |
| 439 | // Any update? So run the upgrade callbacks |
| 440 | $this->$type(); |
| 441 | } |
| 442 | |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | /** |
| 447 | * Return an instance of this class. |
| 448 | * |
| 449 | * @return object A single instance of this class. |
| 450 | */ |
| 451 | public static function get_instance() { |
| 452 | |
| 453 | // If the single instance hasn't been set, set it now. |
| 454 | if ( null == self::$instance ) { |
| 455 | self::$instance = new self; |
| 456 | } |
| 457 | |
| 458 | return self::$instance; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | } |