From a7d053801d1b7cb02b8507c93b18334f63198382 Mon Sep 17 00:00:00 2001 From: Rachel Baker Date: Fri, 9 Apr 2021 21:26:07 +0000 Subject: [PATCH] REST API: Move the `rest_jsonp_enabled` filter before setting the Content-Type header. Fixes an issue where if JSONP was disabled the `Content-Type` HTTP header was still set to `application/javascript`. Props dd32, TimothyBlynJacobs. Fixes #52691. git-svn-id: https://develop.svn.wordpress.org/trunk@50695 602fd350-edb4-49c9-b593-d223f7449a82 --- .../rest-api/class-wp-rest-server.php | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php index ed94151228..1e85d57cd4 100644 --- a/src/wp-includes/rest-api/class-wp-rest-server.php +++ b/src/wp-includes/rest-api/class-wp-rest-server.php @@ -264,7 +264,21 @@ class WP_REST_Server { $current_user = null; } - $content_type = isset( $_GET['_jsonp'] ) ? 'application/javascript' : 'application/json'; + /** + * Filters whether JSONP is enabled for the REST API. + * + * @since 4.4.0 + * + * @param bool $jsonp_enabled Whether JSONP is enabled. Default true. + */ + $jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true ); + + $jsonp_callback = false; + if ( isset( $_GET['_jsonp'] ) ) { + $jsonp_callback = $_GET['_jsonp']; + } + + $content_type = ( $jsonp_callback && $jsonp_enabled ) ? 'application/javascript' : 'application/json'; $this->send_header( 'Content-Type', $content_type . '; charset=' . get_option( 'blog_charset' ) ); $this->send_header( 'X-Robots-Tag', 'noindex' ); @@ -355,24 +369,12 @@ class WP_REST_Server { ) ); - /** - * Filters whether JSONP is enabled for the REST API. - * - * @since 4.4.0 - * - * @param bool $jsonp_enabled Whether JSONP is enabled. Default true. - */ - $jsonp_enabled = apply_filters( 'rest_jsonp_enabled', true ); - - $jsonp_callback = null; - - if ( isset( $_GET['_jsonp'] ) ) { + if ( $jsonp_callback ) { if ( ! $jsonp_enabled ) { echo $this->json_error( 'rest_callback_disabled', __( 'JSONP support is disabled on this site.' ), 400 ); return false; } - $jsonp_callback = $_GET['_jsonp']; if ( ! wp_check_jsonp_callback( $jsonp_callback ) ) { echo $this->json_error( 'rest_callback_invalid', __( 'Invalid JSONP callback function.' ), 400 ); return false;