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