diff --git a/src/wp-admin/includes/class-wp-comments-list-table.php b/src/wp-admin/includes/class-wp-comments-list-table.php index 116a55e06f..cb7801870d 100644 --- a/src/wp-admin/includes/class-wp-comments-list-table.php +++ b/src/wp-admin/includes/class-wp-comments-list-table.php @@ -94,7 +94,7 @@ class WP_Comments_List_Table extends WP_List_Table { $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'] ) ) { $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 = ' | '; // 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'; 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 ) ) diff --git a/src/wp-admin/includes/class-wp-list-table.php b/src/wp-admin/includes/class-wp-list-table.php index ee0f9af593..68053517db 100644 --- a/src/wp-admin/includes/class-wp-list-table.php +++ b/src/wp-admin/includes/class-wp-list-table.php @@ -284,7 +284,7 @@ class WP_List_Table { $args['total_pages'] = ceil( $args['total_items'] / $args['per_page'] ); // 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'] ) ); exit; } diff --git a/src/wp-admin/includes/class-wp-terms-list-table.php b/src/wp-admin/includes/class-wp-terms-list-table.php index e765794a4e..638d4e758f 100644 --- a/src/wp-admin/includes/class-wp-terms-list-table.php +++ b/src/wp-admin/includes/class-wp-terms-list-table.php @@ -367,7 +367,7 @@ class WP_Terms_List_Table extends WP_List_Table { $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( 'wp_http_referer', @@ -425,7 +425,7 @@ class WP_Terms_List_Table extends WP_List_Table { $tax = get_taxonomy( $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( 'wp_http_referer', diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php index e517f956c7..f923c70ce0 100644 --- a/src/wp-admin/includes/dashboard.php +++ b/src/wp-admin/includes/dashboard.php @@ -966,7 +966,7 @@ function wp_dashboard_rss_output( $widget_id ) { */ function wp_dashboard_cached_rss_widget( $widget_id, $callback, $check_urls = array() ) { $loading = '

' . __( 'Loading…' ) . '

' . __( 'This widget requires JavaScript.' ) . '

'; - $doing_ajax = ( defined('DOING_AJAX') && DOING_AJAX ); + $doing_ajax = wp_doing_ajax(); if ( empty($check_urls) ) { $widgets = get_option( 'dashboard_widget_options' ); diff --git a/src/wp-admin/includes/ms.php b/src/wp-admin/includes/ms.php index c88123a1fa..b2e4f86e88 100644 --- a/src/wp-admin/includes/ms.php +++ b/src/wp-admin/includes/ms.php @@ -40,7 +40,7 @@ function check_upload_size( $file ) { $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'] . ' ' . __( 'Back' ) . '' ); } diff --git a/src/wp-admin/includes/theme.php b/src/wp-admin/includes/theme.php index 73567e1fd1..9fbd71298c 100644 --- a/src/wp-admin/includes/theme.php +++ b/src/wp-admin/includes/theme.php @@ -459,7 +459,7 @@ function themes_api( $action, $args = array() ) { $request = wp_remote_post( $url, $http_args ); 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’s configuration. If you continue to have problems, please try the support forums.' ) . ' ' . __( '(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 ); diff --git a/src/wp-includes/class-wp-ajax-response.php b/src/wp-includes/class-wp-ajax-response.php index f02d24ee75..a86d8dd532 100644 --- a/src/wp-includes/class-wp-ajax-response.php +++ b/src/wp-includes/class-wp-ajax-response.php @@ -149,7 +149,7 @@ class WP_Ajax_Response { foreach ( (array) $this->responses as $response ) echo $response; echo ''; - if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) + if ( wp_doing_ajax() ) wp_die(); else die(); diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php index daa5884512..5ee3b6d90d 100644 --- a/src/wp-includes/class-wp-customize-manager.php +++ b/src/wp-includes/class-wp-customize-manager.php @@ -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; } diff --git a/src/wp-includes/comment.php b/src/wp-includes/comment.php index b816a51f4d..72434e0900 100644 --- a/src/wp-includes/comment.php +++ b/src/wp-includes/comment.php @@ -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’ve already said that!') ); } wp_die( __( 'Duplicate comment detected; it looks as though you’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 ); diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0e084fd66d..96312798fc 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -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; diff --git a/src/wp-includes/load.php b/src/wp-includes/load.php index c81cd9b055..403d460641 100644 --- a/src/wp-includes/load.php +++ b/src/wp-includes/load.php @@ -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 ); +} diff --git a/src/wp-includes/option.php b/src/wp-includes/option.php index 233c58d441..2293b0154f 100644 --- a/src/wp-includes/option.php +++ b/src/wp-includes/option.php @@ -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; } diff --git a/src/wp-includes/pluggable.php b/src/wp-includes/pluggable.php index 5c7747774b..0da1f73aaa 100644 --- a/src/wp-includes/pluggable.php +++ b/src/wp-includes/pluggable.php @@ -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' ); diff --git a/src/wp-includes/update.php b/src/wp-includes/update.php index 59b618c31a..5f074dcd14 100644 --- a/src/wp-includes/update.php +++ b/src/wp-includes/update.php @@ -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; }