Make WordPress Core

Ticket #28786: 28786.8.diff

File 28786.8.diff, 6.8 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
  • src/wp-includes/media.php

     
    13991399        }
    14001400        ?></ol>
    14011401        </noscript>
    1402         <script type="application/json"><?php echo json_encode( $data ) ?></script>
     1402        <script type="application/json"><?php echo wp_json_encode( $data ) ?></script>
    14031403</div>
    14041404        <?php
    14051405        return ob_get_clean();
     
    25752575                'limitExceeded' => is_multisite() && ! is_upload_space_available()
    25762576        );
    25772577
    2578         $script = 'var _wpPluploadSettings = ' . json_encode( $settings ) . ';';
     2578        $script = 'var _wpPluploadSettings = ' . wp_json_encode( $settings ) . ';';
    25792579
    25802580        if ( $data )
    25812581                $script = "$data\n$script";
  • src/wp-includes/update.php

     
    9494        );
    9595
    9696        $post_body = array(
    97                 'translations' => json_encode( $translations ),
     97                'translations' => wp_json_encode( $translations ),
    9898        );
    9999
    100100        if ( is_array( $extra_stats ) )
     
    274274        $options = array(
    275275                'timeout' => $timeout,
    276276                '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 ),
    281281                ),
    282282                'user-agent' => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
    283283        );
    284284
    285285        if ( $extra_stats ) {
    286                 $options['body']['update_stats'] = json_encode( $extra_stats );
     286                $options['body']['update_stats'] = wp_json_encode( $extra_stats );
    287287        }
    288288
    289289        $url = $http_url = 'http://api.wordpress.org/plugins/update-check/1.1/';
     
    437437        $options = array(
    438438                'timeout' => $timeout,
    439439                '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 ),
    443443                ),
    444444                'user-agent'    => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' )
    445445        );
    446446
    447447        if ( $extra_stats ) {
    448                 $options['body']['update_stats'] = json_encode( $extra_stats );
     448                $options['body']['update_stats'] = wp_json_encode( $extra_stats );
    449449        }
    450450
    451451        $url = $http_url = 'http://api.wordpress.org/themes/update-check/1.1/';
  • 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}