<!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>[25841] trunk/src: Notify administrators of successful, failed, and pending core updates.</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/25841">25841</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-10-18 16:29:16 +0000 (Fri, 18 Oct 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Notify administrators of successful, failed, and pending core updates.

Blocks future background updates after critical failures, but allow retries in certain situations. More in the ticket.

fixes <a href="http://core.trac.wordpress.org/ticket/10787">#10787</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminaboutphp">trunk/src/wp-admin/about.php</a></li>
<li><a href="#trunksrcwpadminincludesclasswpupgraderphp">trunk/src/wp-admin/includes/class-wp-upgrader.php</a></li>
<li><a href="#trunksrcwpadminincludesupdatecorephp">trunk/src/wp-admin/includes/update-core.php</a></li>
<li><a href="#trunksrcwpadminupdatecorephp">trunk/src/wp-admin/update-core.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="trunksrcwpadminaboutphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/about.php (25840 => 25841)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/about.php     2013-10-18 08:41:40 UTC (rev 25840)
+++ trunk/src/wp-admin/about.php        2013-10-18 16:29:16 UTC (rev 25841)
</span><span class="lines">@@ -55,8 +55,8 @@
</span><span class="cx">                          require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
</span><span class="cx">                          $upgrader = new WP_Automatic_Updater;
</span><span class="cx">                          $future_minor_update = (object) array(
</span><del>-                                       'current'       => $wp_version . '.1-about.php',
-                                       'version'       => $wp_version . '.1-about.php',
</del><ins>+                                        'current'       => $wp_version . '.1.next.minor',
+                                       'version'       => $wp_version . '.1.next.minor',
</ins><span class="cx">                                   'php_version'   => $required_php_version,
</span><span class="cx">                                  'mysql_version' => $required_mysql_version,
</span><span class="cx">                          );
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/class-wp-upgrader.php (25840 => 25841)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/class-wp-upgrader.php        2013-10-18 08:41:40 UTC (rev 25840)
+++ trunk/src/wp-admin/includes/class-wp-upgrader.php   2013-10-18 16:29:16 UTC (rev 25841)
</span><span class="lines">@@ -1437,6 +1437,23 @@
</span><span class="cx">          if ( version_compare( $wp_version, $offered_ver, '>' ) )
</span><span class="cx">                  return false;
</span><span class="cx"> 
</span><ins>+               $failure_data = get_site_option( 'auto_core_update_failed' );
+               if ( $failure_data ) {
+                       // If this was a critical update failure, cannot update.
+                       if ( ! empty( $failure_data['critical'] ) )
+                               return false;
+
+                       // Don't claim we can update on update-core.php if we have a non-critical failure logged.
+                       if ( $wp_version == $failure_data['current'] && false !== strpos( $wp_version, '.1.next.minor' ) )
+                               return false;
+
+                       // Cannot update if we're retrying the same A to B update that caused a non-critical failure.
+                       // Some non-critical failures do allow retries, like download_failed.
+                       // 3.7.1 => 3.7.2 resulted in files_not_writable, if we are still on 3.7.1 and still trying to update to 3.7.2.
+                       if ( empty( $failure_data['retry'] ) && $wp_version == $failure_data['current'] && $offered_ver == $failure_data['attempted'] )
+                               return false;
+               }
+
</ins><span class="cx">           // 3: 3.7-alpha-25000 -> 3.7-alpha-25678 -> 3.7-beta1 -> 3.7-beta2
</span><span class="cx">          if ( $current_is_development_version ) {
</span><span class="cx">                  if ( ! apply_filters( 'allow_dev_auto_core_updates', $upgrade_dev ) )
</span><span class="lines">@@ -1626,8 +1643,24 @@
</span><span class="cx"> 
</span><span class="cx">          // And does the user / plugins want it?
</span><span class="cx">          // Plugins may filter on 'auto_update_plugin', and check the 2nd param, $item, to only enable it for certain Plugins/Themes
</span><del>-               if ( ! apply_filters( 'auto_update_' . $type, $update, $item ) )
</del><ins>+                $update = apply_filters( 'auto_update_' . $type, $update, $item );
+
+               if ( ! $update ) {
+
+                       // See if we need to notify users of a core update.
+                       if ( 'core' == $type && ! empty( $item->notify_email ) ) {
+                               $notify      = true;
+                               $notified    = get_site_option( 'auto_core_update_notified' );
+
+                               // Don't notify if we've already notified the same email address of the same version.
+                               if ( $notified && $notified['email'] == get_site_option( 'admin_email' ) && $notified['version'] == $item->current )
+                                       return false;
+
+                               $this->send_email( 'manual', $item );
+                       }
+
</ins><span class="cx">                   return false;
</span><ins>+               }
</ins><span class="cx"> 
</span><span class="cx">          // If it's a core update, are we actually compatible with its requirements?
</span><span class="cx">          if ( 'core' == $type ) {
</span><span class="lines">@@ -1727,7 +1760,7 @@
</span><span class="cx">   * Kicks off a update request for each item in the update "queue".
</span><span class="cx">   */
</span><span class="cx">  function run() {
</span><del>-               global $wpdb;
</del><ins>+                global $wpdb, $wp_version;
</ins><span class="cx"> 
</span><span class="cx">          if ( ! is_main_network() || ! is_main_site() )
</span><span class="cx">                  return;
</span><span class="lines">@@ -1830,13 +1863,107 @@
</span><span class="cx">                  wp_update_plugins(); // Check for Plugin updates
</span><span class="cx">          }
</span><span class="cx"> 
</span><del>-               $this->send_debug_email();
</del><ins>+                // Send debugging email to all development installs.
+               if ( ! empty( $this->update_results ) ) {
+                       $development_version = false !== strpos( $wp_version, '-' );
+                       if ( apply_filters( 'automatic_updates_send_debug_email', $development_version ) )
+                               $this->send_debug_email();
</ins><span class="cx"> 
</span><ins>+                       if ( ! empty( $this->update_results['core'] ) )
+                               $this->after_core_update( $this->update_results['core'][0] );
+               }
+
</ins><span class="cx">           // Clear the lock
</span><span class="cx">          delete_option( $lock_name );
</span><span class="cx">  }
</span><span class="cx"> 
</span><ins>+       /**
+        * If we tried to perform a core update, check if we should send an email,
+        * and if we need to avoid processing future updates.
+        */
+       protected function after_core_update( $update_result ) {
+               global $wp_version;
+
+               $core_update = $update_result->item;
+               $result      = $update_result->result;
+
+               if ( ! is_wp_error( $result ) ) {
+                       $this->send_email( 'success', $core_update );
+                       return;
+               }
+
+               $error_code = $result->get_error_code();
+
+               // Any of these WP_Error codes are critical failures, as in they occurred after we started to copy core files.
+               // We should not try to perform a background update again until there is a successful one-click update performed by the user.
+               $critical = false;
+               if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
+                       $critical = true;
+               } elseif ( $error_code === 'rollback_was_required' ) {
+                       $error_data = $result->get_error_data();
+                       if ( is_wp_error( $error_data['rollback'] ) )
+                               $critical = true;
+               } elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
+                       $critical = true;
+               }
+
+               if ( $critical ) {
+                       update_site_option( 'auto_core_update_failed', array(
+                               'attempted'  => $core_update->current,
+                               'current'    => $wp_version,
+                               'error_code' => $error_code,
+                               'error_data' => $result->get_error_data(),
+                               'timestamp'  => time(),
+                               'critical'   => true,
+                       ) );
+                       $this->send_email( 'critical', $core_update, $result );
+                       return;
+               }
+
+               /*
+                * Any other WP_Error code (like download_failed or files_not_writable) occurs before
+                * we tried to copy over core files. Thus, the failures are early and graceful.
+                *
+                * We should avoid trying to perform a background update again for the same version.
+                * But we can try again if another version is released.
+                *
+                * For certain 'transient' failures, like download_failed, we should allow retries.
+                * In fact, let's schedule a special update for an hour from now. (It's possible
+                * the issue could actually be on WordPress.org's side.) If that one fails, then email.
+                */
+               $send = true;
+               $transient_failures = array( 'incompatible_archive', 'download_failed', 'insane_distro' );
+               if ( in_array( $error_code, $transient_failures ) && ! get_site_option( 'auto_core_update_failed' ) ) {
+                       wp_schedule_single_event( time() + HOUR_IN_SECONDS, 'wp_maybe_auto_update' );
+                       $send = false;
+               }
+
+               $n = get_site_option( 'auto_core_update_notified' );
+               // Don't notify if we've already notified the same email address of the same version of the same notification type.
+               if ( $n && 'fail' == $n['type'] && $n['email'] == get_site_option( 'admin_email' ) && $n['version'] == $core_update->current )
+                       $send = false;
+
+               update_site_option( 'auto_core_update_failed', array(
+                       'attempted'  => $core_update->current,
+                       'current'    => $wp_version,
+                       'error_code' => $error_code,
+                       'error_data' => $result->get_error_data(),
+                       'timestamp'  => time(),
+                       'retry'      => in_array( $error_code, $transient_failures ),
+               ) );
+
+               if ( $send )
+                       $this->send_email( 'fail', $core_update, $result );
+       }
+
</ins><span class="cx">   protected function send_email( $type, $core_update, $result = null ) {
</span><ins>+               update_site_option( 'auto_core_update_notified', array(
+                       'type'      => $type,
+                       'email'     => get_site_option( 'admin_email' ),
+                       'version'   => $core_update->current,
+                       'timestamp' => time(),
+               ) );
+
</ins><span class="cx">           if ( ! apply_filters( 'automatic_updates_send_email', true, $type, $core_update, $result ) )
</span><span class="cx">                  return;
</span><span class="cx"> 
</span><span class="lines">@@ -1917,9 +2044,11 @@
</span><span class="cx">                  $body .= "\n***\n\n";
</span><span class="cx">                  $body .= __( 'We have some data that describes the error your site encountered.' );
</span><span class="cx">                  $body .= ' ' . __( 'Your hosting company, support forum volunteers, or a friendly developer may be able to use this information to help you:' );
</span><del>-                       $body .= "\n" . sprintf( __( "Error code: %s" ), $result->get_error_code() );
-                       $body .= "\n" . $result->get_error_message();
-                       $body .= "\n" . implode( ', ', (array) $result->get_error_data() );
</del><ins>+                        $body .= "\n\n" . sprintf( __( "Error code: %s" ), $result->get_error_code() );
+                       if ( $result->get_error_message() )
+                               $body .= "\n" . $result->get_error_message();
+                       if ( $result->get_error_data() )
+                               $body .= "\n" . implode( ', ', (array) $result->get_error_data() );
</ins><span class="cx">                   $body .= "\n";
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -1933,9 +2062,6 @@
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="cx">  protected function send_debug_email() {
</span><del>-               if ( empty( $this->update_results ) )
-                       return;
-
</del><span class="cx">           $update_count = 0;
</span><span class="cx">          foreach ( $this->update_results as $type => $updates )
</span><span class="cx">                  $update_count += count( $updates );
</span><span class="lines">@@ -1985,7 +2111,9 @@
</span><span class="cx">                  $body[] = 'BETA TESTING?';
</span><span class="cx">                  $body[] = '=============';
</span><span class="cx">                  $body[] = '';
</span><del>-                       $body[] = 'If you think these failures might be due to a bug in WordPress 3.7, could you report it?';
</del><ins>+                        $body[] = 'This debugging email is sent when you are using a development version of WordPress.';
+                       $body[] = '';
+                       $body[] = 'If you think these failures might be due to a bug in WordPress, could you report it?';
</ins><span class="cx">                   $body[] = ' * Open a thread in the support forums: http://wordpress.org/support/forum/alphabeta';
</span><span class="cx">                  $body[] = " * Or, if you're comfortable writing a bug report: http://core.trac.wordpress.org/";
</span><span class="cx">                  $body[] = '';
</span></span></pre></div>
<a id="trunksrcwpadminincludesupdatecorephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/update-core.php (25840 => 25841)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/update-core.php      2013-10-18 08:41:40 UTC (rev 25840)
+++ trunk/src/wp-admin/includes/update-core.php 2013-10-18 16:29:16 UTC (rev 25841)
</span><span class="lines">@@ -897,6 +897,9 @@
</span><span class="cx">  // If we made it this far:
</span><span class="cx">  do_action( '_core_updated_successfully', $wp_version );
</span><span class="cx"> 
</span><ins>+       // Clear the option that blocks auto updates after failures, now that we've been successful.
+       delete_site_option( 'auto_core_update_failed' );
+
</ins><span class="cx">   return $wp_version;
</span><span class="cx"> }
</span><span class="cx"> 
</span></span></pre></div>
<a id="trunksrcwpadminupdatecorephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/update-core.php (25840 => 25841)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/update-core.php       2013-10-18 08:41:40 UTC (rev 25840)
+++ trunk/src/wp-admin/update-core.php  2013-10-18 16:29:16 UTC (rev 25841)
</span><span class="lines">@@ -150,8 +150,8 @@
</span><span class="cx">                  require_once ABSPATH . 'wp-admin/includes/class-wp-upgrader.php';
</span><span class="cx">                  $upgrader = new WP_Automatic_Updater;
</span><span class="cx">                  $future_minor_update = (object) array(
</span><del>-                               'current'       => $wp_version . '.1-update-core.php',
-                               'version'       => $wp_version . '.1-update-core.php',
</del><ins>+                                'current'       => $wp_version . '.1.next.minor',
+                               'version'       => $wp_version . '.1.next.minor',
</ins><span class="cx">                           'php_version'   => $required_php_version,
</span><span class="cx">                          'mysql_version' => $required_mysql_version,
</span><span class="cx">                  );
</span></span></pre></div>
<a id="trunksrcwpincludesupdatephp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-includes/update.php (25840 => 25841)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-includes/update.php 2013-10-18 08:41:40 UTC (rev 25840)
+++ trunk/src/wp-includes/update.php    2013-10-18 16:29:16 UTC (rev 25841)
</span><span class="lines">@@ -131,7 +131,7 @@
</span><span class="cx">                          $offer[ $offer_key ] = esc_html( $value );
</span><span class="cx">          }
</span><span class="cx">          $offer = (object) array_intersect_key( $offer, array_fill_keys( array( 'response', 'download', 'locale',
</span><del>-                       'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version' ), '' ) );
</del><ins>+                        'packages', 'current', 'version', 'php_version', 'mysql_version', 'new_bundled', 'partial_version', 'notify_email' ), '' ) );
</ins><span class="cx">   }
</span><span class="cx"> 
</span><span class="cx">  $updates = new stdClass();
</span></span></pre>
</div>
</div>

</body>
</html>