Make WordPress Core

Changeset 44625


Ignore:
Timestamp:
01/16/2019 03:21:49 PM (6 years ago)
Author:
flixos90
Message:

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

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

Props spacedmonkey.
See #45933, #44458.

File:
1 edited

Legend:

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

    r44624 r44625  
    29762976         */
    29772977        $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
     2978    } elseif ( wp_is_json_request() ) {
     2979        /**
     2980         * Filters the callback for killing WordPress execution for JSON requests.
     2981         *
     2982         * @since 5.1.0
     2983         *
     2984         * @param callable $function Callback function name.
     2985         */
     2986        $function = apply_filters( 'wp_die_json_handler', '_json_wp_die_handler' );
    29782987    } elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) {
    29792988        /**
     
    32133222</html>
    32143223    <?php
     3224    die();
     3225}
     3226
     3227/**
     3228 * Kill WordPress execution and display JSON message with error message.
     3229 *
     3230 * This is the handler for wp_die when processing JSON requests.
     3231 *
     3232 * @since 5.1.0
     3233 * @access private
     3234 *
     3235 * @param string       $message Error message.
     3236 * @param string       $title   Optional. Error title. Default empty.
     3237 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
     3238 */
     3239function _json_wp_die_handler( $message, $title = '', $args = array() ) {
     3240    $defaults = array( 'response' => 500 );
     3241
     3242    $r = wp_parse_args( $args, $defaults );
     3243
     3244    $data = array(
     3245        'code'    => 'wp_die',
     3246        'message' => $message,
     3247        'status'  => $r['response'],
     3248    );
     3249
     3250    if ( ! headers_sent() ) {
     3251        header( 'Content-Type: application/json; charset=utf-8' );
     3252        if ( null !== $r['response'] ) {
     3253            status_header( $r['response'] );
     3254        }
     3255    }
     3256
     3257    echo wp_json_encode( $data );
    32153258    die();
    32163259}
Note: See TracChangeset for help on using the changeset viewer.