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
This commit is contained in:
nacin 2012-01-31 22:12:58 +00:00
parent 8bd826e43b
commit acd0f7c375
4 changed files with 25 additions and 20 deletions

View File

@ -131,6 +131,6 @@ class WP_Ajax_Response {
foreach ( (array) $this->responses as $response )
echo $response;
echo '</wp_ajax>';
die();
wp_die();
}
}

View File

@ -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<p><a href='javascript:history.back()'>$back_text</a></p>";
}
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() ) {
</style>
</head>
<body id="error-page">
<?php endif; // !function_exists( 'did_action' ) || !did_action( 'admin_head' ) ?>
<?php endif; // ! did_action( 'admin_head' ) ?>
<?php echo $message; ?>
</body>
</html>
@ -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' );
}
/**

View File

@ -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);

View File

@ -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;