General: Add $options parameter to JSON response functions:

* `wp_send_json()`
* `wp_send_json_success()`
* `wp_send_json_error()`

This allows for customizing the options passed to `json_encode()`.

Props eroraghav, hareesh-pillai, garrett-eclipse.
Fixes #51293.

git-svn-id: https://develop.svn.wordpress.org/trunk@49235 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Sergey Biryukov 2020-10-20 15:54:43 +00:00
parent 2defb1ef6c
commit e3f28a9a11

View File

@ -4078,12 +4078,14 @@ function _wp_json_prepare_data( $data ) {
*
* @since 3.5.0
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$options` parameter was added.
*
* @param mixed $response Variable (usually an array or object) to encode as JSON,
* then print and die.
* @param int $status_code The HTTP status code to output.
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $options Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json( $response, $status_code = null ) {
function wp_send_json( $response, $status_code = null, $options = 0 ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
_doing_it_wrong(
__FUNCTION__,
@ -4104,7 +4106,7 @@ function wp_send_json( $response, $status_code = null ) {
}
}
echo wp_json_encode( $response );
echo wp_json_encode( $response, $options );
if ( wp_doing_ajax() ) {
wp_die(
@ -4124,18 +4126,20 @@ function wp_send_json( $response, $status_code = null ) {
*
* @since 3.5.0
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$options` parameter was added.
*
* @param mixed $data Data to encode as JSON, then print and die.
* @param int $status_code The HTTP status code to output.
* @param mixed $data Optional. Data to encode as JSON, then print and die. Default null.
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $options Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json_success( $data = null, $status_code = null ) {
function wp_send_json_success( $data = null, $status_code = null, $options = 0 ) {
$response = array( 'success' => true );
if ( isset( $data ) ) {
$response['data'] = $data;
}
wp_send_json( $response, $status_code );
wp_send_json( $response, $status_code, $options );
}
/**
@ -4149,11 +4153,13 @@ function wp_send_json_success( $data = null, $status_code = null ) {
* @since 3.5.0
* @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$options` parameter was added.
*
* @param mixed $data Data to encode as JSON, then print and die.
* @param int $status_code The HTTP status code to output.
* @param mixed $data Optional. Data to encode as JSON, then print and die. Default null.
* @param int $status_code Optional. The HTTP status code to output. Default null.
* @param int $options Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json_error( $data = null, $status_code = null ) {
function wp_send_json_error( $data = null, $status_code = null, $options = 0 ) {
$response = array( 'success' => false );
if ( isset( $data ) ) {
@ -4174,7 +4180,7 @@ function wp_send_json_error( $data = null, $status_code = null ) {
}
}
wp_send_json( $response, $status_code );
wp_send_json( $response, $status_code, $options );
}
/**