WordPress.org

Make WordPress Core

Changeset 45015


Ignore:
Timestamp:
03/26/2019 10:25:47 PM (10 months 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.

Location:
trunk/src/wp-includes
Files:
2 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();
  • trunk/src/wp-includes/load.php

    r44973 r45015  
    15021502
    15031503}
     1504
     1505/**
     1506 * Checks whether current request is a JSONP request, or is expecting a JSONP response.
     1507 *
     1508 * @since 5.2.0
     1509 *
     1510 * @return bool True if JSONP request, false otherwise.
     1511 */
     1512function wp_is_jsonp_request() {
     1513    if ( ! isset( $_GET['_jsonp'] ) ) {
     1514        return false;
     1515    }
     1516
     1517    if ( ! function_exists( 'wp_check_jsonp_callback' ) ) {
     1518        require_once ABSPATH . WPINC . '/functions.php';
     1519    }
     1520
     1521    $jsonp_callback = $_GET['_jsonp'];
     1522    if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) {
     1523        return false;
     1524    }
     1525
     1526    /** This filter is documented in wp-includes/rest-api/class-wp-rest-server.php */
     1527    $jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true );
     1528
     1529    return $jsonp_enabled;
     1530
     1531}
Note: See TracChangeset for help on using the changeset viewer.