Ticket #48200: 48200.diff
File 48200.diff, 12.0 KB (added by , 4 years ago) |
---|
-
src/js/_enqueues/vendor/plupload/handlers.js
431 431 tryAgain = function( up, error ) { 432 432 var file = error.file; 433 433 var times; 434 var id; 434 435 435 if ( ! file || ! file.id) {436 wpQueueError( error.message || pluploadL10n.default_error);436 if ( ! error || ! error.responseHeaders ) { 437 wpQueueError( pluploadL10n.http_error_image ); 437 438 return; 438 439 } 439 440 441 id = error.responseHeaders.match( /x-wp-upload-attachment-id:\s*(\d+)/i ); 442 443 if ( id && id[1] ) { 444 id = id[1]; 445 } else { 446 wpQueueError( pluploadL10n.http_error_image ); 447 return; 448 } 449 440 450 times = tryAgainCount[ file.id ]; 441 451 442 452 if ( times && times > 4 ) { … … 449 459 dataType: 'json', 450 460 data: { 451 461 action: 'media-create-image-subsizes', 452 _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,453 _wp_temp_upload_ref: file.id,462 _wpnonce: wpUploaderInit.multipart_params._wpnonce, 463 attachment_id: id, 454 464 _wp_upload_failed_cleanup: true, 455 465 } 456 466 }); … … 478 488 data: { 479 489 action: 'media-create-image-subsizes', 480 490 _wpnonce: wpUploaderInit.multipart_params._wpnonce, 481 _wp_temp_upload_ref: file.id,482 _lega sy_support: 'true',491 attachment_id: id, 492 _legacy_support: 'true', 483 493 } 484 494 }).done( function( response ) { 485 495 var message; … … 589 599 uploader.bind( 'UploadComplete', function() { 590 600 uploadComplete(); 591 601 }); 592 593 /**594 * When uploading images add a file reference used to retrieve the attachment_id595 * if the uploading fails due to a server timeout of out of memoty (HTTP 500) error.596 *597 * @param {plupload.Uploader} up Uploader instance.598 * @param {plupload.File} file File for uploading.599 */600 uploader.bind( 'BeforeUpload', function( up, file ) {601 if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {602 up.settings.multipart_params._wp_temp_upload_ref = file.id;603 } else {604 up.settings.multipart_params._wp_temp_upload_ref = '';605 }606 } );607 602 }; 608 603 609 604 if ( typeof( wpUploaderInit ) == 'object' ) { -
src/js/_enqueues/vendor/plupload/wp-plupload.js
119 119 */ 120 120 tryAgain = function( message, data, file ) { 121 121 var times; 122 var id; 122 123 123 if ( ! file || ! file.id) {124 error( pluploadL10n. upload_failed, data, file, 'no-retry' );124 if ( ! data || ! data.responseHeaders ) { 125 error( pluploadL10n.http_error_image, data, file, 'no-retry' ); 125 126 return; 126 127 } 127 128 129 id = data.responseHeaders.match( /x-wp-upload-attachment-id:\s*(\d+)/i ); 130 131 if ( id && id[1] ) { 132 id = id[1]; 133 } else { 134 error( pluploadL10n.http_error_image, data, file, 'no-retry' ); 135 return; 136 } 137 128 138 times = tryAgainCount[ file.id ]; 129 139 130 140 if ( times && times > 4 ) { … … 138 148 data: { 139 149 action: 'media-create-image-subsizes', 140 150 _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce, 141 _wp_temp_upload_ref: file.id,151 attachment_id: id, 142 152 _wp_upload_failed_cleanup: true, 143 153 } 144 154 }); … … 161 171 data: { 162 172 action: 'media-create-image-subsizes', 163 173 _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce, 164 _wp_temp_upload_ref: file.id, // Used to find the new attachment_id.174 attachment_id: id, 165 175 } 166 176 }).done( function( response ) { 167 177 if ( response.success ) { … … 316 326 } 317 327 318 328 /** 319 * When uploading images add a reference used to retrieve the attachment_id.320 * Used if the uploading fails due to a server timeout of out of memoty error (HTTP 500).321 *322 * @param {plupload.Uploader} up Uploader instance.323 * @param {plupload.File} file File for uploading.324 */325 this.uploader.bind( 'BeforeUpload', function( up, file ) {326 if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {327 up.settings.multipart_params._wp_temp_upload_ref = file.id;328 } else {329 up.settings.multipart_params._wp_temp_upload_ref = '';330 }331 } );332 333 /**334 329 * After files were filtered and added to the queue, create a model for each. 335 330 * 336 331 * @param {plupload.Uploader} up Uploader instance. -
src/wp-admin/includes/ajax-actions.php
2422 2422 wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) ); 2423 2423 } 2424 2424 2425 if ( ! empty( $_POST['_wp_temp_upload_ref'] ) ) { 2426 // Uploading of images usually fails while creating the sub-sizes, either because of a timeout or out of memory. 2427 // At this point the file has been uploaded and an attachment post created, but because of the PHP fatal error 2428 // the cliend doesn't know the attachment ID yet. 2429 // To be able to find the new attachment_id in these cases we temporarily store an upload reference sent by the client 2430 // in the original upload request. It is used to save a transient with the attachment_id as value. 2431 // That reference currently is Plupload's `file.id` but can be any sufficiently random alpha-numeric string. 2432 $attachment_id = _wp_get_upload_ref_attachment_id( $_POST['_wp_temp_upload_ref'] ); 2433 } else { 2434 wp_send_json_error( array( 'message' => __( 'Invalid file reference.' ) ) ); 2435 } 2436 2437 if ( empty( $attachment_id ) ) { 2425 if ( empty( $_POST['attachment_id'] ) ) { 2438 2426 wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) ); 2439 2427 } 2440 2428 2429 $attachment_id = (int) $_POST['attachment_id']; 2430 2441 2431 if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) { 2442 2432 // Upload failed. Cleanup. 2443 if ( wp_attachment_is_image( $attachment_id ) ) {2433 if ( wp_attachment_is_image( $attachment_id ) && current_user_can( 'delete_post', $attachment_id ) ) { 2444 2434 $attachment = get_post( $attachment_id ); 2445 2435 2446 // Posted at most 10 min ago.2436 // Created at most 10 min ago. 2447 2437 if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) { 2448 2438 /** 2449 2439 * Runs when an image upload fails during the post-processing phase, … … 2465 2455 // The js that handles the response would need to also handle HTTP 500 errors. 2466 2456 wp_update_image_subsizes( $attachment_id ); 2467 2457 2468 if ( ! empty( $_POST['_lega sy_support'] ) ) {2458 if ( ! empty( $_POST['_legacy_support'] ) ) { 2469 2459 // The old (inline) uploader. Only needs the attachment_id. 2470 2460 $response = array( 'id' => $attachment_id ); 2471 2461 } else { … … 2478 2468 } 2479 2469 2480 2470 // At this point the image has been uploaded successfully. 2481 _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );2482 2483 2471 wp_send_json_success( $response ); 2484 2472 } 2485 2473 -
src/wp-admin/includes/file.php
982 982 } 983 983 984 984 /** 985 * Temporarily stores the client upload reference in a transient.986 *987 * @since 5.3.0988 * @access private989 *990 * @param string $upload_ref The upload reference sent by the client.991 * @param int $attachment_id Attachment post ID.992 * @return bool Whether the transient was set.993 */994 function _wp_set_upload_ref( $upload_ref, $attachment_id ) {995 $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );996 997 if ( ! empty( $upload_ref ) ) {998 return set_transient( '_wp_temp_image_ref:' . $upload_ref, $attachment_id, HOUR_IN_SECONDS );999 }1000 1001 return false;1002 }1003 1004 /**1005 * Get attachment post ID from an upload reference.1006 *1007 * @since 5.3.01008 * @access private1009 *1010 * @param string $upload_ref The upload reference sent by the client.1011 * @return int The attachemtn post ID. Zero if the upload reference has expired or doesn't exist.1012 */1013 function _wp_get_upload_ref_attachment_id( $upload_ref ) {1014 $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );1015 1016 if ( ! empty( $upload_ref ) ) {1017 return (int) get_transient( '_wp_temp_image_ref:' . $upload_ref );1018 }1019 1020 return 0;1021 }1022 1023 /**1024 * Remove the transient that stores a temporary upload reference.1025 *1026 * @since 5.3.01027 * @access private1028 *1029 * @param string $upload_ref The upload reference sent by the client.1030 * @return bool Whether the transient was removed.1031 */1032 function _wp_clear_upload_ref( $upload_ref ) {1033 $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );1034 1035 if ( ! empty( $upload_ref ) ) {1036 return delete_transient( '_wp_temp_image_ref:' . $upload_ref );1037 }1038 1039 return false;1040 }1041 1042 /**1043 985 * Downloads a URL to a local temporary file using the WordPress HTTP API. 1044 986 * 1045 987 * Please note that the calling function must unlink() the file. -
src/wp-admin/includes/image.php
251 251 return $image_meta; 252 252 } 253 253 254 // Set a custom header with the attachment_id. 255 // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 256 if ( ! headers_sent() ) { 257 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 258 } 259 254 260 // Resize the image 255 261 $resized = $editor->resize( $threshold, $threshold ); 256 262 $rotated = null; … … 273 279 if ( true === $rotated && ! empty( $image_meta['image_meta']['orientation'] ) ) { 274 280 $image_meta['image_meta']['orientation'] = 1; 275 281 } 282 283 wp_update_attachment_metadata( $attachment_id, $image_meta ); 276 284 } else { 277 285 // TODO: handle errors. 278 286 } … … 289 297 return $image_meta; 290 298 } 291 299 300 if ( ! headers_sent() ) { 301 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 302 } 303 292 304 // Rotate the image 293 305 $rotated = $editor->maybe_exif_rotate(); 294 306 … … 303 315 if ( ! empty( $image_meta['image_meta']['orientation'] ) ) { 304 316 $image_meta['image_meta']['orientation'] = 1; 305 317 } 318 319 wp_update_attachment_metadata( $attachment_id, $image_meta ); 306 320 } else { 307 321 // TODO: handle errors. 308 322 } … … 386 400 return $image_meta; 387 401 } 388 402 403 // Set a custom header with the attachment_id. 404 // Used by the browser/client to resume creating image sub-sizes after a PHP fatal error. 405 if ( ! headers_sent() ) { 406 header( 'X-WP-Upload-Attachment-ID: ' . $attachment_id ); 407 } 408 389 409 // If stored EXIF data exists, rotate the source image before creating sub-sizes. 390 410 if ( ! empty( $image_meta['image_meta'] ) ) { 391 411 $rotated = $editor->maybe_exif_rotate(); -
src/wp-admin/includes/media.php
314 314 $title = sanitize_text_field( $name ); 315 315 $content = ''; 316 316 $excerpt = ''; 317 $_ref = false;318 317 319 318 if ( preg_match( '#^audio#', $type ) ) { 320 319 $meta = wp_read_audio_metadata( $file ); … … 409 408 $attachment_id = wp_insert_attachment( $attachment, $file, $post_id, true ); 410 409 411 410 if ( ! is_wp_error( $attachment_id ) ) { 412 // If an image, keep the upload reference until all image sub-sizes are created.413 if ( ! empty( $_POST['_wp_temp_upload_ref'] ) && wp_attachment_is_image( $attachment_id ) ) {414 $_ref = _wp_set_upload_ref( $_POST['_wp_temp_upload_ref'], $attachment_id );415 }416 417 411 // The image sub-sizes are created during wp_generate_attachment_metadata(). 418 412 // This is generally slow and may cause timeouts or out of memory errors. 419 413 wp_update_attachment_metadata( $attachment_id, wp_generate_attachment_metadata( $attachment_id, $file ) ); 420 421 // At this point the image is uploaded successfully even if there were specific errors or some sub-sizes were not created.422 // The transient is not needed any more.423 if ( $_ref ) {424 _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );425 }426 414 } 427 415 428 416 return $attachment_id;