root/trunk/wp-includes/class.wp-scripts.php

Revision 1519, 2.9 kB (checked in by donncha, 6 days ago)

WP Merge

Line 
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             echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
51             $eol = ",\n";
52         }
53         echo "\n\t}\n";
54         echo "/* ]]> */\n";
55         echo "</script>\n";
56
57         return true;
58     }
59
60     function do_item( $handle ) {
61         if ( !parent::do_item($handle) )
62             return false;
63
64         $ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version;
65         if ( isset($this->args[$handle]) )
66             $ver .= '&amp;' . $this->args[$handle];
67
68         $src = $this->registered[$handle]->src;
69         if ( !preg_match('|^https?://|', $src) && !preg_match('|^' . preg_quote(WP_CONTENT_URL) . '|', $src) ) {
70             $src = $this->base_url . $src;
71         }
72
73         $src = add_query_arg('ver', $ver, $src);
74         $src = clean_url(apply_filters( 'script_loader_src', $src, $handle ));
75
76         $this->print_scripts_l10n( $handle );
77
78         echo "<script type='text/javascript' src='$src'></script>\n";
79
80         return true;
81     }
82
83     /**
84      * Localizes a script
85      *
86      * Localizes only if script has already been added
87      *
88      * @param string handle Script name
89      * @param string object_name Name of JS object to hold l10n info
90      * @param array l10n Array of JS var name => localized string
91      * @return bool Successful localization
92      */
93     function localize( $handle, $object_name, $l10n ) {
94         if ( !$object_name || !$l10n )
95             return false;
96         return $this->add_data( $handle, 'l10n', array( $object_name, $l10n ) );
97     }
98
99     function all_deps( $handles, $recursion = false ) {
100         $r = parent::all_deps( $handles, $recursion );
101         if ( !$recursion )
102             $this->to_do = apply_filters( 'print_scripts_array', $this->to_do );
103         return $r;
104     }
105 }
106
Note: See TracBrowser for help on using the browser.