Make WordPress Core


Ignore:
Timestamp:
11/21/2023 12:23:01 AM (6 months ago)
Author:
SergeyBiryukov
Message:

General: Rename wp_json_encode() parameters for parity with PHP Core.

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: PHP Manual: json_encode().

Follow-up to [30055].

Props jrf, hellofromTonya.
Fixes #59630.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/functions.php

    r56994 r57130  
    42894289 * @since 4.1.0
    42904290 * @since 5.3.0 No longer handles support for PHP < 5.6.
    4291  *
    4292  * @param mixed $data    Variable (usually an array or object) to encode as JSON.
    4293  * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
    4294  * @param int   $depth   Optional. Maximum depth to walk through $data. Must be
    4295  *                       greater than 0. Default 512.
     4291 * @since 6.5.0 The `$data` parameter has been renamed to `$value` and
     4292 *              the `$options` parameter to `$flags` for parity with PHP.
     4293 *
     4294 * @param mixed $value Variable (usually an array or object) to encode as JSON.
     4295 * @param int   $flags Optional. Options to be passed to json_encode(). Default 0.
     4296 * @param int   $depth Optional. Maximum depth to walk through $value. Must be
     4297 *                     greater than 0. Default 512.
    42964298 * @return string|false The JSON encoded string, or false if it cannot be encoded.
    42974299 */
    4298 function wp_json_encode( $data, $options = 0, $depth = 512 ) {
    4299     $json = json_encode( $data, $options, $depth );
     4300function wp_json_encode( $value, $flags = 0, $depth = 512 ) {
     4301    $json = json_encode( $value, $flags, $depth );
    43004302
    43014303    // If json_encode() was successful, no need to do more sanity checking.
     
    43054307
    43064308    try {
    4307         $data = _wp_json_sanity_check( $data, $depth );
     4309        $value = _wp_json_sanity_check( $value, $depth );
    43084310    } catch ( Exception $e ) {
    43094311        return false;
    43104312    }
    43114313
    4312     return json_encode( $data, $options, $depth );
     4314    return json_encode( $value, $flags, $depth );
    43134315}
    43144316
     
    43244326 * @throws Exception If depth limit is reached.
    43254327 *
    4326  * @param mixed $data Variable (usually an array or object) to encode as JSON.
    4327  * @param int   $depth Maximum depth to walk through $data. Must be greater than 0.
     4328 * @param mixed $value Variable (usually an array or object) to encode as JSON.
     4329 * @param int   $depth Maximum depth to walk through $value. Must be greater than 0.
    43284330 * @return mixed The sanitized data that shall be encoded to JSON.
    43294331 */
    4330 function _wp_json_sanity_check( $data, $depth ) {
     4332function _wp_json_sanity_check( $value, $depth ) {
    43314333    if ( $depth < 0 ) {
    43324334        throw new Exception( 'Reached depth limit' );
    43334335    }
    43344336
    4335     if ( is_array( $data ) ) {
     4337    if ( is_array( $value ) ) {
    43364338        $output = array();
    4337         foreach ( $data as $id => $el ) {
     4339        foreach ( $value as $id => $el ) {
    43384340            // Don't forget to sanitize the ID!
    43394341            if ( is_string( $id ) ) {
     
    43524354            }
    43534355        }
    4354     } elseif ( is_object( $data ) ) {
     4356    } elseif ( is_object( $value ) ) {
    43554357        $output = new stdClass();
    4356         foreach ( $data as $id => $el ) {
     4358        foreach ( $value as $id => $el ) {
    43574359            if ( is_string( $id ) ) {
    43584360                $clean_id = _wp_json_convert_string( $id );
     
    43694371            }
    43704372        }
    4371     } elseif ( is_string( $data ) ) {
    4372         return _wp_json_convert_string( $data );
     4373    } elseif ( is_string( $value ) ) {
     4374        return _wp_json_convert_string( $value );
    43734375    } else {
    4374         return $data;
     4376        return $value;
    43754377    }
    43764378
     
    44194421 * @access private
    44204422 *
    4421  * @param mixed $data Native representation.
     4423 * @param mixed $value Native representation.
    44224424 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
    44234425 */
    4424 function _wp_json_prepare_data( $data ) {
     4426function _wp_json_prepare_data( $value ) {
    44254427    _deprecated_function( __FUNCTION__, '5.3.0' );
    4426     return $data;
     4428    return $value;
    44274429}
    44284430
     
    44324434 * @since 3.5.0
    44334435 * @since 4.7.0 The `$status_code` parameter was added.
    4434  * @since 5.6.0 The `$options` parameter was added.
     4436 * @since 5.6.0 The `$flags` parameter was added.
    44354437 *
    44364438 * @param mixed $response    Variable (usually an array or object) to encode as JSON,
    44374439 *                           then print and die.
    44384440 * @param int   $status_code Optional. The HTTP status code to output. Default null.
    4439  * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
    4440  */
    4441 function wp_send_json( $response, $status_code = null, $options = 0 ) {
     4441 * @param int   $flags       Optional. Options to be passed to json_encode(). Default 0.
     4442 */
     4443function wp_send_json( $response, $status_code = null, $flags = 0 ) {
    44424444    if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) {
    44434445        _doing_it_wrong(
     
    44604462    }
    44614463
    4462     echo wp_json_encode( $response, $options );
     4464    echo wp_json_encode( $response, $flags );
    44634465
    44644466    if ( wp_doing_ajax() ) {
     
    44804482 * @since 3.5.0
    44814483 * @since 4.7.0 The `$status_code` parameter was added.
    4482  * @since 5.6.0 The `$options` parameter was added.
    4483  *
    4484  * @param mixed $data        Optional. Data to encode as JSON, then print and die. Default null.
     4484 * @since 5.6.0 The `$flags` parameter was added.
     4485 *
     4486 * @param mixed $value       Optional. Data to encode as JSON, then print and die. Default null.
    44854487 * @param int   $status_code Optional. The HTTP status code to output. Default null.
    4486  * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
    4487  */
    4488 function wp_send_json_success( $data = null, $status_code = null, $options = 0 ) {
     4488 * @param int   $flags       Optional. Options to be passed to json_encode(). Default 0.
     4489 */
     4490function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) {
    44894491    $response = array( 'success' => true );
    44904492
    4491     if ( isset( $data ) ) {
    4492         $response['data'] = $data;
    4493     }
    4494 
    4495     wp_send_json( $response, $status_code, $options );
     4493    if ( isset( $value ) ) {
     4494        $response['data'] = $value;
     4495    }
     4496
     4497    wp_send_json( $response, $status_code, $flags );
    44964498}
    44974499
     
    44994501 * Sends a JSON response back to an Ajax request, indicating failure.
    45004502 *
    4501  * If the `$data` parameter is a WP_Error object, the errors
     4503 * If the `$value` parameter is a WP_Error object, the errors
    45024504 * within the object are processed and output as an array of error
    45034505 * codes and corresponding messages. All other types are output
     
    45054507 *
    45064508 * @since 3.5.0
    4507  * @since 4.1.0 The `$data` parameter is now processed if a WP_Error object is passed in.
     4509 * @since 4.1.0 The `$value` parameter is now processed if a WP_Error object is passed in.
    45084510 * @since 4.7.0 The `$status_code` parameter was added.
    4509  * @since 5.6.0 The `$options` parameter was added.
    4510  *
    4511  * @param mixed $data        Optional. Data to encode as JSON, then print and die. Default null.
     4511 * @since 5.6.0 The `$flags` parameter was added.
     4512 *
     4513 * @param mixed $value       Optional. Data to encode as JSON, then print and die. Default null.
    45124514 * @param int   $status_code Optional. The HTTP status code to output. Default null.
    4513  * @param int   $options     Optional. Options to be passed to json_encode(). Default 0.
    4514  */
    4515 function wp_send_json_error( $data = null, $status_code = null, $options = 0 ) {
     4515 * @param int   $flags       Optional. Options to be passed to json_encode(). Default 0.
     4516 */
     4517function wp_send_json_error( $value = null, $status_code = null, $flags = 0 ) {
    45164518    $response = array( 'success' => false );
    45174519
    4518     if ( isset( $data ) ) {
    4519         if ( is_wp_error( $data ) ) {
     4520    if ( isset( $value ) ) {
     4521        if ( is_wp_error( $value ) ) {
    45204522            $result = array();
    4521             foreach ( $data->errors as $code => $messages ) {
     4523            foreach ( $value->errors as $code => $messages ) {
    45224524                foreach ( $messages as $message ) {
    45234525                    $result[] = array(
     
    45304532            $response['data'] = $result;
    45314533        } else {
    4532             $response['data'] = $data;
    4533         }
    4534     }
    4535 
    4536     wp_send_json( $response, $status_code, $options );
     4534            $response['data'] = $value;
     4535        }
     4536    }
     4537
     4538    wp_send_json( $response, $status_code, $flags );
    45374539}
    45384540
Note: See TracChangeset for help on using the changeset viewer.