web/wp-includes/functions.wp-scripts.php
changeset 194 32102edaa81b
parent 136 bde1974c263b
child 204 09a1c134465b
equal deleted inserted replaced
193:2f6f6f7551ca 194:32102edaa81b
    22 	do_action( 'wp_print_scripts' );
    22 	do_action( 'wp_print_scripts' );
    23 	if ( '' === $handles ) // for wp_head
    23 	if ( '' === $handles ) // for wp_head
    24 		$handles = false;
    24 		$handles = false;
    25 
    25 
    26 	global $wp_scripts;
    26 	global $wp_scripts;
    27 	if ( !is_a($wp_scripts, 'WP_Scripts') ) {
    27 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
    28 		if ( ! did_action( 'init' ) )
       
    29 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    30 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
       
    31 
    28 		if ( !$handles )
    32 		if ( !$handles )
    29 			return array(); // No need to instantiate if nothing's there.
    33 			return array(); // No need to instantiate if nothing is there.
    30 		else
    34 		else
    31 			$wp_scripts = new WP_Scripts();
    35 			$wp_scripts = new WP_Scripts();
    32 	}
    36 	}
    33 
    37 
    34 	return $wp_scripts->do_items( $handles );
    38 	return $wp_scripts->do_items( $handles );
    35 }
    39 }
    36 
    40 
    37 /**
    41 /**
    38  * Register new JavaScript file.
    42  * Register new Javascript file.
    39  *
    43  *
    40  * @since r16
    44  * @since r16
    41  * @see WP_Dependencies::add() For parameter information.
    45  * @param string $handle Script name
       
    46  * @param string $src Script url
       
    47  * @param array $deps (optional) Array of script names on which this script depends
       
    48  * @param string|bool $ver (optional) Script version (used for cache busting), set to null to disable
       
    49  * @param bool $in_footer (optional) Whether to enqueue the script before </head> or before </body>
       
    50  * @return null
    42  */
    51  */
    43 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    52 function wp_register_script( $handle, $src, $deps = array(), $ver = false, $in_footer = false ) {
    44 	global $wp_scripts;
    53 	global $wp_scripts;
    45 	if ( !is_a($wp_scripts, 'WP_Scripts') )
    54 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
    55 		if ( ! did_action( 'init' ) )
       
    56 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
    57 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    46 		$wp_scripts = new WP_Scripts();
    58 		$wp_scripts = new WP_Scripts();
       
    59 	}
    47 
    60 
    48 	$wp_scripts->add( $handle, $src, $deps, $ver );
    61 	$wp_scripts->add( $handle, $src, $deps, $ver );
    49 	if ( $in_footer )
    62 	if ( $in_footer )
    50 		$wp_scripts->add_data( $handle, 'group', 1 );
    63 		$wp_scripts->add_data( $handle, 'group', 1 );
    51 }
    64 }
    52 
    65 
    53 /**
    66 /**
    54  * Localizes a script.
    67  * Wrapper for $wp_scripts->localize().
    55  *
    68  *
    56  * Localizes only if script has already been added.
    69  * Used to localizes a script.
       
    70  * Works only if the script has already been added.
       
    71  * Accepts an associative array $l10n and creates JS object:
       
    72  * "$object_name" = {
       
    73  *   key: value,
       
    74  *   key: value,
       
    75  *   ...
       
    76  * }
       
    77  * See http://core.trac.wordpress.org/ticket/11520 for more information.
    57  *
    78  *
    58  * @since r16
    79  * @since r16
    59  * @see WP_Script::localize()
    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.
    60  */
    85  */
    61 function wp_localize_script( $handle, $object_name, $l10n ) {
    86 function wp_localize_script( $handle, $object_name, $l10n ) {
    62 	global $wp_scripts;
    87 	global $wp_scripts;
    63 	if ( !is_a($wp_scripts, '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 
    64 		return false;
    93 		return false;
       
    94 	}
    65 
    95 
    66 	return $wp_scripts->localize( $handle, $object_name, $l10n );
    96 	return $wp_scripts->localize( $handle, $object_name, $l10n );
    67 }
    97 }
    68 
    98 
    69 /**
    99 /**
    72  * @since r16
   102  * @since r16
    73  * @see WP_Scripts::remove() For parameter information.
   103  * @see WP_Scripts::remove() For parameter information.
    74  */
   104  */
    75 function wp_deregister_script( $handle ) {
   105 function wp_deregister_script( $handle ) {
    76 	global $wp_scripts;
   106 	global $wp_scripts;
    77 	if ( !is_a($wp_scripts, 'WP_Scripts') )
   107 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
   108 		if ( ! did_action( 'init' ) )
       
   109 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   110 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    78 		$wp_scripts = new WP_Scripts();
   111 		$wp_scripts = new WP_Scripts();
       
   112 	}
    79 
   113 
    80 	$wp_scripts->remove( $handle );
   114 	$wp_scripts->remove( $handle );
    81 }
   115 }
    82 
   116 
    83 /**
   117 /**
    84  * Enqueues script.
   118  * Enqueues script.
    85  *
   119  *
    86  * Registers the script if src provided (does NOT overwrite) and enqueues.
   120  * Registers the script if src provided (does NOT overwrite) and enqueues.
    87  *
   121  *
    88  * @since r16
   122  * @since r16
    89  * @see WP_Script::add(), WP_Script::enqueue()
   123  * @see wp_register_script() For parameter information.
    90 */
   124  */
    91 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
   125 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false, $in_footer = false ) {
    92 	global $wp_scripts;
   126 	global $wp_scripts;
    93 	if ( !is_a($wp_scripts, 'WP_Scripts') )
   127 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
   128 		if ( ! did_action( 'init' ) )
       
   129 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   130 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
    94 		$wp_scripts = new WP_Scripts();
   131 		$wp_scripts = new WP_Scripts();
       
   132 	}
    95 
   133 
    96 	if ( $src ) {
   134 	if ( $src ) {
    97 		$_handle = explode('?', $handle);
   135 		$_handle = explode('?', $handle);
    98 		$wp_scripts->add( $_handle[0], $src, $deps, $ver );
   136 		$wp_scripts->add( $_handle[0], $src, $deps, $ver );
    99 		if ( $in_footer )
   137 		if ( $in_footer )
   100 			$wp_scripts->add_data( $_handle[0], 'group', 1 );
   138 			$wp_scripts->add_data( $_handle[0], 'group', 1 );
   101 	}
   139 	}
   102 	$wp_scripts->enqueue( $handle );
   140 	$wp_scripts->enqueue( $handle );
       
   141 }
       
   142 
       
   143 /**
       
   144  * Remove an enqueued script.
       
   145  *
       
   146  * @since WP 3.1
       
   147  * @see WP_Scripts::dequeue() For parameter information.
       
   148  */
       
   149 function wp_dequeue_script( $handle ) {
       
   150 	global $wp_scripts;
       
   151 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
   152 		if ( ! did_action( 'init' ) )
       
   153 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   154 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
       
   155 		$wp_scripts = new WP_Scripts();
       
   156 	}
       
   157 
       
   158 	$wp_scripts->dequeue( $handle );
   103 }
   159 }
   104 
   160 
   105 /**
   161 /**
   106  * Check whether script has been added to WordPress Scripts.
   162  * Check whether script has been added to WordPress Scripts.
   107  *
   163  *
   114  * @param string $list Optional, defaults to 'queue'. Others values are 'registered', 'queue', 'done', 'to_do'
   170  * @param string $list Optional, defaults to 'queue'. Others values are 'registered', 'queue', 'done', 'to_do'
   115  * @return bool
   171  * @return bool
   116  */
   172  */
   117 function wp_script_is( $handle, $list = 'queue' ) {
   173 function wp_script_is( $handle, $list = 'queue' ) {
   118 	global $wp_scripts;
   174 	global $wp_scripts;
   119 	if ( !is_a($wp_scripts, 'WP_Scripts') )
   175 	if ( ! is_a( $wp_scripts, 'WP_Scripts' ) ) {
       
   176 		if ( ! did_action( 'init' ) )
       
   177 			_doing_it_wrong( __FUNCTION__, sprintf( __( 'Scripts and styles should not be registered or enqueued until the %1$s, %2$s, or %3$s hooks.' ),
       
   178 				'<code>wp_enqueue_scripts</code>', '<code>admin_enqueue_scripts</code>', '<code>init</code>' ), '3.3' );
   120 		$wp_scripts = new WP_Scripts();
   179 		$wp_scripts = new WP_Scripts();
       
   180 	}
   121 
   181 
   122 	$query = $wp_scripts->query( $handle, $list );
   182 	$query = $wp_scripts->query( $handle, $list );
   123 
   183 
   124 	if ( is_object( $query ) )
   184 	if ( is_object( $query ) )
   125 		return true;
   185 		return true;