Wordpress 2.6.2
[autoinstalls/wordpress.git] / wp-includes / class.wp-scripts.php
1 <?php
2
3 class WP_Scripts extends WP_Dependencies {
4         var $base_url; // Full URL with trailing slash
5         var $default_version;
6
7         function __construct() {
8                 do_action_ref_array( 'wp_default_scripts', array(&$this) );
9         }
10
11         /**
12          * Prints scripts
13          *
14          * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
15          *
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
18          */
19         function print_scripts( $handles = false ) {
20                 return $this->do_items( $handles );
21         }
22
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]) )
25                         return false;
26
27                 $object_name = $this->registered[$handle]->extra['l10n'][0];
28
29                 echo "<script type='text/javascript'>\n";
30                 echo "/* <![CDATA[ */\n";
31                 echo "\t$object_name = {\n";
32                 $eol = '';
33                 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
34                         echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
35                         $eol = ",\n";
36                 }
37                 echo "\n\t}\n";
38                 echo "/* ]]> */\n";
39                 echo "</script>\n";
40
41                 return true;
42         }
43
44         function do_item( $handle ) {
45                 if ( !parent::do_item($handle) )
46                         return false;
47
48                 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
49                 if ( isset($this->args[$handle]) )
50                         $ver .= '&amp;' . $this->args[$handle];
51
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;
55                 }
56
57                 $src = add_query_arg('ver', $ver, $src);
58                 $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
59
60                 $this->print_scripts_l10n( $handle );
61
62                 echo "<script type='text/javascript' src='$src'></script>\n";
63
64                 return true;
65         }
66
67         /**
68          * Localizes a script
69          *
70          * Localizes only if script has already been added
71          *
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
76          */
77         function localize( $handle, $object_name, $l10n ) {
78                 if ( !$object_name || !$l10n )
79                         return false;
80                 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
81         }
82
83         function all_deps( $handles, $recursion = false ) {
84                 $r = parent::all_deps( $handles, $recursion );
85                 if ( !$recursion )
86                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
87                 return $r;
88         }
89 }