Skip to content

Commit

Permalink
General: Rename wp_json_encode() parameters for parity with PHP Core.
Browse files Browse the repository at this point in the history
`wp_json_encode()` is a wrapper for the PHP native `json_encode()` function with some extra safety checks.

This commit renames the `$data` parameter in the `wp_json_encode()` function and associated functions to `$value`, and the `$options` parameter to `$flags` for parity with the parameter names used in PHP Core.

Reference: [https://www.php.net/manual/en/function.json-encode.php PHP Manual: json_encode()].

Follow-up to [30055].

Props jrf, hellofromTonya.
Fixes #59630.

git-svn-id: https://develop.svn.wordpress.org/trunk@57130 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information
SergeyBiryukov committed Nov 21, 2023
1 parent b6bf355 commit 2452701
Showing 1 changed file with 45 additions and 43 deletions.
88 changes: 45 additions & 43 deletions src/wp-includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -4288,28 +4288,30 @@ function _wp_die_process_input( $message, $title = '', $args = array() ) {
*
* @since 4.1.0
* @since 5.3.0 No longer handles support for PHP < 5.6.
* @since 6.5.0 The `$data` parameter has been renamed to `$value` and
* the `$options` parameter to `$flags` for parity with PHP.
*
* @param mixed $data Variable (usually an array or object) to encode as JSON.
* @param int $options Optional. Options to be passed to json_encode(). Default 0.
* @param int $depth Optional. Maximum depth to walk through $data. Must be
* greater than 0. Default 512.
* @param mixed $value Variable (usually an array or object) to encode as JSON.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
* @param int $depth Optional. Maximum depth to walk through $value. Must be
* greater than 0. Default 512.
* @return string|false The JSON encoded string, or false if it cannot be encoded.
*/
function wp_json_encode( $data, $options = 0, $depth = 512 ) {
$json = json_encode( $data, $options, $depth );
function wp_json_encode( $value, $flags = 0, $depth = 512 ) {
$json = json_encode( $value, $flags, $depth );

// If json_encode() was successful, no need to do more sanity checking.
if ( false !== $json ) {
return $json;
}

try {
$data = _wp_json_sanity_check( $data, $depth );
$value = _wp_json_sanity_check( $value, $depth );
} catch ( Exception $e ) {
return false;
}

return json_encode( $data, $options, $depth );
return json_encode( $value, $flags, $depth );
}

/**
Expand All @@ -4323,18 +4325,18 @@ function wp_json_encode( $data, $options = 0, $depth = 512 ) {
*
* @throws Exception If depth limit is reached.
*
* @param mixed $data Variable (usually an array or object) to encode as JSON.
* @param int $depth Maximum depth to walk through $data. Must be greater than 0.
* @param mixed $value Variable (usually an array or object) to encode as JSON.
* @param int $depth Maximum depth to walk through $value. Must be greater than 0.
* @return mixed The sanitized data that shall be encoded to JSON.
*/
function _wp_json_sanity_check( $data, $depth ) {
function _wp_json_sanity_check( $value, $depth ) {
if ( $depth < 0 ) {
throw new Exception( 'Reached depth limit' );
}

if ( is_array( $data ) ) {
if ( is_array( $value ) ) {
$output = array();
foreach ( $data as $id => $el ) {
foreach ( $value as $id => $el ) {
// Don't forget to sanitize the ID!
if ( is_string( $id ) ) {
$clean_id = _wp_json_convert_string( $id );
Expand All @@ -4351,9 +4353,9 @@ function _wp_json_sanity_check( $data, $depth ) {
$output[ $clean_id ] = $el;
}
}
} elseif ( is_object( $data ) ) {
} elseif ( is_object( $value ) ) {
$output = new stdClass();
foreach ( $data as $id => $el ) {
foreach ( $value as $id => $el ) {
if ( is_string( $id ) ) {
$clean_id = _wp_json_convert_string( $id );
} else {
Expand All @@ -4368,10 +4370,10 @@ function _wp_json_sanity_check( $data, $depth ) {
$output->$clean_id = $el;
}
}
} elseif ( is_string( $data ) ) {
return _wp_json_convert_string( $data );
} elseif ( is_string( $value ) ) {
return _wp_json_convert_string( $value );
} else {
return $data;
return $value;
}

return $output;
Expand Down Expand Up @@ -4418,27 +4420,27 @@ function _wp_json_convert_string( $input_string ) {
* has been dropped.
* @access private
*
* @param mixed $data Native representation.
* @param mixed $value Native representation.
* @return bool|int|float|null|string|array Data ready for `json_encode()`.
*/
function _wp_json_prepare_data( $data ) {
function _wp_json_prepare_data( $value ) {
_deprecated_function( __FUNCTION__, '5.3.0' );
return $data;
return $value;
}

/**
* Sends a JSON response back to an Ajax request.
*
* @since 3.5.0
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$options` parameter was added.
* @since 5.6.0 The `$flags` parameter was added.
*
* @param mixed $response Variable (usually an array or object) to encode as JSON,
* then print and die.
* @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.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json( $response, $status_code = null, $options = 0 ) {
function wp_send_json( $response, $status_code = null, $flags = 0 ) {
if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
_doing_it_wrong(
__FUNCTION__,
Expand All @@ -4459,7 +4461,7 @@ function wp_send_json( $response, $status_code = null, $options = 0 ) {
}
}

echo wp_json_encode( $response, $options );
echo wp_json_encode( $response, $flags );

if ( wp_doing_ajax() ) {
wp_die(
Expand All @@ -4479,46 +4481,46 @@ function wp_send_json( $response, $status_code = null, $options = 0 ) {
*
* @since 3.5.0
* @since 4.7.0 The `$status_code` parameter was added.
* @since 5.6.0 The `$options` parameter was added.
* @since 5.6.0 The `$flags` parameter was added.
*
* @param mixed $data Optional. Data to encode as JSON, then print and die. Default null.
* @param mixed $value 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.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json_success( $data = null, $status_code = null, $options = 0 ) {
function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {
$response = array( 'success' => true );

if ( isset( $data ) ) {
$response['data'] = $data;
if ( isset( $value ) ) {
$response['data'] = $value;
}

wp_send_json( $response, $status_code, $options );
wp_send_json( $response, $status_code, $flags );
}

/**
* Sends a JSON response back to an Ajax request, indicating failure.
*
* If the `$data` parameter is a WP_Error object, the errors
* If the `$value` parameter is a WP_Error object, the errors
* within the object are processed and output as an array of error
* codes and corresponding messages. All other types are output
* without further processing.
*
* @since 3.5.0
* @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
* @since 4.1.0 The `$value` 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.
* @since 5.6.0 The `$flags` parameter was added.
*
* @param mixed $data Optional. Data to encode as JSON, then print and die. Default null.
* @param mixed $value 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.
* @param int $flags Optional. Options to be passed to json_encode(). Default 0.
*/
function wp_send_json_error( $data = null, $status_code = null, $options = 0 ) {
function wp_send_json_error( $value = null, $status_code = null, $flags = 0 ) {
$response = array( 'success' => false );

if ( isset( $data ) ) {
if ( is_wp_error( $data ) ) {
if ( isset( $value ) ) {
if ( is_wp_error( $value ) ) {
$result = array();
foreach ( $data->errors as $code => $messages ) {
foreach ( $value->errors as $code => $messages ) {
foreach ( $messages as $message ) {
$result[] = array(
'code' => $code,
Expand All @@ -4529,11 +4531,11 @@ function wp_send_json_error( $data = null, $status_code = null, $options = 0 ) {

$response['data'] = $result;
} else {
$response['data'] = $data;
$response['data'] = $value;
}
}

wp_send_json( $response, $status_code, $options );
wp_send_json( $response, $status_code, $flags );
}

/**
Expand Down

0 comments on commit 2452701

Please sign in to comment.