From 390e1756a1667e553960c04934c0311b1b43dc0a Mon Sep 17 00:00:00 2001 From: Andrew Nacin Date: Mon, 26 Nov 2012 20:12:50 +0000 Subject: [PATCH] Don't use wp_send_json_* handlers in the upload-attachment XHR handler. The difference is the content type: application/json (which jQuery deserializes automatically for us) and the default text/html. jQuery correctly handles application/json requests for IE, so we can continue to use the wp_send_json_* handlers elsewhere. Plupload rolls its own requests and does not handle application/json correctly. So, keep the standard text/html content type on upload-attachment. props koopersmith. see #22446. git-svn-id: https://develop.svn.wordpress.org/trunk@22845 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-admin/includes/ajax-actions.php | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/wp-admin/includes/ajax-actions.php b/wp-admin/includes/ajax-actions.php index 1ea7e45053..544c8be8b9 100644 --- a/wp-admin/includes/ajax-actions.php +++ b/wp-admin/includes/ajax-actions.php @@ -1585,12 +1585,12 @@ function wp_ajax_upload_attachment() { check_ajax_referer( 'media-form' ); if ( ! current_user_can( 'upload_files' ) ) - wp_send_json_error(); + wp_die(); if ( isset( $_REQUEST['post_id'] ) ) { $post_id = $_REQUEST['post_id']; if ( ! current_user_can( 'edit_post', $post_id ) ) - wp_send_json_error(); + wp_die(); } else { $post_id = null; } @@ -1601,20 +1601,26 @@ function wp_ajax_upload_attachment() { if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) { $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'], false ); if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) { - wp_send_json_error( array( + echo json_encode( array( + 'success' => false, 'message' => __( 'The uploaded file is not a valid image. Please try again.' ), 'filename' => $_FILES['async-upload']['name'], ) ); + + wp_die(); } } $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data ); if ( is_wp_error( $attachment_id ) ) { - wp_send_json_error( array( + echo json_encode( array( + 'success' => false, 'message' => $attachment_id->get_error_message(), 'filename' => $_FILES['async-upload']['name'], ) ); + + wp_die(); } if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) { @@ -1626,9 +1632,14 @@ function wp_ajax_upload_attachment() { } if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) ) - wp_send_json_error(); + wp_die(); - wp_send_json_success( $attachment ); + echo json_encode( array( + 'success' => true, + 'data' => $attachment, + ) ); + + wp_die(); } function wp_ajax_image_editor() {