AJAX: add a new function, wp_doing_ajax(), which can replace... (wait for it...) DOING_AJAX checks via the constant.

Props Mte90, sebastian.pisula, swissspidy.
Fixes #25669.

Built from https://develop.svn.wordpress.org/trunk@38334


git-svn-id: http://core.svn.wordpress.org/trunk@38275 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor
2016-08-23 14:33:30 +00:00
parent 6f9f3b0c56
commit b7812bd416
15 changed files with 38 additions and 21 deletions

View File

@@ -149,7 +149,7 @@ class WP_Ajax_Response {
foreach ( (array) $this->responses as $response )
echo $response;
echo '</wp_ajax>';
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
if ( wp_doing_ajax() )
wp_die();
else
die();

View File

@@ -315,8 +315,7 @@ final class WP_Customize_Manager {
* @return bool True if it's an Ajax request, false otherwise.
*/
public function doing_ajax( $action = null ) {
$doing_ajax = ( defined( 'DOING_AJAX' ) && DOING_AJAX );
if ( ! $doing_ajax ) {
if ( ! wp_doing_ajax() ) {
return false;
}

View File

@@ -632,7 +632,7 @@ function wp_allow_comment( $commentdata ) {
* @param array $commentdata Comment data.
*/
do_action( 'comment_duplicate_trigger', $commentdata );
if ( defined( 'DOING_AJAX' ) ) {
if ( wp_doing_ajax() ) {
die( __('Duplicate comment detected; it looks as though you&#8217;ve already said that!') );
}
wp_die( __( 'Duplicate comment detected; it looks as though you&#8217;ve already said that!' ), 409 );
@@ -768,7 +768,7 @@ function check_comment_flood_db( $ip, $email, $date ) {
*/
do_action( 'comment_flood_trigger', $time_lastcomment, $time_newcomment );
if ( defined('DOING_AJAX') )
if ( wp_doing_ajax() )
die( __('You are posting comments too quickly. Slow down.') );
wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 );

View File

@@ -2570,7 +2570,7 @@ function wp_die( $message = '', $title = '', $args = array() ) {
$title = '';
}
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
if ( wp_doing_ajax() ) {
/**
* Filters the callback for killing WordPress execution for Ajax requests.
*
@@ -3064,7 +3064,7 @@ function _wp_json_prepare_data( $data ) {
function wp_send_json( $response ) {
@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
echo wp_json_encode( $response );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
if ( wp_doing_ajax() )
wp_die();
else
die;

View File

@@ -334,7 +334,7 @@ function wp_debug_mode() {
error_reporting( E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_ERROR | E_WARNING | E_PARSE | E_USER_ERROR | E_USER_WARNING | E_RECOVERABLE_ERROR );
}
if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || wp_doing_ajax() ) {
@ini_set( 'display_errors', 0 );
}
}
@@ -1027,3 +1027,21 @@ function wp_is_ini_value_changeable( $setting ) {
return false;
}
/**
* Determines whether the current request is a WordPress Ajax request.
*
* @since 4.7.0
*
* @return bool True if it's a WordPress Ajax request, false otherwise.
*/
function wp_doing_ajax() {
/**
* Filter whether the current request is a WordPress Ajax request.
*
* @since 4.7.0
*
* @param bool $wp_doing_ajax Whether the current request is a WordPress Ajax request.
*/
return apply_filters( 'wp_doing_ajax', defined( 'DOING_AJAX' ) && DOING_AJAX );
}

View File

@@ -786,7 +786,7 @@ function set_transient( $transient, $value, $expiration = 0 ) {
*/
function wp_user_settings() {
if ( ! is_admin() || defined( 'DOING_AJAX' ) ) {
if ( ! is_admin() || wp_doing_ajax() ) {
return;
}

View File

@@ -603,7 +603,7 @@ function wp_validate_auth_cookie($cookie = '', $scheme = '') {
$expired = $expiration = $cookie_elements['expiration'];
// Allow a grace period for POST and Ajax requests
if ( defined('DOING_AJAX') || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
if ( wp_doing_ajax() || 'POST' == $_SERVER['REQUEST_METHOD'] ) {
$expired += HOUR_IN_SECONDS;
}
@@ -1104,7 +1104,7 @@ function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
do_action( 'check_ajax_referer', $action, $result );
if ( $die && false === $result ) {
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) {
if ( wp_doing_ajax() ) {
wp_die( -1 );
} else {
die( '-1' );

View File

@@ -678,7 +678,7 @@ function wp_clean_update_cache() {
delete_site_transient( 'update_core' );
}
if ( ( ! is_main_site() && ! is_network_admin() ) || ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
if ( ( ! is_main_site() && ! is_network_admin() ) || wp_doing_ajax() ) {
return;
}

View File

@@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.7-alpha-38333';
$wp_version = '4.7-alpha-38334';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.