Make WordPress Core

Ticket #34999: 34999.diff

File 34999.diff, 2.7 KB (added by joehoyle, 9 years ago)
  • src/wp-includes/rest-api/class-wp-rest-server.php

     
    858858                                        if ( null !== $dispatch_result ) {
    859859                                                $response = $dispatch_result;
    860860                                        } else {
    861                                                 $response = call_user_func( $callback, $request );
     861                                                try {
     862                                                        $response = call_user_func( $callback, $request );
     863                                                } catch ( Exception $e ) {
     864
     865                                                        /**
     866                                                         * A message propagated via wp_die() is safe to show to the user, any other Exception is not
     867                                                         * okay to output for information disclosure reasons.
     868                                                         */
     869                                                        if ( strpos( $e->getMessage(), 'wp_die():' ) === 0 ) {
     870                                                                $response = new WP_Error( 'wp-die-error', $e->getMessage(), array( 'status' => $e->getCode() ) );
     871                                                        } else {
     872                                                                $response = new WP_Error( 'unknown-exception', __( "An unexpected error occured." ), array( 'status' => 500 ) );
     873                                                        }
     874                                                }
    862875                                        }
    863876                                }
    864877
  • src/wp-includes/rest-api.php

     
    117117        add_filter( 'rest_post_dispatch', 'rest_send_allow_header', 10, 3 );
    118118
    119119        add_filter( 'rest_pre_dispatch', 'rest_handle_options_request', 10, 3 );
     120        add_filter( 'rest_pre_dispatch', 'rest_add_wp_die_handler_filter' );
    120121}
    121122
    122123/**
     
    648649
    649650        return array( $local, $utc );
    650651}
     652
     653
     654/**
     655 * Add the filter for the custom wp_die handler.
     656 *
     657 * This is hooked on rest_pre_dispatch, so any uses of wp_die in endpoints
     658 * will be wrapped by our custom wp_die handler to output JSON rather than the
     659 * usual HTML error page.
     660 *
     661 * @since 4.5.0
     662 */
     663function rest_add_wp_die_handler_filter() {
     664        add_filter( 'wp_die_handler', 'rest_wp_die_handler_callback' );
     665}
     666
     667/**
     668 * Callback to set the wp_die handler to rest_wp_die_handler.
     669 *
     670 * @since 4.5.0
     671 * @return string
     672 */
     673function rest_wp_die_handler_callback() {
     674        return 'rest_wp_die_handler';
     675}
     676
     677/**
     678 * A custom wp_die handler for use in the REST API.
     679 *
     680 * When wp_die() is invoked from within an endpoint callback, we override the handler
     681 * to output a valid JSON response.
     682 *
     683 * @since 4.5.9
     684 * @param string $message
     685 * @param string $title
     686 * @param array $args
     687 */
     688function rest_wp_die_handler( $message, $title = '', $args = array() ) {
     689
     690        $args = wp_parse_args( $args, array( 'response' => 500 ) );
     691
     692        if ( $title ) {
     693                $message = "$title: $message";
     694        }
     695
     696        throw new Exception( 'wp_die(): ' . $message , $args['response'] );
     697}