Make WordPress Core


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

Code Modernization: Remove JSON related polyfills.

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 the json_last_error_msg() and JsonSerializable polyfills included in WordPress for full JSON extension support in PHP < 5.6.

Follow up of [46205-46206].

See #47699.
Props jrf, Clorith, pento.

File:
1 edited

Legend:

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

    r45638 r46208  
    290290}
    291291
    292 if ( ! function_exists( 'json_encode' ) ) {
    293         function json_encode( $string ) {
    294                 global $wp_json;
    295 
    296                 if ( ! ( $wp_json instanceof Services_JSON ) ) {
    297                         require_once( ABSPATH . WPINC . '/class-json.php' );
    298                         $wp_json = new Services_JSON();
    299                 }
    300 
    301                 return $wp_json->encodeUnsafe( $string );
    302         }
    303 }
    304 
    305 if ( ! function_exists( 'json_decode' ) ) {
    306         /**
    307          * @global Services_JSON $wp_json
    308          * @param string $string
    309          * @param bool   $assoc_array
    310          * @return object|array
    311          */
    312         function json_decode( $string, $assoc_array = false ) {
    313                 global $wp_json;
    314 
    315                 if ( ! ( $wp_json instanceof Services_JSON ) ) {
    316                         require_once( ABSPATH . WPINC . '/class-json.php' );
    317                         $wp_json = new Services_JSON();
    318                 }
    319 
    320                 $res = $wp_json->decode( $string );
    321                 if ( $assoc_array ) {
    322                         $res = _json_decode_object_helper( $res );
    323                 }
    324                 return $res;
    325         }
    326 
    327         /**
    328          * @param object $data
    329          * @return array
    330          */
    331         function _json_decode_object_helper( $data ) {
    332                 if ( is_object( $data ) ) {
    333                         $data = get_object_vars( $data );
    334                 }
    335                 return is_array( $data ) ? array_map( __FUNCTION__, $data ) : $data;
    336         }
    337 }
    338 
    339292if ( ! function_exists( 'hash_equals' ) ) :
    340293        /**
     
    374327endif;
    375328
    376 // JSON_PRETTY_PRINT was introduced in PHP 5.4
    377 // Defined here to prevent a notice when using it with wp_json_encode()
    378 if ( ! defined( 'JSON_PRETTY_PRINT' ) ) {
    379         define( 'JSON_PRETTY_PRINT', 128 );
    380 }
    381 
    382 if ( ! function_exists( 'json_last_error_msg' ) ) :
    383         /**
    384          * Retrieves the error string of the last json_encode() or json_decode() call.
    385          *
    386          * @since 4.4.0
    387          *
    388          * @internal This is a compatibility function for PHP <5.5
    389          *
    390          * @return bool|string Returns the error message on success, "No Error" if no error has occurred,
    391          *                     or false on failure.
    392          */
    393         function json_last_error_msg() {
    394                 // See https://core.trac.wordpress.org/ticket/27799.
    395                 if ( ! function_exists( 'json_last_error' ) ) {
    396                         return false;
    397                 }
    398 
    399                 $last_error_code = json_last_error();
    400 
    401                 // Just in case JSON_ERROR_NONE is not defined.
    402                 $error_code_none = defined( 'JSON_ERROR_NONE' ) ? JSON_ERROR_NONE : 0;
    403 
    404                 switch ( true ) {
    405                         case $last_error_code === $error_code_none:
    406                                 return 'No error';
    407 
    408                         case defined( 'JSON_ERROR_DEPTH' ) && JSON_ERROR_DEPTH === $last_error_code:
    409                                 return 'Maximum stack depth exceeded';
    410 
    411                         case defined( 'JSON_ERROR_STATE_MISMATCH' ) && JSON_ERROR_STATE_MISMATCH === $last_error_code:
    412                                 return 'State mismatch (invalid or malformed JSON)';
    413 
    414                         case defined( 'JSON_ERROR_CTRL_CHAR' ) && JSON_ERROR_CTRL_CHAR === $last_error_code:
    415                                 return 'Control character error, possibly incorrectly encoded';
    416 
    417                         case defined( 'JSON_ERROR_SYNTAX' ) && JSON_ERROR_SYNTAX === $last_error_code:
    418                                 return 'Syntax error';
    419 
    420                         case defined( 'JSON_ERROR_UTF8' ) && JSON_ERROR_UTF8 === $last_error_code:
    421                                 return 'Malformed UTF-8 characters, possibly incorrectly encoded';
    422 
    423                         case defined( 'JSON_ERROR_RECURSION' ) && JSON_ERROR_RECURSION === $last_error_code:
    424                                 return 'Recursion detected';
    425 
    426                         case defined( 'JSON_ERROR_INF_OR_NAN' ) && JSON_ERROR_INF_OR_NAN === $last_error_code:
    427                                 return 'Inf and NaN cannot be JSON encoded';
    428 
    429                         case defined( 'JSON_ERROR_UNSUPPORTED_TYPE' ) && JSON_ERROR_UNSUPPORTED_TYPE === $last_error_code:
    430                                 return 'Type is not supported';
    431 
    432                         default:
    433                                 return 'An unknown error occurred';
    434                 }
    435         }
    436 endif;
    437 
    438 if ( ! interface_exists( 'JsonSerializable' ) ) {
    439         define( 'WP_JSON_SERIALIZE_COMPATIBLE', true );
    440         /**
    441          * JsonSerializable interface.
    442          *
    443          * Compatibility shim for PHP <5.4
    444          *
    445          * @link https://secure.php.net/jsonserializable
    446          *
    447          * @since 4.4.0
    448          */
    449         interface JsonSerializable {
    450                 // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
    451                 public function jsonSerialize();
    452         }
    453 }
    454 
    455329// random_int was introduced in PHP 7.0
    456330if ( ! function_exists( 'random_int' ) ) {
Note: See TracChangeset for help on using the changeset viewer.