<!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>[58128] trunk/src: Upgrade/Install: Automatically roll back to the previous version when an automatic plugin update results in a fatal error on the front end of the site.</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 { white-space: pre-line; 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" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/58128">58128</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/58128","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2024-05-10 11:46:51 +0000 (Fri, 10 May 2024)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Upgrade/Install: Automatically roll back to the previous version when an automatic plugin update results in a fatal error on the front end of the site.

This builds on the temporary backup system introduced in 6.3 to allow automatic updates to benefit from fatal error protection. A loopback request is performed to the home page of the site and the plugin is rolled back to its backed up version if a fatal error is observed.

For debugging and observability during beta, this change includes several calls to `error_log()` during the upgrade and rollback stages. These calls can be removed or placed behind a flag once we're ready for RC1.

Props costdev, johnbillion, mukesh27, afragen, audrasjb, justlevine, kirasong, peterwilsoncc

Fixes <a href="https://core.trac.wordpress.org/ticket/58281">#58281</a></pre>

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

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpadminincludesclasswpautomaticupdaterphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-automatic-updater.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-automatic-updater.php        2024-05-10 09:11:44 UTC (rev 58127)
+++ trunk/src/wp-admin/includes/class-wp-automatic-updater.php  2024-05-10 11:46:51 UTC (rev 58128)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -446,6 +446,29 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        $allow_relaxed_file_ownership = true;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( 'plugin' === $type ) {
+                       $was_active = is_plugin_active( $upgrader_item );
+                       error_log( '    Upgrading plugin ' . var_export( $item->slug, true ) . '...' );
+               }
+
+               if ( 'theme' === $type ) {
+                       error_log( '    Upgrading theme ' . var_export( $item->theme, true ) . '...' );
+               }
+
+               /*
+                * Enable maintenance mode before upgrading the plugin or theme.
+                *
+                * This avoids potential non-fatal errors being detected
+                * while scraping for a fatal error if some files are still
+                * being moved.
+                *
+                * While these checks are intended only for plugins,
+                * maintenance mode is enabled for all upgrade types as any
+                * update could contain an error or warning, which could cause
+                * the scrape to miss a fatal error in the plugin update.
+                */
+               $upgrader->maintenance_mode( true );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Boom, this site's about to get a whole new splash of paint!
</span><span class="cx" style="display: block; padding: 0 10px">                $upgrade_result = $upgrader->upgrade(
</span><span class="cx" style="display: block; padding: 0 10px">                        $upgrader_item,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -460,6 +483,17 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        )
</span><span class="cx" style="display: block; padding: 0 10px">                );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                /*
+                * After WP_Upgrader::upgrade() completes, maintenance mode is disabled.
+                *
+                * Re-enable maintenance mode while attempting to detect fatal errors
+                * and potentially rolling back.
+                *
+                * This avoids errors if the site is visited while fatal errors exist
+                * or while files are still being moved.
+                */
+               $upgrader->maintenance_mode( true );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // If the filesystem is unavailable, false is returned.
</span><span class="cx" style="display: block; padding: 0 10px">                if ( false === $upgrade_result ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        $upgrade_result = new WP_Error( 'fs_unavailable', __( 'Could not access filesystem.' ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -486,6 +520,92 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( 'theme' === $type ) {
+                       error_log( '    Theme ' . var_export( $item->theme, true ) . ' has been upgraded.' );
+               }
+
+               if ( 'plugin' === $type ) {
+                       error_log( '    Plugin ' . var_export( $item->slug, true ) . ' has been upgraded.' );
+                       if ( is_plugin_inactive( $upgrader_item ) ) {
+                               error_log( '    ' . var_export( $upgrader_item, true ) . ' is inactive and will not be checked for fatal errors.' );
+                       }
+
+                       if ( $was_active && ! is_wp_error( $upgrade_result ) ) {
+
+                               /*
+                                * The usual time limit is five minutes. However, as a loopback request
+                                * is about to be performed, increase the time limit to account for this.
+                                */
+                               if ( function_exists( 'set_time_limit' ) ) {
+                                       set_time_limit( 10 * MINUTE_IN_SECONDS );
+                               }
+
+                               /*
+                                * Avoids a race condition when there are 2 sequential plugins that have
+                                * fatal errors. It seems a slight delay is required for the loopback to
+                                * use the updated plugin code in the request. This can cause the second
+                                * plugin's fatal error checking to be inaccurate, and may also affect
+                                * subsequent plugin checks.
+                                */
+                               sleep( 2 );
+
+                               if ( $this->has_fatal_error() ) {
+                                       $upgrade_result = new WP_Error();
+                                       $temp_backup    = array(
+                                               array(
+                                                       'dir'  => 'plugins',
+                                                       'slug' => $item->slug,
+                                                       'src'  => WP_PLUGIN_DIR,
+                                               ),
+                                       );
+
+                                       $backup_restored = $upgrader->restore_temp_backup( $temp_backup );
+                                       if ( is_wp_error( $backup_restored ) ) {
+                                               $upgrade_result->add(
+                                                       'plugin_update_fatal_error_rollback_failed',
+                                                       sprintf(
+                                                               /* translators: %s: The plugin's slug. */
+                                                               __( "The update for '%s' contained a fatal error. The previously installed version could not be restored." ),
+                                                               $item->slug
+                                                       )
+                                               );
+
+                                               $upgrade_result->merge_from( $backup_restored );
+                                       } else {
+                                               $upgrade_result->add(
+                                                       'plugin_update_fatal_error_rollback_successful',
+                                                       sprintf(
+                                                               /* translators: %s: The plugin's slug. */
+                                                               __( "The update for '%s' contained a fatal error. The previously installed version has been restored." ),
+                                                               $item->slug
+                                                       )
+                                               );
+
+                                               $backup_deleted = $upgrader->delete_temp_backup( $temp_backup );
+                                               if ( is_wp_error( $backup_deleted ) ) {
+                                                       $upgrade_result->merge_from( $backup_deleted );
+                                               }
+                                       }
+
+                                       /*
+                                        * Should emails not be working, log the message(s) so that
+                                        * the log file contains context for the fatal error,
+                                        * and whether a rollback was performed.
+                                        *
+                                        * `trigger_error()` is not used as it outputs a stack trace
+                                        * to this location rather than to the fatal error, which will
+                                        * appear above this entry in the log file.
+                                        */
+                                       error_log( '    ' . implode( "\n", $upgrade_result->get_error_messages() ) );
+                               } else {
+                                       error_log( '    The update for ' . var_export( $item->slug, true ) . ' has no fatal errors.' );
+                               }
+                       }
+               }
+
+               // All processes are complete. Allow visitors to browse the site again.
+               $upgrader->maintenance_mode( false );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->update_results[ $type ][] = (object) array(
</span><span class="cx" style="display: block; padding: 0 10px">                        'item'     => $item,
</span><span class="cx" style="display: block; padding: 0 10px">                        'result'   => $upgrade_result,
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -514,6 +634,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return;
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                error_log( 'Automatic updates starting...' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Don't automatically run these things, as we'll handle it ourselves.
</span><span class="cx" style="display: block; padding: 0 10px">                remove_action( 'upgrader_process_complete', array( 'Language_Pack_Upgrader', 'async_upgrade' ), 20 );
</span><span class="cx" style="display: block; padding: 0 10px">                remove_action( 'upgrader_process_complete', 'wp_version_check' );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -524,11 +646,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_update_plugins(); // Check for plugin updates.
</span><span class="cx" style="display: block; padding: 0 10px">                $plugin_updates = get_site_transient( 'update_plugins' );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $plugin_updates && ! empty( $plugin_updates->response ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        error_log( '  Automatic plugin updates starting...' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( $plugin_updates->response as $plugin ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->update( 'plugin', $plugin );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Force refresh of plugin update information.
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_clean_plugins_cache();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       error_log( '  Automatic plugin updates complete.' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Next, those themes we all love.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -535,13 +662,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_update_themes();  // Check for theme updates.
</span><span class="cx" style="display: block; padding: 0 10px">                $theme_updates = get_site_transient( 'update_themes' );
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $theme_updates && ! empty( $theme_updates->response ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        error_log( '  Automatic theme updates starting...' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         foreach ( $theme_updates->response as $theme ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->update( 'theme', (object) $theme );
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="cx" style="display: block; padding: 0 10px">                        // Force refresh of theme update information.
</span><span class="cx" style="display: block; padding: 0 10px">                        wp_clean_themes_cache();
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                       error_log( '  Automatic theme updates complete.' );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                error_log( 'Automatic updates complete.' );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Next, process any core update.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_version_check(); // Check for core updates.
</span><span class="cx" style="display: block; padding: 0 10px">                $core_update = find_core_auto_update();
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1163,7 +1296,7 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        // List failed plugin updates.
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( ! empty( $failed_updates['plugin'] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                $body[] = __( 'These plugins failed to update:' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         $body[] = __( 'The following plugins failed to update. If there was a fatal error in the update, the previously installed version has been restored.' );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                                foreach ( $failed_updates['plugin'] as $item ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $body_message = '';
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1551,4 +1684,84 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                wp_mail( $email['to'], wp_specialchars_decode( $email['subject'] ), $email['body'], $email['headers'] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * Performs a loopback request to check for potential fatal errors.
+        *
+        * Fatal errors cannot be detected unless maintenance mode is enabled.
+        *
+        * @since 6.6.0
+        *
+        * @global int $upgrading The Unix timestamp marking when upgrading WordPress began.
+        *
+        * @return bool Whether a fatal error was detected.
+        */
+       protected function has_fatal_error() {
+               global $upgrading;
+
+               $maintenance_file = ABSPATH . '.maintenance';
+               if ( ! file_exists( $maintenance_file ) ) {
+                       return false;
+               }
+
+               require $maintenance_file;
+               if ( ! is_int( $upgrading ) ) {
+                       return false;
+               }
+
+               $scrape_key   = md5( $upgrading );
+               $scrape_nonce = (string) $upgrading;
+               $transient    = 'scrape_key_' . $scrape_key;
+               set_transient( $transient, $scrape_nonce, 30 );
+
+               $cookies       = wp_unslash( $_COOKIE );
+               $scrape_params = array(
+                       'wp_scrape_key'   => $scrape_key,
+                       'wp_scrape_nonce' => $scrape_nonce,
+               );
+               $headers       = array(
+                       'Cache-Control' => 'no-cache',
+               );
+
+               /** This filter is documented in wp-includes/class-wp-http-streams.php */
+               $sslverify = apply_filters( 'https_local_ssl_verify', false );
+
+               // Include Basic auth in the loopback request.
+               if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
+                       $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
+               }
+
+               // Time to wait for loopback request to finish.
+               $timeout = 50; // 50 seconds.
+
+               error_log( '    Scraping home page...' );
+
+               $needle_start = "###### wp_scraping_result_start:$scrape_key ######";
+               $needle_end   = "###### wp_scraping_result_end:$scrape_key ######";
+               $url          = add_query_arg( $scrape_params, home_url( '/' ) );
+               $response     = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
+
+               if ( is_wp_error( $response ) ) {
+                       error_log( 'Loopback request failed: ' . $response->get_error_message() );
+                       return true;
+               }
+
+               // If this outputs `true` in the log, it means there were no fatal errors detected.
+               error_log( var_export( substr( $response['body'], strpos( $response['body'], '###### wp_scraping_result_start:' ) ), true ) );
+
+               $body                   = wp_remote_retrieve_body( $response );
+               $scrape_result_position = strpos( $body, $needle_start );
+               $result                 = null;
+
+               if ( false !== $scrape_result_position ) {
+                       $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
+                       $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
+                       $result       = json_decode( trim( $error_output ), true );
+               }
+
+               delete_transient( $transient );
+
+               // Only fatal errors will result in a 'type' key.
+               return isset( $result['type'] );
+       }
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre></div>
<a id="trunksrcwpadminincludesclasswpupgraderphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-admin/includes/class-wp-upgrader.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/class-wp-upgrader.php 2024-05-10 09:11:44 UTC (rev 58127)
+++ trunk/src/wp-admin/includes/class-wp-upgrader.php   2024-05-10 11:46:51 UTC (rev 58128)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -901,6 +901,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->skin->set_result( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( is_wp_error( $result ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        // An automatic plugin update will have already performed its rollback.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( ! empty( $options['hook_extra']['temp_backup'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                $this->temp_restores[] = $options['hook_extra']['temp_backup'];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -909,8 +910,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                 * Actions running on `shutdown` are immune to PHP timeouts,
</span><span class="cx" style="display: block; padding: 0 10px">                                 * so in case the failure was due to a PHP timeout,
</span><span class="cx" style="display: block; padding: 0 10px">                                 * it will still be able to properly restore the previous version.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 *
+                                * Zero arguments are accepted as a string can sometimes be passed
+                                * internally during actions, causing an error because
+                                * `WP_Upgrader::restore_temp_backup()` expects an array.
</ins><span class="cx" style="display: block; padding: 0 10px">                                  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                add_action( 'shutdown', array( $this, 'restore_temp_backup' ) );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         add_action( 'shutdown', array( $this, 'restore_temp_backup' ), 10, 0 );
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                        $this->skin->error( $result );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -983,15 +988,25 @@
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><span class="cx" style="display: block; padding: 0 10px">        public function maintenance_mode( $enable = false ) {
</span><span class="cx" style="display: block; padding: 0 10px">                global $wp_filesystem;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               if ( ! $wp_filesystem ) {
+                       require_once ABSPATH . 'wp-admin/includes/file.php';
+                       WP_Filesystem();
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 $file = $wp_filesystem->abspath() . '.maintenance';
</span><span class="cx" style="display: block; padding: 0 10px">                if ( $enable ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->skin->feedback( 'maintenance_start' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! wp_doing_cron() ) {
+                               $this->skin->feedback( 'maintenance_start' );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         // Create maintenance file to signal that we are upgrading.
</span><span class="cx" style="display: block; padding: 0 10px">                        $maintenance_string = '<?php $upgrading = ' . time() . '; ?>';
</span><span class="cx" style="display: block; padding: 0 10px">                        $wp_filesystem->delete( $file );
</span><span class="cx" style="display: block; padding: 0 10px">                        $wp_filesystem->put_contents( $file, $maintenance_string, FS_CHMOD_FILE );
</span><span class="cx" style="display: block; padding: 0 10px">                } elseif ( ! $enable && $wp_filesystem->exists( $file ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $this->skin->feedback( 'maintenance_end' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! wp_doing_cron() ) {
+                               $this->skin->feedback( 'maintenance_end' );
+                       }
</ins><span class="cx" style="display: block; padding: 0 10px">                         $wp_filesystem->delete( $file );
</span><span class="cx" style="display: block; padding: 0 10px">                }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1133,17 +1148,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Restores the plugin or theme from temporary backup.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added the `$temp_backups` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param array[] $temp_backups {
+        *     Optional. An array of temporary backups.
+        *
+        *     @type array ...$0 {
+        *         Information about the backup.
+        *
+        *         @type string $dir  The temporary backup location in the upgrade-temp-backup directory.
+        *         @type string $slug The item's slug.
+        *         @type string $src  The directory where the original is stored. For example, `WP_PLUGIN_DIR`.
+        *     }
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return bool|WP_Error True on success, false on early exit, otherwise WP_Error.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function restore_temp_backup() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function restore_temp_backup( array $temp_backups = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wp_filesystem;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $errors = new WP_Error();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $this->temp_restores as $args ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $temp_backups ) ) {
+                       $temp_backups = $this->temp_restores;
+               }
+
+               foreach ( $temp_backups as $args ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( empty( $args['slug'] ) || empty( $args['src'] ) || empty( $args['dir'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -1186,17 +1217,33 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * Deletes a temporary backup.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 6.3.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 6.6.0 Added the `$temp_backups` parameter.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @global WP_Filesystem_Base $wp_filesystem WordPress filesystem subclass.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param array[] $temp_backups {
+        *     Optional. An array of temporary backups.
+        *
+        *     @type array ...$0 {
+        *         Information about the backup.
+        *
+        *         @type string $dir  The temporary backup location in the upgrade-temp-backup directory.
+        *         @type string $slug The item's slug.
+        *         @type string $src  The directory where the original is stored. For example, `WP_PLUGIN_DIR`.
+        *     }
+        * }
</ins><span class="cx" style="display: block; padding: 0 10px">          * @return bool|WP_Error True on success, false on early exit, otherwise WP_Error.
</span><span class="cx" style="display: block; padding: 0 10px">         */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        public function delete_temp_backup() {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ public function delete_temp_backup( array $temp_backups = array() ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 global $wp_filesystem;
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                $errors = new WP_Error();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                foreach ( $this->temp_backups as $args ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( empty( $temp_backups ) ) {
+                       $temp_backups = $this->temp_backups;
+               }
+
+               foreach ( $temp_backups as $args ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( empty( $args['slug'] ) || empty( $args['dir'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                return false;
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span></span></pre></div>
<a id="trunksrcwpincludesloadphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/load.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/load.php    2024-05-10 09:11:44 UTC (rev 58127)
+++ trunk/src/wp-includes/load.php      2024-05-10 11:46:51 UTC (rev 58128)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -420,6 +420,16 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+        // Don't enable maintenance mode while scraping for fatal errors.
+       if ( is_int( $upgrading ) && isset( $_REQUEST['wp_scrape_key'], $_REQUEST['wp_scrape_nonce'] ) ) {
+               $key   = stripslashes( $_REQUEST['wp_scrape_key'] );
+               $nonce = stripslashes( $_REQUEST['wp_scrape_nonce'] );
+
+               if ( md5( $upgrading ) === $key && (int) $nonce === $upgrading ) {
+                       return false;
+               }
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filters whether to enable maintenance mode.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span></span></pre>
</div>
</div>

</body>
</html>