Wordpress 2.7.1
[autoinstalls/wordpress.git] / wp-includes / class.wp-scripts.php
1 <?php
2 /**
3  * BackPress Scripts enqueue.
4  *
5  * These classes were refactored from the WordPress WP_Scripts and WordPress
6  * script enqueue API.
7  *
8  * @package BackPress
9  * @since r16
10  */
11
12 /**
13  * BackPress Scripts enqueue class.
14  *
15  * @package BackPress
16  * @uses WP_Dependencies
17  * @since r16
18  */
19 class WP_Scripts extends WP_Dependencies {
20         var $base_url; // Full URL with trailing slash
21         var $default_version;
22
23         function __construct() {
24                 do_action_ref_array( 'wp_default_scripts', array(&$this) );
25         }
26
27         /**
28          * Prints scripts
29          *
30          * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
31          *
32          * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
33          * @return array Scripts that have been printed
34          */
35         function print_scripts( $handles = false ) {
36                 return $this->do_items( $handles );
37         }
38
39         function print_scripts_l10n( $handle ) {
40                 if ( empty($this->registered[$handle]->extra['l10n']) || empty($this->registered[$handle]->extra['l10n'][0]) || !is_array($this->registered[$handle]->extra['l10n'][1]) )
41                         return false;
42
43                 $object_name = $this->registered[$handle]->extra['l10n'][0];
44
45                 echo "<script type='text/javascript'>\n";
46                 echo "/* <![CDATA[ */\n";
47                 echo "\t$object_name = {\n";
48                 $eol = '';
49                 foreach ( $this->registered[$handle]->extra['l10n'][1] as $var => $val ) {
50                         if ( 'l10n_print_after' == $var ) {
51                                 $after = $val;
52                                 continue;
53                         }
54                         echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
55                         $eol = ",\n";
56                 }
57                 echo "\n\t}\n";
58                 echo isset($after) ? "\t$after\n" : '';
59                 echo "/* ]]> */\n";
60                 echo "</script>\n";
61
62                 return true;
63         }
64
65         function do_item( $handle ) {
66                 if ( !parent::do_item($handle) )
67                         return false;
68
69                 $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
70                 if ( isset($this->args[$handle]) )
71                         $ver .= '&amp;' . $this->args[$handle];
72
73                 $src = $this->registered[$handle]->src;
74                 if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) {
75                         $src = $this->base_url . $src;
76                 }
77
78                 $src = add_query_arg('ver', $ver, $src);
79                 $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
80
81                 $this->print_scripts_l10n( $handle );
82
83                 echo "<script type='text/javascript' src='$src'></script>\n";
84
85                 return true;
86         }
87
88         /**
89          * Localizes a script
90          *
91          * Localizes only if script has already been added
92          *
93          * @param string handle Script name
94          * @param string object_name Name of JS object to hold l10n info
95          * @param array l10n Array of JS var name => localized string
96          * @return bool Successful localization
97          */
98         function localize( $handle, $object_name, $l10n ) {
99                 if ( !$object_name || !$l10n )
100                         return false;
101                 return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
102         }
103
104         function all_deps( $handles, $recursion = false ) {
105                 $r = parent::all_deps( $handles, $recursion );
106                 if ( !$recursion )
107                         $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
108                 return $r;
109         }
110 }