From 4df4a4a99b3a29c6704e0e6683ddd2d6d83af986 Mon Sep 17 00:00:00 2001
From: Dustin Falgout <dustin@falgout.us>
Date: Mon, 2 Oct 2017 06:11:37 -0500
Subject: [PATCH] Add new utility function: wp_doing_rest() which works exactly
 like the current wp_doing_ajax/cron functions, but for Rest API requests.

---
 wp-includes/load.php | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/wp-includes/load.php b/wp-includes/load.php
index 63a4b0f64c9..8d79fb33f95 100644
--- a/wp-includes/load.php
+++ b/wp-includes/load.php
@@ -333,7 +333,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( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
+	if ( defined( 'XMLRPC_REQUEST' ) || wp_doing_rest() || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
 		@ini_set( 'display_errors', 0 );
 	}
 }
@@ -1080,6 +1080,24 @@ function wp_doing_cron() {
 }
 
 /**
+ * Determines if the current request is a WordPress Rest API request.
+ *
+ * @since 4.9.0
+ *
+ * @return bool True if it's a WordPress Rest API request, false otherwise.
+ */
+function wp_doing_rest() {
+	/**
+	 * Filters whether or not the current request is a WordPress Rest API request.
+	 *
+	 * @since 4.9.0
+	 *
+	 * @param bool $wp_doing_rest Whether or not the current request is a WordPress Rest API request.
+	 */
+	return apply_filters( 'wp_doing_rest', defined( 'REST_REQUEST' ) && REST_REQUEST );
+}
+
+/**
  * Check whether variable is a WordPress Error.
  *
  * Returns true if $thing is an object of the WP_Error class.
