<!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>[46382] trunk/src: Upload: Fix the method used to create image sub-sizes when uploading fails with a PHP fatal error.</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/46382">46382</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/46382","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>azaozz</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2019-10-03 21:00:10 +0000 (Thu, 03 Oct 2019)</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'>Upload: Fix the method used to create image sub-sizes when uploading fails with a PHP fatal error. Use a custom header to send the new attachment post ID even in HTTP 500 responses instead of an upload reference sent by the client. Also add another cap check and remove the action when deleting an attachment post during a failed upload cleanup.

Props timothyblynjacobs, clorith, azaozz.
Fixes <a href="https://core.trac.wordpress.org/ticket/48200">#48200</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcjs_enqueuesvendorpluploadhandlersjs">trunk/src/js/_enqueues/vendor/plupload/handlers.js</a></li>
<li><a href="#trunksrcjs_enqueuesvendorpluploadwppluploadjs">trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js</a></li>
<li><a href="#trunksrcwpadminincludesajaxactionsphp">trunk/src/wp-admin/includes/ajax-actions.php</a></li>
<li><a href="#trunksrcwpadminincludesfilephp">trunk/src/wp-admin/includes/file.php</a></li>
<li><a href="#trunksrcwpadminincludesimagephp">trunk/src/wp-admin/includes/image.php</a></li>
<li><a href="#trunksrcwpadminincludesmediaphp">trunk/src/wp-admin/includes/media.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcjs_enqueuesvendorpluploadhandlersjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/js/_enqueues/vendor/plupload/handlers.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/js/_enqueues/vendor/plupload/handlers.js        2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/js/_enqueues/vendor/plupload/handlers.js  2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -431,12 +431,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">        tryAgain = function( up, error ) {
</span><span class="cx" style="display: block; padding: 0 10px">                var file = error.file;
</span><span class="cx" style="display: block; padding: 0 10px">                var times;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                var id;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( ! file || ! file.id ) {
-                       wpQueueError( error.message || pluploadL10n.default_error );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( ! error || ! error.responseHeaders ) {
+                       wpQueueError( pluploadL10n.http_error_image );
</ins><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">+                id = error.responseHeaders.match( /x-wp-upload-attachment-id:\s*(\d+)/i );
+
+               if ( id && id[1] ) {
+                       id = id[1];
+               } else {
+                       wpQueueError( pluploadL10n.http_error_image );
+                       return;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 times = tryAgainCount[ file.id ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                if ( times && times > 4 ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -449,8 +459,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                dataType: 'json',
</span><span class="cx" style="display: block; padding: 0 10px">                                data: {
</span><span class="cx" style="display: block; padding: 0 10px">                                        action: 'media-create-image-subsizes',
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
-                                       _wp_temp_upload_ref: file.id,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 _wpnonce: wpUploaderInit.multipart_params._wpnonce,
+                                       attachment_id: id,
</ins><span class="cx" style="display: block; padding: 0 10px">                                         _wp_upload_failed_cleanup: true,
</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">@@ -478,8 +488,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        data: {
</span><span class="cx" style="display: block; padding: 0 10px">                                action: 'media-create-image-subsizes',
</span><span class="cx" style="display: block; padding: 0 10px">                                _wpnonce: wpUploaderInit.multipart_params._wpnonce,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                _wp_temp_upload_ref: file.id,
-                               _legasy_support: 'true',
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                         attachment_id: id,
+                               _legacy_support: 'true',
</ins><span class="cx" style="display: block; padding: 0 10px">                         }
</span><span class="cx" style="display: block; padding: 0 10px">                }).done( function( response ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var message;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -589,21 +599,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">                uploader.bind( 'UploadComplete', function() {
</span><span class="cx" style="display: block; padding: 0 10px">                        uploadComplete();
</span><span class="cx" style="display: block; padding: 0 10px">                });
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               /**
-                * When uploading images add a file reference used to retrieve the attachment_id
-                * if the uploading fails due to a server timeout of out of memoty (HTTP 500) error.
-                *
-                * @param {plupload.Uploader} up   Uploader instance.
-                * @param {plupload.File}     file File for uploading.
-                */
-               uploader.bind( 'BeforeUpload', function( up, file ) {
-                       if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
-                               up.settings.multipart_params._wp_temp_upload_ref = file.id;
-                       } else {
-                               up.settings.multipart_params._wp_temp_upload_ref = '';
-                       }
-               } );
</del><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">        if ( typeof( wpUploaderInit ) == 'object' ) {
</span></span></pre></div>
<a id="trunksrcjs_enqueuesvendorpluploadwppluploadjs"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js     2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js       2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -119,12 +119,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                tryAgain = function( message, data, file ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        var times;
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                        var id;
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        if ( ! file || ! file.id ) {
-                               error( pluploadL10n.upload_failed, data, file, 'no-retry' );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 if ( ! data || ! data.responseHeaders ) {
+                               error( pluploadL10n.http_error_image, data, file, 'no-retry' );
</ins><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">+                        id = data.responseHeaders.match( /x-wp-upload-attachment-id:\s*(\d+)/i );
+
+                       if ( id && id[1] ) {
+                               id = id[1];
+                       } else {
+                               error( pluploadL10n.http_error_image, data, file, 'no-retry' );
+                               return;
+                       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                         times = tryAgainCount[ file.id ];
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                        if ( times && times > 4 ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -138,7 +148,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                        data: {
</span><span class="cx" style="display: block; padding: 0 10px">                                                action: 'media-create-image-subsizes',
</span><span class="cx" style="display: block; padding: 0 10px">                                                _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                                _wp_temp_upload_ref: file.id,
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                         attachment_id: id,
</ins><span class="cx" style="display: block; padding: 0 10px">                                                 _wp_upload_failed_cleanup: true,
</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">@@ -161,7 +171,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                data: {
</span><span class="cx" style="display: block; padding: 0 10px">                                        action: 'media-create-image-subsizes',
</span><span class="cx" style="display: block; padding: 0 10px">                                        _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                        _wp_temp_upload_ref: file.id, // Used to find the new attachment_id.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                                 attachment_id: id,
</ins><span class="cx" style="display: block; padding: 0 10px">                                 }
</span><span class="cx" style="display: block; padding: 0 10px">                        }).done( function( response ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( response.success ) {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -316,21 +326,6 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                 * When uploading images add a reference used to retrieve the attachment_id.
-                * Used if the uploading fails due to a server timeout of out of memoty error (HTTP 500).
-                *
-                * @param {plupload.Uploader} up   Uploader instance.
-                * @param {plupload.File}     file File for uploading.
-                */
-               this.uploader.bind( 'BeforeUpload', function( up, file ) {
-                       if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
-                               up.settings.multipart_params._wp_temp_upload_ref = file.id;
-                       } else {
-                               up.settings.multipart_params._wp_temp_upload_ref = '';
-                       }
-               } );
-
-               /**
</del><span class="cx" style="display: block; padding: 0 10px">                  * After files were filtered and added to the queue, create a model for each.
</span><span class="cx" style="display: block; padding: 0 10px">                 *
</span><span class="cx" style="display: block; padding: 0 10px">                 * @param {plupload.Uploader} up    Uploader instance.
</span></span></pre></div>
<a id="trunksrcwpadminincludesajaxactionsphp"></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/ajax-actions.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/ajax-actions.php      2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/wp-admin/includes/ajax-actions.php        2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2422,39 +2422,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">                wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! empty( $_POST['_wp_temp_upload_ref'] ) ) {
-               // Uploading of images usually fails while creating the sub-sizes, either because of a timeout or out of memory.
-               // At this point the file has been uploaded and an attachment post created, but because of the PHP fatal error
-               // the cliend doesn't know the attachment ID yet.
-               // To be able to find the new attachment_id in these cases we temporarily store an upload reference sent by the client
-               // in the original upload request. It is used to save a transient with the attachment_id as value.
-               // That reference currently is Plupload's `file.id` but can be any sufficiently random alpha-numeric string.
-               $attachment_id = _wp_get_upload_ref_attachment_id( $_POST['_wp_temp_upload_ref'] );
-       } else {
-               wp_send_json_error( array( 'message' => __( 'Invalid file reference.' ) ) );
-       }
-
-       if ( empty( $attachment_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( empty( $_POST['attachment_id'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
</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">+        $attachment_id = (int) $_POST['attachment_id'];
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                // Upload failed. Cleanup.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                if ( wp_attachment_is_image( $attachment_id ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                         $attachment = get_post( $attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        // Posted at most 10 min ago.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 // Created at most 10 min ago.
</ins><span class="cx" style="display: block; padding: 0 10px">                         if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                                /**
-                                * Runs when an image upload fails during the post-processing phase,
-                                * and the newly created attachment post is about to be deleted.
-                                *
-                                * @since 5.3.0
-                                *
-                                * @param int $attachment_id The attachment post ID.
-                                */
-                               do_action( 'wp_upload_failed_cleanup', $attachment_id );
-
</del><span class="cx" style="display: block; padding: 0 10px">                                 wp_delete_attachment( $attachment_id, true );
</span><span class="cx" style="display: block; padding: 0 10px">                                wp_send_json_success();
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2465,7 +2445,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">        // The js that handles the response would need to also handle HTTP 500 errors.
</span><span class="cx" style="display: block; padding: 0 10px">        wp_update_image_subsizes( $attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( ! empty( $_POST['_legasy_support'] ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ if ( ! empty( $_POST['_legacy_support'] ) ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 // The old (inline) uploader. Only needs the attachment_id.
</span><span class="cx" style="display: block; padding: 0 10px">                $response = array( 'id' => $attachment_id );
</span><span class="cx" style="display: block; padding: 0 10px">        } else {
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -2478,8 +2458,6 @@
</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">        // At this point the image has been uploaded successfully.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
-
</del><span class="cx" style="display: block; padding: 0 10px">         wp_send_json_success( $response );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span></span></pre></div>
<a id="trunksrcwpadminincludesfilephp"></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/file.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/file.php      2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/wp-admin/includes/file.php        2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -982,64 +982,6 @@
</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><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Temporarily stores the client upload reference in a transient.
- *
- * @since 5.3.0
- * @access private
- *
- * @param string $upload_ref    The upload reference sent by the client.
- * @param int    $attachment_id Attachment post ID.
- * @return bool Whether the transient was set.
- */
-function _wp_set_upload_ref( $upload_ref, $attachment_id ) {
-       $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
-
-       if ( ! empty( $upload_ref ) ) {
-               return set_transient( '_wp_temp_image_ref:' . $upload_ref, $attachment_id, HOUR_IN_SECONDS );
-       }
-
-       return false;
-}
-
-/**
- * Get attachment post ID from an upload reference.
- *
- * @since 5.3.0
- * @access private
- *
- * @param string $upload_ref    The upload reference sent by the client.
- * @return int The attachemtn post ID. Zero if the upload reference has expired or doesn't exist.
- */
-function _wp_get_upload_ref_attachment_id( $upload_ref ) {
-       $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
-
-       if ( ! empty( $upload_ref ) ) {
-               return (int) get_transient( '_wp_temp_image_ref:' . $upload_ref );
-       }
-
-       return 0;
-}
-
-/**
- * Remove the transient that stores a temporary upload reference.
- *
- * @since 5.3.0
- * @access private
- *
- * @param string $upload_ref    The upload reference sent by the client.
- * @return bool Whether the transient was removed.
- */
-function _wp_clear_upload_ref( $upload_ref ) {
-       $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
-
-       if ( ! empty( $upload_ref ) ) {
-               return delete_transient( '_wp_temp_image_ref:' . $upload_ref );
-       }
-
-       return false;
-}
-
-/**
</del><span class="cx" style="display: block; padding: 0 10px">  * Downloads a URL to a local temporary file using the WordPress HTTP API.
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * Please note that the calling function must unlink() the file.
</span></span></pre></div>
<a id="trunksrcwpadminincludesimagephp"></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/image.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/image.php     2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/wp-admin/includes/image.php       2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -251,6 +251,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $image_meta;
</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">+                // Set a custom header with the attachment_id.
+               // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+               if ( ! headers_sent() ) {
+                       header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Resize the image
</span><span class="cx" style="display: block; padding: 0 10px">                $resized = $editor->resize( $threshold, $threshold );
</span><span class="cx" style="display: block; padding: 0 10px">                $rotated = null;
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -273,6 +279,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $image_meta['image_meta']['orientation'] = 1;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               wp_update_attachment_metadata( $attachment_id, $image_meta );
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                // TODO: handle errors.
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -289,6 +297,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                        return $image_meta;
</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 ( ! headers_sent() ) {
+                       header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 // Rotate the image
</span><span class="cx" style="display: block; padding: 0 10px">                $rotated = $editor->maybe_exif_rotate();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -303,6 +315,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                                if ( ! empty( $image_meta['image_meta']['orientation'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                                        $image_meta['image_meta']['orientation'] = 1;
</span><span class="cx" style="display: block; padding: 0 10px">                                }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+                               wp_update_attachment_metadata( $attachment_id, $image_meta );
</ins><span class="cx" style="display: block; padding: 0 10px">                         } else {
</span><span class="cx" style="display: block; padding: 0 10px">                                // TODO: handle errors.
</span><span class="cx" style="display: block; padding: 0 10px">                        }
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -386,6 +400,12 @@
</span><span class="cx" style="display: block; padding: 0 10px">                return $image_meta;
</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">+        // Set a custom header with the attachment_id.
+       // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error.
+       if ( ! headers_sent() ) {
+               header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px">         // If stored EXIF data exists, rotate the source image before creating sub-sizes.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! empty( $image_meta['image_meta'] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $rotated = $editor->maybe_exif_rotate();
</span></span></pre></div>
<a id="trunksrcwpadminincludesmediaphp"></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/media.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-admin/includes/media.php     2019-10-03 20:38:08 UTC (rev 46381)
+++ trunk/src/wp-admin/includes/media.php       2019-10-03 21:00:10 UTC (rev 46382)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -314,7 +314,6 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $title   = sanitize_text_field( $name );
</span><span class="cx" style="display: block; padding: 0 10px">        $content = '';
</span><span class="cx" style="display: block; padding: 0 10px">        $excerpt = '';
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $_ref    = false;
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( preg_match( '#^audio#', $type ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                $meta = wp_read_audio_metadata( $file );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -409,20 +408,9 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_wp_error( $attachment_id ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // If an image, keep the upload reference until all image sub-sizes are created.
-               if ( ! empty( $_POST['_wp_temp_upload_ref'] ) && wp_attachment_is_image( $attachment_id ) ) {
-                       $_ref = _wp_set_upload_ref( $_POST['_wp_temp_upload_ref'], $attachment_id );
-               }
-
</del><span class="cx" style="display: block; padding: 0 10px">                 // The image sub-sizes are created during wp_generate_attachment_metadata().
</span><span class="cx" style="display: block; padding: 0 10px">                // This is generally slow and may cause timeouts or out of memory errors.
</span><span class="cx" style="display: block; padding: 0 10px">                wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-
-               // At this point the image is uploaded successfully even if there were specific errors or some sub-sizes were not created.
-               // The transient is not needed any more.
-               if ( $_ref ) {
-                       _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
-               }
</del><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">        return $attachment_id;
</span></span></pre>
</div>
</div>

</body>
</html>