3 var $scripts = array();
5 var $to_print = array();
6 var $printed = array();
9 function WP_Scripts() {
10 $this->default_scripts();
13 function default_scripts() {
14 $this->add( 'dbx', '/wp-includes/js/dbx.js', false, '2.05' );
16 $this->add( 'fat', '/wp-includes/js/fat.js', false, '1.0-RC1_3660' );
18 $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
20 $this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3958' );
21 $this->localize( 'quicktags', 'quicktagsL10n', array(
22 'quickLinks' => __('(Quick Links)'),
23 'wordLookup' => __('Enter a word to look up:'),
24 'dictionaryLookup' => attribute_escape(__('Dictionary lookup')),
25 'lookup' => attribute_escape(__('lookup')),
26 'closeAllOpenTags' => attribute_escape(__('Close all open tags')),
27 'closeTags' => attribute_escape(__('close tags')),
28 'enterURL' => __('Enter the URL'),
29 'enterImageURL' => __('Enter the URL of the image'),
30 'enterImageDescription' => __('Enter a description of the image')
33 $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
35 $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '20070528' );
36 $mce_config = apply_filters('tiny_mce_config_url', '/wp-includes/js/tinymce/tiny_mce_config.php');
37 $this->add( 'wp_tiny_mce', $mce_config, array('tiny_mce'), '20070528' );
39 $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.1.1');
41 $this->add( 'autosave', '/wp-includes/js/autosave.js', array('prototype', 'sack'), '20070306');
42 $this->localize( 'autosave', 'autosaveL10n', array(
43 'autosaveInterval' => apply_filters('autosave_interval', '120'),
44 'errorText' => __('Error: %response%'),
45 'saveText' => __('Saved at %time%.'),
46 'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
47 'savingText' => __('Saving Draft...')
50 $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax.js', array('prototype'), '20070306');
51 $this->localize( 'wp-ajax', 'WPAjaxL10n', array(
52 'defaultUrl' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
53 'permText' => __("You don't have permission to do that."),
54 'strangeText' => __("Something strange happened. Try refreshing the page."),
55 'whoaText' => __("Slow down, I'm still sending your data!")
58 $this->add( 'listman', '/wp-includes/js/list-manipulation.js', array('wp-ajax', 'fat'), '20070306' );
59 $this->localize( 'listman', 'listManL10n', array(
60 'jumpText' => __('Jump to new item'),
61 'delText' => __('Are you sure you want to delete this %thing%?')
64 $this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/scriptaculous.js', array('prototype'), '1.7.1-b3');
65 $this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.7.1-b3');
66 $this->add( 'scriptaculous-dragdrop', '/wp-includes/js/scriptaculous/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.7.1-b3');
67 $this->add( 'scriptaculous-effects', '/wp-includes/js/scriptaculous/effects.js', array('scriptaculous-root'), '1.7.1-b3');
68 $this->add( 'scriptaculous-slider', '/wp-includes/js/scriptaculous/slider.js', array('scriptaculous-effects'), '1.7.1-b3');
69 $this->add( 'scriptaculous-sound', '/wp-includes/js/scriptaculous/sound.js', array( 'scriptaculous-root' ), '1.7.1-b3' );
70 $this->add( 'scriptaculous-controls', '/wp-includes/js/scriptaculous/controls.js', array('scriptaculous-root'), '1.7.1-b3');
71 $this->add( 'scriptaculous', '', array('scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls'), '1.7.1-b3');
73 $this->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118');
75 $this->add( 'jquery', '/wp-includes/js/jquery/jquery.js', false, '1.1.4');
76 $this->add( 'jquery-form', '/wp-includes/js/jquery/jquery.form.js', array('jquery'), '1.0.3');
77 $this->add( 'interface', '/wp-includes/js/jquery/interface.js', array('jquery'), '1.2');
97 $this->add( 'dbx-admin-key', '/wp-admin/js/dbx-admin-key.js', array('dbx'), '20070417' );
98 $this->localize( 'dbx-admin-key', 'dbxL10n', array(
100 'open' => __('open'),
101 'close' => __('close'),
102 'moveMouse' => __('click-down and drag to move this box'),
103 'toggleMouse' => __('click to %toggle% this box'),
104 'moveKey' => __('use the arrow keys to move this box'),
105 'toggleKey' => __(', or press the enter key to %toggle% it'),
108 $this->add( 'ajaxcat', '/wp-admin/js/cat.js', array('listman'), '20070724' );
109 $this->localize( 'ajaxcat', 'catL10n', array(
110 'add' => attribute_escape(__('Add')),
111 'how' => __('Separate multiple categories with commas.')
113 $this->add( 'ajaxlinkcat', '/wp-admin/js/link-cat.js', array('listman'), '200700601' );
114 $this->localize( 'ajaxlinkcat', 'linkcatL10n', array(
115 'add' => attribute_escape(__('Add')),
116 'how' => __('Separate multiple categories with commas.')
118 $this->add( 'admin-categories', '/wp-admin/js/categories.js', array('listman'), '3684' );
119 $this->add( 'admin-custom-fields', '/wp-admin/js/custom-fields.js', array('listman'), '3733' );
120 $this->add( 'admin-comments', '/wp-admin/js/edit-comments.js', array('listman'), '20070327' );
121 $this->add( 'admin-users', '/wp-admin/js/users.js', array('listman'), '4583' );
122 $this->add( 'xfn', '/wp-admin/js/xfn.js', false, '3517' );
123 $this->add( 'upload', '/wp-admin/js/upload.js', array('jquery'), '20070518' );
124 $this->localize( 'upload', 'uploadL10n', array(
125 'browseTitle' => attribute_escape(__('Browse your files')),
126 'back' => __('« Back'),
127 'directTitle' => attribute_escape(__('Direct link to file')),
128 'edit' => __('Edit'),
129 'thumb' => __('Thumbnail'),
130 'full' => __('Full size'),
131 'icon' => __('Icon'),
132 'title' => __('Title'),
133 'show' => __('Show:'),
134 'link' => __('Link to:'),
135 'file' => __('File'),
136 'page' => __('Page'),
137 'none' => __('None'),
138 'editorText' => attribute_escape(__('Send to editor »')),
139 'insert' => __('Insert'),
140 'urlText' => __('URL'),
141 'desc' => __('Description'),
142 'deleteText' => attribute_escape(__('Delete File')),
143 'saveText' => attribute_escape(__('Save »')),
144 'confirmText' => __("Are you sure you want to delete the file '%title%'?\nClick ok to delete or cancel to go back.")
152 * Prints the scripts passed to it or the print queue. Also prints all necessary dependencies.
154 * @param mixed handles (optional) Scripts to be printed. (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
155 * @return array Scripts that have been printed
157 function print_scripts( $handles = false ) {
158 global $wp_db_version;
160 // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
161 $handles = false === $handles ? $this->queue : (array) $handles;
162 $this->all_deps( $handles );
164 $to_print = apply_filters( 'print_scripts_array', array_keys($this->to_print) );
166 foreach( $to_print as $handle ) {
167 if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
168 if ( $this->scripts[$handle]->src ) { // Else it defines a group.
169 $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
170 if ( isset($this->args[$handle]) )
171 $ver .= '&' . $this->args[$handle];
172 $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_option( 'siteurl' ) . $this->scripts[$handle]->src;
173 $src = $this->scripts[$handle]->src;
175 if (!preg_match('|^https?://|', $src)) {
176 $src = get_option('siteurl') . $src;
179 $src = add_query_arg('ver', $ver, $src);
180 $src = clean_url(apply_filters( 'script_loader_src', $src ));
181 echo "<script type='text/javascript' src='$src'></script>\n";
182 $this->print_scripts_l10n( $handle );
184 $this->printed[] = $handle;
188 $this->to_print = array();
189 return $this->printed;
192 function print_scripts_l10n( $handle ) {
193 if ( empty($this->scripts[$handle]->l10n_object) || empty($this->scripts[$handle]->l10n) || !is_array($this->scripts[$handle]->l10n) )
196 $object_name = $this->scripts[$handle]->l10n_object;
198 echo "<script type='text/javascript'>\n";
199 echo "/* <![CDATA[ */\n";
200 echo "\t$object_name = {\n";
202 foreach ( $this->scripts[$handle]->l10n as $var => $val ) {
203 echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
212 * Determines dependencies of scripts
214 * Recursively builds array of scripts to print taking dependencies into account. Does NOT catch infinite loops.
216 * @param mixed handles Accepts (string) script name or (array of strings) script names
217 * @param bool recursion Used internally when function calls itself
219 function all_deps( $handles, $recursion = false ) {
220 if ( !$handles = (array) $handles )
223 foreach ( $handles as $handle ) {
224 $handle = explode('?', $handle);
225 if ( isset($handle[1]) )
226 $this->args[$handle[0]] = $handle[1];
227 $handle = $handle[0];
229 if ( isset($this->to_print[$handle]) ) // Already grobbed it and its deps
233 if ( !isset($this->scripts[$handle]) )
234 $keep_going = false; // Script doesn't exist
235 elseif ( $this->scripts[$handle]->deps && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
236 $keep_going = false; // Script requires deps which don't exist (not a necessary check. efficiency?)
237 elseif ( $this->scripts[$handle]->deps && !$this->all_deps( $this->scripts[$handle]->deps, true ) )
238 $keep_going = false; // Script requires deps which don't exist
240 if ( !$keep_going ) { // Either script or its deps don't exist.
242 return false; // Abort this branch.
244 continue; // We're at the top level. Move on to the next one.
247 $this->to_print[$handle] = true;
256 * Adds the script only if no script of that name already exists
258 * @param string handle Script name
259 * @param string src Script url
260 * @param array deps (optional) Array of script names on which this script depends
261 * @param string ver (optional) Script version (used for cache busting)
262 * @return array Hierarchical array of dependencies
264 function add( $handle, $src, $deps = array(), $ver = false ) {
265 if ( isset($this->scripts[$handle]) )
267 $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver );
274 * Localizes only if script has already been added
276 * @param string handle Script name
277 * @param string object_name Name of JS object to hold l10n info
278 * @param array l10n Array of JS var name => localized string
279 * @return bool Successful localization
281 function localize( $handle, $object_name, $l10n ) {
282 if ( !isset($this->scripts[$handle]) )
284 return $this->scripts[$handle]->localize( $object_name, $l10n );
287 function remove( $handles ) {
288 foreach ( (array) $handles as $handle )
289 unset($this->scripts[$handle]);
292 function enqueue( $handles ) {
293 foreach ( (array) $handles as $handle ) {
294 $handle = explode('?', $handle);
295 if ( !in_array($handle[0], $this->queue) && isset($this->scripts[$handle[0]]) ) {
296 $this->queue[] = $handle[0];
297 if ( isset($handle[1]) )
298 $this->args[$handle[0]] = $handle[1];
303 function dequeue( $handles ) {
304 foreach ( (array) $handles as $handle )
305 unset( $this->queue[$handle] );
308 function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
311 if ( isset($this->scripts[$handle]) )
312 return $this->scripts[$handle];
315 if ( in_array($handle, $this->$list) )
329 var $l10n_object = '';
332 function _WP_Script() {
333 @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
334 if ( !is_array($this->deps) )
335 $this->deps = array();
340 function localize( $object_name, $l10n ) {
341 if ( !$object_name || !is_array($l10n) )
343 $this->l10n_object = $object_name;
350 * Prints script tags in document head
352 * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load,
353 * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
354 * Does make use of already instantiated $wp_scripts if present.
355 * Use provided wp_print_scripts hook to register/enqueue new scripts.
357 * @see WP_Scripts::print_scripts()
359 function wp_print_scripts( $handles = false ) {
360 do_action( 'wp_print_scripts' );
361 if ( '' === $handles ) // for wp_head
365 if ( !is_a($wp_scripts, 'WP_Scripts') ) {
367 return array(); // No need to instantiate if nothing's there.
369 $wp_scripts = new WP_Scripts();
372 return $wp_scripts->print_scripts( $handles );
375 function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
377 if ( !is_a($wp_scripts, 'WP_Scripts') )
378 $wp_scripts = new WP_Scripts();
380 $wp_scripts->add( $handle, $src, $deps, $ver );
386 * Localizes only if script has already been added
388 * @see WP_Script::localize()
390 function wp_localize_script( $handle, $object_name, $l10n ) {
392 if ( !is_a($wp_scripts, 'WP_Scripts') )
395 return $wp_scripts->localize( $handle, $object_name, $l10n );
398 function wp_deregister_script( $handle ) {
400 if ( !is_a($wp_scripts, 'WP_Scripts') )
401 $wp_scripts = new WP_Scripts();
403 $wp_scripts->remove( $handle );
409 * Registers the script if src provided (does NOT overwrite) and enqueues.
411 * @see WP_Script::add(), WP_Script::enqueue()
413 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) {
415 if ( !is_a($wp_scripts, 'WP_Scripts') )
416 $wp_scripts = new WP_Scripts();
419 $_handle = explode('?', $handle);
420 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
422 $wp_scripts->enqueue( $handle );
425 function wp_prototype_before_jquery( $js_array ) {
426 if ( false === $jquery = array_search( 'jquery', $js_array ) )
429 if ( false === $prototype = array_search( 'prototype', $js_array ) )
432 if ( $prototype < $jquery )
435 unset($js_array[$prototype]);
437 array_splice( $js_array, $jquery, 0, 'prototype' );
442 add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );