Make WordPress Core

Ticket #29722: 29722.4.diff

File 29722.4.diff, 5.0 KB (added by jtsternberg, 9 years ago)

Add documentation and recursively html_entity_decode array values

  • src/wp-includes/class.wp-scripts.php

     
    204204        }
    205205
    206206        /**
    207          * Localizes a script, only if the script has already been added
     207         * Localizes a script, but only if the script has already been added.
    208208         *
    209          * @param string $handle
    210          * @param string $object_name
    211          * @param array $l10n
    212          * @return bool
     209         * This serves as a method to pass data from the PHP environment to the
     210         * front-end to be accessible from javascript scripts.
     211         *
     212         * @param  string       $handle      Unique script handle.
     213         *                                   The localized variables will be loaded
     214         *                                   before this script (as long as the
     215         *                                   script has beeen enqueued/registered).
     216         *
     217         * @param  string       $object_name The javascript varibale name.
     218         *
     219         * @param  array|scalar $l10n        An array or scalar value which should
     220         *                                   be encoded as the value to the
     221         *                                   variable name.
     222         *
     223         * @return bool True on success, false on failure.
    213224         */
    214225        public function localize( $handle, $object_name, $l10n ) {
    215                 if ( $handle === 'jquery' )
     226                if ( 'jquery' === $handle ) {
    216227                        $handle = 'jquery-core';
     228                }
    217229
    218230                if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
    219231                        $after = $l10n['l10n_print_after'];
     
    220232                        unset($l10n['l10n_print_after']);
    221233                }
    222234
    223                 foreach ( (array) $l10n as $key => $value ) {
    224                         if ( !is_scalar($value) )
    225                                 continue;
    226 
    227                         $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
     235                if ( is_scalar( $l10n ) ) {
     236                        // If the value is scalar, simply decode it
     237                        $l10n = html_entity_decode( (string) $l10n, ENT_QUOTES, 'UTF-8' );
     238                } else {
     239                        $l10n = $this->entity_decode_array_values( $l10n );
    228240                }
    229241
    230242                $script = "var $object_name = " . wp_json_encode( $l10n ) . ';';
     
    241253        }
    242254
    243255        /**
     256         * Handles recursively html entity decodeing multi-dimensional array values.
     257         *
     258         * @param  array $l10n_array Array of localizable data which should be decoded.
     259         *
     260         * @return array Array with decoded values.
     261         */
     262        public function entity_decode_array_values( $l10n_array ) {
     263                // begin a new array so that non-decoded values do not get added.
     264                $l10n = array();
     265
     266                foreach ( (array) $l10n_array as $key => $value ) {
     267                        if ( is_array( $value ) ) {
     268                                $l10n[ $key ] = $this->entity_decode_array_values( $value );
     269                        }
     270
     271                        // non-array, non-scalar values should not be added
     272                        if ( ! is_scalar( $value ) ) {
     273                                continue;
     274                        }
     275
     276                        $l10n[ $key ] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8' );
     277                }
     278
     279                return $l10n;
     280        }
     281
     282        /**
    244283         * @param string $handle    Name of the item. Should be unique.
    245284         * @param bool   $recursion Internal flag that calling function was called recursively.
    246285         * @param mixed  $group     Group level.
  • tests/phpunit/tests/dependencies/scripts.php

     
    156156                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
    157157        }
    158158
     159        /**
     160         * Testing `wp_localize_script` with scalar and array values.
     161         * @ticket 29722
     162         */
     163        function test_wp_localize_script_with_non_array() {
     164                // Enqueue & localize variables
     165                wp_enqueue_script( 'test-l10n-data', 'example.com', array(), null );
     166                wp_localize_script( 'test-l10n-data', 'data', true );
     167
     168                $test_array = array( 'var' => 'string' );
     169                wp_localize_script( 'test-l10n-data', 'dataArray', $test_array );
     170
     171                $test_multidimensional = array(
     172                        'first-level' => array(
     173                                'second-level' => array(
     174                                        'index' => "I'll \"walk\" the <b>dog</b> now",
     175                                ),
     176                        ),
     177                );
     178                wp_localize_script( 'test-l10n-data', 'dataMultiDimensionalArray', $test_multidimensional );
     179
     180                // Boolean output.
     181                $expected = "var data = \"1\";\n";
     182
     183                // One-dimensional array output.
     184                $expected .= "var dataArray = {\"var\":\"string\"};\n";
     185
     186                // Multi-dimensional array output with odd characters.
     187                $string = '\"walk\"';
     188                $expected .= "var dataMultiDimensionalArray = {\"first-level\":{\"second-level\":{\"index\":\"I'll " . $string . " the <b>dog<\/b> now\"}}};\n";
     189
     190                // var script wrapper output
     191                $expected = "<script type='text/javascript'>\n/* <![CDATA[ */\n$expected/* ]]> */\n</script>\n";
     192                // script link output
     193                $expected .= "<script type='text/javascript' src='http://example.com'></script>\n";
     194
     195                $this->assertEquals( $expected, get_echo( 'wp_print_scripts' ) );
     196
     197                // No scripts left to print
     198                $this->assertEquals( '', get_echo( 'wp_print_scripts' ) );
     199        }
     200
    159201    /**
    160202     * Testing 'wp_register_script' return boolean success/failure value.
    161203     *