Skip to content

Commit c39143f

Browse files
committed
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. git-svn-id: https://develop.svn.wordpress.org/trunk@25823 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d0bd68c commit c39143f

4 files changed

Lines changed: 53 additions & 61 deletions

File tree

src/wp-admin/includes/class-wp-upgrader.php

Lines changed: 39 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1551,33 +1551,32 @@ function cleanup() {
15511551
}
15521552

15531553
/**
1554-
* WordPress Automatic Upgrader helper class
1554+
* WordPress automatic background upgrader.
15551555
*
15561556
* @since 3.7.0
15571557
*/
15581558
class WP_Automatic_Upgrader {
15591559

1560-
static $upgrade_results = array();
1560+
protected $update_results = array();
15611561

1562-
static function upgrader_disabled() {
1563-
// That's a no if you don't want files changes
1562+
function is_disabled() {
1563+
// Background updates are disabled if you don't want file changes.
15641564
if ( defined( 'DISALLOW_FILE_MODS' ) && DISALLOW_FILE_MODS )
15651565
return true;
15661566

1567-
// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters
1568-
if ( defined( 'AUTOMATIC_UPDATER_DISABLED' ) && AUTOMATIC_UPDATER_DISABLED )
1569-
return true;
1570-
15711567
if ( defined( 'WP_INSTALLING' ) )
15721568
return true;
15731569

1574-
return apply_filters( 'auto_upgrader_disabled', false );
1570+
// More fine grained control can be done through the WP_AUTO_UPDATE_CORE constant and filters.
1571+
$disabled = defined( 'AUTOMATIC_UPDATES_DISABLED' ) && AUTOMATIC_UPDATES_DISABLED;
1572+
1573+
return apply_filters( 'auto_upgrader_disabled', $disabled );
15751574
}
15761575

15771576
/**
15781577
* Check for GIT/SVN checkouts.
15791578
*/
1580-
static function is_vcs_checkout( $context ) {
1579+
function is_vcs_checkout( $context ) {
15811580
$context_dirs = array( untrailingslashit( $context ) );
15821581
if ( $context !== ABSPATH )
15831582
$context_dirs[] = untrailingslashit( ABSPATH );
@@ -1607,17 +1606,16 @@ static function is_vcs_checkout( $context ) {
16071606
/**
16081607
* Tests to see if we can and should upgrade a specific item.
16091608
*/
1610-
static function should_auto_update( $type, $item, $context ) {
1609+
function should_upgrade( $type, $item, $context ) {
1610+
if ( $this->is_disabled() )
1611+
return false;
16111612

16121613
// Checks to see if WP_Filesystem is set up to allow unattended upgrades.
16131614
$skin = new Automatic_Upgrader_Skin;
16141615
if ( ! $skin->request_filesystem_credentials( false, $context ) )
16151616
return false;
16161617

1617-
if ( self::upgrader_disabled() )
1618-
return false;
1619-
1620-
if ( self::is_vcs_checkout( $context ) )
1618+
if ( $this->is_vcs_checkout( $context ) )
16211619
return false;
16221620

16231621
// Next up, do we actually have it enabled for this type of update?
@@ -1657,7 +1655,7 @@ static function should_auto_update( $type, $item, $context ) {
16571655
return true;
16581656
}
16591657

1660-
static function upgrade( $type, $item ) {
1658+
function upgrade( $type, $item ) {
16611659

16621660
$skin = new Automatic_Upgrader_Skin();
16631661

@@ -1683,7 +1681,7 @@ static function upgrade( $type, $item ) {
16831681
}
16841682

16851683
// Determine whether we can and should perform this upgrade.
1686-
if ( ! self::should_auto_update( $type, $item, $context ) )
1684+
if ( ! $this->should_upgrade( $type, $item, $context ) )
16871685
return false;
16881686

16891687
switch ( $type ) {
@@ -1724,7 +1722,7 @@ static function upgrade( $type, $item ) {
17241722
}
17251723
}
17261724

1727-
self::$upgrade_results[ $type ][] = (object) array(
1725+
$this->update_results[ $type ][] = (object) array(
17281726
'item' => $item,
17291727
'result' => $upgrade_result,
17301728
'name' => $item_name,
@@ -1737,8 +1735,7 @@ static function upgrade( $type, $item ) {
17371735
/**
17381736
* Kicks off a upgrade request for each item in the upgrade "queue"
17391737
*/
1740-
static function perform_auto_updates() {
1741-
1738+
function run() {
17421739
$lock_name = 'auto_upgrader.lock';
17431740
if ( get_site_option( $lock_name ) ) {
17441741
// Test to see if it was set more than an hour ago, if so, cleanup.
@@ -1762,7 +1759,7 @@ static function perform_auto_updates() {
17621759
$plugin_updates = get_site_transient( 'update_plugins' );
17631760
if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
17641761
foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
1765-
self::upgrade( 'plugin', $plugin );
1762+
$this->upgrade( 'plugin', $plugin );
17661763
}
17671764
// Force refresh of plugin update information
17681765
wp_clean_plugins_cache();
@@ -1773,7 +1770,7 @@ static function perform_auto_updates() {
17731770
$theme_updates = get_site_transient( 'update_themes' );
17741771
if ( $theme_updates && !empty( $theme_updates->response ) ) {
17751772
foreach ( array_keys( $theme_updates->response ) as $theme ) {
1776-
self::upgrade( 'theme', $theme );
1773+
$this->upgrade( 'theme', $theme );
17771774
}
17781775
// Force refresh of theme update information
17791776
wp_clean_themes_cache();
@@ -1783,19 +1780,24 @@ static function perform_auto_updates() {
17831780
wp_version_check(); // Check for Core updates
17841781
$extra_update_stats = array();
17851782
$core_update = find_core_auto_update();
1783+
17861784
if ( $core_update ) {
17871785
$start_time = time();
1788-
$core_update_result = self::upgrade( 'core', $core_update );
1786+
1787+
$core_update_result = $this->upgrade( 'core', $core_update );
17891788
delete_site_transient( 'update_core' );
1789+
17901790
$extra_update_stats['success'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_code() : true;
17911791
$extra_update_stats['error_data'] = is_wp_error( $core_update_result ) ? $core_update_result->get_error_data() : '';
1792+
17921793
if ( is_wp_error( $core_update_result ) && 'rollback_was_required' == $core_update_result->get_error_code() ) {
17931794
$rollback_data = $core_update_result->get_error_data();
17941795
$extra_update_stats['success'] = is_wp_error( $rollback_data['update'] ) ? $rollback_data['update']->get_error_code() : $rollback_data['update'];
17951796
$extra_update_stats['error_data'] = is_wp_error( $rollback_data['update'] ) ? $rollback_data['update']->get_error_data() : '';
17961797
$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.
17971798
$extra_update_stats['rollback_data'] = is_wp_error( $rollback_data['rollback'] ) ? $rollback_data['rollback']->get_error_data() : '';
17981799
}
1800+
17991801
$extra_update_stats['fs_method'] = $GLOBALS['wp_filesystem']->method;
18001802
$extra_update_stats['fs_method_forced'] = defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' );
18011803
$extra_update_stats['time_taken'] = ( time() - $start_time );
@@ -1811,8 +1813,9 @@ static function perform_auto_updates() {
18111813
$language_updates = wp_get_translation_updates();
18121814
if ( $language_updates ) {
18131815
foreach ( $language_updates as $update ) {
1814-
self::upgrade( 'language', $update );
1816+
$this->upgrade( 'language', $update );
18151817
}
1818+
18161819
// Clear existing caches
18171820
wp_clean_plugins_cache();
18181821
wp_clean_themes_cache();
@@ -1823,33 +1826,20 @@ static function perform_auto_updates() {
18231826
wp_update_plugins(); // Check for Plugin updates
18241827
}
18251828

1826-
/**
1827-
* Filter whether to email an update summary to the site administrator.
1828-
*
1829-
* @since 3.7.0
1830-
*
1831-
* @param bool Whether or not email should be sent to administrator. Default true.
1832-
* @param bool|array $core_update An array of core update data, false otherwise.
1833-
* @param object $theme_updates Object containing theme update properties.
1834-
* @param object $plugin_updates Object containing plugin update properties.
1835-
* @param array $language_updates Array containing the Language updates available.
1836-
* @param array $upgrade_results Array of the upgrade results keyed by upgrade type, and plugin/theme slug.
1837-
*/
1838-
if ( apply_filters( 'enable_auto_upgrade_email', true, $core_update, $theme_updates, $plugin_updates, $language_updates, self::$upgrade_results ) )
1839-
self::send_email();
1829+
$this->send_debug_email();
18401830

18411831
// Clear the lock
18421832
delete_site_option( $lock_name );
18431833

18441834
}
18451835

1846-
static function send_email() {
1836+
function send_debug_email() {
18471837

1848-
if ( empty( self::$upgrade_results ) )
1838+
if ( empty( $this->update_results ) )
18491839
return;
18501840

18511841
$upgrade_count = 0;
1852-
foreach ( self::$upgrade_results as $type => $upgrades )
1842+
foreach ( $this->update_results as $type => $upgrades )
18531843
$upgrade_count += count( $upgrades );
18541844

18551845
$body = array();
@@ -1858,8 +1848,8 @@ static function send_email() {
18581848
$body[] = 'WordPress site: ' . network_home_url( '/' );
18591849

18601850
// Core
1861-
if ( isset( self::$upgrade_results['core'] ) ) {
1862-
$result = self::$upgrade_results['core'][0];
1851+
if ( isset( $this->update_results['core'] ) ) {
1852+
$result = $this->update_results['core'][0];
18631853
if ( $result->result && ! is_wp_error( $result->result ) ) {
18641854
$body[] = sprintf( 'SUCCESS: WordPress was successfully updated to %s', $result->name );
18651855
} else {
@@ -1871,18 +1861,18 @@ static function send_email() {
18711861

18721862
// Plugins, Themes, Languages
18731863
foreach ( array( 'plugin', 'theme', 'language' ) as $type ) {
1874-
if ( ! isset( self::$upgrade_results[ $type ] ) )
1864+
if ( ! isset( $this->update_results[ $type ] ) )
18751865
continue;
1876-
$success_items = wp_list_filter( self::$upgrade_results[ $type ], array( 'result' => true ) );
1866+
$success_items = wp_list_filter( $this->update_results[ $type ], array( 'result' => true ) );
18771867
if ( $success_items ) {
18781868
$body[] = "The following {$type}s were successfully updated:";
18791869
foreach ( wp_list_pluck( $success_items, 'name' ) as $name )
18801870
$body[] = ' * SUCCESS: ' . $name;
18811871
}
1882-
if ( $success_items != self::$upgrade_results[ $type ] ) {
1872+
if ( $success_items != $this->update_results[ $type ] ) {
18831873
// Failed updates
18841874
$body[] = "The following {$type}s failed to update:";
1885-
foreach ( self::$upgrade_results[ $type ] as $item ) {
1875+
foreach ( $this->update_results[ $type ] as $item ) {
18861876
if ( ! $item->result || is_wp_error( $item->result ) ) {
18871877
$body[] = ' * FAILED: ' . $item->name;
18881878
$failures++;
@@ -1913,9 +1903,9 @@ static function send_email() {
19131903
$body[] = '';
19141904

19151905
foreach ( array( 'core', 'plugin', 'theme', 'language' ) as $type ) {
1916-
if ( ! isset( self::$upgrade_results[ $type ] ) )
1906+
if ( ! isset( $this->update_results[ $type ] ) )
19171907
continue;
1918-
foreach ( self::$upgrade_results[ $type ] as $upgrade ) {
1908+
foreach ( $this->update_results[ $type ] as $upgrade ) {
19191909
$body[] = $upgrade->name;
19201910
$body[] = str_repeat( '-', strlen( $upgrade->name ) );
19211911
foreach ( $upgrade->messages as $message )

src/wp-admin/includes/update.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,12 @@ function find_core_auto_update() {
7777
include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
7878

7979
$auto_update = false;
80+
$upgrader = new WP_Automatic_Upgrader;
8081
foreach ( $updates->updates as $update ) {
8182
if ( 'autoupdate' != $update->response )
8283
continue;
8384

84-
if ( ! WP_Automatic_Upgrader::should_auto_update( 'core', $update, ABSPATH ) )
85+
if ( ! $upgrader->should_upgrade( 'core', $update, ABSPATH ) )
8586
continue;
8687

8788
if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )

src/wp-admin/update-core.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,14 @@ function core_upgrade_preamble() {
148148

149149
if ( wp_http_supports( 'ssl' ) ) {
150150
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
151+
$upgrader = new WP_Automatic_Upgrader;
151152
$future_minor_update = (object) array(
152153
'current' => $wp_version . '.1-update-core.php',
153154
'version' => $wp_version . '.1-update-core.php',
154155
'php_version' => $required_php_version,
155156
'mysql_version' => $required_mysql_version,
156157
);
157-
$should_auto_update = WP_Automatic_Upgrader::should_auto_update( 'core', $future_minor_update, ABSPATH );
158+
$should_auto_update = $upgrader->should_upgrade( 'core', $future_minor_update, ABSPATH );
158159
if ( $should_auto_update )
159160
echo ' ' . __( 'Future security updates will be applied automatically.' );
160161
}
@@ -171,7 +172,8 @@ function core_upgrade_preamble() {
171172

172173
if ( isset( $updates[0] ) && $updates[0]->response == 'development' ) {
173174
require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
174-
if ( wp_http_supports( 'ssl' ) && WP_Automatic_Upgrader::should_auto_update( 'core', $updates[0], ABSPATH ) )
175+
$upgrader = new WP_Automatic_Upgrader;
176+
if ( wp_http_supports( 'ssl' ) && $upgrader->should_upgrade( 'core', $updates[0], ABSPATH ) )
175177
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>';
176178
}
177179

src/wp-includes/update.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -406,18 +406,19 @@ function wp_update_themes() {
406406
}
407407

408408
/**
409-
* Cron entry which runs the WordPress Automatic Updates
409+
* Performs WordPress automatic background updates.
410410
*
411411
* @since 3.7.0
412412
*/
413-
function wp_auto_updates_maybe_update() {
413+
function wp_maybe_auto_update() {
414414
include_once ABSPATH . '/wp-admin/includes/admin.php';
415415
include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
416416

417-
if ( WP_Automatic_Upgrader::upgrader_disabled() )
417+
$upgrader = new WP_Automatic_Upgrader;
418+
if ( $upgrader->is_disabled() )
418419
return;
419420

420-
WP_Automatic_Upgrader::perform_auto_updates();
421+
$upgrader->run();
421422
}
422423

423424
/**
@@ -565,9 +566,8 @@ function wp_schedule_update_checks() {
565566
if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
566567
wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
567568

568-
if ( !wp_next_scheduled( 'wp_auto_updates_maybe_update' ) && ! defined( 'WP_INSTALLING' ) )
569-
wp_schedule_event( time(), 'twicedaily', 'wp_auto_updates_maybe_update' );
570-
569+
if ( !wp_next_scheduled( 'wp_maybe_auto_update' ) && ! defined( 'WP_INSTALLING' ) )
570+
wp_schedule_event( time(), 'twicedaily', 'wp_maybe_auto_update' );
571571
}
572572

573573
if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) )
@@ -591,7 +591,6 @@ function wp_schedule_update_checks() {
591591
add_action( 'wp_update_themes', 'wp_update_themes' );
592592
add_action( 'upgrader_process_complete', 'wp_update_themes' );
593593

594-
// Automatic Updates - Cron callback
595-
add_action( 'wp_auto_updates_maybe_update', 'wp_auto_updates_maybe_update' );
594+
add_action( 'wp_maybe_auto_update', 'wp_maybe_auto_update' );
596595

597596
add_action('init', 'wp_schedule_update_checks');

0 commit comments

Comments
 (0)