From 7166def7ece1ccaef93dd3512625f9b58c6aab3a Mon Sep 17 00:00:00 2001 From: Jake Spurlock Date: Tue, 7 Jul 2020 04:59:51 +0000 Subject: [PATCH] REST API: Trigger `_doing_it_wrong()` if `wp_send_json()` is used on a REST API request In addition to triggering the `_doing_it_wrong()` logging, also adds a `X-WP-DoingItWrong` header. Fixes #36271. Props rmccue, TimothyBlynJacobs. git-svn-id: https://develop.svn.wordpress.org/trunk@48361 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 4 ++++ src/wp-includes/rest-api.php | 29 +++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index f7ee143c57..b1f74c68ac 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -4038,6 +4038,10 @@ function _wp_json_prepare_data( $data ) { * @param int $status_code The HTTP status code to output. */ function wp_send_json( $response, $status_code = null ) { + if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { + _doing_it_wrong( __FUNCTION__, __( 'Return a WP_REST_Response or WP_Error object from your callback when using the REST API.' ), '5.5.0' ); + } + if ( ! headers_sent() ) { header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); if ( null !== $status_code ) { diff --git a/src/wp-includes/rest-api.php b/src/wp-includes/rest-api.php index e0387500f0..6baef3df49 100644 --- a/src/wp-includes/rest-api.php +++ b/src/wp-includes/rest-api.php @@ -184,6 +184,8 @@ function rest_api_default_filters() { add_filter( 'deprecated_function_trigger_error', '__return_false' ); add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 ); add_filter( 'deprecated_argument_trigger_error', '__return_false' ); + add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 ); + add_filter( 'doing_it_wrong_trigger_error', '__return_false' ); } // Default serving. @@ -597,6 +599,33 @@ function rest_handle_deprecated_argument( $function, $message, $version ) { header( sprintf( 'X-WP-DeprecatedParam: %s', $string ) ); } +/** + * Handles _doing_it_wrong errors. + * + * @since 5.5.0 + * + * @param string $function The function that was called. + * @param string $message A message explaining what has been done incorrectly. + * @param string|null $version The version of WordPress where the message was added. + */ +function rest_handle_doing_it_wrong( $function, $message, $version ) { + if ( ! WP_DEBUG || headers_sent() ) { + return; + } + + if ( is_null( $version ) ) { + /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message */ + $string = __( '%1$s (%2$s)' ); + $string = sprintf( $string, $function, $message ); + } else { + /* translators: Developer debugging message. 1: PHP function name, 2: Version information message, 3: Explanatory message. */ + $string = __( '%1$s (since %2$s; %3$s)' ); + $string = sprintf( $string, $function, $version, $message ); + } + + header( sprintf( 'X-WP-DoingItWrong: %s', $string ) ); +} + /** * Sends Cross-Origin Resource Sharing headers with API requests. *