REST API: Introduce _pretty query parameter to opt in to JSON_PRETTY_PRINT.
Add support for a "_pretty" meta-parameter on all REST controllers which instructs WordPress to return pretty-printed JSON, for better readability when inspecting endpoint responses in curl output or certain developer tools. Introduce the "rest_json_encode_options" filter to permit site owners to control this behavior globally. Props Viper007Bond, TimothyBlynJacobs, chrisguitarguy, johnbillion, swissspidy, adamsilverstein, danielbachhuber, rmccue. Fixes #41998. Built from https://develop.svn.wordpress.org/trunk@54127 git-svn-id: http://core.svn.wordpress.org/trunk@53686 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
parent
7973933ad6
commit
bd607c1118
|
@ -230,6 +230,33 @@ class WP_REST_Server {
|
|||
return wp_json_encode( $error );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the encoding options passed to {@see wp_json_encode}.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @param \WP_REST_Request $request The current request object.
|
||||
*
|
||||
* @return int The JSON encode options.
|
||||
*/
|
||||
protected function get_json_encode_options( WP_REST_Request $request ) {
|
||||
$options = 0;
|
||||
|
||||
if ( $request->has_param( '_pretty' ) ) {
|
||||
$options |= JSON_PRETTY_PRINT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filters the JSON encoding options used to send the REST API response.
|
||||
*
|
||||
* @since 6.1.0
|
||||
*
|
||||
* @param int $options JSON encoding options {@see json_encode()}.
|
||||
* @param WP_REST_Request $request Current request object.
|
||||
*/
|
||||
return apply_filters( 'rest_json_encode_options', $options, $request );
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles serving a REST API request.
|
||||
*
|
||||
|
@ -493,7 +520,7 @@ class WP_REST_Server {
|
|||
return null;
|
||||
}
|
||||
|
||||
$result = wp_json_encode( $result );
|
||||
$result = wp_json_encode( $result, $this->get_json_encode_options( $request ) );
|
||||
|
||||
$json_error_message = $this->get_json_last_error();
|
||||
|
||||
|
@ -506,7 +533,7 @@ class WP_REST_Server {
|
|||
);
|
||||
|
||||
$result = $this->error_to_response( $json_error_obj );
|
||||
$result = wp_json_encode( $result->data );
|
||||
$result = wp_json_encode( $result->data, $this->get_json_encode_options( $request ) );
|
||||
}
|
||||
|
||||
if ( $jsonp_callback ) {
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
*
|
||||
* @global string $wp_version
|
||||
*/
|
||||
$wp_version = '6.1-alpha-54126';
|
||||
$wp_version = '6.1-alpha-54127';
|
||||
|
||||
/**
|
||||
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.
|
||||
|
|
Loading…
Reference in New Issue