<!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>[25863] trunk/src/wp-admin/includes/class-wp-upgrader.php: Updates: Saner stats and rollback handling.</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/25863">25863</a></dd>
<dt>Author</dt> <dd>nacin</dd>
<dt>Date</dt> <dd>2013-10-22 09:48:36 +0000 (Tue, 22 Oct 2013)</dd>
</dl>

<h3>Log Message</h3>
<pre>Updates: Saner stats and rollback handling. WP.org now collects basic stats on non-auto updates as they have been hugely informative.

props dd32 for initial patch.
fixes <a href="http://core.trac.wordpress.org/ticket/25657">#25657</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpadminincludesclasswpupgraderphp">trunk/src/wp-admin/includes/class-wp-upgrader.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4>Modified: trunk/src/wp-admin/includes/class-wp-upgrader.php (25862 => 25863)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/src/wp-admin/includes/class-wp-upgrader.php        2013-10-22 05:47:15 UTC (rev 25862)
+++ trunk/src/wp-admin/includes/class-wp-upgrader.php   2013-10-22 09:48:36 UTC (rev 25863)
</span><span class="lines">@@ -1316,6 +1316,8 @@
</span><span class="cx">  function upgrade( $current, $args = array() ) {
</span><span class="cx">          global $wp_filesystem, $wp_version;
</span><span class="cx"> 
</span><ins>+               $start_time = time();
+
</ins><span class="cx">           $defaults = array(
</span><span class="cx">                  'pre_check_md5'    => true,
</span><span class="cx">                  'attempt_rollback' => false,
</span><span class="lines">@@ -1402,12 +1404,47 @@
</span><span class="cx"> 
</span><span class="cx">                          $rollback_result = $this->upgrade( $current, array_merge( $parsed_args, array( 'do_rollback' => true ) ) );
</span><span class="cx"> 
</span><del>-                               $result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], array( 'rollback' => $rollback_result, 'update' => $result ) );
</del><ins>+                                $original_result = $result;
+                               $result = new WP_Error( 'rollback_was_required', $this->strings['rollback_was_required'], (object) array( 'update' => $original_result, 'rollback' => $rollback_result ) );
</ins><span class="cx">                   }
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          do_action( 'upgrader_process_complete', $this, array( 'action' => 'update', 'type' => 'core' ) );
</span><span class="cx"> 
</span><ins>+               // Clear the current updates
+               delete_site_transient( 'update_core' );
+
+               if ( ! $parsed_args['do_rollback'] ) {
+                       $stats = array(
+                               'update_type'      => $current->response,
+                               'success'          => true,
+                               'fs_method'        => $wp_filesystem->method,
+                               'fs_method_forced' => defined( 'FS_METHOD' ) || has_filter( 'filesystem_method' ),
+                               'time_taken'       => time() - $start_time,
+                               'attempted'        => $current->version,
+                       );
+
+                       if ( is_wp_error( $result ) ) {
+                               $stats['success'] = false;
+                               // Did a rollback occur?
+                               if ( ! empty( $try_rollback ) ) {
+                                       $stats['error_code'] = $original_result->get_error_code();
+                                       $stats['error_data'] = $original_result->get_error_data();
+                                       // Was the rollback successful? If not, collect its error too.
+                                       $stats['rollback'] = ! is_wp_error( $rollback_result );
+                                       if ( is_wp_error( $rollback_result ) ) {
+                                               $stats['rollback_code'] = $rollback_result->get_error_code();
+                                               $stats['rollback_data'] = $rollback_result->get_error_data();
+                                       }
+                               } else {
+                                       $stats['error_code'] = $result->get_error_code();
+                                       $stats['error_data'] = $result->get_error_data();
+                               }
+                       }
+
+                       wp_version_check( $stats );
+               }
+
</ins><span class="cx">           return $result;
</span><span class="cx">  }
</span><span class="cx"> 
</span><span class="lines">@@ -1906,34 +1943,13 @@
</span><span class="cx"> 
</span><span class="cx">          // Next, Process any core update
</span><span class="cx">          wp_version_check(); // Check for Core updates
</span><del>-               $extra_update_stats = array();
</del><span class="cx">           $core_update = find_core_auto_update();
</span><span class="cx"> 
</span><del>-               if ( $core_update ) {
-                       $start_time = time();
</del><ins>+                if ( $core_update )
+                       $this->update( 'core', $core_update );
</ins><span class="cx"> 
</span><del>-                       $core_update_result = $this->update( 'core', $core_update );
-                       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['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() ) {
-                               $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['error_data'] = is_wp_error( $rollback_data['update'] ) ? $rollback_data['update']->get_error_data() : '';
-                               $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['fs_method'] = $GLOBALS['wp_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['attempted'] = $core_update->version;
-               }
-
-               // Cleanup, and check for any pending translations
-               wp_version_check( $extra_update_stats );  // check for Core updates
</del><ins>+                // Clean up, and check for any pending translations
+               // (Core_Upgrader checks for core updates)
</ins><span class="cx">           wp_update_themes();  // Check for Theme updates
</span><span class="cx">          wp_update_plugins(); // Check for Plugin updates
</span><span class="cx"> 
</span><span class="lines">@@ -1999,23 +2015,28 @@
</span><span class="cx">          $critical = false;
</span><span class="cx">          if ( $error_code === 'disk_full' || false !== strpos( $error_code, '__copy_dir' ) ) {
</span><span class="cx">                  $critical = true;
</span><del>-               } elseif ( $error_code === 'rollback_was_required' ) {
-                       $error_data = $result->get_error_data();
-                       if ( is_wp_error( $error_data['rollback'] ) )
-                               $critical = true;
</del><ins>+                } elseif ( $error_code === 'rollback_was_required' && is_wp_error( $result->get_error_data()->rollback ) ) {
+                       // A rollback is only critical if it failed too.
+                       $critical = true;
+                       $rollback_result = $result->get_error_data()->rollback;
</ins><span class="cx">           } elseif ( false !== strpos( $error_code, 'do_rollback' ) ) {
</span><span class="cx">                  $critical = true;
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="cx">          if ( $critical ) {
</span><del>-                       update_site_option( 'auto_core_update_failed', array(
</del><ins>+                        $critical_data = array(
</ins><span class="cx">                           'attempted'  => $core_update->current,
</span><span class="cx">                          'current'    => $wp_version,
</span><span class="cx">                          'error_code' => $error_code,
</span><span class="cx">                          'error_data' => $result->get_error_data(),
</span><span class="cx">                          'timestamp'  => time(),
</span><span class="cx">                          'critical'   => true,
</span><del>-                       ) );
</del><ins>+                        );
+                       if ( isset( $rollback_result ) ) {
+                               $critical_data['rollback_code'] = $rollback_result->get_error_code();
+                               $critical_data['rollback_data'] = $rollback_result->get_error_data();
+                       }
+                       update_site_option( 'auto_core_update_failed', $critical_data );
</ins><span class="cx">                   $this->send_email( 'critical', $core_update, $result );
</span><span class="cx">                  return;
</span><span class="cx">          }
</span><span class="lines">@@ -2163,11 +2184,27 @@
</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\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() );
</del><ins>+
+                       // If we had a rollback and we're still critical, then the rollback failed too.
+                       // Loop through all errors (the main WP_Error, the update result, the rollback result) for code, data, etc.
+                       if ( 'rollback_was_required' == $result->get_error_code() )
+                               $errors = array( $result, $result->get_error_data()->update, $result->get_error_data()->rollback );
+                       else
+                               $errors = array( $result );
+
+                       foreach ( $errors as $error ) {
+                               if ( ! is_wp_error( $error ) )
+                                       continue;
+                               $error_code = $error->get_error_code();
+                               $body .= "\n\n" . sprintf( __( "Error code: %s" ), $error_code );
+                               if ( 'rollback_was_required' == $error_code )
+                                       continue;
+                               if ( $error->get_error_message() )
+                                       $body .= "\n" . $error->get_error_message();
+                               $error_data = $error->get_error_data();
+                               if ( $error_data )
+                                       $body .= "\n" . implode( ', ', (array) $error_data );
+                       }
</ins><span class="cx">                   $body .= "\n";
</span><span class="cx">          }
</span><span class="cx"> 
</span><span class="lines">@@ -2278,9 +2315,17 @@
</span><span class="cx">                          foreach ( $update->messages as $message )
</span><span class="cx">                                  $body[] = "  " . html_entity_decode( str_replace( '&#8230;', '...', $message ) );
</span><span class="cx">                          if ( is_wp_error( $update->result ) ) {
</span><del>-                                       $body[] = '  Error: [' . $update->result->get_error_code() . '] ' . $update->result->get_error_message();
-                                       if ( $update->result->get_error_data() )
-                                               $body[] = '         ' . implode( ', ', (array) $update->result->get_error_data() );
</del><ins>+                                        $results = array( 'update' => $update->result );
+                                       // If we rolled back, we want to know an error that occurred then too.
+                                       if ( 'rollback_was_required' === $update->result->get_error_code() )
+                                               $results = (array) $update->result->get_error_data();
+                                       foreach ( $results as $result_type => $result ) {
+                                               if ( ! is_wp_error( $result ) )
+                                                       continue;
+                                               $body[] = '  ' . ( 'rollback' === $result_type ? 'Rollback ' : '' ) . 'Error: [' . $result->get_error_code() . '] ' . $result->get_error_message();
+                                               if ( $result->get_error_data() )
+                                                       $body[] = '         ' . implode( ', ', (array) $result->get_error_data() );
+                                       }
</ins><span class="cx">                           }
</span><span class="cx">                          $body[] = '';
</span><span class="cx">                  }
</span></span></pre>
</div>
</div>

</body>
</html>