From 0cb93dd166434347cabd9c4b113b0ae51a9fa7c2 Mon Sep 17 00:00:00 2001 From: Gary Pendergast Date: Tue, 25 Nov 2014 05:00:36 +0000 Subject: [PATCH] When `json_encode()` returns a JSON string containing `'null'` in PHP 5.4 or earlier, `wp_json_encode()` will now sanity check the data, as older versions of PHP failed to encode non UTF-8 characters correctly, instead returning `'null'`. Fixes #30471. git-svn-id: https://develop.svn.wordpress.org/trunk@30561 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/functions.php | 4 +++- tests/phpunit/tests/functions.php | 20 ++------------------ 2 files changed, 5 insertions(+), 19 deletions(-) diff --git a/src/wp-includes/functions.php b/src/wp-includes/functions.php index 0fb2117835..9f229291a7 100644 --- a/src/wp-includes/functions.php +++ b/src/wp-includes/functions.php @@ -2662,7 +2662,9 @@ function wp_json_encode( $data, $options = 0, $depth = 512 ) { $json = call_user_func_array( 'json_encode', $args ); // If json_encode() was successful, no need to do more sanity checking. - if ( false !== $json ) { + // ... 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' ) ) ) { return $json; } diff --git a/tests/phpunit/tests/functions.php b/tests/phpunit/tests/functions.php index 7b2870c6e5..1edd143fbc 100644 --- a/tests/phpunit/tests/functions.php +++ b/tests/phpunit/tests/functions.php @@ -554,15 +554,7 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals( 'aあb', $utf8 ); - // json_encode() returns different things in different PHP versions. - // See: https://core.trac.wordpress.org/ticket/30471 - if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { - $expected = '"a\u3042b"'; - } else { - $expected = 'null'; - } - - $this->assertEquals( $expected, wp_json_encode( $eucjp ) ); + $this->assertEquals( '"a\u3042b"', wp_json_encode( $eucjp ) ); mb_detect_order( $old_charsets ); } @@ -582,15 +574,7 @@ class Tests_Functions extends WP_UnitTestCase { $this->assertEquals( 'aあb', $utf8 ); - // json_encode() returns different things in different PHP versions. - // See: https://core.trac.wordpress.org/ticket/30471 - if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { - $expected = '["c","a\u3042b"]'; - } else { - $expected = '["c",null]'; - } - - $this->assertEquals( $expected, wp_json_encode( array( 'c', $eucjp ) ) ); + $this->assertEquals( '["c","a\u3042b"]', wp_json_encode( array( 'c', $eucjp ) ) ); mb_detect_order( $old_charsets ); }