[wp-trac] [WordPress Trac] #47699: Remove redundant JSON polyfills for PHP native functionality

WordPress Trac noreply at wordpress.org
Sun Jul 14 17:07:16 UTC 2019


#47699: Remove redundant JSON polyfills for PHP native functionality
-------------------------+-------------------------------------------------
 Reporter:  jrf          |      Owner:  (none)
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:  has-patch 2nd-opinion has-unit-
  Focuses:  coding-      |  tests
  standards              |
-------------------------+-------------------------------------------------
 Now the new minimum PHP requirement for WordPress is PHP 5.6.20, the JSON
 functionality related polyfills for the native PHP functions and related
 work-arounds can be removed.

 I will add a patch for this to this ticket.

 Let's talk through it all:

 == **Current implementation**

 WP polyfills the following PHP native JSON functionality:
 * The function [https://www.php.net/manual/en/function.json-encode.php
 `json_encode()`] (PHP 5.2.0+)
 * The function [https://www.php.net/manual/en/function.json-decode.php
 `json_decode()`] (PHP 5.2.0+)
 * The function [https://www.php.net/manual/en/function.json-last-error-
 msg.php `json_last_error_msg()`] (PHP 5.5.0+)
 * The constant [https://www.php.net/manual/en/json.constants.php#constant
 .json-pretty-print `JSON_PRETTY_PRINT`] (PHP 5.4.0+)
 * The constant [https://www.php.net/manual/en/json.constants.php#constant
 .json-error-none `JSON_ERROR_NONE`] (as part of `json_last_error_msg()`)
 (PHP 5.3.0+)
 * The interface [https://www.php.net/manual/en/class.jsonserializable.php
 `JsonSerializable`] (PHP 5.4.0+)

 In addition to this, it:
 * adds the `_json_decode_object_helper()`function, the
 `WP_JSON_SERIALIZE_COMPATIBLE` constant and the `$wp_json` global variable
 in the `wp-includes/compat.php` file.
 * contains various work-arounds for differences between PHP versions in
 the `wp_json_encode()` function in the `wp-includes/functions.php` file,
 including the `_wp_json_prepare_data()` function which is 100% work-around
 code.
 * adds various constants, a `Services_JSON` and a `Services_JSON_Error`
 class in the `wp-includes/class-json.php` file


 == **Why all of the above can be safely deprecated/removed**

 === **Step 1**

 As of PHP 5.2.0, the [https://www.php.net/manual/en/json.installation.php
 JSON extension is bundled and compiled with PHP by default] and can not be
 disabled.

 This means in effect that the following code has not been used since WP
 3.3.0 when the minimum PHP requirement went up to PHP 5.2.6:
 * `json_encode()` polyfill, including the dummy copy in `wp-
 admin/includes/noop.php`
 * `json_decode()` polyfill
 * `_json_decode_object_helper()` function which would only be declared if
 the PHP native `json_decode()` function didn't exist and was used
 exclusively by that polyfill.
 * `$wp_json` variable as it would only be assigned a value if the PHP
 native `json_encode()` and `json_decode()` functions didn't exist.
 * All of the code in the `wp-includes/class-json.php` file.
 * The associated `test_json_encode_decode()` unit test.

 With the above in mind, there can be no discussion about removing the
 first two polyfills - `json_encode()` and `json_decode()` -.

 As for removing the `_json_decode_object_helper()`, `$wp_json` and the
 code in the `wp-includes/class-json.php` file:
 As those were only available conditionally, any userland code referring to
 this function/variable/these classes/constants, would already have to have
 been surrounded by appropriate `function_exists()`, `class_exists()`,
 `defined()` or an `isset()` calls.

 So, having said that, IMO it is safe to remove all of this.

 I'm making one caveat, namely that while the code in the `wp-includes
 /class-json.php` file can be safely removed, the file itself should
 probably remain (for now) and should generate a ''deprecated file'' notice
 just in case a userland plugin/theme would require it directly, though if
 they do, they've already had eight years to remove that file-include and
 they will run into trouble now that the code in the file has been removed,
 but in my estimation, it would be exceedingly rare, most likely non-
 existent, for any plugin/theme to include this file directly.


 === **Step 2**

 Now, as the minimum PHP requirement for WP is now 5.6.20, we can also
 safely remove:
 * the `json_last_error_msg()` polyfill
 * the `JSON_PRETTY_PRINT` constant polyfill
 * the `JSON_ERROR_NONE` constant polyfill
 * the `JsonSerializable` polyfill
 * any and all work-arounds for differences between PHP versions regarding
 `json_encode()`
 * any and all work-arounds for the missing polyfill for
 `json_last_error()` - see #27799

 This includes deprecating the `_wp_json_prepare_data()` function which was
 only needed for PHP 5.2-5.3.


 == **Unit tests**

 As these functions are used throughout core, removing them can be
 considered sufficiently unit tested when all the WP Core unit tests pass,
 and they still do: https://travis-ci.com/WordPress/wordpress-
 develop/builds/119072132


 == **Other**
 * I've tried to be comprehensive with this ticket and have searched the
 codebase for any and all relevant keywords and reviewed all the related
 code. All the same, as there were quite a lot of different bits and pieces
 involved in this, I will not guarantee that nothing remains, though I'm
 fairly confident about the completeness of this patch.
 * The patch includes removing the `wp-includes/class-json.php` from the
 PHPCS exclusions list.

 == **Related tickets**
 * This ticket is related to #47698 which addresses the other polyfills
 which can be removed.
 * This ticket is related to #27799, #30139.


 /cc @pento

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/47699>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list