- Timestamp:
- 11/21/2023 12:23:01 AM (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r56994 r57130 4289 4289 * @since 4.1.0 4290 4290 * @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. 4296 4298 * @return string|false The JSON encoded string, or false if it cannot be encoded. 4297 4299 */ 4298 function wp_json_encode( $ data, $options = 0, $depth = 512 ) {4299 $json = json_encode( $ data, $options, $depth );4300 function wp_json_encode( $value, $flags = 0, $depth = 512 ) { 4301 $json = json_encode( $value, $flags, $depth ); 4300 4302 4301 4303 // If json_encode() was successful, no need to do more sanity checking. … … 4305 4307 4306 4308 try { 4307 $ data = _wp_json_sanity_check( $data, $depth );4309 $value = _wp_json_sanity_check( $value, $depth ); 4308 4310 } catch ( Exception $e ) { 4309 4311 return false; 4310 4312 } 4311 4313 4312 return json_encode( $ data, $options, $depth );4314 return json_encode( $value, $flags, $depth ); 4313 4315 } 4314 4316 … … 4324 4326 * @throws Exception If depth limit is reached. 4325 4327 * 4326 * @param mixed $ dataVariable (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. 4328 4330 * @return mixed The sanitized data that shall be encoded to JSON. 4329 4331 */ 4330 function _wp_json_sanity_check( $ data, $depth ) {4332 function _wp_json_sanity_check( $value, $depth ) { 4331 4333 if ( $depth < 0 ) { 4332 4334 throw new Exception( 'Reached depth limit' ); 4333 4335 } 4334 4336 4335 if ( is_array( $ data) ) {4337 if ( is_array( $value ) ) { 4336 4338 $output = array(); 4337 foreach ( $ dataas $id => $el ) {4339 foreach ( $value as $id => $el ) { 4338 4340 // Don't forget to sanitize the ID! 4339 4341 if ( is_string( $id ) ) { … … 4352 4354 } 4353 4355 } 4354 } elseif ( is_object( $ data) ) {4356 } elseif ( is_object( $value ) ) { 4355 4357 $output = new stdClass(); 4356 foreach ( $ dataas $id => $el ) {4358 foreach ( $value as $id => $el ) { 4357 4359 if ( is_string( $id ) ) { 4358 4360 $clean_id = _wp_json_convert_string( $id ); … … 4369 4371 } 4370 4372 } 4371 } elseif ( is_string( $ data) ) {4372 return _wp_json_convert_string( $ data);4373 } elseif ( is_string( $value ) ) { 4374 return _wp_json_convert_string( $value ); 4373 4375 } else { 4374 return $ data;4376 return $value; 4375 4377 } 4376 4378 … … 4419 4421 * @access private 4420 4422 * 4421 * @param mixed $ dataNative representation.4423 * @param mixed $value Native representation. 4422 4424 * @return bool|int|float|null|string|array Data ready for `json_encode()`. 4423 4425 */ 4424 function _wp_json_prepare_data( $ data) {4426 function _wp_json_prepare_data( $value ) { 4425 4427 _deprecated_function( __FUNCTION__, '5.3.0' ); 4426 return $ data;4428 return $value; 4427 4429 } 4428 4430 … … 4432 4434 * @since 3.5.0 4433 4435 * @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. 4435 4437 * 4436 4438 * @param mixed $response Variable (usually an array or object) to encode as JSON, 4437 4439 * then print and die. 4438 4440 * @param int $status_code Optional. The HTTP status code to output. Default null. 4439 * @param int $ optionsOptional. 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 */ 4443 function wp_send_json( $response, $status_code = null, $flags = 0 ) { 4442 4444 if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { 4443 4445 _doing_it_wrong( … … 4460 4462 } 4461 4463 4462 echo wp_json_encode( $response, $ options );4464 echo wp_json_encode( $response, $flags ); 4463 4465 4464 4466 if ( wp_doing_ajax() ) { … … 4480 4482 * @since 3.5.0 4481 4483 * @since 4.7.0 The `$status_code` parameter was added. 4482 * @since 5.6.0 The `$ options` parameter was added.4483 * 4484 * @param mixed $ dataOptional. 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. 4485 4487 * @param int $status_code Optional. The HTTP status code to output. Default null. 4486 * @param int $ optionsOptional. 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 */ 4490 function wp_send_json_success( $value = null, $status_code = null, $flags = 0 ) { 4489 4491 $response = array( 'success' => true ); 4490 4492 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 ); 4496 4498 } 4497 4499 … … 4499 4501 * Sends a JSON response back to an Ajax request, indicating failure. 4500 4502 * 4501 * If the `$ data` parameter is a WP_Error object, the errors4503 * If the `$value` parameter is a WP_Error object, the errors 4502 4504 * within the object are processed and output as an array of error 4503 4505 * codes and corresponding messages. All other types are output … … 4505 4507 * 4506 4508 * @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. 4508 4510 * @since 4.7.0 The `$status_code` parameter was added. 4509 * @since 5.6.0 The `$ options` parameter was added.4510 * 4511 * @param mixed $ dataOptional. 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. 4512 4514 * @param int $status_code Optional. The HTTP status code to output. Default null. 4513 * @param int $ optionsOptional. 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 */ 4517 function wp_send_json_error( $value = null, $status_code = null, $flags = 0 ) { 4516 4518 $response = array( 'success' => false ); 4517 4519 4518 if ( isset( $ data) ) {4519 if ( is_wp_error( $ data) ) {4520 if ( isset( $value ) ) { 4521 if ( is_wp_error( $value ) ) { 4520 4522 $result = array(); 4521 foreach ( $ data->errors as $code => $messages ) {4523 foreach ( $value->errors as $code => $messages ) { 4522 4524 foreach ( $messages as $message ) { 4523 4525 $result[] = array( … … 4530 4532 $response['data'] = $result; 4531 4533 } 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 ); 4537 4539 } 4538 4540
Note: See TracChangeset
for help on using the changeset viewer.