Make WordPress Core

Ticket #31037: 31037.diff

File 31037.diff, 6.1 KB (added by dd32, 10 years ago)
  • src/wp-admin/includes/ajax-actions.php

     
    18381838        $wp_customize->widgets->wp_ajax_update_widget();
    18391839}
    18401840
    18411841/**
    18421842 * Ajax handler for uploading attachments
    18431843 *
    18441844 * @since 3.3.0
    18451845 */
    18461846function wp_ajax_upload_attachment() {
    18471847        check_ajax_referer( 'media-form' );
    18481848
    18491849        if ( ! current_user_can( 'upload_files' ) ) {
    18501850                wp_send_json_error( array(
    18511851                        'message'  => __( "You don't have permission to upload files." ),
    18521852                        'filename' => $_FILES['async-upload']['name'],
    1853                 ) );
     1853                ), 'text/html' );
    18541854        }
    18551855
    18561856        if ( isset( $_REQUEST['post_id'] ) ) {
    18571857                $post_id = $_REQUEST['post_id'];
    18581858                if ( ! current_user_can( 'edit_post', $post_id ) ) {
    18591859                        wp_send_json_error( array(
    18601860                                'message'  => __( "You don't have permission to attach files to this post." ),
    18611861                                'filename' => $_FILES['async-upload']['name'],
    1862                         ) );
     1862                        ), 'text/html' );
    18631863                }
    18641864        } else {
    18651865                $post_id = null;
    18661866        }
    18671867
    18681868        $post_data = isset( $_REQUEST['post_data'] ) ? $_REQUEST['post_data'] : array();
    18691869
    18701870        // If the context is custom header or background, make sure the uploaded file is an image.
    18711871        if ( isset( $post_data['context'] ) && in_array( $post_data['context'], array( 'custom-header', 'custom-background' ) ) ) {
    18721872                $wp_filetype = wp_check_filetype_and_ext( $_FILES['async-upload']['tmp_name'], $_FILES['async-upload']['name'] );
    18731873                if ( ! wp_match_mime_types( 'image', $wp_filetype['type'] ) ) {
    18741874                        wp_send_json_error( array(
    18751875                                'message'  => __( 'The uploaded file is not a valid image. Please try again.' ),
    18761876                                'filename' => $_FILES['async-upload']['name'],
    1877                         ) );
     1877                        ), 'text/html' );
    18781878                }
    18791879        }
    18801880
    18811881        $attachment_id = media_handle_upload( 'async-upload', $post_id, $post_data );
    18821882
    18831883        if ( is_wp_error( $attachment_id ) ) {
    18841884                wp_send_json_error( array(
    18851885                        'message'  => $attachment_id->get_error_message(),
    18861886                        'filename' => $_FILES['async-upload']['name'],
    1887                 ) );
     1887                ), 'text/html' );
    18881888        }
    18891889
    18901890        if ( isset( $post_data['context'] ) && isset( $post_data['theme'] ) ) {
    18911891                if ( 'custom-background' === $post_data['context'] )
    18921892                        update_post_meta( $attachment_id, '_wp_attachment_is_custom_background', $post_data['theme'] );
    18931893
    18941894                if ( 'custom-header' === $post_data['context'] )
    18951895                        update_post_meta( $attachment_id, '_wp_attachment_is_custom_header', $post_data['theme'] );
    18961896        }
    18971897
    18981898        if ( ! $attachment = wp_prepare_attachment_for_js( $attachment_id ) )
    18991899                wp_die();
    19001900
    1901         wp_send_json_success( $attachment );
     1901        wp_send_json_success( $attachment, 'text/html' );
    19021902}
    19031903
    19041904/**
    19051905 * Ajax handler for image editing.
    19061906 *
    19071907 * @since 3.1.0
    19081908 */
    19091909function wp_ajax_image_editor() {
    19101910        $attachment_id = intval($_POST['postid']);
    19111911        if ( empty($attachment_id) || !current_user_can('edit_post', $attachment_id) )
    19121912                wp_die( -1 );
    19131913
    19141914        check_ajax_referer( "image_editor-$attachment_id" );
    19151915        include_once( ABSPATH . 'wp-admin/includes/image-edit.php' );
    19161916
  • src/wp-includes/functions.php

     
    27692769                } else {
    27702770                        return mb_convert_encoding( $string, 'UTF-8', 'UTF-8' );
    27712771                }
    27722772        } else {
    27732773                return wp_check_invalid_utf8( $string, true );
    27742774        }
    27752775}
    27762776
    27772777/**
    27782778 * Send a JSON response back to an Ajax request.
    27792779 *
    27802780 * @since 3.5.0
    27812781 *
    27822782 * @param mixed $response Variable (usually an array or object) to encode as JSON,
    27832783 *                        then print and die.
     2784 * @param string $content_type Content-Type to return the JSON as.
    27842785 */
    2785 function wp_send_json( $response ) {
    2786         @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
     2786function wp_send_json( $response, $content_type = null ) {
     2787        if ( ! $content_type ) {
     2788                $content_type = 'application/json';
     2789        }
     2790        @header( 'Content-Type: ' . $content_type . '; charset=' . get_option( 'blog_charset' ) );
     2791
    27872792        echo wp_json_encode( $response );
     2793
    27882794        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    27892795                wp_die();
    27902796        else
    27912797                die;
    27922798}
    27932799
    27942800/**
    27952801 * Send a JSON response back to an Ajax request, indicating success.
    27962802 *
    27972803 * @since 3.5.0
    27982804 *
    27992805 * @param mixed $data Data to encode as JSON, then print and die.
     2806 * @param string $content_type Content-Type to return the JSON as.
    28002807 */
    2801 function wp_send_json_success( $data = null ) {
     2808function wp_send_json_success( $data = null, $content_type = null ) {
    28022809        $response = array( 'success' => true );
    28032810
    28042811        if ( isset( $data ) )
    28052812                $response['data'] = $data;
    28062813
    2807         wp_send_json( $response );
     2814        wp_send_json( $response, $content_type );
    28082815}
    28092816
    28102817/**
    28112818 * Send a JSON response back to an Ajax request, indicating failure.
    28122819 *
    28132820 * If the `$data` parameter is a {@see WP_Error} object, the errors
    28142821 * within the object are processed and output as an array of error
    28152822 * codes and corresponding messages. All other types are output
    28162823 * without further processing.
    28172824 *
    28182825 * @since 3.5.0
    28192826 * @since 4.1.0 The `$data` parameter is now processed if a {@see WP_Error}
    28202827 *              object is passed in.
    28212828 *
    28222829 * @param mixed $data Data to encode as JSON, then print and die.
     2830 * @param string $content_type Content-Type to return the JSON as.
    28232831 */
    2824 function wp_send_json_error( $data = null ) {
     2832function wp_send_json_error( $data = null, $content_type = null ) {
    28252833        $response = array( 'success' => false );
    28262834
    28272835        if ( isset( $data ) ) {
    28282836                if ( is_wp_error( $data ) ) {
    28292837                        $result = array();
    28302838                        foreach ( $data->errors as $code => $messages ) {
    28312839                                foreach ( $messages as $message ) {
    28322840                                        $result[] = array( 'code' => $code, 'message' => $message );
    28332841                                }
    28342842                        }
    28352843
    28362844                        $response['data'] = $result;
    28372845                } else {
    28382846                        $response['data'] = $data;
    28392847                }
    28402848        }
    28412849
    2842         wp_send_json( $response );
     2850        wp_send_json( $response, $content_type );
    28432851}
    28442852
    28452853/**
    28462854 * Retrieve the WordPress home page URL.
    28472855 *
    28482856 * If the constant named 'WP_HOME' exists, then it will be used and returned
    28492857 * by the function. This can be used to counter the redirection on your local
    28502858 * development environment.
    28512859 *
    28522860 * @since 2.2.0
    28532861 * @access private
    28542862 *
    28552863 * @see WP_HOME
    28562864 *
    28572865 * @param string $url URL for the home location.