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