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.
Built from https://develop.svn.wordpress.org/trunk@49235


git-svn-id: http://core.svn.wordpress.org/trunk@48997 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Sergey Biryukov 2020-10-20 15:56:08 +00:00
parent e856a91321
commit e2e3c8ec47
2 changed files with 18 additions and 12 deletions

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 );
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.6-alpha-49234';
$wp_version = '5.6-alpha-49235';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.