WordPress.org

Make WordPress Core

Ticket #47699: 47699-WP-5.3-remove-JSON-related-work-arounds.patch

File 47699-WP-5.3-remove-JSON-related-work-arounds.patch, 10.5 KB (added by jrf, 15 months ago)

WP 5.3: Remove JSON extension related work-arounds

  • src/wp-admin/includes/noop.php

    From 4f643a5b05dee6b22fe7378422b70de0b084ee0a Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Fri, 19 Jul 2019 12:04:47 +0200
    Subject: [PATCH] WP 5.3: Remove JSON extension related work-arounds
    
    ---
     src/wp-admin/includes/noop.php                |  7 --
     .../class-wp-customize-manager.php            | 15 ++--
     src/wp-includes/functions.php                 | 71 +++----------------
     .../rest-api/class-wp-rest-request.php        | 13 ++--
     .../rest-api/class-wp-rest-server.php         |  7 +-
     tests/phpunit/tests/customize/manager.php     |  4 +-
     .../tests/rest-api/rest-schema-setup.php      | 12 ++--
     7 files changed, 28 insertions(+), 101 deletions(-)
    
    diff --git a/src/wp-admin/includes/noop.php b/src/wp-admin/includes/noop.php
    index 134bef7efe..6e2397ea2e 100644
    a b function includes_url() {} 
    9393 */
    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
    10598        if ( function_exists( 'realpath' ) ) {
  • src/wp-includes/class-wp-customize-manager.php

    diff --git a/src/wp-includes/class-wp-customize-manager.php b/src/wp-includes/class-wp-customize-manager.php
    index be369a1cb3..731701cb5d 100644
    a b final class WP_Customize_Manager { 
    11161116                        return new WP_Error( 'wrong_post_type' );
    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 ) ) {
    11231124                        return new WP_Error( 'expected_array' );
    final class WP_Customize_Manager { 
    28432844                }
    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'] ) {
    28552852                        $post_array['post_title'] = $args['title'];
  • src/wp-includes/functions.php

    diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php
    index a66b3fa406..73ed7ef7b5 100644
    a b function _wp_die_process_input( $message, $title = '', $args = array() ) { 
    35713571 * Encode a variable into JSON, with some sanity checks.
    35723572 *
    35733573 * @since 4.1.0
     3574 * @since 5.3.0 No longer handles support for PHP < 5.6.
    35743575 *
    35753576 * @param mixed $data    Variable (usually an array or object) to encode as JSON.
    35763577 * @param int   $options Optional. Options to be passed to json_encode(). Default 0.
    function _wp_die_process_input( $message, $title = '', $args = array() ) { 
    35793580 * @return string|false The JSON encoded string, or false if it cannot be encoded.
    35803581 */
    35813582function wp_json_encode( $data, $options = 0, $depth = 512 ) {
    3582         /*
    3583          * json_encode() has had extra params added over the years.
    3584          * $options was added in 5.3, and $depth in 5.5.
    3585          * We need to make sure we call it with the correct arguments.
    3586          */
    3587         if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) {
    3588                 $args = array( $data, $options, $depth );
    3589         } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) {
    3590                 $args = array( $data, $options );
    3591         } else {
    3592                 $args = array( $data );
    3593         }
    3594 
    3595         // Prepare the data for JSON serialization.
    3596         $args[0] = _wp_json_prepare_data( $data );
    3597 
    3598         // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- json_encode() errors are handled after this call
    3599         $json = @call_user_func_array( 'json_encode', $args );
     3583        $json = json_encode( $data, $options, $depth );
    36003584
    36013585        // If json_encode() was successful, no need to do more sanity checking.
    3602         // ... unless we're in an old version of PHP, and json_encode() returned
    3603         // a string containing 'null'. Then we need to do more sanity checking.
    3604         if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) {
     3586        if ( false !== $json ) {
    36053587                return $json;
    36063588        }
    36073589
    36083590        try {
    3609                 $args[0] = _wp_json_sanity_check( $data, $depth );
     3591                $data = _wp_json_sanity_check( $data, $depth );
    36103592        } catch ( Exception $e ) {
    36113593                return false;
    36123594        }
    36133595
    3614         return call_user_func_array( 'json_encode', $args );
     3596        return json_encode( $data, $options, $depth );
    36153597}
    36163598
    36173599/**
    function _wp_json_convert_string( $string ) { 
    37153697 * This supports the JsonSerializable interface for PHP 5.2-5.3 as well.
    37163698 *
    37173699 * @ignore
    3718  * @since 4.4.0
    3719  * @access private
     3700 * @since      4.4.0
     3701 * @deprecated 5.3.0 This function is no longer needed as support for PHP 5.2-5.3
     3702 *                   has been dropped.
     3703 * @access     private
    37203704 *
    37213705 * @param mixed $data Native representation.
    37223706 * @return bool|int|float|null|string|array Data ready for `json_encode()`.
    37233707 */
    37243708function _wp_json_prepare_data( $data ) {
    3725         if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) {
    3726                 return $data;
    3727         }
    3728 
    3729         switch ( gettype( $data ) ) {
    3730                 case 'boolean':
    3731                 case 'integer':
    3732                 case 'double':
    3733                 case 'string':
    3734                 case 'NULL':
    3735                         // These values can be passed through.
    3736                         return $data;
    3737 
    3738                 case 'array':
    3739                         // Arrays must be mapped in case they also return objects.
    3740                         return array_map( '_wp_json_prepare_data', $data );
    3741 
    3742                 case 'object':
    3743                         // If this is an incomplete object (__PHP_Incomplete_Class), bail.
    3744                         if ( ! is_object( $data ) ) {
    3745                                 return null;
    3746                         }
    3747 
    3748                         if ( $data instanceof JsonSerializable ) {
    3749                                 $data = $data->jsonSerialize();
    3750                         } else {
    3751                                 $data = get_object_vars( $data );
    3752                         }
    3753 
    3754                         // Now, pass the array (or whatever was returned from jsonSerialize through).
    3755                         return _wp_json_prepare_data( $data );
    3756 
    3757                 default:
    3758                         return null;
    3759         }
     3709        _deprecated_function( __FUNCTION__, '5.3.0' );
     3710        return $data;
    37603711}
    37613712
    37623713/**
  • src/wp-includes/rest-api/class-wp-rest-request.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-request.php b/src/wp-includes/rest-api/class-wp-rest-request.php
    index 3089547ce7..2b4cbd4fa0 100644
    a b class WP_REST_Request implements ArrayAccess { 
    639639
    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 );
    659654                }
  • src/wp-includes/rest-api/class-wp-rest-server.php

    diff --git a/src/wp-includes/rest-api/class-wp-rest-server.php b/src/wp-includes/rest-api/class-wp-rest-server.php
    index f34a6106e3..9bd7190169 100644
    a b class WP_REST_Server { 
    996996         * @return bool|string Boolean false or string error message.
    997997         */
    998998        protected function get_json_last_error() {
    999                 // See https://core.trac.wordpress.org/ticket/27799.
    1000                 if ( ! function_exists( 'json_last_error' ) ) {
    1001                         return false;
    1002                 }
    1003 
    1004999                $last_error_code = json_last_error();
    10051000
    1006                 if ( ( defined( 'JSON_ERROR_NONE' ) && JSON_ERROR_NONE === $last_error_code ) || empty( $last_error_code ) ) {
     1001                if ( JSON_ERROR_NONE === $last_error_code || empty( $last_error_code ) ) {
    10071002                        return false;
    10081003                }
    10091004
  • tests/phpunit/tests/customize/manager.php

    diff --git a/tests/phpunit/tests/customize/manager.php b/tests/phpunit/tests/customize/manager.php
    index f081245c6b..5656fd9ebe 100644
    a b class Tests_WP_Customize_Manager extends WP_UnitTestCase { 
    18561856
    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(
    18641862                        array(
  • tests/phpunit/tests/rest-api/rest-schema-setup.php

    diff --git a/tests/phpunit/tests/rest-api/rest-schema-setup.php b/tests/phpunit/tests/rest-api/rest-schema-setup.php
    index 92219b31da..f4e330e827 100644
    a b class WP_Test_REST_Schema_Initialization extends WP_Test_REST_TestCase { 
    449449                        );
    450450                        $this->assertTrue( ! empty( $data ), $route['name'] . ' route should return data.' );
    451451
    452                         if ( version_compare( PHP_VERSION, '5.4', '>=' ) ) {
    453                                 $fixture           = $this->normalize_fixture( $data, $route['name'] );
    454                                 $mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = '
    455                                         . json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
    456                                         . ";\n";
    457                         }
     452                        $fixture           = $this->normalize_fixture( $data, $route['name'] );
     453                        $mocked_responses .= "\nmockedApiResponse." . $route['name'] . ' = '
     454                                . json_encode( $fixture, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES )
     455                                . ";\n";
    458456                }
    459457
    460458                // Only generate API client fixtures in single site and when required JSON_* constants are supported.
    461                 if ( ! is_multisite() && version_compare( PHP_VERSION, '5.4', '>=' ) ) {
     459                if ( ! is_multisite() ) {
    462460                        // Save the route object for QUnit tests.
    463461                        $file = dirname( DIR_TESTROOT ) . '/qunit/fixtures/wp-api-generated.js';
    464462                        file_put_contents( $file, $mocked_responses );