WordPress.org

Make WordPress Core

Ticket #47699: 47699-WP-5.3-deprecate-services-json.patch

File 47699-WP-5.3-deprecate-services-json.patch, 7.6 KB (added by jrf, 15 months ago)

WP 5.3: Agressively deprecate the Services_JSON and the Services_JSON_Error classes

  • src/wp-includes/class-json.php

    From 251edd47f277169690413d659dfaba2c5e98b5ba Mon Sep 17 00:00:00 2001
    From: jrfnl <jrfnl@users.noreply.github.com>
    Date: Fri, 19 Jul 2019 11:34:37 +0200
    Subject: [PATCH] WP 5.3: Agressively deprecate the `Services_JSON` and the
     `Services_JSON_Error` classes
    
    ---
     src/wp-includes/class-json.php | 45 ++++++++++++++++++++++++++++------
     tests/phpunit/tests/compat.php |  3 +++
     2 files changed, 40 insertions(+), 8 deletions(-)
    
    diff --git a/src/wp-includes/class-json.php b/src/wp-includes/class-json.php
    index a8c61c92e9..e0d1fea104 100644
    a b  
    11<?php
     2_deprecated_file( basename( __FILE__ ), '5.3.0', null, 'The PHP native JSON extension is now a requirement.' );
     3
    24if ( ! class_exists( 'Services_JSON' ) ) :
    35/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
    46/**
    class Services_JSON 
    142144    */
    143145    function __construct( $use = 0 )
    144146    {
     147        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     148
    145149        $this->use = $use;
    146150        $this->_mb_strlen            = function_exists('mb_strlen');
    147151        $this->_mb_convert_encoding  = function_exists('mb_convert_encoding');
    class Services_JSON 
    152156         * PHP4 constructor.
    153157         */
    154158        public function Services_JSON( $use = 0 ) {
     159                _deprecated_constructor( 'Services_JSON', '5.3.0', get_class( $this ) );
    155160                self::__construct( $use );
    156161        }
    157162    // private - cache the mbstring lookup results..
    class Services_JSON 
    172177    */
    173178    function utf162utf8($utf16)
    174179    {
     180        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     181
    175182        // oh please oh please oh please oh please oh please
    176183        if($this->_mb_convert_encoding) {
    177184            return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
    class Services_JSON 
    216223    */
    217224    function utf82utf16($utf8)
    218225    {
     226        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     227
    219228        // oh please oh please oh please oh please oh please
    220229        if($this->_mb_convert_encoding) {
    221230            return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
    class Services_JSON 
    260269    */
    261270    function encode($var)
    262271    {
     272        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     273
    263274        header('Content-type: application/json');
    264275        return $this->encodeUnsafe($var);
    265276    }
    class Services_JSON 
    276287    */
    277288    function encodeUnsafe($var)
    278289    {
     290        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     291
    279292        // see bug #16908 - regarding numeric locale printing
    280293        $lc = setlocale(LC_NUMERIC, 0);
    281294        setlocale(LC_NUMERIC, 'C');
    class Services_JSON 
    297310    */
    298311    function _encode($var)
    299312    {
    300          
     313        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     314
    301315        switch (gettype($var)) {
    302316            case 'boolean':
    303317                return $var ? 'true' : 'false';
    class Services_JSON 
    539553    */
    540554    function name_value($name, $value)
    541555    {
     556        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     557
    542558        $encoded_value = $this->_encode($value);
    543559
    544560        if(Services_JSON::isError($encoded_value)) {
    class Services_JSON 
    558574    */
    559575    function reduce_string($str)
    560576    {
     577        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     578
    561579        $str = preg_replace(array(
    562580
    563581                // eliminate single line comments in '// ...' form
    class Services_JSON 
    589607    */
    590608    function decode($str)
    591609    {
     610        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     611
    592612        $str = $this->reduce_string($str);
    593613
    594614        switch (strtolower($str)) {
    class Services_JSON 
    774794                                $parts = array();
    775795                               
    776796                               if (preg_match('/^\s*(["\'].*[^\\\]["\'])\s*:/Uis', $slice, $parts)) {
    777                                       // "name":value pair
     797                                    // "name":value pair
    778798                                    $key = $this->decode($parts[1]);
    779799                                    $val = $this->decode(trim(substr($slice, strlen($parts[0])), ", \t\n\r\0\x0B"));
    780800                                    if ($this->use & SERVICES_JSON_LOOSE_TYPE) {
    class Services_JSON 
    870890     */
    871891    function isError($data, $code = null)
    872892    {
     893        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     894
    873895        if (class_exists('pear')) {
    874896            return PEAR::isError($data, $code);
    875897        } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
    class Services_JSON 
    887909    */
    888910    function strlen8( $str )
    889911    {
     912        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     913
    890914        if ( $this->_mb_strlen ) {
    891915            return mb_strlen( $str, "8bit" );
    892916        }
    class Services_JSON 
    902926    */
    903927    function substr8( $string, $start, $length=false )
    904928    {
     929        _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     930
    905931        if ( $length === false ) {
    906932            $length = $this->strlen8( $string ) - $start;
    907933        }
    if (class_exists('PEAR_Error')) { 
    920946        function __construct($message = 'unknown error', $code = null,
    921947                                     $mode = null, $options = null, $userinfo = null)
    922948        {
     949            _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
     950
    923951            parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
    924952        }
    925953
    926         public function Services_JSON_Error($message = 'unknown error', $code = null,
     954                public function Services_JSON_Error($message = 'unknown error', $code = null,
    927955                                     $mode = null, $options = null, $userinfo = null) {
    928                 self::__construct($message = 'unknown error', $code = null,
    929                                      $mode = null, $options = null, $userinfo = null);
    930         }
     956                        _deprecated_constructor( 'Services_JSON_Error', '5.3.0', get_class( $this ) );
     957                        self::__construct($message, $code, $mode, $options, $userinfo);
     958                }
    931959    }
    932960
    933961} else {
    if (class_exists('PEAR_Error')) { 
    943971        function __construct( $message = 'unknown error', $code = null,
    944972                                     $mode = null, $options = null, $userinfo = null )
    945973        {
    946 
     974            _deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
    947975        }
    948976
    949977            /**
    if (class_exists('PEAR_Error')) { 
    951979             */
    952980                public function Services_JSON_Error( $message = 'unknown error', $code = null,
    953981                                             $mode = null, $options = null, $userinfo = null ) {
     982                        _deprecated_constructor( 'Services_JSON_Error', '5.3.0', get_class( $this ) );
    954983                        self::__construct( $message, $code, $mode, $options, $userinfo );
    955984                }
    956985    }
    957    
     986
    958987}
    959988
    960989endif;
  • tests/phpunit/tests/compat.php

    diff --git a/tests/phpunit/tests/compat.php b/tests/phpunit/tests/compat.php
    index d70d0271b1..92cefbf946 100644
    a b EOT; 
    179179                $this->assertEquals( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
    180180        }
    181181
     182        /**
     183         * @expectedException PHPUnit_Framework_Error_Notice
     184         */
    182185        function test_json_encode_decode() {
    183186                require_once( ABSPATH . WPINC . '/class-json.php' );
    184187                $json = new Services_JSON();