| 1 | <?php |
| 2 | |
| 3 | class WP_Background_Upgrader { |
| 4 | |
| 5 | static $skin; |
| 6 | |
| 7 | static function init() { |
| 8 | |
| 9 | // Detect updates for Core, Plugins, and, Themes |
| 10 | add_action( 'set_site_transient_' . 'update_core', array( 'WP_Background_Upgrader', 'queue_updates_check_core' ) ); |
| 11 | add_action( 'set_site_transient_' . 'update_plugins', array( 'WP_Background_Upgrader', 'queue_updates_check_plugins_themes' ) ); |
| 12 | add_action( 'set_site_transient_' . 'update_themes', array( 'WP_Background_Upgrader', 'queue_updates_check_plugins_themes' ) ); |
| 13 | // Uh.. Yeah, work around for #25213 |
| 14 | add_action( 'set_site_transient_' . '_site_transient_' . 'update_core', array( 'WP_Background_Upgrader', 'queue_updates_check_core' ) ); |
| 15 | add_action( 'set_site_transient_' . '_site_transient_' . 'update_plugins', array( 'WP_Background_Upgrader', 'queue_updates_check_plugins_themes' ) ); |
| 16 | add_action( 'set_site_transient_' . '_site_transient_' . 'update_themes', array( 'WP_Background_Upgrader', 'queue_updates_check_plugins_themes' ) ); |
| 17 | |
| 18 | // Cron Updates |
| 19 | add_action( 'wp_background_upgrader_process', array( 'WP_Background_Upgrader', 'perform_queued_updates' ) ); |
| 20 | |
| 21 | // Temporary way of testing to see if the install is fataling |
| 22 | // Used with WP_Background_Upgrader::test_if_site_is_ok() |
| 23 | add_action( 'template_redirect', array( 'WP_Background_Upgrader','test_if_site_is_ok_callback' ) , 1 ); |
| 24 | |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Loads the WordPress Admin environment for performing the updates. |
| 29 | * Cron runs as a front-end process |
| 30 | */ |
| 31 | static function load_required_admin_includes() { |
| 32 | error_reporting( E_ALL ); |
| 33 | // include ABSPATH . 'wp-admin/includes/admin.php'; // Load the whole lot of 'em |
| 34 | include ABSPATH . 'wp-admin/includes/update.php'; |
| 35 | include ABSPATH . 'wp-admin/includes/plugin.php'; |
| 36 | include ABSPATH . 'wp-admin/includes/plugin-install.php'; |
| 37 | include ABSPATH . 'wp-admin/includes/theme.php'; |
| 38 | include ABSPATH . 'wp-admin/includes/theme-install.php'; |
| 39 | include ABSPATH . 'wp-admin/includes/file.php'; |
| 40 | include ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; |
| 41 | } |
| 42 | |
| 43 | static function queue_updates_check_core( $updates ) { |
| 44 | |
| 45 | if ( self::upgrader_disabled() ) |
| 46 | return; |
| 47 | |
| 48 | if ( ! self::find_core_auto_update() ) |
| 49 | return; |
| 50 | |
| 51 | if ( ! wp_next_scheduled( 'wp_background_upgrader_process' ) ) { |
| 52 | // If the transient update was triggered by a user pageview, update in an hours time, else, now. |
| 53 | // What if the user is active in an hours time and maintainence mode kicks in? |
| 54 | // Schedule it for $user_id_1_timezone midnight? |
| 55 | $when_to_update = get_current_user_id() ? time() + HOUR_IN_SECONDS : time(); |
| 56 | $when_to_update = apply_filters( 'auto_upgrade_when_to_upgrade', $when_to_update ); |
| 57 | |
| 58 | wp_schedule_single_event( $when_to_update, 'wp_background_upgrader_process' ); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | static function queue_updates_check_plugins_themes( $transient_value ) { |
| 63 | |
| 64 | if ( self::upgrader_disabled() ) |
| 65 | return; |
| 66 | |
| 67 | if ( ! $transient_value || empty( $transient_value->response ) ) |
| 68 | return; |
| 69 | |
| 70 | if ( ! wp_next_scheduled( 'wp_auto_upgrader_upgrade_all_the_things' ) ) { |
| 71 | // If the transient update was triggered by a user pageview, update in an hours time, else, now. |
| 72 | $when_to_update = get_current_user_id() ? time() + HOUR_IN_SECONDS : time(); |
| 73 | $when_to_update = apply_filters( 'auto_upgrade_when_to_upgrade', $when_to_update ); |
| 74 | |
| 75 | wp_schedule_single_event( $when_to_update, 'wp_auto_upgrader_upgrade_all_the_things' ); |
| 76 | } |
| 77 | |
| 78 | } |
| 79 | |
| 80 | static function perform_queued_updates() { |
| 81 | |
| 82 | $lock_name = 'auto_upgrader.lock'; |
| 83 | if ( get_site_transient( $lock_name ) ) { |
| 84 | // Test to see if it was set more than an hour ago, if so, cleanup. |
| 85 | if ( true || get_site_transient( $lock_name ) < ( time() - HOUR_IN_SECONDS ) ) |
| 86 | delete_site_transient( $lock_name ); |
| 87 | else // Recent lock |
| 88 | return; |
| 89 | } |
| 90 | // Lock upgrades for us for half an hour |
| 91 | if ( ! set_site_transient( $lock_name, microtime( true ), HOUR_IN_SECONDS / 2 ) ) |
| 92 | return; |
| 93 | |
| 94 | // Admin-ize ourselves |
| 95 | self::load_required_admin_includes(); |
| 96 | |
| 97 | // Next, Plugins |
| 98 | wp_update_plugins(); // Check for Plugin updates |
| 99 | $plugin_updates = get_site_transient( 'update_plugins' ); |
| 100 | if ( $plugin_updates && !empty( $plugin_updates->response ) ) { |
| 101 | foreach ( array_keys( $plugin_updates->response ) as $plugin ) { |
| 102 | self::upgrade( 'plugin', $plugin ); |
| 103 | } |
| 104 | // Force refresh of plugin update information |
| 105 | delete_site_transient('update_plugins'); |
| 106 | wp_cache_delete( 'plugins', 'plugins' ); |
| 107 | } |
| 108 | |
| 109 | // Next, those themes we all love |
| 110 | wp_update_themes(); // Check for Theme updates |
| 111 | $theme_updates = get_site_transient( 'update_themes' ); |
| 112 | if ( $theme_updates && !empty( $theme_updates->response ) ) { |
| 113 | foreach ( array_keys( $theme_updates->response ) as $theme ) { |
| 114 | self::upgrade( 'theme', $theme ); |
| 115 | } |
| 116 | // Force refresh of theme update information |
| 117 | wp_clean_themes_cache(); |
| 118 | } |
| 119 | |
| 120 | // Up first, Core. |
| 121 | wp_version_check(); // Check for Core updates |
| 122 | $core_update = self::find_core_auto_update(); |
| 123 | if ( $core_update ) |
| 124 | self::upgrade( 'core', $core_update ); |
| 125 | |
| 126 | // Cleanup, These won't trigger any updates this time due to the locking transient |
| 127 | wp_version_check(); // check for Core updates |
| 128 | wp_update_themes(); // Check for Theme updates |
| 129 | wp_update_plugins(); // Check for Plugin updates |
| 130 | |
| 131 | delete_site_transient( $lock_name ); |
| 132 | |
| 133 | } |
| 134 | |
| 135 | /** |
| 136 | * Finds the best auto-update available for this site. |
| 137 | * If there's 1.2.3 and 1.3 on offer, it'll choose 1.3 if the install allows it, else, 1.2.3 |
| 138 | */ |
| 139 | static function find_core_auto_update() { |
| 140 | $updates = get_site_transient( 'update_core' ); |
| 141 | $auto_update = false; |
| 142 | foreach ( $updates->updates as $update ) { |
| 143 | if ( 'autoupdate' != $update->response ) |
| 144 | continue; |
| 145 | |
| 146 | if ( ! self::should_upgrade( 'core', $update, ABSPATH ) ) |
| 147 | continue; |
| 148 | |
| 149 | if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) ) |
| 150 | $auto_update = $update; |
| 151 | } |
| 152 | return $auto_update; |
| 153 | } |
| 154 | |
| 155 | static function upgrader_disabled() { |
| 156 | // That's a no if you don't want files changes |
| 157 | if ( defined( 'DISABLE_FILE_MODS' ) && DISABLE_FILE_MODS ) |
| 158 | return true; |
| 159 | |
| 160 | if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED ) |
| 161 | return true; |
| 162 | |
| 163 | return apply_filters( 'auto_upgrader_disabled', false ); |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * Tests to see if we should upgrade a specific item, does not test to see if we CAN update the item. |
| 168 | */ |
| 169 | static function should_upgrade( $type, $item, $context ) { |
| 170 | |
| 171 | if ( self::upgrader_disabled() ) |
| 172 | return false; |
| 173 | |
| 174 | // ..and also check for GIT/SVN checkouts |
| 175 | if ( ! apply_filters( 'auto_upgrade_ignore_checkout_status', false ) ) { |
| 176 | $stop_dirs = array( |
| 177 | ABSPATH, |
| 178 | untrailingslashit( $context ), |
| 179 | ); |
| 180 | if ( ! file_exists( ABSPATH . '/wp-config.php' ) ) // wp-config.php up one folder in a deployment situation |
| 181 | $stop_dirs[] = dirname( ABSPATH ); |
| 182 | foreach ( array_unique( $stop_dirs ) as $dir ) { |
| 183 | if ( file_exists( $dir . '/.svn' ) || file_exists( $dir . '/.git' ) ) |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Next up, do we actually have it enabled for this type of update? |
| 189 | switch ( $type ) { |
| 190 | default: $upgrade = false; break; |
| 191 | case 'core': |
| 192 | $upgrade = self::should_upgrade_to_core_version( $item->current ); |
| 193 | break; |
| 194 | case 'plugin': $upgrade = false; break; |
| 195 | case 'theme': $upgrade = false; break; |
| 196 | case 'lang': $upgrade = false; break; |
| 197 | } |
| 198 | |
| 199 | // And does the user / plugins want it? |
| 200 | if ( ! apply_filters( 'auto_upgrade_' . $type, $upgrade, $item ) ) |
| 201 | return false; |
| 202 | |
| 203 | // If it's a core update, are we actually compatible with it? |
| 204 | if ( 'core' == $type ) { |
| 205 | global $wpdb; |
| 206 | |
| 207 | $php_compat = version_compare( phpversion(), $item->php_version, '>=' ); |
| 208 | if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) ) |
| 209 | $mysql_compat = true; |
| 210 | else |
| 211 | $mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' ); |
| 212 | |
| 213 | if ( ! $php_compat || ! $mysql_compat ) |
| 214 | return false; |
| 215 | } |
| 216 | |
| 217 | // Make sure loop-back HTTP requests work, we need to be able to verify everything worked OK and to be able to upgrade the database |
| 218 | // If this doesn't pass, chances are they don't want auto-updates anyway |
| 219 | if ( ! self::test_if_site_is_ok() ) |
| 220 | return false; |
| 221 | |
| 222 | return true; |
| 223 | } |
| 224 | |
| 225 | // Checks to see if WP_Filesystem is setup to allow unattended upgrades |
| 226 | static function can_upgrade( $context ) { |
| 227 | if ( ! self::$skin ) |
| 228 | self::$skin = new WP_Background_Upgrader_Skin(); |
| 229 | return (bool) self::$skin->request_filesystem_credentials(); |
| 230 | } |
| 231 | |
| 232 | // Determines if this WordPress Core version should update to $offered_ver or not |
| 233 | static function should_upgrade_to_core_version( $offered_ver /* x.y.z */ ) { |
| 234 | include ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z |
| 235 | |
| 236 | $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y |
| 237 | $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y |
| 238 | $current_is_development_version = (bool) strpos( $wp_version, '-' ); |
| 239 | |
| 240 | // Defaults: |
| 241 | $upgrade_dev = false; |
| 242 | $upgrade_minor = true; |
| 243 | $upgrade_major = false; |
| 244 | |
| 245 | // WP_AUTO_UPDATE_CORE = true (all), 'minor', false. |
| 246 | if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) { |
| 247 | if ( false === WP_AUTO_UPDATE_CORE ) { |
| 248 | // Defaults to turned off, unless a filter allows it |
| 249 | $upgrade_dev = $upgrade_minor = $upgrade_major = false; |
| 250 | } elseif ( true === WP_AUTO_UPDATE_CORE ) { |
| 251 | // ALL updates for core |
| 252 | $upgrade_dev = $upgrade_minor = $upgrade_major = true; |
| 253 | } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) { |
| 254 | // Only minor updates for core |
| 255 | $upgrade_dev = $upgrade_major = false; |
| 256 | $upgrade_minor = true; |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // 1: If we're already on that version, not much point in updating? |
| 261 | if ( $offered_ver == $wp_version ) |
| 262 | return false; |
| 263 | |
| 264 | // 2: If we're running a newer version, that's a nope |
| 265 | if ( version_compare( $wp_version, $offered_ver, '>=' ) ) |
| 266 | return false; |
| 267 | |
| 268 | // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2 |
| 269 | if ( $current_is_development_version ) { |
| 270 | if ( ! apply_filters( 'allow_dev_background_core_updates', $upgrade_dev ) ) |
| 271 | return false; |
| 272 | // else fall through to minor + major branches below |
| 273 | } |
| 274 | |
| 275 | // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4) |
| 276 | if ( $current_branch == $new_branch ) |
| 277 | return apply_filters( 'allow_minor_background_core_updates', $upgrade_minor ); |
| 278 | |
| 279 | // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1) |
| 280 | if ( version_compare( $new_branch, $current_branch, '>' ) ) |
| 281 | return apply_filters( 'allow_major_background_core_updates', $upgrade_major ); |
| 282 | |
| 283 | // If we're not sure, we don't want it |
| 284 | return false; |
| 285 | } |
| 286 | |
| 287 | static function upgrade( $type, $item ) { |
| 288 | |
| 289 | wp_mail( |
| 290 | get_site_option( 'admin_email' ), |
| 291 | __METHOD__, |
| 292 | "Starting an upgrade for:\n\n" . var_export( compact( 'type', 'item' ), true ) . "\n\n" . wp_debug_backtrace_summary() |
| 293 | ); |
| 294 | |
| 295 | self::$skin = new Background_Upgrader_Skin(); |
| 296 | |
| 297 | switch( $type ) { |
| 298 | case 'core': |
| 299 | // Okay, Why does the Core upgrader not use the Upgrader's skin during the actual main part of the upgrade??? |
| 300 | add_filter( 'update_feedback', function( $message ) { |
| 301 | WP_Background_Upgrader::$skin->feedback( $message ); |
| 302 | } ); |
| 303 | $upgrader = new Core_Upgrader( self::$skin ); |
| 304 | $context = ABSPATH; |
| 305 | break; |
| 306 | case 'plugin': |
| 307 | $upgrader = new Plugin_Upgrader( self::$skin ); |
| 308 | $context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR |
| 309 | break; |
| 310 | case 'theme': |
| 311 | $upgrader = new Theme_Upgrader( self::$skin ); |
| 312 | $context = get_theme_root( $item ); |
| 313 | break; |
| 314 | case 'lang': |
| 315 | return false; // Not quite yet! |
| 316 | // $upgrader = new Language_Upgrader( self::$skin ); |
| 317 | $context = WP_LANG_DIR; |
| 318 | break; |
| 319 | } |
| 320 | |
| 321 | // Determine if we can perform this upgrade or not |
| 322 | if ( ! self::should_upgrade( $type, $item, $context ) || ! self::can_upgrade( $context ) ) |
| 323 | return false; |
| 324 | |
| 325 | // Boom, This sites about to get a whole new splash of paint! |
| 326 | $upgrade_result = $upgrader->upgrade( $item, array( |
| 327 | 'clear_update_cache' => false, |
| 328 | ) ); |
| 329 | |
| 330 | // Core doesn't output this, so lets append it so we don't get confused |
| 331 | if ( 'core' == $type ) { |
| 332 | if ( is_wp_error( $upgrade_result ) ) { |
| 333 | self::$skin->error( __( 'Installation Failed' ), $upgrade_result ); |
| 334 | } else { |
| 335 | self::$skin->feedback( __( 'WordPress updated successfully' ) ); |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | var_dump( compact( 'type', 'item', 'upgrader', 'upgrade_result' ) ); |
| 340 | |
| 341 | wp_mail( |
| 342 | get_site_option( 'admin_email' ), |
| 343 | __METHOD__, |
| 344 | var_export( array( |
| 345 | $upgrade_result, |
| 346 | $upgrader, |
| 347 | self::$skin, |
| 348 | ), true ) |
| 349 | ); |
| 350 | |
| 351 | return $upgrade_result; |
| 352 | } |
| 353 | |
| 354 | static function test_if_site_is_ok() { |
| 355 | // Is the Admin ajax handler returning what we expect of it? |
| 356 | $http_result = wp_remote_get( admin_url( 'admin-ajax.php' ), array( 'timeout' => 10 ) ); |
| 357 | $admin_ok = ! is_wp_error( $http_result ) && ( '0' === wp_remote_retrieve_body( $http_result ) ); |
| 358 | |
| 359 | // Is the front-end returning what we'd expect of it? (Note, this is a filter on template_redirect + wp_footer outside of this class below) |
| 360 | // This WILL break with caching plugins, Include a something that can be used to prevent the caching somehow? a GET parameter isn't enough for some caching plugins |
| 361 | $http_result = wp_remote_get( site_url('?test-upgrade-OK=1'), array( 'timeout' => 10 ) ); |
| 362 | $front_ok = ! is_wp_error( $http_result ) && ( 'OK' === wp_remote_retrieve_body( $http_result ) ); |
| 363 | |
| 364 | return $admin_ok && $front_ok; |
| 365 | } |
| 366 | |
| 367 | static function test_if_site_is_ok_callback() { |
| 368 | if ( ! isset( $_GET['test-upgrade-OK'] ) ) |
| 369 | return; |
| 370 | |
| 371 | ob_start(); |
| 372 | add_action( 'wp_footer', function() { |
| 373 | ob_end_clean(); |
| 374 | die( 'OK' ); |
| 375 | }, 999 ); |
| 376 | } |
| 377 | |
| 378 | // A static lifestyle is best. Go on, try it, I dare you. |
| 379 | private function __construct() {} |
| 380 | } |
| 381 | WP_Background_Upgrader::init(); |