Make WordPress Core


Ignore:
Timestamp:
11/08/2011 06:05:59 PM (15 years ago)
Author:
azaozz
Message:

Ressurect WP_Scripts::localize() and fix public function names, fixes #11520

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/class.wp-scripts.php

    r18813 r19217  
    4848        }
    4949
    50         // Deprecated since 3.3, see print_script_data()
     50        // Deprecated since 3.3, see print_extra_script()
    5151        function print_scripts_l10n( $handle, $echo = true ) {
    52                 _deprecated_function( __FUNCTION__, '3.3', 'print_script_data()' );
    53                 return $this->print_script_data( $handle, $echo, true );
    54         }
    55 
    56         function print_script_data( $handle, $echo = true, $_l10n = false ) {
    57                 if ( $_l10n ) {
    58                         list( $name, $data ) = $this->get_data( $handle, 'l10n' );
    59                         $after = '';
    60 
    61                         if ( is_array($data) && isset($data['l10n_print_after']) ) {
    62                                 $after = $data['l10n_print_after'];
    63                                 unset($data['l10n_print_after']);
    64                         }
    65 
    66                         $data = $this->decode_html_entities($data);
    67                         $output = "var $name = " . json_encode( $data ) . "; $after\n";
    68                 } else {
    69                         $data = $this->get_data( $handle, 'data' );
    70 
    71                         if ( empty( $data ) )
    72                                 return false;
    73 
    74                         foreach ( (array) $data as $name => $value ) {
    75                                 $value = $this->decode_html_entities($value);
    76                                 $output = "var $name = " . json_encode( $value ) . ";\n";
    77                         }
    78                 }
     52                _deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' );
     53                return $this->print_extra_script( $handle, $echo );
     54        }
     55
     56        function print_extra_script( $handle, $echo = true ) {
     57                if ( !$output = $this->get_data( $handle, 'data' ) )
     58                        return;
    7959
    8060                if ( !$echo )
    8161                        return $output;
    8262
    83                 echo "<script type='text/javascript'>\n";
    84                 echo "/* <![CDATA[ */\n"; // CDATA is not needed for HTML 5
    85                 echo $output;
     63                echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5
     64                echo "/* <![CDATA[ */\n";
     65                echo "$output\n";
    8666                echo "/* ]]> */\n";
    8767                echo "</script>\n";
    … …  
    11595                        $srce = apply_filters( 'script_loader_src', $src, $handle );
    11696                        if ( $this->in_default_dir($srce) ) {
    117                                 $this->print_code .= $this->print_script_data( $handle, false );
     97                                $this->print_code .= $this->print_extra_script( $handle, false );
    11898                                $this->concat .= "$handle,";
    11999                                $this->concat_version .= "$handle$ver";
    … …  
    125105                }
    126106
    127                 $this->print_script_data( $handle );
     107                $this->print_extra_script( $handle );
    128108                if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
    129109                        $src = $this->base_url . $src;
    … …  
    132112                if ( !empty($ver) )
    133113                        $src = add_query_arg('ver', $ver, $src);
    134                 $src = esc_url(apply_filters( 'script_loader_src', $src, $handle ));
     114
     115                $src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) );
    135116
    136117                if ( $this->do_concat )
    … …  
    143124
    144125        /**
    145          * Localizes a script (Deprecated)
    146          *
    147          * Localizes only if script has already been added
    148          *
    149          * @deprecated WP 3.3
     126         * Localizes a script
     127         *
     128         * Localizes only if the script has already been added
    150129         */
    151130        function localize( $handle, $object_name, $l10n ) {
    152                 _deprecated_function( __FUNCTION__, '3.3', 'add_script_data()' );
    153                 return $this->add_script_data( $handle, $object_name, $l10n );
     131                if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present
     132                        $after = $l10n['l10n_print_after'];
     133                        unset($l10n['l10n_print_after']);
     134                }
     135
     136                foreach ( (array) $l10n as $key => $value ) {
     137                        if ( !is_scalar($value) )
     138                                continue;
     139
     140                        $l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8');
     141                }
     142
     143                $script = "var $object_name = " . json_encode($l10n) . ';';
     144
     145                if ( !empty($after) )
     146                        $script .= "\n$after";
     147
     148                return $this->add_script_data( $handle, $script );
    154149        }
    155150
    … …  
    160155         *
    161156         * @param string $handle Script name
    162          * @param string $name Name of JS object to hold the data
    163          * @param array $args Associative array of JS object attributes
     157         * @param string $script Extra JS to add before the script
    164158         * @return bool Successful or not
    165159         */
    166         function add_script_data( $handle, $name, $args ) {
    167                 if ( !$name || !is_array( $args ) )
     160        function add_script_data( $handle, $script ) {
     161                if ( !is_string( $script ) )
    168162                        return false;
    169163
    170164                $data = $this->get_data( $handle, 'data' );
    171165
    172                 if ( !empty( $data[$name] ) )
    173                         $args = array_merge( $data[$name], $args );
    174 
    175                 return $this->add_data( $handle, 'data', array( $name => $args ) );
     166                if ( !empty( $data ) )
     167                        $script = "$data;\n$script";
     168
     169                return $this->add_data( $handle, 'data', $script );
    176170        }
    177171
    … …  
    218212                }
    219213                return false;
    220         }
    221 
    222         function decode_html_entities($data) {
    223                 foreach ( (array) $data as $key => $value ) {
    224                         if ( is_array($value) )
    225                                 $data[$key] = $this->decode_html_entities($value);
    226                         elseif ( is_string($value) )
    227                                 $data[$key] = html_entity_decode($value, ENT_QUOTES, 'UTF-8');
    228                 }
    229                 return $data;
    230214        }
    231215
Note: See TracChangeset for help on using the changeset viewer.