Ticket #28786: 28786.9.diff
| File 28786.9.diff, 6.9 KB (added by , 12 years ago) |
|---|
-
src/wp-includes/functions.php
2612 2612 } 2613 2613 2614 2614 /** 2615 * Encode a variable into JSON, with some sanity checks 2616 * 2617 * @since 4.1 2618 * 2619 * @param mixed $data Variable (usually an array or object) to encode as JSON 2620 * @param int $options Options to be passed to json_encode(). Default 0. 2621 * @param int $depth Maximum depth to walk through $data. Must be greater than 0, default 512.t 2622 * 2623 * @return bool|string The JSON encoded string, or false if it cannot be encoded 2624 */ 2625 function wp_json_encode( $data, $options = 0, $depth = 512 ) { 2626 // json_encode has had extra params added over the years. 2627 // $options was added in 5.3, and $depth in 5.5. 2628 // We need to make sure we call it with the correct arguments. 2629 if ( version_compare( PHP_VERSION, '5.5', '>=' ) ) { 2630 $args = array( $data, $options, $depth ); 2631 } else if ( version_compare( PHP_VERSION, '5.3', '>=' ) ) { 2632 $args = array( $data, $options ); 2633 } else { 2634 $args = array( $data ); 2635 } 2636 2637 $json = call_user_func_array( 'json_encode', $args ); 2638 2639 if ( false !== $json ) { 2640 // If json_encode was successful, no need to do more sanity checking 2641 return $json; 2642 } 2643 2644 try { 2645 $args[0] = _wp_json_sanity_check( $data, $depth ); 2646 } catch ( Exception $e ) { 2647 return false; 2648 } 2649 2650 return call_user_func_array( 'json_encode', $args ); 2651 } 2652 2653 function _wp_json_sanity_check( $data, $depth ) { 2654 if ( $depth < 0 ) { 2655 throw new Exception( 'Reached depth limit' ); 2656 } 2657 2658 if ( is_array( $data ) ) { 2659 foreach ( $data as $id => $el ) { 2660 if ( is_array( $el ) || is_object( $el ) || is_string( $el ) ) { 2661 $data[ $id ] = _wp_json_sanity_check( $el, $depth - 1 ); 2662 } 2663 } 2664 } else if ( is_object( $data ) ) { 2665 foreach ( $data as $id => $el ) { 2666 if ( is_array( $el ) || is_object( $el ) || is_string( $el ) ) { 2667 $data->$id = _wp_json_sanity_check( $el, $depth - 1 ); 2668 } 2669 } 2670 } else if ( is_string( $data ) ) { 2671 if ( function_exists( 'mb_convert_encoding' ) ) { 2672 $encoding = mb_detect_encoding( $data ); 2673 if ( $encoding ) { 2674 $data = mb_convert_encoding( $data, 'UTF-8', $encoding ); 2675 } else { 2676 $data = mb_convert_encoding( $data, 'UTF-8', 'UTF-8' ); 2677 } 2678 } else { 2679 $data = wp_check_invalid_utf8( $data, true ); 2680 } 2681 } 2682 2683 return $data; 2684 } 2685 2686 /** 2615 2687 * Send a JSON response back to an Ajax request. 2616 2688 * 2617 2689 * @since 3.5.0 … … 2621 2693 */ 2622 2694 function wp_send_json( $response ) { 2623 2695 @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) ); 2624 echo json_encode( $response );2696 echo wp_json_encode( $response ); 2625 2697 if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) 2626 2698 wp_die(); 2627 2699 else -
src/wp-includes/media.php
1399 1399 } 1400 1400 ?></ol> 1401 1401 </noscript> 1402 <script type="application/json" class="wp-playlist-script"><?php echo json_encode( $data ) ?></script>1402 <script type="application/json" class="wp-playlist-script"><?php echo wp_json_encode( $data ) ?></script> 1403 1403 </div> 1404 1404 <?php 1405 1405 return ob_get_clean(); … … 2575 2575 'limitExceeded' => is_multisite() && ! is_upload_space_available() 2576 2576 ); 2577 2577 2578 $script = 'var _wpPluploadSettings = ' . json_encode( $settings ) . ';';2578 $script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';'; 2579 2579 2580 2580 if ( $data ) 2581 2581 $script = "$data\n$script"; -
src/wp-includes/update.php
94 94 ); 95 95 96 96 $post_body = array( 97 'translations' => json_encode( $translations ),97 'translations' => wp_json_encode( $translations ), 98 98 ); 99 99 100 100 if ( is_array( $extra_stats ) ) … … 274 274 $options = array( 275 275 'timeout' => $timeout, 276 276 'body' => array( 277 'plugins' => json_encode( $to_send ),278 'translations' => json_encode( $translations ),279 'locale' => json_encode( $locales ),280 'all' => json_encode( true ),277 'plugins' => wp_json_encode( $to_send ), 278 'translations' => wp_json_encode( $translations ), 279 'locale' => wp_json_encode( $locales ), 280 'all' => wp_json_encode( true ), 281 281 ), 282 282 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) 283 283 ); 284 284 285 285 if ( $extra_stats ) { 286 $options['body']['update_stats'] = json_encode( $extra_stats );286 $options['body']['update_stats'] = wp_json_encode( $extra_stats ); 287 287 } 288 288 289 289 $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/'; … … 437 437 $options = array( 438 438 'timeout' => $timeout, 439 439 'body' => array( 440 'themes' => json_encode( $request ),441 'translations' => json_encode( $translations ),442 'locale' => json_encode( $locales ),440 'themes' => wp_json_encode( $request ), 441 'translations' => wp_json_encode( $translations ), 442 'locale' => wp_json_encode( $locales ), 443 443 ), 444 444 'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ) 445 445 ); 446 446 447 447 if ( $extra_stats ) { 448 $options['body']['update_stats'] = json_encode( $extra_stats );448 $options['body']['update_stats'] = wp_json_encode( $extra_stats ); 449 449 } 450 450 451 451 $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/'; -
tests/phpunit/tests/functions.php
524 524 $this->assertCount( 8, $urls ); 525 525 $this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls ); 526 526 } 527 528 /** 529 * @ticket 28786 530 */ 531 function test_wp_json_encode() { 532 $this->assertEquals( wp_json_encode( 'a' ), '"a"' ); 533 $this->assertEquals( wp_json_encode( '这' ), '"\u8fd9"' ); 534 535 $old_charsets = $charsets = mb_detect_order(); 536 if ( ! in_array( 'EUC-JP', $charsets ) ) { 537 $charsets[] = 'EUC-JP'; 538 mb_detect_order( $charsets ); 539 } 540 541 $eucjp = mb_convert_encoding( 'aあb', 'EUC-JP', 'UTF-8' ); 542 $utf8 = mb_convert_encoding( $eucjp, 'UTF-8', 'EUC-JP' ); 543 544 $this->assertEquals( 'aあb', $utf8 ); 545 546 $this->assertEquals( wp_json_encode( $eucjp ), '"a\u3042b"' ); 547 548 $this->assertEquals( wp_json_encode( array( 'a' ) ), '["a"]' ); 549 550 $object = new stdClass; 551 $object->a = 'b'; 552 $this->assertEquals( wp_json_encode( $object ), '{"a":"b"}' ); 553 554 mb_detect_order( $old_charsets ); 555 } 556 557 /** 558 * @ticket 28786 559 */ 560 function test_wp_json_encode_depth() { 561 $data = array( array( array( 1, 2, 3 ) ) ); 562 $json = wp_json_encode( $data, 0, 1 ); 563 $this->assertFalse( $json ); 564 565 $data = array( 'あ', array( array( 1, 2, 3 ) ) ); 566 $json = wp_json_encode( $data, 0, 1 ); 567 $this->assertFalse( $json ); 568 } 527 569 }
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)