3 class WP_Scripts extends WP_Dependencies {
4 var $base_url; // Full URL with trailing slash
7 function __construct() {
8 do_action_ref_array( 'wp_default_scripts', array(&$this) );
14 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
16 * @param mixed handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
17 * @return array Scripts that have been printed
19 function print_scripts( $handles = false ) {
20 return $this->do_items( $handles );
23 function print_scripts_l10n( $handle ) {
24 if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
27 $object_name = $this->registered[$handle]->extra['l10n'][0];
29 echo "<script type='text/javascript'>\n";
30 echo "/* <![CDATA[ */\n";
31 echo "\t$object_name = {\n";
33 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
34 echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
44 function do_item( $handle ) {
45 if ( !parent::do_item($handle) )
48 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
49 if ( isset($this->args[$handle]) )
50 $ver .= '&' . $this->args[$handle];
52 $src = $this->registered[$handle]->src;
53 if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) {
54 $src = $this->base_url . $src;
57 $src = add_query_arg('ver', $ver, $src);
58 $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
60 $this->print_scripts_l10n( $handle );
62 echo "<script type='text/javascript' src='$src'></script>\n";
70 * Localizes only if script has already been added
72 * @param string handle Script name
73 * @param string object_name Name of JS object to hold l10n info
74 * @param array l10n Array of JS var name => localized string
75 * @return bool Successful localization
77 function localize( $handle, $object_name, $l10n ) {
78 if ( !$object_name || !$l10n )
80 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
83 function all_deps( $handles, $recursion = false ) {
84 $r = parent::all_deps( $handles, $recursion );
86 $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );