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. Built from https://develop.svn.wordpress.org/trunk@46174 git-svn-id: http://core.svn.wordpress.org/trunk@45986 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
308e616baf
commit
463e050b25
|
@ -2422,21 +2422,18 @@ function wp_ajax_media_create_image_subsizes() {
|
||||||
wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) );
|
wp_send_json_error( array( 'message' => __( 'Sorry, you are not allowed to upload files.' ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Using Plupload `file.id` as ref.
|
if ( ! empty( $_POST['_wp_temp_upload_ref'] ) ) {
|
||||||
if ( ! empty( $_POST['_wp_temp_image_ref'] ) ) {
|
// Uploading of images usually fails while creating the sub-sizes, either because of a timeout or out of memory.
|
||||||
$image_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $_POST['_wp_temp_image_ref'] );
|
// 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 {
|
} else {
|
||||||
wp_send_json_error( array( 'message' => __( 'Invalid file reference.' ) ) );
|
wp_send_json_error( array( 'message' => __( 'Invalid file reference.' ) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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 = get_transient( '_wp_temp_image_ref:' . $image_ref );
|
|
||||||
|
|
||||||
if ( empty( $attachment_id ) ) {
|
if ( empty( $attachment_id ) ) {
|
||||||
wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
|
wp_send_json_error( array( 'message' => __( 'Upload failed. Please reload and try again.' ) ) );
|
||||||
}
|
}
|
||||||
|
@ -2481,7 +2478,7 @@ function wp_ajax_media_create_image_subsizes() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// At this point the image has been uploaded successfully.
|
// At this point the image has been uploaded successfully.
|
||||||
delete_transient( '_wp_temp_image_ref:' . $image_ref );
|
_wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
|
||||||
|
|
||||||
wp_send_json_success( $response );
|
wp_send_json_success( $response );
|
||||||
}
|
}
|
||||||
|
|
|
@ -978,6 +978,63 @@ function wp_handle_sideload( &$file, $overrides = false, $time = null ) {
|
||||||
return _wp_handle_upload( $file, $overrides, $time, $action );
|
return _wp_handle_upload( $file, $overrides, $time, $action );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Downloads a URL to a local temporary file using the WordPress HTTP API.
|
* Downloads a URL to a local temporary file using the WordPress HTTP API.
|
||||||
|
|
|
@ -308,13 +308,13 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid
|
||||||
$ext = pathinfo( $name, PATHINFO_EXTENSION );
|
$ext = pathinfo( $name, PATHINFO_EXTENSION );
|
||||||
$name = wp_basename( $name, ".$ext" );
|
$name = wp_basename( $name, ".$ext" );
|
||||||
|
|
||||||
$url = $file['url'];
|
$url = $file['url'];
|
||||||
$type = $file['type'];
|
$type = $file['type'];
|
||||||
$file = $file['file'];
|
$file = $file['file'];
|
||||||
$title = sanitize_text_field( $name );
|
$title = sanitize_text_field( $name );
|
||||||
$content = '';
|
$content = '';
|
||||||
$excerpt = '';
|
$excerpt = '';
|
||||||
$image_ref = false;
|
$_ref = false;
|
||||||
|
|
||||||
if ( preg_match( '#^audio#', $type ) ) {
|
if ( preg_match( '#^audio#', $type ) ) {
|
||||||
$meta = wp_read_audio_metadata( $file );
|
$meta = wp_read_audio_metadata( $file );
|
||||||
|
@ -376,11 +376,6 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid
|
||||||
|
|
||||||
// Use image exif/iptc data for title and caption defaults if possible.
|
// Use image exif/iptc data for title and caption defaults if possible.
|
||||||
} elseif ( 0 === strpos( $type, 'image/' ) ) {
|
} elseif ( 0 === strpos( $type, 'image/' ) ) {
|
||||||
// Image file reference passed by the uploader.
|
|
||||||
if ( ! empty( $_POST['_wp_temp_image_ref'] ) ) {
|
|
||||||
$image_ref = preg_replace( '/[^a-zA-Z0-9_]/', '', $_POST['_wp_temp_image_ref'] );
|
|
||||||
}
|
|
||||||
|
|
||||||
$image_meta = wp_read_image_metadata( $file );
|
$image_meta = wp_read_image_metadata( $file );
|
||||||
|
|
||||||
if ( $image_meta ) {
|
if ( $image_meta ) {
|
||||||
|
@ -415,8 +410,8 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid
|
||||||
|
|
||||||
if ( ! is_wp_error( $attachment_id ) ) {
|
if ( ! is_wp_error( $attachment_id ) ) {
|
||||||
// If an image, keep the upload reference until all image sub-sizes are created.
|
// If an image, keep the upload reference until all image sub-sizes are created.
|
||||||
if ( $image_ref ) {
|
if ( ! empty( $_POST['_wp_temp_upload_ref'] ) && wp_attachment_is_image( $attachment_id ) ) {
|
||||||
set_transient( '_wp_temp_image_ref:' . $image_ref, $attachment_id, HOUR_IN_SECONDS );
|
$_ref = _wp_set_upload_ref( $_POST['_wp_temp_upload_ref'], $attachment_id );
|
||||||
}
|
}
|
||||||
|
|
||||||
// The image sub-sizes are created during wp_generate_attachment_metadata().
|
// The image sub-sizes are created during wp_generate_attachment_metadata().
|
||||||
|
@ -425,8 +420,8 @@ function media_handle_upload( $file_id, $post_id, $post_data = array(), $overrid
|
||||||
|
|
||||||
// At this point the image is uploaded successfully even if there were specific errors or some sub-sizes were not created.
|
// 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.
|
// The transient is not needed any more.
|
||||||
if ( $image_ref ) {
|
if ( $_ref ) {
|
||||||
delete_transient( '_wp_temp_image_ref:' . $image_ref );
|
_wp_clear_upload_ref( $_POST['_wp_temp_upload_ref'] );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -450,7 +450,7 @@ jQuery( document ).ready( function( $ ) {
|
||||||
data: {
|
data: {
|
||||||
action: 'media-create-image-subsizes',
|
action: 'media-create-image-subsizes',
|
||||||
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
||||||
_wp_temp_image_ref: file.id,
|
_wp_temp_upload_ref: file.id,
|
||||||
_wp_upload_failed_cleanup: true,
|
_wp_upload_failed_cleanup: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -478,7 +478,7 @@ jQuery( document ).ready( function( $ ) {
|
||||||
data: {
|
data: {
|
||||||
action: 'media-create-image-subsizes',
|
action: 'media-create-image-subsizes',
|
||||||
_wpnonce: wpUploaderInit.multipart_params._wpnonce,
|
_wpnonce: wpUploaderInit.multipart_params._wpnonce,
|
||||||
_wp_temp_image_ref: file.id,
|
_wp_temp_upload_ref: file.id,
|
||||||
_legasy_support: 'true',
|
_legasy_support: 'true',
|
||||||
}
|
}
|
||||||
}).done( function( response ) {
|
}).done( function( response ) {
|
||||||
|
@ -599,9 +599,9 @@ jQuery( document ).ready( function( $ ) {
|
||||||
*/
|
*/
|
||||||
uploader.bind( 'BeforeUpload', function( up, file ) {
|
uploader.bind( 'BeforeUpload', function( up, file ) {
|
||||||
if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
|
if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
|
||||||
up.settings.multipart_params._wp_temp_image_ref = file.id;
|
up.settings.multipart_params._wp_temp_upload_ref = file.id;
|
||||||
} else {
|
} else {
|
||||||
up.settings.multipart_params._wp_temp_image_ref = '';
|
up.settings.multipart_params._wp_temp_upload_ref = '';
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
};
|
};
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -138,7 +138,7 @@ window.wp = window.wp || {};
|
||||||
data: {
|
data: {
|
||||||
action: 'media-create-image-subsizes',
|
action: 'media-create-image-subsizes',
|
||||||
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
||||||
_wp_temp_image_ref: file.id,
|
_wp_temp_upload_ref: file.id,
|
||||||
_wp_upload_failed_cleanup: true,
|
_wp_upload_failed_cleanup: true,
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
@ -161,7 +161,7 @@ window.wp = window.wp || {};
|
||||||
data: {
|
data: {
|
||||||
action: 'media-create-image-subsizes',
|
action: 'media-create-image-subsizes',
|
||||||
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
_wpnonce: _wpPluploadSettings.defaults.multipart_params._wpnonce,
|
||||||
_wp_temp_image_ref: file.id, // Used to find the new attachment_id.
|
_wp_temp_upload_ref: file.id, // Used to find the new attachment_id.
|
||||||
}
|
}
|
||||||
}).done( function( response ) {
|
}).done( function( response ) {
|
||||||
if ( response.success ) {
|
if ( response.success ) {
|
||||||
|
@ -324,9 +324,9 @@ window.wp = window.wp || {};
|
||||||
*/
|
*/
|
||||||
this.uploader.bind( 'BeforeUpload', function( up, file ) {
|
this.uploader.bind( 'BeforeUpload', function( up, file ) {
|
||||||
if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
|
if ( file.type && file.type.indexOf( 'image/' ) === 0 ) {
|
||||||
up.settings.multipart_params._wp_temp_image_ref = file.id;
|
up.settings.multipart_params._wp_temp_upload_ref = file.id;
|
||||||
} else {
|
} else {
|
||||||
up.settings.multipart_params._wp_temp_image_ref = '';
|
up.settings.multipart_params._wp_temp_upload_ref = '';
|
||||||
}
|
}
|
||||||
} );
|
} );
|
||||||
|
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -13,7 +13,7 @@
|
||||||
*
|
*
|
||||||
* @global string $wp_version
|
* @global string $wp_version
|
||||||
*/
|
*/
|
||||||
$wp_version = '5.3-alpha-46173';
|
$wp_version = '5.3-alpha-46174';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||||
|
|
Loading…
Reference in New Issue