WordPress.org

Make WordPress Core

Changeset 48361


Ignore:
Timestamp:
07/07/2020 04:59:51 AM (2 months ago)
Author:
whyisjake
Message:

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.

Location:
trunk/src/wp-includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r48359 r48361  
    40394039 */
    40404040function wp_send_json( $response, $status_code = null ) {
     4041    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
     4042        _doing_it_wrong( __FUNCTION__, __( 'Return a WP_REST_Response or WP_Error object from your callback when using the REST API.' ), '5.5.0' );
     4043    }
     4044
    40414045    if ( ! headers_sent() ) {
    40424046        header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
  • trunk/src/wp-includes/rest-api.php

    r48357 r48361  
    185185        add_action( 'deprecated_argument_run', 'rest_handle_deprecated_argument', 10, 3 );
    186186        add_filter( 'deprecated_argument_trigger_error', '__return_false' );
     187        add_action( 'doing_it_wrong_run', 'rest_handle_doing_it_wrong', 10, 3 );
     188        add_filter( 'doing_it_wrong_trigger_error', '__return_false' );
    187189    }
    188190
     
    596598
    597599    header( sprintf( 'X-WP-DeprecatedParam: %s', $string ) );
     600}
     601
     602/**
     603 * Handles _doing_it_wrong errors.
     604 *
     605 * @since 5.5.0
     606 *
     607 * @param string      $function The function that was called.
     608 * @param string      $message  A message explaining what has been done incorrectly.
     609 * @param string|null $version  The version of WordPress where the message was added.
     610 */
     611function rest_handle_doing_it_wrong( $function, $message, $version ) {
     612    if ( ! WP_DEBUG || headers_sent() ) {
     613        return;
     614    }
     615
     616    if ( is_null( $version ) ) {
     617        /* translators: Developer debugging message. 1: PHP function name, 2: Explanatory message */
     618        $string = __( '%1$s (%2$s)' );
     619        $string = sprintf( $string, $function, $message );
     620    } else {
     621        /* translators: Developer debugging message. 1: PHP function name, 2: Version information message, 3: Explanatory message. */
     622        $string = __( '%1$s (since %2$s; %3$s)' );
     623        $string = sprintf( $string, $function, $version, $message );
     624    }
     625
     626    header( sprintf( 'X-WP-DoingItWrong: %s', $string ) );
    598627}
    599628
Note: See TracChangeset for help on using the changeset viewer.