Make WordPress Core

Ticket #28786: 28786.7.diff

File 28786.7.diff, 4.5 KB (added by pento, 12 years ago)
  • src/wp-includes/functions.php

     
    26122612}
    26132613
    26142614/**
     2615 * Encode a variable into JSON, with some sanity checks
     2616 *
     2617 * @since 4.0
     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 */
     2625function 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
     2653function _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/**
    26152687 * Send a JSON response back to an Ajax request.
    26162688 *
    26172689 * @since 3.5.0
     
    26212693 */
    26222694function wp_send_json( $response ) {
    26232695        @header( 'Content-Type: application/json; charset=' . get_option( 'blog_charset' ) );
    2624         echo json_encode( $response );
     2696        echo wp_json_encode( $response );
    26252697        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
    26262698                wp_die();
    26272699        else
     
    35053577 * Determine if the scheme of the given URL is https.
    35063578 *
    35073579 * @since 4.0.0
    3508  * 
     3580 *
    35093581 * @param string $url The URL.
    35103582 * @return bool True if the given URL uses https, false if not (or if the URL
    35113583 *              is not valid).
  • tests/phpunit/tests/functions.php

     
    524524                $this->assertCount( 8, $urls );
    525525                $this->assertEquals( array_slice( $original_urls, 0, 8 ), $urls );
    526526        }
     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        }
    527569}