diff --git a/wp-admin/includes/noop.php b/wp-admin/includes/noop.php index 134bef7efe..6e2397ea2e 100644 --- a/wp-admin/includes/noop.php +++ b/wp-admin/includes/noop.php @@ -93,13 +93,6 @@ function includes_url() {} */ function wp_guess_url() {} -if ( ! function_exists( 'json_encode' ) ) : - /** - * @ignore - */ - function json_encode() {} -endif; - function get_file( $path ) { if ( function_exists( 'realpath' ) ) { diff --git a/wp-includes/class-wp-customize-manager.php b/wp-includes/class-wp-customize-manager.php index 1948e7a794..2636489bea 100644 --- a/wp-includes/class-wp-customize-manager.php +++ b/wp-includes/class-wp-customize-manager.php @@ -1116,8 +1116,9 @@ final class WP_Customize_Manager { return new WP_Error( 'wrong_post_type' ); } $changeset_data = json_decode( $changeset_post->post_content, true ); - if ( function_exists( 'json_last_error' ) && json_last_error() ) { - return new WP_Error( 'json_parse_error', '', json_last_error() ); + $last_error = json_last_error(); + if ( $last_error ) { + return new WP_Error( 'json_parse_error', '', $last_error ); } if ( ! is_array( $changeset_data ) ) { return new WP_Error( 'expected_array' ); @@ -2843,13 +2844,9 @@ final class WP_Customize_Manager { } // Gather the data for wp_insert_post()/wp_update_post(). - $json_options = 0; - if ( defined( 'JSON_UNESCAPED_SLASHES' ) ) { - $json_options |= JSON_UNESCAPED_SLASHES; // Introduced in PHP 5.4. This is only to improve readability as slashes needn't be escaped in storage. - } - $json_options |= JSON_PRETTY_PRINT; // Also introduced in PHP 5.4, but WP defines constant for back compat. See WP Trac #30139. - $post_array = array( - 'post_content' => wp_json_encode( $data, $json_options ), + $post_array = array( + // JSON_UNESCAPED_SLASHES is only to improve readability as slashes needn't be escaped in storage. + 'post_content' => wp_json_encode( $data, JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT ), ); if ( $args['title'] ) { $post_array['post_title'] = $args['title']; diff --git a/wp-includes/functions.php b/wp-includes/functions.php index d6230bd478..c5f98e5c38 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -3721,6 +3721,7 @@ function _wp_die_process_input( $message, $title = '', $args = array() ) { * Encode a variable into JSON, with some sanity checks. * * @since 4.1.0 + * @since 5.3.0 No longer handles support for PHP < 5.6. * * @param mixed $data Variable (usually an array or object) to encode as JSON. * @param int $options Optional. Options to be passed to json_encode(). Default 0. @@ -3729,39 +3730,20 @@ function _wp_die_process_input( $message, $title = '', $args = array() ) { * @return string|false The JSON encoded string, or false if it cannot be encoded. */ function wp_json_encode( $data, $options = 0, $depth = 512 ) { - /* - * json_encode() has had extra params added over the years. - * $options was added in 5.3, and $depth in 5.5. - * We need to make sure we call it with the correct arguments. - */ - if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { - $args = array( $data, $options, $depth ); - } elseif ( version_compare( PHP_VERSION, '5.3', '>=' ) ) { - $args = array( $data, $options ); - } else { - $args = array( $data ); - } - - // Prepare the data for JSON serialization. - $args[0] = _wp_json_prepare_data( $data ); - - // phpcs:ignore WordPress.PHP.NoSilencedErrors.Discouraged -- json_encode() errors are handled after this call - $json = @call_user_func_array( 'json_encode', $args ); + $json = json_encode( $data, $options, $depth ); // If json_encode() was successful, no need to do more sanity checking. - // ... unless we're in an old version of PHP, and json_encode() returned - // a string containing 'null'. Then we need to do more sanity checking. - if ( false !== $json && ( version_compare( PHP_VERSION, '5.5', '>=' ) || false === strpos( $json, 'null' ) ) ) { + if ( false !== $json ) { return $json; } try { - $args[0] = _wp_json_sanity_check( $data, $depth ); + $data = _wp_json_sanity_check( $data, $depth ); } catch ( Exception $e ) { return false; } - return call_user_func_array( 'json_encode', $args ); + return json_encode( $data, $options, $depth ); } /** @@ -3865,48 +3847,17 @@ function _wp_json_convert_string( $string ) { * This supports the JsonSerializable interface for PHP 5.2-5.3 as well. * * @ignore - * @since 4.4.0 - * @access private + * @since 4.4.0 + * @deprecated 5.3.0 This function is no longer needed as support for PHP 5.2-5.3 + * has been dropped. + * @access private * * @param mixed $data Native representation. * @return bool|int|float|null|string|array Data ready for `json_encode()`. */ function _wp_json_prepare_data( $data ) { - if ( ! defined( 'WP_JSON_SERIALIZE_COMPATIBLE' ) || WP_JSON_SERIALIZE_COMPATIBLE === false ) { - return $data; - } - - switch ( gettype( $data ) ) { - case 'boolean': - case 'integer': - case 'double': - case 'string': - case 'NULL': - // These values can be passed through. - return $data; - - case 'array': - // Arrays must be mapped in case they also return objects. - return array_map( '_wp_json_prepare_data', $data ); - - case 'object': - // If this is an incomplete object (__PHP_Incomplete_Class), bail. - if ( ! is_object( $data ) ) { - return null; - } - - if ( $data instanceof JsonSerializable ) { - $data = $data->jsonSerialize(); - } else { - $data = get_object_vars( $data ); - } - - // Now, pass the array (or whatever was returned from jsonSerialize through). - return _wp_json_prepare_data( $data ); - - default: - return null; - } + _deprecated_function( __FUNCTION__, '5.3.0' ); + return $data; } /** diff --git a/wp-includes/rest-api/class-wp-rest-request.php b/wp-includes/rest-api/class-wp-rest-request.php index 7b2a32e618..81a5564464 100644 --- a/wp-includes/rest-api/class-wp-rest-request.php +++ b/wp-includes/rest-api/class-wp-rest-request.php @@ -639,21 +639,16 @@ class WP_REST_Request implements ArrayAccess { /* * Check for a parsing error. - * - * Note that due to WP's JSON compatibility functions, json_last_error - * might not be defined: https://core.trac.wordpress.org/ticket/27799 */ - if ( null === $params && ( ! function_exists( 'json_last_error' ) || JSON_ERROR_NONE !== json_last_error() ) ) { + if ( null === $params && JSON_ERROR_NONE !== json_last_error() ) { // Ensure subsequent calls receive error instance. $this->parsed_json = false; $error_data = array( - 'status' => WP_Http::BAD_REQUEST, + 'status' => WP_Http::BAD_REQUEST, + 'json_error_code' => json_last_error(), + 'json_error_message' => json_last_error_msg(), ); - if ( function_exists( 'json_last_error' ) ) { - $error_data['json_error_code'] = json_last_error(); - $error_data['json_error_message'] = json_last_error_msg(); - } return new WP_Error( 'rest_invalid_json', __( 'Invalid JSON body passed.' ), $error_data ); } diff --git a/wp-includes/rest-api/class-wp-rest-server.php b/wp-includes/rest-api/class-wp-rest-server.php index 43e2a33b7c..05cc240c02 100644 --- a/wp-includes/rest-api/class-wp-rest-server.php +++ b/wp-includes/rest-api/class-wp-rest-server.php @@ -1001,14 +1001,9 @@ class WP_REST_Server { * @return bool|string Boolean false or string error message. */ protected function get_json_last_error() { - // See https://core.trac.wordpress.org/ticket/27799. - if ( ! function_exists( 'json_last_error' ) ) { - return false; - } - $last_error_code = json_last_error(); - if ( ( defined( 'JSON_ERROR_NONE' ) && JSON_ERROR_NONE === $last_error_code ) || empty( $last_error_code ) ) { + if ( JSON_ERROR_NONE === $last_error_code || empty( $last_error_code ) ) { return false; } diff --git a/wp-includes/version.php b/wp-includes/version.php index 509b247ae4..119b65c602 100644 --- a/wp-includes/version.php +++ b/wp-includes/version.php @@ -13,7 +13,7 @@ * * @global string $wp_version */ -$wp_version = '5.3-alpha-46205'; +$wp_version = '5.3-alpha-46206'; /** * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.