WordPress.org

Make WordPress Core

Changeset 19801


Ignore:
Timestamp:
01/31/2012 10:12:58 PM (9 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.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class-wp-ajax-response.php

    r19712 r19801  
    132132            echo $response;
    133133        echo '</wp_ajax>';
    134         die();
     134        wp_die();
    135135    }
    136136}
  • 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
  • trunk/wp-includes/pluggable.php

    r19771 r19801  
    830830    $result = wp_verify_nonce( $nonce, $action );
    831831
    832     if ( $die && false == $result )
    833         die('-1');
     832    if ( $die && false == $result ) {
     833        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
     834            wp_die( -1 );
     835        else
     836            die( '-1' );
     837    }
    834838
    835839    do_action('check_ajax_referer', $action, $result);
  • trunk/xmlrpc.php

    r19712 r19801  
    9999    logIO("I", $HTTP_RAW_POST_DATA);
    100100
    101 // Make sure wp_die output is XML
    102 add_filter( 'wp_die_handler', '_xmlrpc_wp_die_filter' );
    103 
    104101// Allow for a plugin to insert a different class to handle requests.
    105102$wp_xmlrpc_server_class = apply_filters('wp_xmlrpc_server_class', 'wp_xmlrpc_server');
Note: See TracChangeset for help on using the changeset viewer.