Make WordPress Core


Ignore:
Timestamp:
09/20/2019 08:07:28 PM (7 years ago)
Author:
desrosj
Message:

Code Modernization: Remove JSON extension workarounds for PHP < 5.6.

The PHP native JSON extension has been bundled and compiled with PHP by default since version 5.2.0. Because the minimum version of PHP required by WordPress is now 5.6.20 (see #46594 and [45058]), JSON extension related polyfills and backwards compatibility code can now be removed.

This change removes code that supported JSON related functionality on older versions of PHP. This includes (but is not limited to) checks that json_last_error() exists, checking and setting the JSON_UNESCAPED_SLASHES and JSON_PRETTY_PRINT constants if not previously defined, and deprecating the _wp_json_prepare_data() function (which was 100% workaround code).

Follow up of [46205].

See #47699.
Props jrf, Clorith, pento.

File:
1 edited

Legend:

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

    r46176 r46206  
    37223722 *
    37233723 * @since 4.1.0
     3724 * @since 5.3.0 No longer handles support for PHP < 5.6.
    37243725 *
    37253726 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
     
    37303731 */
    37313732function 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 );
    37503734
    37513735        // 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 ) {
    37553737                return $json;
    37563738        }
    37573739
    37583740        try {
    3759                 $args[0] = _wp_json_sanity_check( $data, $depth );
     3741                $data = _wp_json_sanity_check( $data, $depth );
    37603742        } catch ( Exception $e ) {
    37613743                return false;
    37623744        }
    37633745
    3764         return call_user_func_array( 'json_encode', $args );
     3746        return json_encode( $data, $options, $depth );
    37653747}
    37663748
     
    38663848 *
    38673849 * @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
    38703854 *
    38713855 * @param mixed $data Native representation.
     
    38733857 */
    38743858function _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;
    39103861}
    39113862
Note: See TracChangeset for help on using the changeset viewer.