author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 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 $content_url; |
|
22 |
var $default_version; |
|
23 |
var $in_footer = array(); |
|
24 |
var $concat = ''; |
|
25 |
var $concat_version = ''; |
|
26 |
var $do_concat = false; |
|
27 |
var $print_html = ''; |
|
28 |
var $print_code = ''; |
|
29 |
var $ext_handles = ''; |
|
30 |
var $ext_version = ''; |
|
31 |
var $default_dirs; |
|
32 |
||
33 |
function __construct() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
34 |
$this->init(); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
add_action( 'init', array( $this, 'init' ), 0 ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
36 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
37 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
38 |
function init() { |
136 | 39 |
do_action_ref_array( 'wp_default_scripts', array(&$this) ); |
40 |
} |
|
41 |
||
42 |
/** |
|
43 |
* Prints scripts |
|
44 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
45 |
* Prints the scripts passed to it or the print queue. Also prints all necessary dependencies. |
136 | 46 |
* |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
* @param mixed $handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
* @param int $group (optional) If scripts were queued in groups prints this group number. |
136 | 49 |
* @return array Scripts that have been printed |
50 |
*/ |
|
51 |
function print_scripts( $handles = false, $group = false ) { |
|
52 |
return $this->do_items( $handles, $group ); |
|
53 |
} |
|
54 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
// Deprecated since 3.3, see print_extra_script() |
136 | 56 |
function print_scripts_l10n( $handle, $echo = true ) { |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
_deprecated_function( __FUNCTION__, '3.3', 'print_extra_script()' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
return $this->print_extra_script( $handle, $echo ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
} |
136 | 60 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
function print_extra_script( $handle, $echo = true ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
if ( !$output = $this->get_data( $handle, 'data' ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
return; |
136 | 64 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
65 |
if ( !$echo ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
return $output; |
136 | 67 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
68 |
echo "<script type='text/javascript'>\n"; // CDATA and type='text/javascript' is not needed for HTML 5 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
69 |
echo "/* <![CDATA[ */\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
70 |
echo "$output\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
71 |
echo "/* ]]> */\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
72 |
echo "</script>\n"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
73 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
74 |
return true; |
136 | 75 |
} |
76 |
||
77 |
function do_item( $handle, $group = false ) { |
|
78 |
if ( !parent::do_item($handle) ) |
|
79 |
return false; |
|
80 |
||
81 |
if ( 0 === $group && $this->groups[$handle] > 0 ) { |
|
82 |
$this->in_footer[] = $handle; |
|
83 |
return false; |
|
84 |
} |
|
85 |
||
86 |
if ( false === $group && in_array($handle, $this->in_footer, true) ) |
|
87 |
$this->in_footer = array_diff( $this->in_footer, (array) $handle ); |
|
88 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
if ( null === $this->registered[$handle]->ver ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
90 |
$ver = ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
$ver = $this->registered[$handle]->ver ? $this->registered[$handle]->ver : $this->default_version; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
|
136 | 94 |
if ( isset($this->args[$handle]) ) |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
95 |
$ver = $ver ? $ver . '&' . $this->args[$handle] : $this->args[$handle]; |
136 | 96 |
|
97 |
$src = $this->registered[$handle]->src; |
|
98 |
||
99 |
if ( $this->do_concat ) { |
|
100 |
$srce = apply_filters( 'script_loader_src', $src, $handle ); |
|
101 |
if ( $this->in_default_dir($srce) ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
102 |
$this->print_code .= $this->print_extra_script( $handle, false ); |
136 | 103 |
$this->concat .= "$handle,"; |
104 |
$this->concat_version .= "$handle$ver"; |
|
105 |
return true; |
|
106 |
} else { |
|
107 |
$this->ext_handles .= "$handle,"; |
|
108 |
$this->ext_version .= "$handle$ver"; |
|
109 |
} |
|
110 |
} |
|
111 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
112 |
$this->print_extra_script( $handle ); |
136 | 113 |
if ( !preg_match('|^https?://|', $src) && ! ( $this->content_url && 0 === strpos($src, $this->content_url) ) ) { |
114 |
$src = $this->base_url . $src; |
|
115 |
} |
|
116 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
117 |
if ( !empty($ver) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
118 |
$src = add_query_arg('ver', $ver, $src); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
119 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
120 |
$src = esc_url( apply_filters( 'script_loader_src', $src, $handle ) ); |
136 | 121 |
|
122 |
if ( $this->do_concat ) |
|
123 |
$this->print_html .= "<script type='text/javascript' src='$src'></script>\n"; |
|
124 |
else |
|
125 |
echo "<script type='text/javascript' src='$src'></script>\n"; |
|
126 |
||
127 |
return true; |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Localizes a script |
|
132 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
133 |
* Localizes only if the script has already been added |
136 | 134 |
*/ |
135 |
function localize( $handle, $object_name, $l10n ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
136 |
if ( is_array($l10n) && isset($l10n['l10n_print_after']) ) { // back compat, preserve the code in 'l10n_print_after' if present |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
137 |
$after = $l10n['l10n_print_after']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
138 |
unset($l10n['l10n_print_after']); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
139 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
140 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
141 |
foreach ( (array) $l10n as $key => $value ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
142 |
if ( !is_scalar($value) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
143 |
continue; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
144 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
145 |
$l10n[$key] = html_entity_decode( (string) $value, ENT_QUOTES, 'UTF-8'); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
146 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
147 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
148 |
$script = "var $object_name = " . json_encode($l10n) . ';'; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
149 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
150 |
if ( !empty($after) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
151 |
$script .= "\n$after;"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
152 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
153 |
$data = $this->get_data( $handle, 'data' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
154 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
155 |
if ( !empty( $data ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
156 |
$script = "$data\n$script"; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
157 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
158 |
return $this->add_data( $handle, 'data', $script ); |
136 | 159 |
} |
160 |
||
161 |
function set_group( $handle, $recursion, $group = false ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
162 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
163 |
if ( $this->registered[$handle]->args === 1 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
164 |
$grp = 1; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
165 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
166 |
$grp = (int) $this->get_data( $handle, 'group' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
|
136 | 168 |
if ( false !== $group && $grp > $group ) |
169 |
$grp = $group; |
|
170 |
||
171 |
return parent::set_group( $handle, $recursion, $grp ); |
|
172 |
} |
|
173 |
||
174 |
function all_deps( $handles, $recursion = false, $group = false ) { |
|
175 |
$r = parent::all_deps( $handles, $recursion ); |
|
176 |
if ( !$recursion ) |
|
177 |
$this->to_do = apply_filters( 'print_scripts_array', $this->to_do ); |
|
178 |
return $r; |
|
179 |
} |
|
180 |
||
181 |
function do_head_items() { |
|
182 |
$this->do_items(false, 0); |
|
183 |
return $this->done; |
|
184 |
} |
|
185 |
||
186 |
function do_footer_items() { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
187 |
$this->do_items(false, 1); |
136 | 188 |
return $this->done; |
189 |
} |
|
190 |
||
191 |
function in_default_dir($src) { |
|
192 |
if ( ! $this->default_dirs ) |
|
193 |
return true; |
|
194 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
195 |
if ( 0 === strpos( $src, '/wp-includes/js/l10n' ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
196 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
197 |
|
136 | 198 |
foreach ( (array) $this->default_dirs as $test ) { |
199 |
if ( 0 === strpos($src, $test) ) |
|
200 |
return true; |
|
201 |
} |
|
202 |
return false; |
|
203 |
} |
|
204 |
||
205 |
function reset() { |
|
206 |
$this->do_concat = false; |
|
207 |
$this->print_code = ''; |
|
208 |
$this->concat = ''; |
|
209 |
$this->concat_version = ''; |
|
210 |
$this->print_html = ''; |
|
211 |
$this->ext_version = ''; |
|
212 |
$this->ext_handles = ''; |
|
213 |
} |
|
214 |
} |