Add wp_json_encode(), a wrapper for json_encode() that ensures everything is converted to UTF-8.

Change all core calls from `json_encode()` to `wp_json_encode()`.

Fixes #28786.



git-svn-id: https://develop.svn.wordpress.org/trunk@30055 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast
2014-10-28 18:34:16 +00:00
parent 3d35d8bc5d
commit 0e68ecc0b6
17 changed files with 194 additions and 39 deletions
+42
View File
@@ -529,4 +529,46 @@ class Tests_Functions extends WP_UnitTestCase {
$this->assertCount( 8, $urls );
$this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
}
/**
* @ticket 28786
*/
function test_wp_json_encode() {
$this->assertEquals( wp_json_encode( 'a' ), '"a"' );
$this->assertEquals( wp_json_encode( '这' ), '"\u8fd9"' );
$old_charsets = $charsets = mb_detect_order();
if ( ! in_array( 'EUC-JP', $charsets ) ) {
$charsets[] = 'EUC-JP';
mb_detect_order( $charsets );
}
$eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' );
$utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' );
$this->assertEquals( 'aあb', $utf8 );
$this->assertEquals( wp_json_encode( $eucjp ), '"a\u3042b"' );
$this->assertEquals( wp_json_encode( array( 'a' ) ), '["a"]' );
$object = new stdClass;
$object->a = 'b';
$this->assertEquals( wp_json_encode( $object ), '{"a":"b"}' );
mb_detect_order( $old_charsets );
}
/**
* @ticket 28786
*/
function test_wp_json_encode_depth() {
$data = array( array( array( 1, 2, 3 ) ) );
$json = wp_json_encode( $data, 0, 1 );
$this->assertFalse( $json );
$data = array( 'あ', array( array( 1, 2, 3 ) ) );
$json = wp_json_encode( $data, 0, 1 );
$this->assertFalse( $json );
}
}