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

@ -94,7 +94,7 @@ class WP_Comments_List_Table extends WP_List_Table {
$comments_per_page = $this->get_per_page( $comment_status ); $comments_per_page = $this->get_per_page( $comment_status );
$doing_ajax = defined( 'DOING_AJAX' ) && DOING_AJAX; $doing_ajax = wp_doing_ajax();
if ( isset( $_REQUEST['number'] ) ) { if ( isset( $_REQUEST['number'] ) ) {
$number = (int) $_REQUEST['number']; $number = (int) $_REQUEST['number'];
@ -600,7 +600,7 @@ class WP_Comments_List_Table extends WP_List_Table {
( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | '; ( ( ( 'approve' === $action || 'unapprove' === $action ) && 2 === $i ) || 1 === $i ) ? $sep = '' : $sep = ' | ';
// Reply and quickedit need a hide-if-no-js span when not added with ajax // Reply and quickedit need a hide-if-no-js span when not added with ajax
if ( ( 'reply' === $action || 'quickedit' === $action ) && ! defined('DOING_AJAX') ) if ( ( 'reply' === $action || 'quickedit' === $action ) && ! wp_doing_ajax() )
$action .= ' hide-if-no-js'; $action .= ' hide-if-no-js';
elseif ( ( $action === 'untrash' && $the_comment_status === 'trash' ) || ( $action === 'unspam' && $the_comment_status === 'spam' ) ) { elseif ( ( $action === 'untrash' && $the_comment_status === 'trash' ) || ( $action === 'unspam' && $the_comment_status === 'spam' ) ) {
if ( '1' == get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) ) if ( '1' == get_comment_meta( $comment->comment_ID, '_wp_trash_meta_status', true ) )

View File

@ -284,7 +284,7 @@ class WP_List_Table {
$args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] ); $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] );
// Redirect if page number is invalid and headers are not already sent. // Redirect if page number is invalid and headers are not already sent.
if ( ! headers_sent() && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) { if ( ! headers_sent() && ! wp_doing_ajax() && $args['total_pages'] > 0 && $this->get_pagenum() > $args['total_pages'] ) {
wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) ); wp_redirect( add_query_arg( 'paged', $args['total_pages'] ) );
exit; exit;
} }

View File

@ -367,7 +367,7 @@ class WP_Terms_List_Table extends WP_List_Table {
$qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' ); $qe_data = get_term( $tag->term_id, $taxonomy, OBJECT, 'edit' );
$uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI']; $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
$edit_link = add_query_arg( $edit_link = add_query_arg(
'wp_http_referer', 'wp_http_referer',
@ -425,7 +425,7 @@ class WP_Terms_List_Table extends WP_List_Table {
$tax = get_taxonomy( $taxonomy ); $tax = get_taxonomy( $taxonomy );
$default_term = get_option( 'default_' . $taxonomy ); $default_term = get_option( 'default_' . $taxonomy );
$uri = ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ? wp_get_referer() : $_SERVER['REQUEST_URI']; $uri = wp_doing_ajax() ? wp_get_referer() : $_SERVER['REQUEST_URI'];
$edit_link = add_query_arg( $edit_link = add_query_arg(
'wp_http_referer', 'wp_http_referer',

View File

@ -966,7 +966,7 @@ function wp_dashboard_rss_output( $widget_id ) {
*/ */
function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) { function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) {
$loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&#8230;' ) . '</p><p class="hide-if-js">' . __( 'This widget requires JavaScript.' ) . '</p>'; $loading = '<p class="widget-loading hide-if-no-js">' . __( 'Loading&#8230;' ) . '</p><p class="hide-if-js">' . __( 'This widget requires JavaScript.' ) . '</p>';
$doing_ajax = ( defined('DOING_AJAX') && DOING_AJAX ); $doing_ajax = wp_doing_ajax();
if ( empty($check_urls) ) { if ( empty($check_urls) ) {
$widgets = get_option( 'dashboard_widget_options' ); $widgets = get_option( 'dashboard_widget_options' );

View File

@ -40,7 +40,7 @@ function check_upload_size( $file ) {
$file['error'] = __( 'You have used your space quota. Please delete files before uploading.' ); $file['error'] = __( 'You have used your space quota. Please delete files before uploading.' );
} }
if ( $file['error'] != '0' && ! isset( $_POST['html-upload'] ) && ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) ) { if ( $file['error'] != '0' && ! isset( $_POST['html-upload'] ) && ! wp_doing_ajax() ) {
wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' ); wp_die( $file['error'] . ' <a href="javascript:history.go(-1)">' . __( 'Back' ) . '</a>' );
} }

View File

@ -459,7 +459,7 @@ function themes_api( $action, $args = array() ) {
$request = wp_remote_post( $url, $http_args ); $request = wp_remote_post( $url, $http_args );
if ( $ssl && is_wp_error( $request ) ) { if ( $ssl && is_wp_error( $request ) ) {
if ( ! defined( 'DOING_AJAX' ) || ! DOING_AJAX ) { if ( ! wp_doing_ajax() ) {
trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE ); trigger_error( __( 'An unexpected error occurred. Something may be wrong with WordPress.org or this server&#8217;s configuration. If you continue to have problems, please try the <a href="https://wordpress.org/support/">support forums</a>.' ) . ' ' . __( '(WordPress could not establish a secure connection to WordPress.org. Please contact your server administrator.)' ), headers_sent() || WP_DEBUG ? E_USER_WARNING : E_USER_NOTICE );
} }
$request = wp_remote_post( $http_url, $http_args ); $request = wp_remote_post( $http_url, $http_args );

View File

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

View File

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

View File

@ -632,7 +632,7 @@ function wp_allow_comment( $commentdata ) {
* @param array $commentdata Comment data. * @param array $commentdata Comment data.
*/ */
do_action( 'comment_duplicate_trigger', $commentdata ); 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!') ); 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 ); 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 ); 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.') ); die( __('You are posting comments too quickly. Slow down.') );
wp_die( __( 'You are posting comments too quickly. Slow down.' ), 429 ); 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 = ''; $title = '';
} }
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { if ( wp_doing_ajax() ) {
/** /**
* Filters the callback for killing WordPress execution for Ajax requests. * 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 ) { function wp_send_json( $response ) {
@header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
echo wp_json_encode( $response ); echo wp_json_encode( $response );
if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) if ( wp_doing_ajax() )
wp_die(); wp_die();
else else
die; 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 ); 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 ); @ini_set( 'display_errors', 0 );
} }
} }
@ -1027,3 +1027,21 @@ function wp_is_ini_value_changeable( $setting ) {
return false; 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() { function wp_user_settings() {
if ( ! is_admin() || defined( 'DOING_AJAX' ) ) { if ( ! is_admin() || wp_doing_ajax() ) {
return; return;
} }

View File

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

View File

@ -678,7 +678,7 @@ function wp_clean_update_cache() {
delete_site_transient( 'update_core' ); 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; return;
} }

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @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. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.