From 348c50b2a857109f2eaf0fb0f4db616b3e985fd6 Mon Sep 17 00:00:00 2001 From: John Blackbourn Date: Sun, 28 Aug 2016 17:42:28 +0000 Subject: [PATCH] Formatting: Add a parameter to `wp_send_json_error()`, `wp_send_json_success()`, and `wp_send_json()` for specifying the HTTP response code. Defaults to `200` in all cases, but can be used, for example, to return a `403` when using `wp_send_json_error()`. Fixes #35666 Props stephenharris Built from https://develop.svn.wordpress.org/trunk@38422 git-svn-id: http://core.svn.wordpress.org/trunk@38363 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/functions.php | 25 ++++++++++++++++--------- wp-includes/version.php | 2 +- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index d3a8bcd2d7..ca445ec610 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3090,12 +3090,15 @@ function _wp_json_prepare_data( $data ) { * Send a JSON response back to an Ajax request. * * @since 3.5.0 + * @since 4.7.0 The `$status_code` parameter was added. * - * @param mixed $response Variable (usually an array or object) to encode as JSON, - * then print and die. + * @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. */ -function wp_send_json( $response ) { +function wp_send_json( $response, $status_code = 200 ) { @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); + status_header( $status_code ); echo wp_json_encode( $response ); if ( wp_doing_ajax() ) wp_die(); @@ -3107,16 +3110,18 @@ function wp_send_json( $response ) { * Send a JSON response back to an Ajax request, indicating success. * * @since 3.5.0 + * @since 4.7.0 The `$status_code` parameter was added. * - * @param mixed $data Data to encode as JSON, then print and die. + * @param mixed $data Data to encode as JSON, then print and die. + * @param int $status_code The HTTP status code to output. */ -function wp_send_json_success( $data = null ) { +function wp_send_json_success( $data = null, $status_code = 200 ) { $response = array( 'success' => true ); if ( isset( $data ) ) $response['data'] = $data; - wp_send_json( $response ); + wp_send_json( $response, $status_code ); } /** @@ -3129,10 +3134,12 @@ function wp_send_json_success( $data = 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. * - * @param mixed $data Data to encode as JSON, then print and die. + * @param mixed $data Data to encode as JSON, then print and die. + * @param int $status_code The HTTP status code to output. */ -function wp_send_json_error( $data = null ) { +function wp_send_json_error( $data = null, $status_code = 200 ) { $response = array( 'success' => false ); if ( isset( $data ) ) { @@ -3150,7 +3157,7 @@ function wp_send_json_error( $data = null ) { } } - wp_send_json( $response ); + wp_send_json( $response, $status_code ); } /** diff --git a/wp-includes/version.php b/wp-includes/version.php index 07027bf891..b53408dc1f 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -4,7 +4,7 @@ * * @global string $wp_version */ -$wp_version = '4.7-alpha-38421'; +$wp_version = '4.7-alpha-38422'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.