Code Modernization: Deprecate the `Services_JSON` and `Services_JSON_Error` classes.

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.

Since there are still some plugins and themes that utilize the `Services_JSON` class, these classes will remain for the time being, but including the `wp-includes/class-json.php` file and creating `Services_JSON` instances will now throw deprecated notices.

See #47699.
Props jrf, Clorith, pento.

git-svn-id: https://develop.svn.wordpress.org/trunk@46205 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Jonathan Desrosiers 2019-09-20 19:52:38 +00:00
parent 66ef5ec15b
commit 080e7a609e
2 changed files with 40 additions and 8 deletions

View File

@ -1,4 +1,6 @@
<?php <?php
_deprecated_file( basename( __FILE__ ), '5.3.0', null, 'The PHP native JSON extension is now a requirement.' );
if ( ! class_exists( 'Services_JSON' ) ) : if ( ! class_exists( 'Services_JSON' ) ) :
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */ /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/** /**
@ -142,6 +144,8 @@ class Services_JSON
*/ */
function __construct( $use = 0 ) function __construct( $use = 0 )
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
$this->use = $use; $this->use = $use;
$this->_mb_strlen = function_exists('mb_strlen'); $this->_mb_strlen = function_exists('mb_strlen');
$this->_mb_convert_encoding = function_exists('mb_convert_encoding'); $this->_mb_convert_encoding = function_exists('mb_convert_encoding');
@ -152,6 +156,7 @@ class Services_JSON
* PHP4 constructor. * PHP4 constructor.
*/ */
public function Services_JSON( $use = 0 ) { public function Services_JSON( $use = 0 ) {
_deprecated_constructor( 'Services_JSON', '5.3.0', get_class( $this ) );
self::__construct( $use ); self::__construct( $use );
} }
// private - cache the mbstring lookup results.. // private - cache the mbstring lookup results..
@ -172,6 +177,8 @@ class Services_JSON
*/ */
function utf162utf8($utf16) function utf162utf8($utf16)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
// oh please oh please oh please oh please oh please // oh please oh please oh please oh please oh please
if($this->_mb_convert_encoding) { if($this->_mb_convert_encoding) {
return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16'); return mb_convert_encoding($utf16, 'UTF-8', 'UTF-16');
@ -216,6 +223,8 @@ class Services_JSON
*/ */
function utf82utf16($utf8) function utf82utf16($utf8)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
// oh please oh please oh please oh please oh please // oh please oh please oh please oh please oh please
if($this->_mb_convert_encoding) { if($this->_mb_convert_encoding) {
return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8'); return mb_convert_encoding($utf8, 'UTF-16', 'UTF-8');
@ -260,6 +269,8 @@ class Services_JSON
*/ */
function encode($var) function encode($var)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
header('Content-type: application/json'); header('Content-type: application/json');
return $this->encodeUnsafe($var); return $this->encodeUnsafe($var);
} }
@ -276,6 +287,8 @@ class Services_JSON
*/ */
function encodeUnsafe($var) function encodeUnsafe($var)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
// see bug #16908 - regarding numeric locale printing // see bug #16908 - regarding numeric locale printing
$lc = setlocale(LC_NUMERIC, 0); $lc = setlocale(LC_NUMERIC, 0);
setlocale(LC_NUMERIC, 'C'); setlocale(LC_NUMERIC, 'C');
@ -297,6 +310,7 @@ class Services_JSON
*/ */
function _encode($var) function _encode($var)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
switch (gettype($var)) { switch (gettype($var)) {
case 'boolean': case 'boolean':
@ -539,6 +553,8 @@ class Services_JSON
*/ */
function name_value($name, $value) function name_value($name, $value)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
$encoded_value = $this->_encode($value); $encoded_value = $this->_encode($value);
if(Services_JSON::isError($encoded_value)) { if(Services_JSON::isError($encoded_value)) {
@ -558,6 +574,8 @@ class Services_JSON
*/ */
function reduce_string($str) function reduce_string($str)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
$str = preg_replace(array( $str = preg_replace(array(
// eliminate single line comments in '// ...' form // eliminate single line comments in '// ...' form
@ -589,6 +607,8 @@ class Services_JSON
*/ */
function decode($str) function decode($str)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
$str = $this->reduce_string($str); $str = $this->reduce_string($str);
switch (strtolower($str)) { switch (strtolower($str)) {
@ -870,6 +890,8 @@ class Services_JSON
*/ */
function isError($data, $code = null) function isError($data, $code = null)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
if (class_exists('pear')) { if (class_exists('pear')) {
return PEAR::isError($data, $code); return PEAR::isError($data, $code);
} elseif (is_object($data) && (get_class($data) == 'services_json_error' || } elseif (is_object($data) && (get_class($data) == 'services_json_error' ||
@ -887,6 +909,8 @@ class Services_JSON
*/ */
function strlen8( $str ) function strlen8( $str )
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
if ( $this->_mb_strlen ) { if ( $this->_mb_strlen ) {
return mb_strlen( $str, "8bit" ); return mb_strlen( $str, "8bit" );
} }
@ -902,6 +926,8 @@ class Services_JSON
*/ */
function substr8( $string, $start, $length=false ) function substr8( $string, $start, $length=false )
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
if ( $length === false ) { if ( $length === false ) {
$length = $this->strlen8( $string ) - $start; $length = $this->strlen8( $string ) - $start;
} }
@ -920,13 +946,15 @@ if (class_exists('PEAR_Error')) {
function __construct($message = 'unknown error', $code = null, function __construct($message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null) $mode = null, $options = null, $userinfo = null)
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
parent::PEAR_Error($message, $code, $mode, $options, $userinfo); parent::PEAR_Error($message, $code, $mode, $options, $userinfo);
} }
public function Services_JSON_Error($message = 'unknown error', $code = null, public function Services_JSON_Error($message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null) { $mode = null, $options = null, $userinfo = null) {
self::__construct($message = 'unknown error', $code = null, _deprecated_constructor( 'Services_JSON_Error', '5.3.0', get_class( $this ) );
$mode = null, $options = null, $userinfo = null); self::__construct($message, $code, $mode, $options, $userinfo);
} }
} }
@ -943,7 +971,7 @@ if (class_exists('PEAR_Error')) {
function __construct( $message = 'unknown error', $code = null, function __construct( $message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null ) $mode = null, $options = null, $userinfo = null )
{ {
_deprecated_function( __METHOD__, '5.3.0', 'The PHP native JSON extension' );
} }
/** /**
@ -951,6 +979,7 @@ if (class_exists('PEAR_Error')) {
*/ */
public function Services_JSON_Error( $message = 'unknown error', $code = null, public function Services_JSON_Error( $message = 'unknown error', $code = null,
$mode = null, $options = null, $userinfo = null ) { $mode = null, $options = null, $userinfo = null ) {
_deprecated_constructor( 'Services_JSON_Error', '5.3.0', get_class( $this ) );
self::__construct( $message, $code, $mode, $options, $userinfo ); self::__construct( $message, $code, $mode, $options, $userinfo );
} }
} }

View File

@ -179,6 +179,9 @@ EOT;
$this->assertEquals( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) ); $this->assertEquals( array( 1 => '993003b95758e0ac2eba451a4c5877eb1bb7b92a' ), unpack( 'H40', _hash_hmac( 'sha1', 'simple', 'key', true ) ) );
} }
/**
* @expectedException PHPUnit_Framework_Error_Notice
*/
function test_json_encode_decode() { function test_json_encode_decode() {
require_once( ABSPATH . WPINC . '/class-json.php' ); require_once( ABSPATH . WPINC . '/class-json.php' );
$json = new Services_JSON(); $json = new Services_JSON();