Make WP_Automatic_Upgrader a proper object that gets instantiated. Renames nearly all of its methods.

Also renames wp_auto_updates_maybe_update() to wp_maybe_auto_update().

see #22704.

Built from https://develop.svn.wordpress.org/trunk@25823


git-svn-id: http://core.svn.wordpress.org/trunk@25735 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Andrew Nacin 2013-10-17 00:55:09 +00:00
parent 640f73ecd2
commit 191efaef61
4 changed files with 53 additions and 61 deletions

View File

@ -1551,33 +1551,32 @@ class File_Upload_Upgrader {
} }
/** /**
* WordPress Automatic Upgrader helper class * WordPress automatic background upgrader.
* *
* @since 3.7.0 * @since 3.7.0
*/ */
class WP_Automatic_Upgrader { class WP_Automatic_Upgrader {
static $upgrade_results = array(); protected $update_results = array();
static function upgrader_disabled() { function is_disabled() {
// That's a no if you don't want files changes // Background updates are disabled if you don't want file changes.
if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS ) if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
return true; return true;
// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED )
return true;
if ( defined( 'WP_INSTALLING' ) ) if ( defined( 'WP_INSTALLING' ) )
return true; return true;
return apply_filters( 'auto_upgrader_disabled', false ); // More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
$disabled = defined( 'AUTOMATIC_UPDATES_DISABLED' ) && AUTOMATIC_UPDATES_DISABLED;
return apply_filters( 'auto_upgrader_disabled', $disabled );
} }
/** /**
* Check for GIT/SVN checkouts. * Check for GIT/SVN checkouts.
*/ */
static function is_vcs_checkout( $context ) { function is_vcs_checkout( $context ) {
$context_dirs = array( untrailingslashit( $context ) ); $context_dirs = array( untrailingslashit( $context ) );
if ( $context !== ABSPATH ) if ( $context !== ABSPATH )
$context_dirs[] = untrailingslashit( ABSPATH ); $context_dirs[] = untrailingslashit( ABSPATH );
@ -1607,17 +1606,16 @@ class WP_Automatic_Upgrader {
/** /**
* Tests to see if we can and should upgrade a specific item. * Tests to see if we can and should upgrade a specific item.
*/ */
static function should_auto_update( $type, $item, $context ) { function should_upgrade( $type, $item, $context ) {
if ( $this->is_disabled() )
return false;
// Checks to see if WP_Filesystem is set up to allow unattended upgrades. // Checks to see if WP_Filesystem is set up to allow unattended upgrades.
$skin = new Automatic_Upgrader_Skin; $skin = new Automatic_Upgrader_Skin;
if ( ! $skin->request_filesystem_credentials( false, $context ) ) if ( ! $skin->request_filesystem_credentials( false, $context ) )
return false; return false;
if ( self::upgrader_disabled() ) if ( $this->is_vcs_checkout( $context ) )
return false;
if ( self::is_vcs_checkout( $context ) )
return false; return false;
// Next up, do we actually have it enabled for this type of update? // Next up, do we actually have it enabled for this type of update?
@ -1657,7 +1655,7 @@ class WP_Automatic_Upgrader {
return true; return true;
} }
static function upgrade( $type, $item ) { function upgrade( $type, $item ) {
$skin = new Automatic_Upgrader_Skin(); $skin = new Automatic_Upgrader_Skin();
@ -1683,7 +1681,7 @@ class WP_Automatic_Upgrader {
} }
// Determine whether we can and should perform this upgrade. // Determine whether we can and should perform this upgrade.
if ( ! self::should_auto_update( $type, $item, $context ) ) if ( ! $this->should_upgrade( $type, $item, $context ) )
return false; return false;
switch ( $type ) { switch ( $type ) {
@ -1724,7 +1722,7 @@ class WP_Automatic_Upgrader {
} }
} }
self::$upgrade_results[ $type ][] = (object) array( $this->update_results[ $type ][] = (object) array(
'item' => $item, 'item' => $item,
'result' => $upgrade_result, 'result' => $upgrade_result,
'name' => $item_name, 'name' => $item_name,
@ -1737,8 +1735,7 @@ class WP_Automatic_Upgrader {
/** /**
* Kicks off a upgrade request for each item in the upgrade "queue" * Kicks off a upgrade request for each item in the upgrade "queue"
*/ */
static function perform_auto_updates() { function run() {
$lock_name = 'auto_upgrader.lock'; $lock_name = 'auto_upgrader.lock';
if ( get_site_option( $lock_name ) ) { if ( get_site_option( $lock_name ) ) {
// Test to see if it was set more than an hour ago, if so, cleanup. // Test to see if it was set more than an hour ago, if so, cleanup.
@ -1762,7 +1759,7 @@ class WP_Automatic_Upgrader {
$plugin_updates = get_site_transient( 'update_plugins' ); $plugin_updates = get_site_transient( 'update_plugins' );
if ( $plugin_updates && !empty( $plugin_updates->response ) ) { if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
foreach ( array_keys( $plugin_updates->response ) as $plugin ) { foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
self::upgrade( 'plugin', $plugin ); $this->upgrade( 'plugin', $plugin );
} }
// Force refresh of plugin update information // Force refresh of plugin update information
wp_clean_plugins_cache(); wp_clean_plugins_cache();
@ -1773,7 +1770,7 @@ class WP_Automatic_Upgrader {
$theme_updates = get_site_transient( 'update_themes' ); $theme_updates = get_site_transient( 'update_themes' );
if ( $theme_updates && !empty( $theme_updates->response ) ) { if ( $theme_updates && !empty( $theme_updates->response ) ) {
foreach ( array_keys( $theme_updates->response ) as $theme ) { foreach ( array_keys( $theme_updates->response ) as $theme ) {
self::upgrade( 'theme', $theme ); $this->upgrade( 'theme', $theme );
} }
// Force refresh of theme update information // Force refresh of theme update information
wp_clean_themes_cache(); wp_clean_themes_cache();
@ -1783,12 +1780,16 @@ class WP_Automatic_Upgrader {
wp_version_check(); // Check for Core updates wp_version_check(); // Check for Core updates
$extra_update_stats = array(); $extra_update_stats = array();
$core_update = find_core_auto_update(); $core_update = find_core_auto_update();
if ( $core_update ) { if ( $core_update ) {
$start_time = time(); $start_time = time();
$core_update_result = self::upgrade( 'core', $core_update );
$core_update_result = $this->upgrade( 'core', $core_update );
delete_site_transient( 'update_core' ); delete_site_transient( 'update_core' );
$extra_update_stats['success'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_code() : true; $extra_update_stats['success'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_code() : true;
$extra_update_stats['error_data'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_data() : ''; $extra_update_stats['error_data'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_data() : '';
if ( is_wp_error( $core_update_result ) && 'rollback_was_required' == $core_update_result->get_error_code() ) { if ( is_wp_error( $core_update_result ) && 'rollback_was_required' == $core_update_result->get_error_code() ) {
$rollback_data = $core_update_result->get_error_data(); $rollback_data = $core_update_result->get_error_data();
$extra_update_stats['success'] = is_wp_error( $rollback_data['update'] ) ? $rollback_data['update']->get_error_code() : $rollback_data['update']; $extra_update_stats['success'] = is_wp_error( $rollback_data['update'] ) ? $rollback_data['update']->get_error_code() : $rollback_data['update'];
@ -1796,6 +1797,7 @@ class WP_Automatic_Upgrader {
$extra_update_stats['rollback'] = is_wp_error( $rollback_data['rollback'] ) ? $rollback_data['rollback']->get_error_code() : true; // If it's not a WP_Error, the rollback was successful. $extra_update_stats['rollback'] = is_wp_error( $rollback_data['rollback'] ) ? $rollback_data['rollback']->get_error_code() : true; // If it's not a WP_Error, the rollback was successful.
$extra_update_stats['rollback_data'] = is_wp_error( $rollback_data['rollback'] ) ? $rollback_data['rollback']->get_error_data() : ''; $extra_update_stats['rollback_data'] = is_wp_error( $rollback_data['rollback'] ) ? $rollback_data['rollback']->get_error_data() : '';
} }
$extra_update_stats['fs_method'] = $GLOBALS['wp_filesystem']->method; $extra_update_stats['fs_method'] = $GLOBALS['wp_filesystem']->method;
$extra_update_stats['fs_method_forced'] = defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ); $extra_update_stats['fs_method_forced'] = defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' );
$extra_update_stats['time_taken'] = ( time() - $start_time ); $extra_update_stats['time_taken'] = ( time() - $start_time );
@ -1811,8 +1813,9 @@ class WP_Automatic_Upgrader {
$language_updates = wp_get_translation_updates(); $language_updates = wp_get_translation_updates();
if ( $language_updates ) { if ( $language_updates ) {
foreach ( $language_updates as $update ) { foreach ( $language_updates as $update ) {
self::upgrade( 'language', $update ); $this->upgrade( 'language', $update );
} }
// Clear existing caches // Clear existing caches
wp_clean_plugins_cache(); wp_clean_plugins_cache();
wp_clean_themes_cache(); wp_clean_themes_cache();
@ -1823,33 +1826,20 @@ class WP_Automatic_Upgrader {
wp_update_plugins(); // Check for Plugin updates wp_update_plugins(); // Check for Plugin updates
} }
/** $this->send_debug_email();
* Filter whether to email an update summary to the site administrator.
*
* @since 3.7.0
*
* @param bool Whether or not email should be sent to administrator. Default true.
* @param bool|array $core_update An array of core update data, false otherwise.
* @param object $theme_updates Object containing theme update properties.
* @param object $plugin_updates Object containing plugin update properties.
* @param array $language_updates Array containing the Language updates available.
* @param array $upgrade_results Array of the upgrade results keyed by upgrade type, and plugin/theme slug.
*/
if ( apply_filters( 'enable_auto_upgrade_email', true, $core_update, $theme_updates, $plugin_updates, $language_updates, self::$upgrade_results ) )
self::send_email();
// Clear the lock // Clear the lock
delete_site_option( $lock_name ); delete_site_option( $lock_name );
} }
static function send_email() { function send_debug_email() {
if ( empty( self::$upgrade_results ) ) if ( empty( $this->update_results ) )
return; return;
$upgrade_count = 0; $upgrade_count = 0;
foreach ( self::$upgrade_results as $type => $upgrades ) foreach ( $this->update_results as $type => $upgrades )
$upgrade_count += count( $upgrades ); $upgrade_count += count( $upgrades );
$body = array(); $body = array();
@ -1858,8 +1848,8 @@ class WP_Automatic_Upgrader {
$body[] = 'WordPress site: ' . network_home_url( '/' ); $body[] = 'WordPress site: ' . network_home_url( '/' );
// Core // Core
if ( isset( self::$upgrade_results['core'] ) ) { if ( isset( $this->update_results['core'] ) ) {
$result = self::$upgrade_results['core'][0]; $result = $this->update_results['core'][0];
if ( $result->result && ! is_wp_error( $result->result ) ) { if ( $result->result && ! is_wp_error( $result->result ) ) {
$body[] = sprintf( 'SUCCESS: WordPress was successfully updated to %s', $result->name ); $body[] = sprintf( 'SUCCESS: WordPress was successfully updated to %s', $result->name );
} else { } else {
@ -1871,18 +1861,18 @@ class WP_Automatic_Upgrader {
// Plugins, Themes, Languages // Plugins, Themes, Languages
foreach ( array( 'plugin', 'theme', 'language' ) as $type ) { foreach ( array( 'plugin', 'theme', 'language' ) as $type ) {
if ( ! isset( self::$upgrade_results[ $type ] ) ) if ( ! isset( $this->update_results[ $type ] ) )
continue; continue;
$success_items = wp_list_filter( self::$upgrade_results[ $type ], array( 'result' => true ) ); $success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
if ( $success_items ) { if ( $success_items ) {
$body[] = "The following {$type}s were successfully updated:"; $body[] = "The following {$type}s were successfully updated:";
foreach ( wp_list_pluck( $success_items, 'name' ) as $name ) foreach ( wp_list_pluck( $success_items, 'name' ) as $name )
$body[] = ' * SUCCESS: ' . $name; $body[] = ' * SUCCESS: ' . $name;
} }
if ( $success_items != self::$upgrade_results[ $type ] ) { if ( $success_items != $this->update_results[ $type ] ) {
// Failed updates // Failed updates
$body[] = "The following {$type}s failed to update:"; $body[] = "The following {$type}s failed to update:";
foreach ( self::$upgrade_results[ $type ] as $item ) { foreach ( $this->update_results[ $type ] as $item ) {
if ( ! $item->result || is_wp_error( $item->result ) ) { if ( ! $item->result || is_wp_error( $item->result ) ) {
$body[] = ' * FAILED: ' . $item->name; $body[] = ' * FAILED: ' . $item->name;
$failures++; $failures++;
@ -1913,9 +1903,9 @@ class WP_Automatic_Upgrader {
$body[] = ''; $body[] = '';
foreach ( array( 'core', 'plugin', 'theme', 'language' ) as $type ) { foreach ( array( 'core', 'plugin', 'theme', 'language' ) as $type ) {
if ( ! isset( self::$upgrade_results[ $type ] ) ) if ( ! isset( $this->update_results[ $type ] ) )
continue; continue;
foreach ( self::$upgrade_results[ $type ] as $upgrade ) { foreach ( $this->update_results[ $type ] as $upgrade ) {
$body[] = $upgrade->name; $body[] = $upgrade->name;
$body[] = str_repeat( '-', strlen( $upgrade->name ) ); $body[] = str_repeat( '-', strlen( $upgrade->name ) );
foreach ( $upgrade->messages as $message ) foreach ( $upgrade->messages as $message )

View File

@ -77,11 +77,12 @@ function find_core_auto_update() {
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$auto_update = false; $auto_update = false;
$upgrader = new WP_Automatic_Upgrader;
foreach ( $updates->updates as $update ) { foreach ( $updates->updates as $update ) {
if ( 'autoupdate' != $update->response ) if ( 'autoupdate' != $update->response )
continue; continue;
if ( ! WP_Automatic_Upgrader::should_auto_update( 'core', $update, ABSPATH ) ) if ( ! $upgrader->should_upgrade( 'core', $update, ABSPATH ) )
continue; continue;
if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) ) if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )

View File

@ -148,13 +148,14 @@ function core_upgrade_preamble() {
if ( wp_http_supports( 'ssl' ) ) { if ( wp_http_supports( 'ssl' ) ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
$upgrader = new WP_Automatic_Upgrader;
$future_minor_update = (object) array( $future_minor_update = (object) array(
'current' => $wp_version . '.1-update-core.php', 'current' => $wp_version . '.1-update-core.php',
'version' => $wp_version . '.1-update-core.php', 'version' => $wp_version . '.1-update-core.php',
'php_version' => $required_php_version, 'php_version' => $required_php_version,
'mysql_version' => $required_mysql_version, 'mysql_version' => $required_mysql_version,
); );
$should_auto_update = WP_Automatic_Upgrader::should_auto_update( 'core', $future_minor_update, ABSPATH ); $should_auto_update = $upgrader->should_upgrade( 'core', $future_minor_update, ABSPATH );
if ( $should_auto_update ) if ( $should_auto_update )
echo ' ' . __( 'Future security updates will be applied automatically.' ); echo ' ' . __( 'Future security updates will be applied automatically.' );
} }
@ -171,7 +172,8 @@ function core_upgrade_preamble() {
if ( isset( $updates[0] ) && $updates[0]->response == 'development' ) { if ( isset( $updates[0] ) && $updates[0]->response == 'development' ) {
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php'; require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
if ( wp_http_supports( 'ssl' ) && WP_Automatic_Upgrader::should_auto_update( 'core', $updates[0], ABSPATH ) ) $upgrader = new WP_Automatic_Upgrader;
if ( wp_http_supports( 'ssl' ) && $upgrader->should_upgrade( 'core', $updates[0], ABSPATH ) )
echo '<div class="updated inline"><p><strong>BETA TESTERS:</strong> This site is set up to install updates of future beta versions automatically.</p></div>'; echo '<div class="updated inline"><p><strong>BETA TESTERS:</strong> This site is set up to install updates of future beta versions automatically.</p></div>';
} }

View File

@ -406,18 +406,19 @@ function wp_update_themes() {
} }
/** /**
* Cron entry which runs the WordPress Automatic Updates * Performs WordPress automatic background updates.
* *
* @since 3.7.0 * @since 3.7.0
*/ */
function wp_auto_updates_maybe_update() { function wp_maybe_auto_update() {
include_once ABSPATH . '/wp-admin/includes/admin.php'; include_once ABSPATH . '/wp-admin/includes/admin.php';
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php'; include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
if ( WP_Automatic_Upgrader::upgrader_disabled() ) $upgrader = new WP_Automatic_Upgrader;
if ( $upgrader->is_disabled() )
return; return;
WP_Automatic_Upgrader::perform_auto_updates(); $upgrader->run();
} }
/** /**
@ -565,9 +566,8 @@ function wp_schedule_update_checks() {
if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') ) if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
wp_schedule_event(time(), 'twicedaily', 'wp_update_themes'); wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
if ( !wp_next_scheduled( 'wp_auto_updates_maybe_update' ) && ! defined( 'WP_INSTALLING' ) ) if ( !wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) )
wp_schedule_event( time(), 'twicedaily', 'wp_auto_updates_maybe_update' ); wp_schedule_event( time(), 'twicedaily', 'wp_maybe_auto_update' );
} }
if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
@ -591,7 +591,6 @@ add_action( 'admin_init', '_maybe_update_themes' );
add_action( 'wp_update_themes', 'wp_update_themes' ); add_action( 'wp_update_themes', 'wp_update_themes' );
add_action( 'upgrader_process_complete', 'wp_update_themes' ); add_action( 'upgrader_process_complete', 'wp_update_themes' );
// Automatic Updates - Cron callback add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
add_action( 'wp_auto_updates_maybe_update', 'wp_auto_updates_maybe_update' );
add_action('init', 'wp_schedule_update_checks'); add_action('init', 'wp_schedule_update_checks');