Changeset 46206 for trunk/src/wp-includes/functions.php
- Timestamp:
- 09/20/2019 08:07:28 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/functions.php
r46176 r46206 3722 3722 * 3723 3723 * @since 4.1.0 3724 * @since 5.3.0 No longer handles support for PHP < 5.6. 3724 3725 * 3725 3726 * @param mixed $data Variable (usually an array or object) to encode as JSON. … … 3730 3731 */ 3731 3732 function wp_json_encode( $data, $options = 0, $depth = 512 ) { 3732 /* 3733 * json_encode() has had extra params added over the years. 3734 * $options was added in 5.3, and $depth in 5.5. 3735 * We need to make sure we call it with the correct arguments. 3736 */ 3737 if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { 3738 $args = array( $data, $options, $depth ); 3739 } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) { 3740 $args = array( $data, $options ); 3741 } else { 3742 $args = array( $data ); 3743 } 3744 3745 // Prepare the data for JSON serialization. 3746 $args[0] = _wp_json_prepare_data( $data ); 3747 3748 // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- json_encode() errors are handled after this call 3749 $json = @call_user_func_array( 'json_encode', $args ); 3733 $json = json_encode( $data, $options, $depth ); 3750 3734 3751 3735 // If json_encode() was successful, no need to do more sanity checking. 3752 // ... unless we're in an old version of PHP, and json_encode() returned 3753 // a string containing 'null'. Then we need to do more sanity checking. 3754 if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) { 3736 if ( false !== $json ) { 3755 3737 return $json; 3756 3738 } 3757 3739 3758 3740 try { 3759 $ args[0]= _wp_json_sanity_check( $data, $depth );3741 $data = _wp_json_sanity_check( $data, $depth ); 3760 3742 } catch ( Exception $e ) { 3761 3743 return false; 3762 3744 } 3763 3745 3764 return call_user_func_array( 'json_encode', $args);3746 return json_encode( $data, $options, $depth ); 3765 3747 } 3766 3748 … … 3866 3848 * 3867 3849 * @ignore 3868 * @since 4.4.0 3869 * @access private 3850 * @since 4.4.0 3851 * @deprecated 5.3.0 This function is no longer needed as support for PHP 5.2-5.3 3852 * has been dropped. 3853 * @access private 3870 3854 * 3871 3855 * @param mixed $data Native representation. … … 3873 3857 */ 3874 3858 function _wp_json_prepare_data( $data ) { 3875 if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) { 3876 return $data; 3877 } 3878 3879 switch ( gettype( $data ) ) { 3880 case 'boolean': 3881 case 'integer': 3882 case 'double': 3883 case 'string': 3884 case 'NULL': 3885 // These values can be passed through. 3886 return $data; 3887 3888 case 'array': 3889 // Arrays must be mapped in case they also return objects. 3890 return array_map( '_wp_json_prepare_data', $data ); 3891 3892 case 'object': 3893 // If this is an incomplete object (__PHP_Incomplete_Class), bail. 3894 if ( ! is_object( $data ) ) { 3895 return null; 3896 } 3897 3898 if ( $data instanceof JsonSerializable ) { 3899 $data = $data->jsonSerialize(); 3900 } else { 3901 $data = get_object_vars( $data ); 3902 } 3903 3904 // Now, pass the array (or whatever was returned from jsonSerialize through). 3905 return _wp_json_prepare_data( $data ); 3906 3907 default: 3908 return null; 3909 } 3859 _deprecated_function( __FUNCTION__, '5.3.0' ); 3860 return $data; 3910 3861 } 3911 3862
Note: See TracChangeset
for help on using the changeset viewer.