WordPress.org

Make WordPress Core

Ticket #44534: 44534.3.diff

File 44534.3.diff, 1.4 KB (added by earnjam, 2 years ago)

Adds check for HTTP Accept header

  • src/wp-includes/load.php

    diff --git src/wp-includes/load.php src/wp-includes/load.php
    index e2b388fb77..0d8db5e42a 100644
    function wp_debug_mode() { 
    333333                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 );
    334334        }
    335335
    336         if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() ) {
     336        if ( defined( 'XMLRPC_REQUEST' ) || defined( 'REST_REQUEST' ) || ( defined( 'WP_INSTALLING' ) && WP_INSTALLING ) || wp_doing_ajax() || wp_is_json_request() ) {
    337337                @ini_set( 'display_errors', 0 );
    338338        }
    339339}
    function wp_finalize_scraping_edited_file_errors( $scrape_key ) { 
    11621162        }
    11631163        echo "\n###### wp_scraping_result_end:$scrape_key ######\n";
    11641164}
     1165
     1166/**
     1167 * Check whether current request is a JSON request, or is expecting a JSON response
     1168 *
     1169 * @since 5.0.0
     1170 *
     1171 * @return bool True if Accepts or Content-Type headers contain application/json, false otherwise
     1172 */
     1173function wp_is_json_request() {
     1174
     1175        if ( isset( $_SERVER['HTTP_ACCEPT'] ) && strpos( $_SERVER['HTTP_ACCEPT'], 'application/json' ) !== false ) {
     1176                return true;
     1177        }
     1178
     1179        if ( isset( $_SERVER['CONTENT_TYPE'] ) && 'application/json' === $_SERVER['CONTENT_TYPE'] ) {
     1180                return true;
     1181        }
     1182
     1183        return false;
     1184
     1185}