Make WordPress Core


Ignore:
Timestamp:
03/26/2019 10:25:47 PM (6 years ago)
Author:
SergeyBiryukov
Message:

Bootstrap/Load: Add support for JSONP requests to wp_die().

In addition to AJAX, XML-RPC, and JSON requests, wp_die() now handles JSONP requests correctly, returning information in the expected content type.

Props spacedmonkey, TimothyBlynJacobs.
Fixes #46025. See #44458.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r44981 r45015  
    29902990         */
    29912991        $function = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
     2992    } elseif ( wp_is_jsonp_request() ) {
     2993        /**
     2994         * Filters the callback for killing WordPress execution for JSONP requests.
     2995         *
     2996         * @since 5.2.0
     2997         *
     2998         * @param callable $function Callback function name.
     2999         */
     3000        $function = apply_filters( 'wp_die_jsonp_handler', '_jsonp_wp_die_handler' );
    29923001    } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
    29933002        /**
     
    32433252
    32443253    echo wp_json_encode( $data );
     3254    if ( $r['exit'] ) {
     3255        die();
     3256    }
     3257}
     3258
     3259/**
     3260 * Kill WordPress execution and display JSONP message with error message.
     3261 *
     3262 * This is the handler for wp_die when processing JSONP requests.
     3263 *
     3264 * @since 5.2.0
     3265 * @access private
     3266 *
     3267 * @param string       $message Error message.
     3268 * @param string       $title   Optional. Error title. Default empty.
     3269 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
     3270 */
     3271function _jsonp_wp_die_handler( $message, $title = '', $args = array() ) {
     3272    list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args );
     3273
     3274    $data = array(
     3275        'code'              => $r['code'],
     3276        'message'           => $message,
     3277        'data'              => array(
     3278            'status' => $r['response'],
     3279        ),
     3280        'additional_errors' => $r['additional_errors'],
     3281    );
     3282
     3283    if ( ! headers_sent() ) {
     3284        header( 'Content-Type: application/javascript; charset=utf-8' );
     3285        header( 'X-Content-Type-Options: nosniff' );
     3286        header( 'X-Robots-Tag: noindex' );
     3287        if ( null !== $r['response'] ) {
     3288            status_header( $r['response'] );
     3289        }
     3290        nocache_headers();
     3291    }
     3292
     3293    $result         = wp_json_encode( $data );
     3294    $jsonp_callback = $_GET['_jsonp'];
     3295    echo '/**/' . $jsonp_callback . '(' . $result . ')';
    32453296    if ( $r['exit'] ) {
    32463297        die();
Note: See TracChangeset for help on using the changeset viewer.