From acd0f7c375e640058959b91a428e82bf5cf5ea9a Mon Sep 17 00:00:00 2001 From: nacin Date: Tue, 31 Jan 2012 22:12:58 +0000 Subject: [PATCH] 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. git-svn-id: http://svn.automattic.com/wordpress/trunk@19801 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/class-wp-ajax-response.php | 2 +- wp-includes/functions.php | 32 +++++++++++++++----------- wp-includes/pluggable.php | 8 +++++-- xmlrpc.php | 3 --- 4 files changed, 25 insertions(+), 20 deletions(-) diff --git a/wp-includes/class-wp-ajax-response.php b/wp-includes/class-wp-ajax-response.php index 2b981b0b14..296c7bae1e 100644 --- a/wp-includes/class-wp-ajax-response.php +++ b/wp-includes/class-wp-ajax-response.php @@ -131,6 +131,6 @@ class WP_Ajax_Response { foreach ( (array) $this->responses as $response ) echo $response; echo ''; - die(); + wp_die(); } } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 7f90ee5146..6f7030ae62 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -2043,15 +2043,13 @@ function wp_nonce_ays( $action ) { * @param string $title Error title. * @param string|array $args Optional arguments to control behavior. */ -function wp_die( $message, $title = '', $args = array() ) { +function wp_die( $message = '', $title = '', $args = array() ) { if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) - die('-1'); - - if ( function_exists( 'apply_filters' ) ) { - $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler'); - } else { - $function = '_default_wp_die_handler'; - } + $function = apply_filters( 'wp_die_ajax_handler', '_ajax_wp_die_handler' ); + elseif ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) + $function = apply_filters( 'wp_die_xmlrpc_handler', '_xmlrpc_wp_die_handler' ); + else + $function = apply_filters( 'wp_die_handler', '_default_wp_die_handler' ); call_user_func( $function, $message, $title, $args ); } @@ -2102,7 +2100,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { $message .= "\n

$back_text

"; } - if ( !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ) : + if ( ! did_action( 'admin_head' ) ) : if ( !headers_sent() ) { status_header( $r['response'] ); nocache_headers(); @@ -2206,7 +2204,7 @@ function _default_wp_die_handler( $message, $title = '', $args = array() ) { - + @@ -2240,13 +2238,19 @@ function _xmlrpc_wp_die_handler( $message, $title = '', $args = array() ) { } /** - * Filter to enable special wp_die handler for xmlrpc requests. + * Kill WordPress ajax execution. * - * @since 3.2.0 + * This is the handler for wp_die when processing Ajax requests. + * + * @since 3.4.0 * @access private + * + * @param string $message Optional. Response to print. */ -function _xmlrpc_wp_die_filter() { - return '_xmlrpc_wp_die_handler'; +function _ajax_wp_die_handler( $message = '' ) { + if ( is_scalar( $message ) ) + die( (string) $message ); + die( '0' ); } /** diff --git a/wp-includes/pluggable.php b/wp-includes/pluggable.php index 172ab70130..a6b7b47ea7 100644 --- a/wp-includes/pluggable.php +++ b/wp-includes/pluggable.php @@ -829,8 +829,12 @@ function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) { $result = wp_verify_nonce( $nonce, $action ); - if ( $die && false == $result ) - die('-1'); + if ( $die && false == $result ) { + if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) + wp_die( -1 ); + else + die( '-1' ); + } do_action('check_ajax_referer', $action, $result); diff --git a/xmlrpc.php b/xmlrpc.php index e4dafe7929..891e66f448 100644 --- a/xmlrpc.php +++ b/xmlrpc.php @@ -98,9 +98,6 @@ function logIO($io,$msg) { if ( isset($HTTP_RAW_POST_DATA) ) logIO("I", $HTTP_RAW_POST_DATA); -// Make sure wp_die output is XML -add_filter( 'wp_die_handler', '_xmlrpc_wp_die_filter' ); - // Allow for a plugin to insert a different class to handle requests. $wp_xmlrpc_server_class = apply_filters('wp_xmlrpc_server_class', 'wp_xmlrpc_server'); $wp_xmlrpc_server = new $wp_xmlrpc_server_class;