WordPress.org

Make WordPress Core


Ignore:
Timestamp:
07/25/2011 12:36:06 AM (8 years ago)
Author:
azaozz
Message:

Use json_encode() for adding script data (formerly l10n). Add the same functionality to WP_Styles for adding inline css after a stylesheet has been outputted. See #11520

File:
1 edited

Legend:

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

    r18446 r18464  
    4848    }
    4949
     50    // Deprecated since 3.3, see print_script_data()
    5051    function print_scripts_l10n( $handle, $echo = true ) {
    51         if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
    52             return false;
    53 
    54         $object_name = $this->registered[$handle]->extra['l10n'][0];
    55 
    56         $data = "var $object_name = {\n";
    57         $eol = '';
    58         foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
    59             if ( 'l10n_print_after' == $var ) {
    60                 $after = $val;
    61                 continue;
     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 ( empty($this->registered[$handle]->extra['data']) )
     58            return false;
     59
     60        if ( $_l10n ) {
     61            $name = $this->registered[$handle]->extra['l10n'][0];
     62            $data = $this->registered[$handle]->extra['l10n'][1];
     63            $after = '';
     64
     65            if ( is_array($data) && isset($data['l10n_print_after']) ) {
     66                $after = $data['l10n_print_after'];
     67                unset($data['l10n_print_after']);
    6268            }
    63             $data .= "$eol\t$var: \"" . esc_js( $val ) . '"';
    64             $eol = ",\n";
    65         }
    66         $data .= "\n};\n";
    67         $data .= isset($after) ? "$after\n" : '';
    68 
    69         if ( $echo ) {
    70             echo "<script type='text/javascript'>\n";
    71             echo "/* <![CDATA[ */\n";
    72             echo $data;
    73             echo "/* ]]> */\n";
    74             echo "</script>\n";
    75             return true;
     69            $output = "var $name = " . json_encode($data) . "; $after\n";
    7670        } else {
    77             return $data;
    78         }
     71            foreach ( (array) $this->registered[$handle]->extra['data'] as $name => $data ) {
     72                $output = "var $name = " . json_encode($data) . ";\n";
     73            }
     74        }
     75
     76        if ( !$echo )
     77            return $output;
     78
     79        echo "<script type='text/javascript'>\n";
     80        echo "/* <![CDATA[ */\n"; // not needed in HTML 5
     81        echo $output;
     82        echo "\n/* ]]> */";
     83        echo "\n</script>\n";
     84
     85        return true;
    7986    }
    8087
     
    104111            $srce = apply_filters( 'script_loader_src', $src, $handle );
    105112            if ( $this->in_default_dir($srce) ) {
    106                 $this->print_code .= $this->print_scripts_l10n( $handle, false );
     113                $this->print_code .= $this->print_script_data( $handle, false );
    107114                $this->concat .= "$handle,";
    108115                $this->concat_version .= "$handle$ver";
     
    114121        }
    115122
    116         $this->print_scripts_l10n( $handle );
     123        $this->print_script_data( $handle );
    117124        if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) {
    118125            $src = $this->base_url . $src;
     
    132139
    133140    /**
    134      * Localizes a script
     141     * Localizes a script (Deprecated)
    135142     *
    136143     * Localizes only if script has already been added
    137144     *
    138      * @param string $handle Script name
    139      * @param string $object_name Name of JS object to hold l10n info
    140      * @param array $l10n Array of JS var name => localized string
    141      * @return bool Successful localization
     145     * @since
     146     * @deprecated WP 3.3
    142147     */
    143148    function localize( $handle, $object_name, $l10n ) {
    144         if ( !$object_name || !$l10n )
    145             return false;
    146         return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
     149        _deprecated_function( __FUNCTION__, '3.3', 'add_script_data()' );
     150        return $this->add_script_data( $handle, $object_name, $l10n );
     151    }
     152
     153    /**
     154     * Add extra Javascript
     155     *
     156     * Only if script has already been added.
     157     *
     158     * @param string $handle Script name
     159     * @param string $name Name of JS object to hold the data
     160     * @param array $data Associative array of JS name => value
     161     * @return bool Successful or not
     162     */
     163    function add_script_data( $handle, $name, $data ) {
     164        if ( !$name || !is_array($data) )
     165            return false;
     166
     167        if ( !empty( $this->registered[$handle]->extra['data'][$name] ) )
     168            $data = array_merge( $data, (array) $this->registered[$handle]->extra['data'][$name] );
     169
     170        return $this->add_data( $handle, 'data', array( $name => $data ) );
    147171    }
    148172
Note: See TracChangeset for help on using the changeset viewer.