From e2e3c8ec479f043a87bdd122f11d8894dbb2e963 Mon Sep 17 00:00:00 2001 From: Sergey Biryukov Date: Tue, 20 Oct 2020 15:56:08 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 28 +++++++++++++++++----------- wp-includes/version.php | 2 +- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 7b5aeb8948..537425d898 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -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 ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 79b19a670f..f79dc901f6 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -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.