Make WordPress Core


Ignore:
Timestamp:
10/03/2019 09:00:10 PM (7 years ago)
Author:
azaozz
Message:

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 #48200.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/ajax-actions.php

    r46253 r46382  
    24232423        }
    24242424
    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'] ) ) {
    24382426                wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
    24392427        }
     2428
     2429        $attachment_id = (int) $_POST['attachment_id'];
    24402430
    24412431        if ( ! empty( $_POST['_wp_upload_failed_cleanup'] ) ) {
    24422432                // 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 ) ) {
    24442434                        $attachment = get_post( $attachment_id );
    24452435
    2446                         // Posted at most 10 min ago.
     2436                        // Created at most 10 min ago.
    24472437                        if ( $attachment && ( time() - strtotime( $attachment->post_date_gmt ) < 600 ) ) {
    2448                                 /**
    2449                                  * Runs when an image upload fails during the post-processing phase,
    2450                                  * and the newly created attachment post is about to be deleted.
    2451                                  *
    2452                                  * @since 5.3.0
    2453                                  *
    2454                                  * @param int $attachment_id The attachment post ID.
    2455                                  */
    2456                                 do_action( 'wp_upload_failed_cleanup', $attachment_id );
    2457 
    24582438                                wp_delete_attachment( $attachment_id, true );
    24592439                                wp_send_json_success();
     
    24662446        wp_update_image_subsizes( $attachment_id );
    24672447
    2468         if ( ! empty( $_POST['_legasy_support'] ) ) {
     2448        if ( ! empty( $_POST['_legacy_support'] ) ) {
    24692449                // The old (inline) uploader. Only needs the attachment_id.
    24702450                $response = array( 'id' => $attachment_id );
     
    24792459
    24802460        // At this point the image has been uploaded successfully.
    2481         _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
    2482 
    24832461        wp_send_json_success( $response );
    24842462}
Note: See TracChangeset for help on using the changeset viewer.