]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/script-loader.php
Wordpress 2.5.1
[autoinstalls/wordpress.git] / wp-includes / script-loader.php
1 <?php
2 class WP_Scripts {
3         var $scripts = array();
4         var $queue = array();
5         var $to_print = array();
6         var $printed = array();
7         var $args = array();
8
9         function WP_Scripts() {
10                 $this->default_scripts();
11         }
12
13         function default_scripts() {
14                 $this->add( 'common', '/wp-admin/js/common.js', array('jquery'), '20080318' );
15                 $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
16
17                 $this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3958' );
18                 $this->localize( 'quicktags', 'quicktagsL10n', array(
19                         'quickLinks' => __('(Quick Links)'),
20                         'wordLookup' => __('Enter a word to look up:'),
21                         'dictionaryLookup' => attribute_escape(__('Dictionary lookup')),
22                         'lookup' => attribute_escape(__('lookup')),
23                         'closeAllOpenTags' => attribute_escape(__('Close all open tags')),
24                         'closeTags' => attribute_escape(__('close tags')),
25                         'enterURL' => __('Enter the URL'),
26                         'enterImageURL' => __('Enter the URL of the image'),
27                         'enterImageDescription' => __('Enter a description of the image')
28                 ) );
29
30                 $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', array('prototype'), '3517' );
31
32                 // Let a plugin replace the visual editor
33                 $visual_editor = apply_filters('visual_editor', array('tiny_mce'));
34                 $this->add( 'editor', false, $visual_editor, '20080321' );
35
36                 $this->add( 'editor_functions', '/wp-admin/js/editor.js', false, '20080325' );
37
38                 // Modify this version when tinyMCE plugins are changed.
39                 $mce_version = apply_filters('tiny_mce_version', '20080414');
40                 $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('editor_functions'), $mce_version );
41
42                 $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.6');
43
44                 $this->add( 'wp-ajax-response', '/wp-includes/js/wp-ajax-response.js', array('jquery'), '20080316' );
45                 $this->localize( 'wp-ajax-response', 'wpAjax', array(
46                         'noPerm' => __('You do not have permission to do that.'),
47                         'broken' => __('An unidentified error has occurred.')
48                 ) );
49
50                 $this->add( 'autosave', '/wp-includes/js/autosave.js', array('schedule', 'wp-ajax-response'), '20080424' );
51
52                 $this->add( 'wp-ajax', '/wp-includes/js/wp-ajax.js', array('prototype'), '20070306');
53                 $this->localize( 'wp-ajax', 'WPAjaxL10n', array(
54                         'defaultUrl' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
55                         'permText' => __("You do not have permission to do that."),
56                         'strangeText' => __("Something strange happened.  Try refreshing the page."),
57                         'whoaText' => __("Slow down, I'm still sending your data!")
58                 ) );
59
60                 $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('wp-ajax-response'), '20080411' );
61                 $this->localize( 'wp-lists', 'wpListL10n', array(
62                         'url' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php'
63                 ) );
64
65                 $this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/scriptaculous.js', array('prototype'), '1.8.0');
66                 $this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.8.0');
67                 $this->add( 'scriptaculous-dragdrop', '/wp-includes/js/scriptaculous/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.8.0');
68                 $this->add( 'scriptaculous-effects', '/wp-includes/js/scriptaculous/effects.js', array('scriptaculous-root'), '1.8.0');
69                 $this->add( 'scriptaculous-slider', '/wp-includes/js/scriptaculous/slider.js', array('scriptaculous-effects'), '1.8.0');
70                 $this->add( 'scriptaculous-sound', '/wp-includes/js/scriptaculous/sound.js', array( 'scriptaculous-root' ), '1.8.0' );
71                 $this->add( 'scriptaculous-controls', '/wp-includes/js/scriptaculous/controls.js', array('scriptaculous-root'), '1.8.0');
72                 $this->add( 'scriptaculous', '', array('scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls'), '1.8.0');
73
74                 $this->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118');
75
76                 $this->add( 'jquery', '/wp-includes/js/jquery/jquery.js', false, '1.2.3');
77                 $this->add( 'jquery-form', '/wp-includes/js/jquery/jquery.form.js', array('jquery'), '2.02');
78                 $this->add( 'jquery-color', '/wp-includes/js/jquery/jquery.color.js', array('jquery'), '2.0-4561');
79                 $this->add( 'interface', '/wp-includes/js/jquery/interface.js', array('jquery'), '1.2' );
80                 $this->add( 'dimensions', '/wp-includes/js/jquery/jquery.dimensions.min.js', array('jquery'), '1.1.2');
81                 $this->add( 'suggest', '/wp-includes/js/jquery/suggest.js', array('dimensions'), '1.1');
82                 $this->add( 'schedule', '/wp-includes/js/jquery/jquery.schedule.js', array('jquery'), '20');
83                 $this->add( 'thickbox', '/wp-includes/js/thickbox/thickbox.js', array('jquery'), '3.1');
84                 $this->add( 'swfupload', '/wp-includes/js/swfupload/swfupload.js', false, '2.0.2');
85                 $this->add( 'swfupload-degrade', '/wp-includes/js/swfupload/plugins/swfupload.graceful_degradation.js', array('swfupload'), '2.0.2');
86                 $this->localize( 'swfupload-degrade', 'uploadDegradeOptions', array(
87                         'is_lighttpd_before_150' => is_lighttpd_before_150(),
88                 ) );
89                 $this->add( 'swfupload-queue', '/wp-includes/js/swfupload/plugins/swfupload.queue.js', array('swfupload'), '2.0.2');
90                 $this->add( 'swfupload-handlers', '/wp-includes/js/swfupload/handlers.js', array('swfupload'), '2.0.2-20080407');
91                 // these error messages came from the sample swfupload js, they might need changing.
92                 $this->localize( 'swfupload-handlers', 'swfuploadL10n', array(
93                                 'queue_limit_exceeded' => __('You have attempted to queue too many files.'),
94                                 'file_exceeds_size_limit' => sprintf(__('This file is too big. Your php.ini upload_max_filesize is %s.'), @ini_get('upload_max_filesize')),
95                                 'zero_byte_file' => __('This file is empty. Please try another.'),
96                                 'invalid_filetype' => __('This file type is not allowed. Please try another.'),
97                                 'default_error' => __('An error occurred in the upload. Please try again later.'),
98                                 'missing_upload_url' => __('There was a configuration error. Please contact the server administrator.'),
99                                 'upload_limit_exceeded' => __('You may only upload 1 file.'),
100                                 'http_error' => __('HTTP error.'),
101                                 'upload_failed' => __('Upload failed.'),
102                                 'io_error' => __('IO error.'),
103                                 'security_error' => __('Security error.'),
104                                 'file_cancelled' => __('File cancelled.'),
105                                 'upload_stopped' => __('Upload stopped.'),
106                                 'dismiss' => __('Dismiss'),
107                                 'crunching' => __('Crunching&hellip;'),
108                                 'deleted' => __('Deleted'),
109                 ) );
110
111                 $this->add( 'jquery-ui-tabs', '/wp-includes/js/jquery/ui.tabs.js', array('jquery'), '3' );
112
113                 if ( is_admin() ) {
114                         $this->add( 'ajaxcat', '/wp-admin/js/cat.js', array( 'wp-lists' ), '20071101' );
115                         $this->localize( 'ajaxcat', 'catL10n', array(
116                                 'add' => attribute_escape(__('Add')),
117                                 'how' => __('Separate multiple categories with commas.')
118                         ) );
119                         $this->add( 'admin-categories', '/wp-admin/js/categories.js', array('wp-lists'), '20071031' );
120                         $this->add( 'admin-tags', '/wp-admin/js/tags.js', array('wp-lists'), '20071031' );
121                         $this->add( 'admin-custom-fields', '/wp-admin/js/custom-fields.js', array('wp-lists'), '20070823' );
122                         $this->add( 'password-strength-meter', '/wp-admin/js/password-strength-meter.js', array('jquery'), '20070405' );
123                         $this->localize( 'password-strength-meter', 'pwsL10n', array(
124                                 'short' => __('Too short'),
125                                 'bad' => __('Bad'),
126                                 'good' => __('Good'),
127                                 'strong' => __('Strong')
128                         ) );
129                         $this->add( 'admin-comments', '/wp-admin/js/edit-comments.js', array('wp-lists'), '20080311' );
130                         $this->localize( 'admin-comments', 'adminCommentsL10n', array(
131                                 'pending' => __('%i% pending') // must look like: "# blah blah"
132                         ) );
133                         $this->add( 'admin-users', '/wp-admin/js/users.js', array('wp-lists'), '20070823' );
134                         $this->add( 'admin-forms', '/wp-admin/js/forms.js', false, '20080317' );
135                         $this->add( 'xfn', '/wp-admin/js/xfn.js', false, '3517' );
136                         $this->add( 'upload', '/wp-admin/js/upload.js', array('jquery'), '20070518' );
137                         $this->add( 'postbox', '/wp-admin/js/postbox.js', array('jquery'), '20080128' );
138                         $this->localize( 'postbox', 'postboxL10n', array(
139                                 'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
140                         ) );
141                         $this->add( 'slug', '/wp-admin/js/slug.js', array('jquery'), '20080208' );
142                         $this->localize( 'slug', 'slugL10n', array(
143                                 'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
144                                 'save' => __('Save'),
145                                 'cancel' => __('Cancel'),
146                         ) );
147                         $this->add( 'post', '/wp-admin/js/post.js', array('suggest', 'jquery-ui-tabs', 'wp-lists', 'postbox', 'slug'), '20080422' );
148                         $this->localize( 'post', 'postL10n', array(
149                                 'tagsUsed' =>  __('Tags used on this post:'),
150                                 'add' => attribute_escape(__('Add')),
151                                 'addTag' => attribute_escape(__('Add new tag')),
152                                 'separate' => __('Separate tags with commas'),
153                                 'cancel' => __('Cancel'),
154                                 'edit' => __('Edit'),
155                         ) );
156                         $this->add( 'page', '/wp-admin/js/page.js', array('jquery', 'slug', 'postbox'), '20080318' );
157                         $this->localize( 'page', 'postL10n', array(
158                                 'cancel' => __('Cancel'),
159                                 'edit' => __('Edit'),
160                         ) );
161                         $this->add( 'link', '/wp-admin/js/link.js', array('jquery-ui-tabs', 'wp-lists', 'postbox'), '20080131' );
162                         $this->add( 'comment', '/wp-admin/js/comment.js', array('postbox'), '20080219' );
163                         $this->localize( 'comment', 'commentL10n', array(
164                                         'cancel' => __('Cancel'),
165                                         'edit' => __('Edit'),
166                                 ) );
167                         $this->add( 'media-upload', '/wp-admin/js/media-upload.js', false, '20080109' );
168                         $this->localize( 'upload', 'uploadL10n', array(
169                                 'browseTitle' => attribute_escape(__('Browse your files')),
170                                 'back' => __('&laquo; Back'),
171                                 'directTitle' => attribute_escape(__('Direct link to file')),
172                                 'edit' => __('Edit'),
173                                 'thumb' => __('Thumbnail'),
174                                 'full' => __('Full size'),
175                                 'icon' => __('Icon'),
176                                 'title' => __('Title'),
177                                 'show' => __('Show:'),
178                                 'link' => __('Link to:'),
179                                 'file' => __('File'),
180                                 'page' => __('Page'),
181                                 'none' => __('None'),
182                                 'editorText' => attribute_escape(__('Send to editor &raquo;')),
183                                 'insert' => __('Insert'),
184                                 'urlText' => __('URL'),
185                                 'desc' => __('Description'),
186                                 'deleteText' => attribute_escape(__('Delete File')),
187                                 'saveText' => attribute_escape(__('Save &raquo;')),
188                                 'confirmText' => __("Are you sure you want to delete the file '%title%'?\nClick ok to delete or cancel to go back.")
189                         ) );
190                         $this->add( 'admin-widgets', '/wp-admin/js/widgets.js', array( 'interface' ), '20080407c' );
191                         $this->localize( 'admin-widgets', 'widgetsL10n', array(
192                                 'add' => __('Add'),
193                                 'edit' => __('Edit'),
194                                 'cancel' => __('Cancel'),
195                         ));
196                         $this->add( 'editor', '/wp-admin/js/editor.js', array('tiny_mce'), '20080221' );
197                 }
198         }
199
200         /**
201          * Prints script tags
202          *
203          * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
204          *
205          * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
206          * @return array Scripts that have been printed
207          */
208         function print_scripts( $handles = false ) {
209                 global $wp_db_version;
210
211                 // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
212                 $handles = false === $handles ? $this->queue : (array) $handles;
213                 $this->all_deps( $handles );
214
215                 $to_print = apply_filters( 'print_scripts_array', array_keys($this->to_print) );
216
217                 foreach( $to_print as $handle ) {
218                         if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
219                                 if ( $this->scripts[$handle]->src ) { // Else it defines a group.
220                                         $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
221                                         if ( isset($this->args[$handle]) )
222                                                 $ver .= '&amp;' . $this->args[$handle];
223                                         $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_option( 'siteurl' ) . $this->scripts[$handle]->src;
224                                         $src = $this->scripts[$handle]->src;
225
226                                         if (!preg_match('|^https?://|', $src)) {
227                                                 $src = get_option('siteurl') . $src;
228                                         }
229
230                                         $src = add_query_arg('ver', $ver, $src);
231                                         $src = clean_url(apply_filters( 'script_loader_src', $src ));
232                                         $this->print_scripts_l10n( $handle );
233                                         echo "<script type='text/javascript' src='$src'></script>\n";
234                                 }
235                                 $this->printed[] = $handle;
236                         }
237                 }
238
239                 $this->to_print = array();
240                 return $this->printed;
241         }
242
243         function print_scripts_l10n( $handle ) {
244                 if ( empty($this->scripts[$handle]->l10n_object) || empty($this->scripts[$handle]->l10n) || !is_array($this->scripts[$handle]->l10n) )
245                         return;
246
247                 $object_name = $this->scripts[$handle]->l10n_object;
248
249                 echo "<script type='text/javascript'>\n";
250                 echo "/* <![CDATA[ */\n";
251                 echo "\t$object_name = {\n";
252                 $eol = '';
253                 foreach ( $this->scripts[$handle]->l10n as $var => $val ) {
254                         echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
255                         $eol = ",\n";
256                 }
257                 echo "\n\t}\n";
258                 echo "/* ]]> */\n";
259                 echo "</script>\n";
260         }
261
262         /**
263          * Determines dependencies of scripts
264          *
265          * Recursively builds array of scripts to print taking dependencies into account.  Does NOT catch infinite loops.
266          *
267          * @param mixed handles Accepts (string) script name or (array of strings) script names
268          * @param bool recursion Used internally when function calls itself
269          */
270         function all_deps( $handles, $recursion = false ) {
271                 if ( !$handles = (array) $handles )
272                         return false;
273
274                 foreach ( $handles as $handle ) {
275                         $handle = explode('?', $handle);
276                         if ( isset($handle[1]) )
277                                 $this->args[$handle[0]] = $handle[1];
278                         $handle = $handle[0];
279
280                         if ( isset($this->to_print[$handle]) ) // Already grobbed it and its deps
281                                 continue;
282
283                         $keep_going = true;
284                         if ( !isset($this->scripts[$handle]) )
285                                 $keep_going = false; // Script doesn't exist
286                         elseif ( $this->scripts[$handle]->deps && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
287                                 $keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
288                         elseif ( $this->scripts[$handle]->deps && !$this->all_deps( $this->scripts[$handle]->deps, true ) )
289                                 $keep_going = false; // Script requires deps which don't exist
290
291                         if ( !$keep_going ) { // Either script or its deps don't exist.
292                                 if ( $recursion )
293                                         return false; // Abort this branch.
294                                 else
295                                         continue; // We're at the top level.  Move on to the next one.
296                         }
297
298                         $this->to_print[$handle] = true;
299                 }
300
301                 return true;
302         }
303
304         /**
305          * Adds script
306          *
307          * Adds the script only if no script of that name already exists
308          *
309          * @param string handle Script name
310          * @param string src Script url
311          * @param array deps (optional) Array of script names on which this script depends
312          * @param string ver (optional) Script version (used for cache busting)
313          * @return array Hierarchical array of dependencies
314          */
315         function add( $handle, $src, $deps = array(), $ver = false ) {
316                 if ( isset($this->scripts[$handle]) )
317                         return false;
318                 $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver );
319                 return true;
320         }
321
322         /**
323          * Localizes a script
324          *
325          * Localizes only if script has already been added
326          *
327          * @param string handle Script name
328          * @param string object_name Name of JS object to hold l10n info
329          * @param array l10n Array of JS var name => localized string
330          * @return bool Successful localization
331          */
332         function localize( $handle, $object_name, $l10n ) {
333                 if ( !isset($this->scripts[$handle]) )
334                         return false;
335                 return $this->scripts[$handle]->localize( $object_name, $l10n );
336         }
337
338         function remove( $handles ) {
339                 foreach ( (array) $handles as $handle )
340                         unset($this->scripts[$handle]);
341         }
342
343         function enqueue( $handles ) {
344                 foreach ( (array) $handles as $handle ) {
345                         $handle = explode('?', $handle);
346                         if ( !in_array($handle[0], $this->queue) && isset($this->scripts[$handle[0]]) ) {
347                                 $this->queue[] = $handle[0];
348                                 if ( isset($handle[1]) )
349                                         $this->args[$handle[0]] = $handle[1];
350                         }
351                 }
352         }
353
354         function dequeue( $handles ) {
355                 foreach ( (array) $handles as $handle )
356                         unset( $this->queue[$handle] );
357         }
358
359         function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
360                 switch ( $list ) :
361                 case 'scripts':
362                         if ( isset($this->scripts[$handle]) )
363                                 return $this->scripts[$handle];
364                         break;
365                 default:
366                         if ( in_array($handle, $this->$list) )
367                                 return true;
368                         break;
369                 endswitch;
370                 return false;
371         }
372
373 }
374
375 class _WP_Script {
376         var $handle;
377         var $src;
378         var $deps = array();
379         var $ver = false;
380         var $l10n_object = '';
381         var $l10n = array();
382
383         function _WP_Script() {
384                 @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
385                 if ( !is_array($this->deps) )
386                         $this->deps = array();
387                 if ( !$this->ver )
388                         $this->ver = false;
389         }
390
391         function localize( $object_name, $l10n ) {
392                 if ( !$object_name || !is_array($l10n) )
393                         return false;
394                 $this->l10n_object = $object_name;
395                 $this->l10n = $l10n;
396                 return true;
397         }
398 }
399
400 /**
401  * Prints script tags in document head
402  *
403  * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load,
404  * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
405  * Does make use of already instantiated $wp_scripts if present.
406  * Use provided wp_print_scripts hook to register/enqueue new scripts.
407  *
408  * @see WP_Scripts::print_scripts()
409  */
410 function wp_print_scripts( $handles = false ) {
411         do_action( 'wp_print_scripts' );
412         if ( '' === $handles ) // for wp_head
413                 $handles = false;
414
415         global $wp_scripts;
416         if ( !is_a($wp_scripts, 'WP_Scripts') ) {
417                 if ( !$handles )
418                         return array(); // No need to instantiate if nothing's there.
419                 else
420                         $wp_scripts = new WP_Scripts();
421         }
422
423         return $wp_scripts->print_scripts( $handles );
424 }
425
426 function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
427         global $wp_scripts;
428         if ( !is_a($wp_scripts, 'WP_Scripts') )
429                 $wp_scripts = new WP_Scripts();
430
431         $wp_scripts->add( $handle, $src, $deps, $ver );
432 }
433
434 /**
435  * Localizes a script
436  *
437  * Localizes only if script has already been added
438  *
439  * @see WP_Script::localize()
440  */
441 function wp_localize_script( $handle, $object_name, $l10n ) {
442         global $wp_scripts;
443         if ( !is_a($wp_scripts, 'WP_Scripts') )
444                 return false;
445
446         return $wp_scripts->localize( $handle, $object_name, $l10n );
447 }
448
449 function wp_deregister_script( $handle ) {
450         global $wp_scripts;
451         if ( !is_a($wp_scripts, 'WP_Scripts') )
452                 $wp_scripts = new WP_Scripts();
453
454         $wp_scripts->remove( $handle );
455 }
456
457 /**
458  * Equeues script
459  *
460  * Registers the script if src provided (does NOT overwrite) and enqueues.
461  *
462  * @see WP_Script::add(), WP_Script::enqueue()
463 */
464 function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) {
465         global $wp_scripts;
466         if ( !is_a($wp_scripts, 'WP_Scripts') )
467                 $wp_scripts = new WP_Scripts();
468
469         if ( $src ) {
470                 $_handle = explode('?', $handle);
471                 $wp_scripts->add( $_handle[0], $src, $deps, $ver );
472         }
473         $wp_scripts->enqueue( $handle );
474 }
475
476 function wp_prototype_before_jquery( $js_array ) {
477         if ( false === $jquery = array_search( 'jquery', $js_array ) )
478                 return $js_array;
479
480         if ( false === $prototype = array_search( 'prototype', $js_array ) )
481                 return $js_array;
482
483         if ( $prototype < $jquery )
484                 return $js_array;
485
486         unset($js_array[$prototype]);
487
488         array_splice( $js_array, $jquery, 0, 'prototype' );
489
490         return $js_array;
491 }
492
493 // These localizations require information that may not be loaded even by init
494 function wp_just_in_time_script_localization() {
495         wp_localize_script( 'tiny_mce', 'wpTinyMCEConfig', array( 'defaultEditor' => wp_default_editor() ) );
496         wp_localize_script( 'autosave', 'autosaveL10n', array(
497                 'autosaveInterval' => AUTOSAVE_INTERVAL,
498                 'previewPageText' => __('Preview this Page'),
499                 'previewPostText' => __('Preview this Post'),
500                 'requestFile' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php',
501                 'savingText' => __('Saving Draft&#8230;')
502         ) );
503 }
504
505 add_filter( 'wp_print_scripts', 'wp_just_in_time_script_localization' );
506 add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
507
508 ?>