Make WordPress Core


Ignore:
Timestamp:
11/08/2011 06:05:59 PM (13 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/functions.wp-scripts.php

    r19054 r19217  
    6565
    6666/**
    67  * Adds extra Javascript data.
    68  *
     67 * Wrapper for $wp_scripts->localize().
     68 *
     69 * Used to localizes a script.
    6970 * Works only if the script has already been added.
    70  * Accepts an associative array $data and creates JS object:
    71  * "$name" = {
     71 * Accepts an associative array $l10n and creates JS object:
     72 * "$object_name" = {
    7273 *   key: value,
    7374 *   key: value,
    7475 *   ...
    7576 * }
    76  * The $name is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
    77  * The $data array is JSON encoded. If called more than once for the same $handle with the same $name,
    78  * the object would contain all values. In that case if two or more keys are the same,
    79  * the last value overwrites the previous. The function is named "localize_script" because of historical reasons.
    80  *
    81  * @since r16
     77 * See http://core.trac.wordpress.org/ticket/11520 for more information.
     78 *
     79 * @since r16
     80 *
     81 * @param string $handle The script handle that was registered or used in script-loader
     82 * @param string $object_name Name for the created JS object. This is passed directly so it should be qualified JS variable /[a-zA-Z0-9_]+/
     83 * @param array $l10n Associative PHP array containing the translated strings. HTML entities will be converted and the array will be JSON encoded.
     84 * @return bool Whether the localization was added successfully.
     85 */
     86function wp_localize_script( $handle, $object_name, $l10n ) {
     87    global $wp_scripts;
     88    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     89        if ( ! did_action( 'init' ) )
     90            _doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
     91                '<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
     92
     93        return false;
     94    }
     95
     96    return $wp_scripts->localize( $handle, $object_name, $l10n );
     97}
     98
     99/**
     100 * Adds extra Javascript.
     101 *
     102 * Works only if the script referenced by $handle has already been added.
     103 * Accepts string $extra that will be printed as script before the main script tag.
     104 *
     105 * @since 3.3
    82106 * @see WP_Scripts::add_script_data()
    83107 */
    84 function wp_localize_script( $handle, $name, $data ) {
     108function wp_add_script_before( $handle, $script ) {
    85109    global $wp_scripts;
    86110    if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
     
    91115    }
    92116
    93     return $wp_scripts->add_script_data( $handle, $name, $data );
     117    return $wp_scripts->add_script_data( $handle, $script );
    94118}
    95119
Note: See TracChangeset for help on using the changeset viewer.