<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[25421] trunk/src: WordPress Core Automatic Updates: Add the first slice of Automatic Upgrades, This is presently disabled, and requires a filter to enable ( 'auto_upgrade_core' ).</title>
</head>
<body>
<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://core.trac.wordpress.org/changeset/25421">25421</a></dd>
<dt>Author</dt> <dd>dd32</dd>
<dt>Date</dt> <dd>2013-09-13 06:18:16 +0000 (Fri, 13 Sep 2013)</dd>
</dl>
<h3>Log Message</h3>
<pre>WordPress Core Automatic Updates: Add the first slice of Automatic Upgrades, This is presently disabled, and requires a filter to enable ( 'auto_upgrade_core' ). See <a href="http://core.trac.wordpress.org/ticket/22704">#22704</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpupgraderskinsphp">trunk/src/wp-admin/includes/class-wp-upgrader-skins.php</a></li>
<li><a href="#trunksrcwpadminincludesclasswpupgraderphp">trunk/src/wp-admin/includes/class-wp-upgrader.php</a></li>
<li><a href="#trunksrcwpadminincludesupdatephp">trunk/src/wp-admin/includes/update.php</a></li>
<li><a href="#trunksrcwpincludesupdatephp">trunk/src/wp-includes/update.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpupgraderskinsphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/class-wp-upgrader-skins.php (25420 => 25421)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/class-wp-upgrader-skins.php 2013-09-12 23:14:55 UTC (rev 25420)
+++ trunk/src/wp-admin/includes/class-wp-upgrader-skins.php 2013-09-13 06:18:16 UTC (rev 25421)
</span><span class="lines">@@ -529,7 +529,7 @@
</span><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><del>- * Upgrader Skin for Background WordPress Upgrades
</del><ins>+ * Upgrader Skin for Automatic WordPress Upgrades
</ins><span class="cx"> *
</span><span class="cx"> * This skin is designed to be used when no output is intended, all output
</span><span class="cx"> * is captured and stored for the caller to process and log/email/discard.
</span><span class="lines">@@ -538,7 +538,7 @@
</span><span class="cx"> * @subpackage Upgrader
</span><span class="cx"> * @since 3.7.0
</span><span class="cx"> */
</span><del>-class Background_Upgrader_Skin extends WP_Upgrader_Skin {
</del><ins>+class Automatic_Upgrader_Skin extends WP_Upgrader_Skin {
</ins><span class="cx"> var $messages = array();
</span><span class="cx">
</span><span class="cx"> function request_filesystem_credentials( $error = false ) {
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/class-wp-upgrader.php (25420 => 25421)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/class-wp-upgrader.php 2013-09-12 23:14:55 UTC (rev 25420)
+++ trunk/src/wp-admin/includes/class-wp-upgrader.php 2013-09-13 06:18:16 UTC (rev 25421)
</span><span class="lines">@@ -1137,6 +1137,61 @@
</span><span class="cx"> return $result;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+ // Determines if this WordPress Core version should update to $offered_ver or not
+ static function should_upgrade_to_version( $offered_ver /* x.y.z */ ) {
+ include ABSPATH . WPINC . '/version.php'; // $wp_version; // x.y.z
+
+ $current_branch = implode( '.', array_slice( preg_split( '/[.-]/', $wp_version ), 0, 2 ) ); // x.y
+ $new_branch = implode( '.', array_slice( preg_split( '/[.-]/', $offered_ver ), 0, 2 ) ); // x.y
+ $current_is_development_version = (bool) strpos( $wp_version, '-' );
+
+ // Defaults:
+ $upgrade_dev = false;
+ $upgrade_minor = false; // @TODO: Update for release by toggling to true.
+ $upgrade_major = false;
+
+ // WP_AUTO_UPDATE_CORE = true (all), 'minor', false.
+ if ( defined( 'WP_AUTO_UPDATE_CORE' ) ) {
+ if ( false === WP_AUTO_UPDATE_CORE ) {
+ // Defaults to turned off, unless a filter allows it
+ $upgrade_dev = $upgrade_minor = $upgrade_major = false;
+ } elseif ( true === WP_AUTO_UPDATE_CORE ) {
+ // ALL updates for core
+ $upgrade_dev = $upgrade_minor = $upgrade_major = true;
+ } elseif ( 'minor' === WP_AUTO_UPDATE_CORE ) {
+ // Only minor updates for core
+ $upgrade_dev = $upgrade_major = false;
+ $upgrade_minor = true;
+ }
+ }
+
+ // 1: If we're already on that version, not much point in updating?
+ if ( $offered_ver == $wp_version )
+ return false;
+
+ // 2: If we're running a newer version, that's a nope
+ if ( version_compare( $wp_version, $offered_ver, '>=' ) )
+ return false;
+
+ // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
+ if ( $current_is_development_version ) {
+ if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
+ return false;
+ // else fall through to minor + major branches below
+ }
+
+ // 4: Minor In-branch updates (3.7.0 -> 3.7.1 -> 3.7.2 -> 3.7.4)
+ if ( $current_branch == $new_branch )
+ return apply_filters( 'allow_minor_auto_core_updates', $upgrade_minor );
+
+ // 5: Major version updates (3.7.0 -> 3.8.0 -> 3.9.1)
+ if ( version_compare( $new_branch, $current_branch, '>' ) )
+ return apply_filters( 'allow_major_auto_core_updates', $upgrade_major );
+
+ // If we're not sure, we don't want it
+ return false;
+ }
+
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -1212,3 +1267,223 @@
</span><span class="cx"> return true;
</span><span class="cx"> }
</span><span class="cx"> }
</span><ins>+
+/**
+ * WordPress Automatic Upgrader helper class
+ *
+ * @since 3.7.0
+ */
+class WP_Automatic_Upgrader {
+
+ static $skin;
+
+ static function upgrader_disabled() {
+ // That's a no if you don't want files changes
+ if ( defined( 'DISABLE_FILE_MODS' ) && DISABLE_FILE_MODS )
+ 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' ) )
+ return true;
+
+ return apply_filters( 'auto_upgrader_disabled', false );
+ }
+
+ /**
+ * Tests to see if we should upgrade a specific item, does not test to see if we CAN update the item.
+ */
+ static function should_auto_update( $type, $item, $context ) {
+
+ if ( self::upgrader_disabled() )
+ return false;
+
+ // ..and also check for GIT/SVN checkouts
+ if ( ! apply_filters( 'auto_upgrade_ignore_checkout_status', false ) ) {
+ $stop_dirs = array(
+ ABSPATH,
+ untrailingslashit( $context ),
+ );
+ if ( ! file_exists( ABSPATH . '/wp-config.php' ) ) // wp-config.php up one folder in a deployment situation
+ $stop_dirs[] = dirname( ABSPATH );
+ foreach ( array_unique( $stop_dirs ) as $dir ) {
+ if ( file_exists( $dir . '/.svn' ) || file_exists( $dir . '/.git' ) )
+ return false;
+ }
+ }
+
+ // Next up, do we actually have it enabled for this type of update?
+ switch ( $type ) {
+ case 'core':
+ $upgrade = Core_Upgrader::should_upgrade_to_version( $item->current );
+ break;
+ default:
+ case 'plugin':
+ case 'theme':
+ $upgrade = false;
+ break;
+ }
+
+ // And does the user / plugins want it?
+ // Plugins may filter on 'auto_upgrade_plugin', and check the 2nd param, $item, to only enable it for certain Plugins/Themes
+ if ( ! apply_filters( 'auto_upgrade_' . $type, $upgrade, $item ) )
+ return false;
+
+ // If it's a core update, are we actually compatible with it's requirements?
+ if ( 'core' == $type ) {
+ global $wpdb;
+
+ $php_compat = version_compare( phpversion(), $item->php_version, '>=' );
+ if ( file_exists( WP_CONTENT_DIR . '/db.php' ) && empty( $wpdb->is_mysql ) )
+ $mysql_compat = true;
+ else
+ $mysql_compat = version_compare( $wpdb->db_version(), $item->mysql_version, '>=' );
+
+ if ( ! $php_compat || ! $mysql_compat )
+ return false;
+ }
+
+ return true;
+ }
+
+ // Checks to see if WP_Filesystem is setup to allow unattended upgrades
+ static function can_auto_update( $context ) {
+ if ( ! self::$skin )
+ self::$skin = new Automatic_Upgrader_Skin();
+ return (bool) self::$skin->request_filesystem_credentials();
+ }
+
+ static function upgrade( $type, $item ) {
+
+ self::$skin = new Automatic_Upgrader_Skin();
+
+ switch ( $type ) {
+ case 'core':
+ // The Core upgrader doesn't use the Upgrader's skin during the actual main part of the upgrade, instead, firing a filter
+ add_filter( 'update_feedback', function( $message ) {
+ WP_Background_Upgrader::$skin->feedback( $message );
+ return $message;
+ } );
+ $upgrader = new Core_Upgrader( self::$skin );
+ $context = ABSPATH;
+ break;
+ case 'plugin':
+ $upgrader = new Plugin_Upgrader( self::$skin );
+ $context = WP_PLUGIN_DIR; // We don't support custom Plugin directories, or updates for WPMU_PLUGIN_DIR
+ break;
+ case 'theme':
+ $upgrader = new Theme_Upgrader( self::$skin );
+ $context = get_theme_root( $item );
+ break;
+ }
+
+ // Determine if we can perform this upgrade or not
+ if ( ! self::should_auto_update( $type, $item, $context ) || ! self::can_auto_update( $context ) )
+ return false;
+
+ /*wp_mail(
+ get_site_option( 'admin_email' ),
+ __METHOD__,
+ "Starting an upgrade for:\n\n" . var_export( compact( 'type', 'item' ), true ) . "\n\n" . wp_debug_backtrace_summary()
+ );*/
+
+ // Boom, This sites about to get a whole new splash of paint!
+ $upgrade_result = $upgrader->upgrade( $item, array(
+ 'clear_update_cache' => false,
+ ) );
+
+ // Core doesn't output this, so lets append it so we don't get confused
+ if ( 'core' == $type ) {
+ if ( is_wp_error( $upgrade_result ) ) {
+ self::$skin->error( __( 'Installation Failed' ), $upgrade_result );
+ } else {
+ self::$skin->feedback( __( 'WordPress updated successfully' ) );
+ }
+ }
+
+ // Clear cache's and transients
+ switch ( $type ) {
+ case 'core':
+ delete_site_transient( 'update_core' );
+ break;
+ case 'theme':
+ wp_clean_themes_cache();
+ break;
+ case 'plugin':
+ wp_clean_plugins_cache();
+ break;
+ }
+
+ //var_dump( compact( 'type', 'item', 'upgrader', 'upgrade_result' ) );
+
+ wp_mail(
+ get_site_option( 'admin_email' ),
+ __METHOD__,
+ var_export( array(
+ $upgrade_result,
+ $upgrader,
+ self::$skin,
+ ), true )
+ );
+
+ return $upgrade_result;
+ }
+
+ /**
+ * Kicks off a upgrade request for each item in the upgrade "queue"
+ */
+ static function perform_auto_updates() {
+
+ $lock_name = 'auto_upgrader.lock';
+ if ( get_site_transient( $lock_name ) ) {
+ // Test to see if it was set more than an hour ago, if so, cleanup.
+ if ( true || get_site_transient( $lock_name ) < ( time() - HOUR_IN_SECONDS ) )
+ delete_site_transient( $lock_name );
+ else // Recent lock
+ return;
+ }
+ // Lock upgrades for us for half an hour
+ if ( ! set_site_transient( $lock_name, microtime( true ), HOUR_IN_SECONDS / 2 ) )
+ return;
+
+ // Next, Plugins
+ wp_update_plugins(); // Check for Plugin updates
+ $plugin_updates = get_site_transient( 'update_plugins' );
+ if ( $plugin_updates && !empty( $plugin_updates->response ) ) {
+ foreach ( array_keys( $plugin_updates->response ) as $plugin ) {
+ self::upgrade( 'plugin', $plugin );
+ }
+ // Force refresh of plugin update information
+ wp_clean_plugins_cache();
+ }
+
+ // Next, those themes we all love
+ wp_update_themes(); // Check for Theme updates
+ $theme_updates = get_site_transient( 'update_themes' );
+ if ( $theme_updates && !empty( $theme_updates->response ) ) {
+ foreach ( array_keys( $theme_updates->response ) as $theme ) {
+ self::upgrade( 'theme', $theme );
+ }
+ // Force refresh of theme update information
+ wp_clean_themes_cache();
+ }
+
+ // Finally, Process any core upgrade
+ wp_version_check(); // Check for Core updates
+ $core_update = find_core_auto_update();
+ if ( $core_update )
+ self::upgrade( 'core', $core_update );
+
+ // Cleanup, These won't trigger any updates this time due to the locking transient
+ wp_version_check(); // check for Core updates
+ wp_update_themes(); // Check for Theme updates
+ wp_update_plugins(); // Check for Plugin updates
+
+ // TODO The core database upgrade has already cleared this transient..
+ delete_site_transient( $lock_name );
+
+ }
+
+}
</ins><span class="cx">\ No newline at end of file
</span></span></pre></div>
<a id="trunksrcwpadminincludesupdatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/update.php (25420 => 25421)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/update.php 2013-09-12 23:14:55 UTC (rev 25420)
+++ trunk/src/wp-admin/includes/update.php 2013-09-13 06:18:16 UTC (rev 25421)
</span><span class="lines">@@ -57,6 +57,36 @@
</span><span class="cx"> return $result;
</span><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Gets the best available (and enabled) Auto-Update for WordPress Core.
+ *
+ * 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
+ *
+ * @since 3.7.0
+ *
+ * @return bool|array False on failure, otherwise the core update offering.
+ */
+function find_core_auto_update() {
+ $updates = get_site_transient( 'update_core' );
+ if ( ! $updates || empty( $updates->updates ) )
+ return false;
+
+ include_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
+
+ $auto_update = false;
+ foreach ( $updates->updates as $update ) {
+ if ( 'autoupdate' != $update->response )
+ continue;
+
+ if ( ! WP_Automatic_Upgrader::should_auto_update( 'core', $update, ABSPATH ) )
+ continue;
+
+ if ( ! $auto_update || version_compare( $update->current, $auto_update->current, '>' ) )
+ $auto_update = $update;
+ }
+ return $auto_update;
+}
+
</ins><span class="cx"> function dismiss_core_update( $update ) {
</span><span class="cx"> $dismissed = get_site_option( 'dismissed_update_core' );
</span><span class="cx"> $dismissed[ $update->current . '|' . $update->locale ] = true;
</span></span></pre></div>
<a id="trunksrcwpincludesupdatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/update.php (25420 => 25421)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/update.php 2013-09-12 23:14:55 UTC (rev 25420)
+++ trunk/src/wp-includes/update.php 2013-09-13 06:18:16 UTC (rev 25421)
</span><span class="lines">@@ -120,6 +120,8 @@
</span><span class="cx"> $updates->last_checked = time();
</span><span class="cx"> $updates->version_checked = $wp_version;
</span><span class="cx"> set_site_transient( 'update_core', $updates);
</span><ins>+
+ wp_auto_updates_maybe_queue( 'core' );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -221,6 +223,8 @@
</span><span class="cx"> $new_option->response = array();
</span><span class="cx">
</span><span class="cx"> set_site_transient( 'update_plugins', $new_option );
</span><ins>+
+ wp_auto_updates_maybe_queue( 'plugins' );
</ins><span class="cx"> }
</span><span class="cx">
</span><span class="cx"> /**
</span><span class="lines">@@ -331,8 +335,55 @@
</span><span class="cx"> $new_update->response = $response;
</span><span class="cx">
</span><span class="cx"> set_site_transient( 'update_themes', $new_update );
</span><ins>+
+ wp_auto_updates_maybe_queue( 'themes' );
</ins><span class="cx"> }
</span><span class="cx">
</span><ins>+/**
+ * Queues a cron entry if a potentially upgrade is detected.
+ *
+ * @since 3.7.0
+ *
+ * @param string $type The type of update to check for, may be 'core', 'plugins', or, 'themes'.
+ */
+function wp_auto_updates_maybe_queue( $type = 'core' ) {
+ include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
+ include_once ABSPATH . '/wp-admin/includes/update.php';
+
+ if ( WP_Automatic_Upgrader::upgrader_disabled() )
+ return;
+
+ $updates_available = false;
+ if ( 'core' == $type ) {
+ $updates_available = (bool) find_core_auto_update();
+ } elseif ( 'plugins' == $type ) {
+ $plugin_updates = get_site_transient( 'update_plugins' );
+ $updates_available = !empty( $plugin_updates->response );
+ } elseif ( 'themes' == $type ) {
+ $theme_updates = get_site_transient( 'update_themes' );
+ $updates_available = empty( $theme_updates->response );
+ }
+
+ if ( $updates_available && ! wp_next_scheduled( 'wp_auto_updates_execute' ) ) {
+ // If the transient update was triggered by a user pageview, update in an hours time, else, now.
+ $when_to_update = get_current_user_id() ? time() + HOUR_IN_SECONDS : time();
+ $when_to_update = apply_filters( 'auto_upgrade_when_to_upgrade', $when_to_update );
+
+ wp_schedule_single_event( $when_to_update, 'wp_auto_updates_execute' );
+ }
+
+}
+
+function wp_auto_updates_execute() {
+ include_once ABSPATH . '/wp-admin/includes/admin.php';
+ include_once ABSPATH . '/wp-admin/includes/class-wp-upgrader.php';
+
+ if ( WP_Automatic_Upgrader::upgrader_disabled() )
+ return;
+
+ WP_Automatic_Upgrader::perform_auto_updates();
+}
+
</ins><span class="cx"> /*
</span><span class="cx"> * Collect counts and UI strings for available updates
</span><span class="cx"> *
</span><span class="lines">@@ -456,4 +507,7 @@
</span><span class="cx"> add_action( 'admin_init', '_maybe_update_themes' );
</span><span class="cx"> add_action( 'wp_update_themes', 'wp_update_themes' );
</span><span class="cx">
</span><ins>+// Automatic Updates - Cron callback
+add_action( 'wp_auto_updates_execute', 'wp_auto_updates_execute' );
+
</ins><span class="cx"> add_action('init', 'wp_schedule_update_checks');
</span></span></pre>
</div>
</div>
</body>
</html>