WordPress.org

Make WordPress Core

Changeset 43730


Ignore:
Timestamp:
10/15/2018 10:05:18 PM (3 months ago)
Author:
danielbachhuber
Message:

Load: Disable PHP errors for JSON requests

Because WP REST API requests aren't identified until parse_request, it's impractical to reference the REST_REQUEST constant in wp_debug_mode(). Instead, it's more helpful to assume that a request wanting a JSON response probably doesn't want PHP errors breaking the response.

Props chrisl27, duanestorey, earnjam.
Fixes #44534.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/5.0/src/wp-includes/load.php

    r43722 r43730  
    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    }
     
    11631163    echo "\n###### wp_scraping_result_end:$scrape_key ######\n";
    11641164}
     1165
     1166/**
     1167 * Checks 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'] ) && false !== strpos( $_SERVER['HTTP_ACCEPT'], 'application/json' ) ) {
     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}
Note: See TracChangeset for help on using the changeset viewer.