WordPress.org

Make WordPress Core

Changeset 46206


Ignore:
Timestamp:
09/20/2019 08:07:28 PM (2 months 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.

Location:
trunk
Files:
7 edited

Legend:

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

    r42343 r46206  
    9494function wp_guess_url() {}
    9595
    96 if ( ! function_exists( 'json_encode' ) ) :
    97     /**
    98      * @ignore
    99      */
    100     function json_encode() {}
    101 endif;
    102 
    10396function get_file( $path ) {
    10497
  • trunk/src/wp-includes/class-wp-customize-manager.php

    r46133 r46206  
    11171117        }
    11181118        $changeset_data = json_decode( $changeset_post->post_content, true );
    1119         if ( function_exists( 'json_last_error' ) && json_last_error() ) {
    1120             return new WP_Error( 'json_parse_error', '', json_last_error() );
     1119        $last_error     = json_last_error();
     1120        if ( $last_error ) {
     1121            return new WP_Error( 'json_parse_error', '', $last_error );
    11211122        }
    11221123        if ( ! is_array( $changeset_data ) ) {
     
    28442845
    28452846        // Gather the data for wp_insert_post()/wp_update_post().
    2846         $json_options = 0;
    2847         if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) {
    2848             $json_options |= JSON_UNESCAPED_SLASHES; // Introduced in PHP 5.4. This is only to improve readability as slashes needn't be escaped in storage.
    2849         }
    2850         $json_options |= JSON_PRETTY_PRINT; // Also introduced in PHP 5.4, but WP defines constant for back compat. See WP Trac #30139.
    2851         $post_array    = array(
    2852             'post_content' => wp_json_encode( $data, $json_options ),
     2847        $post_array = array(
     2848            // JSON_UNESCAPED_SLASHES is only to improve readability as slashes needn't be escaped in storage.
     2849            'post_content' => wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ),
    28532850        );
    28542851        if ( $args['title'] ) {
  • 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
  • trunk/src/wp-includes/rest-api/class-wp-rest-request.php

    r46105 r46206  
    640640        /*
    641641         * Check for a parsing error.
    642          *
    643          * Note that due to WP's JSON compatibility functions, json_last_error
    644          * might not be defined: https://core.trac.wordpress.org/ticket/27799
    645642         */
    646         if ( null === $params && ( ! function_exists( 'json_last_error' ) || JSON_ERROR_NONE !== json_last_error() ) ) {
     643        if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) {
    647644            // Ensure subsequent calls receive error instance.
    648645            $this->parsed_json = false;
    649646
    650647            $error_data = array(
    651                 'status' => WP_Http::BAD_REQUEST,
     648                'status'             => WP_Http::BAD_REQUEST,
     649                'json_error_code'    => json_last_error(),
     650                'json_error_message' => json_last_error_msg(),
    652651            );
    653             if ( function_exists( 'json_last_error' ) ) {
    654                 $error_data['json_error_code']    = json_last_error();
    655                 $error_data['json_error_message'] = json_last_error_msg();
    656             }
    657652
    658653            return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data );
  • trunk/src/wp-includes/rest-api/class-wp-rest-server.php

    r46191 r46206  
    10021002     */
    10031003    protected function get_json_last_error() {
    1004         // See https://core.trac.wordpress.org/ticket/27799.
    1005         if ( ! function_exists( 'json_last_error' ) ) {
    1006             return false;
    1007         }
    1008 
    10091004        $last_error_code = json_last_error();
    10101005
    1011         if ( ( defined( 'JSON_ERROR_NONE' ) && JSON_ERROR_NONE === $last_error_code ) || empty( $last_error_code ) ) {
     1006        if ( JSON_ERROR_NONE === $last_error_code || empty( $last_error_code ) ) {
    10121007            return false;
    10131008        }
  • trunk/tests/phpunit/tests/customize/manager.php

    r46127 r46206  
    18571857        $r = $manager->save_changeset_post( $args );
    18581858        $this->assertInstanceOf( 'WP_Error', $r );
    1859         if ( function_exists( 'json_last_error' ) ) {
    1860             $this->assertEquals( 'json_parse_error', $r->get_error_code() );
    1861         }
     1859        $this->assertEquals( 'json_parse_error', $r->get_error_code() );
    18621860
    18631861        wp_update_post(
  • trunk/tests/phpunit/tests/rest-api/rest-schema-setup.php

    r46190 r46206  
    489489            $this->assertTrue( ! empty( $data ), $route['name'] . ' route should return data.' );
    490490
    491             if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
    492                 $fixture           = $this->normalize_fixture( $data, $route['name'] );
    493                 $mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = '
    494                     . json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
    495                     . ";\n";
    496             }
     491            $fixture           = $this->normalize_fixture( $data, $route['name'] );
     492            $mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = '
     493                . json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
     494                . ";\n";
    497495        }
    498496
    499497        // Only generate API client fixtures in single site and when required JSON_* constants are supported.
    500         if ( ! is_multisite() && version_compare( PHP_VERSION, '5.4', '>=' ) ) {
     498        if ( ! is_multisite() ) {
    501499            // Save the route object for QUnit tests.
    502500            $file = dirname( DIR_TESTROOT ) . '/qunit/fixtures/wp-api-generated.js';
Note: See TracChangeset for help on using the changeset viewer.