WordPress.org

Make WordPress Core

Changeset 46174


Ignore:
Timestamp:
09/19/2019 01:25:14 AM (2 months ago)
Author:
azaozz
Message:

Uploads: add helper functions for setting, getting, and deleting the temp upload reference used to the attachment_id when retrying to make image sub-sizes.

See #47872.

Location:
trunk/src
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/js/_enqueues/vendor/plupload/handlers.js

    r46081 r46174  
    451451                    action: 'media-create-image-subsizes',
    452452                    _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
    453                     _wp_temp_image_ref: file.id,
     453                    _wp_temp_upload_ref: file.id,
    454454                    _wp_upload_failed_cleanup: true,
    455455                }
     
    479479                action: 'media-create-image-subsizes',
    480480                _wpnonce: wpUploaderInit.multipart_params._wpnonce,
    481                 _wp_temp_image_ref: file.id,
     481                _wp_temp_upload_ref: file.id,
    482482                _legasy_support: 'true',
    483483            }
     
    600600        uploader.bind( 'BeforeUpload', function( up, file ) {
    601601            if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
    602                 up.settings.multipart_params._wp_temp_image_ref = file.id;
     602                up.settings.multipart_params._wp_temp_upload_ref = file.id;
    603603            } else {
    604                 up.settings.multipart_params._wp_temp_image_ref = '';
     604                up.settings.multipart_params._wp_temp_upload_ref = '';
    605605            }
    606606        } );
  • trunk/src/js/_enqueues/vendor/plupload/wp-plupload.js

    r46081 r46174  
    139139                        action: 'media-create-image-subsizes',
    140140                        _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
    141                         _wp_temp_image_ref: file.id,
     141                        _wp_temp_upload_ref: file.id,
    142142                        _wp_upload_failed_cleanup: true,
    143143                    }
     
    162162                    action: 'media-create-image-subsizes',
    163163                    _wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
    164                     _wp_temp_image_ref: file.id, // Used to find the new attachment_id.
     164                    _wp_temp_upload_ref: file.id, // Used to find the new attachment_id.
    165165                }
    166166            }).done( function( response ) {
     
    325325        this.uploader.bind( 'BeforeUpload', function( up, file ) {
    326326            if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
    327                 up.settings.multipart_params._wp_temp_image_ref = file.id;
     327                up.settings.multipart_params._wp_temp_upload_ref = file.id;
    328328            } else {
    329                 up.settings.multipart_params._wp_temp_image_ref = '';
     329                up.settings.multipart_params._wp_temp_upload_ref = '';
    330330            }
    331331        } );
  • trunk/src/wp-admin/includes/ajax-actions.php

    r46155 r46174  
    24232423    }
    24242424
    2425     // Using Plupload `file.id` as ref.
    2426     if ( ! empty( $_POST['_wp_temp_image_ref'] ) ) {
    2427         $image_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $_POST['_wp_temp_image_ref'] );
     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'] );
    24282433    } else {
    24292434        wp_send_json_error( array( 'message' => __( 'Invalid file reference.' ) ) );
    24302435    }
    2431 
    2432     // Uploading of images usually fails while creating the sub-sizes, either because of a timeout or out of memory.
    2433     // At this point the file has been uploaded and an attachment post created, but because of the PHP fatal error
    2434     // the cliend doesn't know the attachment ID yet.
    2435     // To be able to find the new attachment_id in these cases we temporarily store an upload reference sent by the client
    2436     // in the original upload request. It is used to save a transient with the attachment_id as value.
    2437     // That reference currently is Plupload's `file.id` but can be any sufficiently random alpha-numeric string.
    2438     $attachment_id = get_transient( '_wp_temp_image_ref:' . $image_ref );
    24392436
    24402437    if ( empty( $attachment_id ) ) {
     
    24822479
    24832480    // At this point the image has been uploaded successfully.
    2484     delete_transient( '_wp_temp_image_ref:' . $image_ref );
     2481    _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
    24852482
    24862483    wp_send_json_success( $response );
  • trunk/src/wp-admin/includes/file.php

    r45932 r46174  
    979979}
    980980
     981/**
     982 * Temporarily stores the client upload reference in a transient.
     983 *
     984 * @since 5.3.0
     985 * @access private
     986 *
     987 * @param string $upload_ref    The upload reference sent by the client.
     988 * @param int    $attachment_id Attachment post ID.
     989 * @return bool Whether the transient was set.
     990 */
     991function _wp_set_upload_ref( $upload_ref, $attachment_id ) {
     992    $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
     993
     994    if ( ! empty( $upload_ref ) ) {
     995        return set_transient( '_wp_temp_image_ref:' . $upload_ref, $attachment_id, HOUR_IN_SECONDS );
     996    }
     997
     998    return false;
     999}
     1000
     1001/**
     1002 * Get attachment post ID from an upload reference.
     1003 *
     1004 * @since 5.3.0
     1005 * @access private
     1006 *
     1007 * @param string $upload_ref    The upload reference sent by the client.
     1008 * @return int The attachemtn post ID. Zero if the upload reference has expired or doesn't exist.
     1009 */
     1010function _wp_get_upload_ref_attachment_id( $upload_ref ) {
     1011    $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
     1012
     1013    if ( ! empty( $upload_ref ) ) {
     1014        return (int) get_transient( '_wp_temp_image_ref:' . $upload_ref );
     1015    }
     1016
     1017    return 0;
     1018}
     1019
     1020/**
     1021 * Remove the transient that stores a temporary upload reference.
     1022 *
     1023 * @since 5.3.0
     1024 * @access private
     1025 *
     1026 * @param string $upload_ref    The upload reference sent by the client.
     1027 * @return bool Whether the transient was removed.
     1028 */
     1029function _wp_clear_upload_ref( $upload_ref ) {
     1030    $upload_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $upload_ref );
     1031
     1032    if ( ! empty( $upload_ref ) ) {
     1033        return delete_transient( '_wp_temp_image_ref:' . $upload_ref );
     1034    }
     1035
     1036    return false;
     1037}
    9811038
    9821039/**
  • trunk/src/wp-admin/includes/media.php

    r46131 r46174  
    309309    $name = wp_basename( $name, ".$ext" );
    310310
    311     $url       = $file['url'];
    312     $type      = $file['type'];
    313     $file      = $file['file'];
    314     $title     = sanitize_text_field( $name );
    315     $content   = '';
    316     $excerpt   = '';
    317     $image_ref = false;
     311    $url     = $file['url'];
     312    $type    = $file['type'];
     313    $file    = $file['file'];
     314    $title   = sanitize_text_field( $name );
     315    $content = '';
     316    $excerpt = '';
     317    $_ref    = false;
    318318
    319319    if ( preg_match( '#^audio#', $type ) ) {
     
    377377        // Use image exif/iptc data for title and caption defaults if possible.
    378378    } elseif ( 0 === strpos( $type, 'image/' ) ) {
    379         // Image file reference passed by the uploader.
    380         if ( ! empty( $_POST['_wp_temp_image_ref'] ) ) {
    381             $image_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $_POST['_wp_temp_image_ref'] );
    382         }
    383 
    384379        $image_meta = wp_read_image_metadata( $file );
    385380
     
    416411    if ( ! is_wp_error( $attachment_id ) ) {
    417412        // If an image, keep the upload reference until all image sub-sizes are created.
    418         if ( $image_ref ) {
    419             set_transient( '_wp_temp_image_ref:' . $image_ref, $attachment_id, HOUR_IN_SECONDS );
     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 );
    420415        }
    421416
     
    426421        // At this point the image is uploaded successfully even if there were specific errors or some sub-sizes were not created.
    427422        // The transient is not needed any more.
    428         if ( $image_ref ) {
    429             delete_transient( '_wp_temp_image_ref:' . $image_ref );
     423        if ( $_ref ) {
     424            _wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
    430425        }
    431426    }
Note: See TracChangeset for help on using the changeset viewer.