Make WordPress Core


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

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

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

Props spacedmonkey, birgire.
Fixes #46026. See #44458.

File:
1 edited

Legend:

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

    r45015 r45016  
    30083008         */
    30093009        $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
     3010    } elseif ( wp_is_xml_request()
     3011        || function_exists( 'is_feed' ) && is_feed()
     3012        || function_exists( 'is_comment_feed' ) && is_comment_feed()
     3013        || function_exists( 'is_trackback' ) && is_trackback() ) {
     3014        /**
     3015         * Filters the callback for killing WordPress execution for XML requests.
     3016         *
     3017         * @since 5.2.0
     3018         *
     3019         * @param callable $function Callback function name.
     3020         */
     3021        $function = apply_filters( 'wp_die_xml_handler', '_xml_wp_die_handler' );
    30103022    } else {
    30113023        /**
    3012          * Filters the callback for killing WordPress execution for all non-Ajax, non-XML-RPC requests.
     3024         * Filters the callback for killing WordPress execution for all non-Ajax, non-JSON, non-XML requests.
    30133025         *
    30143026         * @since 3.0.0
     
    32203232
    32213233/**
     3234 * Kills WordPress execution and displays Ajax response with an error message.
     3235 *
     3236 * This is the handler for wp_die() when processing Ajax requests.
     3237 *
     3238 * @since 3.4.0
     3239 * @access private
     3240 *
     3241 * @param string       $message Error message.
     3242 * @param string       $title   Optional. Error title (unused). Default empty.
     3243 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
     3244 */
     3245function _ajax_wp_die_handler( $message, $title = '', $args = array() ) {
     3246    // Set default 'response' to 200 for AJAX requests.
     3247    $args = wp_parse_args(
     3248        $args,
     3249        array( 'response' => 200 )
     3250    );
     3251
     3252    list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args );
     3253
     3254    if ( ! headers_sent() ) {
     3255        // This is intentional. For backward-compatibility, support passing null here.
     3256        if ( null !== $args['response'] ) {
     3257            status_header( $r['response'] );
     3258        }
     3259        nocache_headers();
     3260    }
     3261
     3262    if ( is_scalar( $message ) ) {
     3263        $message = (string) $message;
     3264    } else {
     3265        $message = '0';
     3266    }
     3267
     3268    if ( $r['exit'] ) {
     3269        die( $message );
     3270    }
     3271
     3272    echo $message;
     3273}
     3274
     3275/**
    32223276 * Kills WordPress execution and displays JSON response with an error message.
    32233277 *
     
    33323386
    33333387/**
    3334  * Kills WordPress execution and displays Ajax response with an error message.
    3335  *
    3336  * This is the handler for wp_die() when processing Ajax requests.
    3337  *
    3338  * @since 3.4.0
     3388 * Kill WordPress execution and display XML message with error message.
     3389 *
     3390 * This is the handler for wp_die when processing XML requests.
     3391 *
     3392 * @since 5.2.0
    33393393 * @access private
    33403394 *
    33413395 * @param string       $message Error message.
    3342  * @param string       $title   Optional. Error title (unused). Default empty.
     3396 * @param string       $title   Optional. Error title. Default empty.
    33433397 * @param string|array $args    Optional. Arguments to control behavior. Default empty array.
    33443398 */
    3345 function _ajax_wp_die_handler( $message, $title = '', $args = array() ) {
    3346     // Set default 'response' to 200 for AJAX requests.
    3347     $args = wp_parse_args(
    3348         $args,
    3349         array( 'response' => 200 )
    3350     );
    3351 
     3399function _xml_wp_die_handler( $message, $title = '', $args = array() ) {
    33523400    list( $message, $title, $r ) = _wp_die_process_input( $message, $title, $args );
    33533401
     3402    $message = htmlspecialchars( $message );
     3403    $title   = htmlspecialchars( $title );
     3404
     3405    $xml = <<<EOD
     3406<error>
     3407    <code>{$r['code']}</code>
     3408    <title><![CDATA[{$title}]]></title>
     3409    <message><![CDATA[{$message}]]></message>
     3410    <data>
     3411        <status>{$r['response']}</status>
     3412    </data>
     3413</error>
     3414
     3415EOD;
     3416
    33543417    if ( ! headers_sent() ) {
    3355         // This is intentional. For backward-compatibility, support passing null here.
    3356         if ( null !== $args['response'] ) {
     3418        header( 'Content-Type: text/xml; charset=utf-8' );
     3419        if ( null !== $r['response'] ) {
    33573420            status_header( $r['response'] );
    33583421        }
     
    33603423    }
    33613424
    3362     if ( is_scalar( $message ) ) {
    3363         $message = (string) $message;
    3364     } else {
    3365         $message = '0';
    3366     }
    3367 
     3425    echo $xml;
    33683426    if ( $r['exit'] ) {
    3369         die( $message );
    3370     }
    3371 
    3372     echo $message;
     3427        die();
     3428    }
    33733429}
    33743430
Note: See TracChangeset for help on using the changeset viewer.