Make WordPress Core


Ignore:
Timestamp:
01/31/2012 10:12:58 PM (13 years ago)
Author:
nacin
Message:

Re-purpose wp_die() for ajax responses.

  • Allows unit testing of core ajax actions.
  • wp_die() now has separate filters to choose a handler depending on the context (ajax, XML-RPC, else).
  • wp_die) in ajax context does not need to be called with a string. Conversion takes place before die().

props kurtpayne, see #15327.

File:
1 edited

Legend:

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

    r19777 r19801  
    20442044 * @param string|array $args Optional arguments to control behavior.
    20452045 */
    2046 function wp_die( $message, $title = '', $args = array() ) {
     2046function wp_die( $message = '', $title = '', $args = array() ) {
    20472047    if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    2048         die('-1');
    2049 
    2050     if ( function_exists( 'apply_filters' ) ) {
    2051         $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler');
    2052     } else {
    2053         $function = '_default_wp_die_handler';
    2054     }
     2048        $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' );
     2049    elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST )
     2050        $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' );
     2051    else
     2052        $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' );
    20552053
    20562054    call_user_func( $function, $message, $title, $args );
     
    21032101    }
    21042102
    2105     if ( !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ) :
     2103    if ( ! did_action( 'admin_head' ) ) :
    21062104        if ( !headers_sent() ) {
    21072105            status_header( $r['response'] );
     
    22072205</head>
    22082206<body id="error-page">
    2209 <?php endif; // !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ?>
     2207<?php endif; // ! did_action( 'admin_head' ) ?>
    22102208    <?php echo $message; ?>
    22112209</body>
     
    22412239
    22422240/**
    2243  * Filter to enable special wp_die handler for xmlrpc requests.
    2244  *
    2245  * @since 3.2.0
     2241 * Kill WordPress ajax execution.
     2242 *
     2243 * This is the handler for wp_die when processing Ajax requests.
     2244 *
     2245 * @since 3.4.0
    22462246 * @access private
    2247  */
    2248 function _xmlrpc_wp_die_filter() {
    2249     return '_xmlrpc_wp_die_handler';
     2247 *
     2248 * @param string $message Optional. Response to print.
     2249 */
     2250function _ajax_wp_die_handler( $message = '' ) {
     2251    if ( is_scalar( $message ) )
     2252        die( (string) $message );
     2253    die( '0' );
    22502254}
    22512255
Note: See TracChangeset for help on using the changeset viewer.