PHP Cross Reference of WordPress Subversion HEAD

[ Index ]     [ Classes ]     [ Functions ]     [ Variables ]     [ Constants ]

title

Body

[close]

/wp-includes/ -> script-loader.php (source)

   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( 'dbx', '/wp-includes/js/dbx.js', false, '2.05' );
  15  
  16          $this->add( 'fat', '/wp-includes/js/fat.js', false, '1.0-RC1_3660' );
  17  
  18          $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
  19  
  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')
  31          ) );
  32  
  33          $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
  34  
  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' );
  38  
  39          $this->add( 'prototype', '/wp-includes/js/prototype.js', false, '1.5.1.1');
  40  
  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...')
  48          ) );
  49  
  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!")
  56          ) );
  57  
  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%?')
  62          ) );
  63  
  64          $this->add( 'wp-lists', '/wp-includes/js/wp-lists.js', array('jquery'), '20071101' );
  65          $this->localize( 'wp-lists', 'wpListL10n', array(
  66              'url' => get_option( 'siteurl' ) . '/wp-admin/admin-ajax.php'
  67          ) );
  68  
  69          $this->add( 'scriptaculous-root', '/wp-includes/js/scriptaculous/scriptaculous.js', array('prototype'), '1.7.1-b3');
  70          $this->add( 'scriptaculous-builder', '/wp-includes/js/scriptaculous/builder.js', array('scriptaculous-root'), '1.7.1-b3');
  71          $this->add( 'scriptaculous-dragdrop', '/wp-includes/js/scriptaculous/dragdrop.js', array('scriptaculous-builder', 'scriptaculous-effects'), '1.7.1-b3');
  72          $this->add( 'scriptaculous-effects', '/wp-includes/js/scriptaculous/effects.js', array('scriptaculous-root'), '1.7.1-b3');
  73          $this->add( 'scriptaculous-slider', '/wp-includes/js/scriptaculous/slider.js', array('scriptaculous-effects'), '1.7.1-b3');
  74          $this->add( 'scriptaculous-sound', '/wp-includes/js/scriptaculous/sound.js', array( 'scriptaculous-root' ), '1.7.1-b3' );
  75          $this->add( 'scriptaculous-controls', '/wp-includes/js/scriptaculous/controls.js', array('scriptaculous-root'), '1.7.1-b3');
  76          $this->add( 'scriptaculous', '', array('scriptaculous-dragdrop', 'scriptaculous-slider', 'scriptaculous-controls'), '1.7.1-b3');
  77  
  78          $this->add( 'cropper', '/wp-includes/js/crop/cropper.js', array('scriptaculous-dragdrop'), '20070118');
  79  
  80          $this->add( 'jquery', '/wp-includes/js/jquery/jquery.js', false, '1.1.4');
  81          $this->add( 'jquery-form', '/wp-includes/js/jquery/jquery.form.js', array('jquery'), '1.0.3');
  82          $this->add( 'interface', '/wp-includes/js/jquery/interface.js', array('jquery'), '1.2');
  83  
  84          if ( is_admin() ) {
  85              global $pagenow;
  86              $man = false;
  87              switch ( $pagenow ) :
  88              case 'post.php' :
  89              case 'post-new.php' :
  90                  $man = 'postmeta';
  91                  break;
  92              case 'page.php' :
  93              case 'page-new.php' :
  94                  $man = 'pagemeta';
  95                  break;
  96              case 'link-add.php' :
  97              case 'link.php' :
  98                  $man = 'linkmeta';
  99                  break;
 100              endswitch;
 101              if ( $man ) {
 102                  $this->add( 'dbx-admin-key', '/wp-admin/js/dbx-admin-key.js', array('dbx'), '20070417' );
 103                  $this->localize( 'dbx-admin-key', 'dbxL10n', array(
 104                      'manager' => $man,
 105                      'open' => __('open'),
 106                      'close' => __('close'),
 107                      'moveMouse' => __('click-down and drag to move this box'),
 108                      'toggleMouse' => __('click to %toggle% this box'),
 109                      'moveKey' => __('use the arrow keys to move this box'),
 110                      'toggleKey' => __(', or press the enter key to %toggle% it'),
 111                  ) );
 112              }
 113              $this->add( 'ajaxcat', '/wp-admin/js/cat.js', array( 'wp-lists' ), '20071101' );
 114              $this->localize( 'ajaxcat', 'catL10n', array(
 115                  'add' => attribute_escape(__('Add')),
 116                  'how' => __('Separate multiple categories with commas.')
 117              ) );
 118              $this->add( 'admin-categories', '/wp-admin/js/categories.js', array('wp-lists'), '20071031' );
 119              $this->add( 'admin-custom-fields', '/wp-admin/js/custom-fields.js', array('wp-lists'), '20070823' );
 120              $this->add( 'admin-comments', '/wp-admin/js/edit-comments.js', array('wp-lists'), '20071104' );
 121              $this->add( 'admin-posts', '/wp-admin/js/edit-posts.js', array('wp-lists'), '20071023' );
 122              $this->add( 'admin-users', '/wp-admin/js/users.js', array('wp-lists'), '20070823' );
 123              $this->add( 'xfn', '/wp-admin/js/xfn.js', false, '3517' );
 124              $this->add( 'upload', '/wp-admin/js/upload.js', array('jquery'), '20070518' );
 125              $this->localize( 'upload', 'uploadL10n', array(
 126                  'browseTitle' => attribute_escape(__('Browse your files')),
 127                  'back' => __('&laquo; Back'),
 128                  'directTitle' => attribute_escape(__('Direct link to file')),
 129                  'edit' => __('Edit'),
 130                  'thumb' => __('Thumbnail'),
 131                  'full' => __('Full size'),
 132                  'icon' => __('Icon'),
 133                  'title' => __('Title'),
 134                  'show' => __('Show:'),
 135                  'link' => __('Link to:'),
 136                  'file' => __('File'),
 137                  'page' => __('Page'),
 138                  'none' => __('None'),
 139                  'editorText' => attribute_escape(__('Send to editor &raquo;')),
 140                  'insert' => __('Insert'),
 141                  'urlText' => __('URL'),
 142                  'desc' => __('Description'),
 143                  'deleteText' => attribute_escape(__('Delete File')),
 144                  'saveText' => attribute_escape(__('Save &raquo;')),
 145                  'confirmText' => __("Are you sure you want to delete the file '%title%'?\nClick ok to delete or cancel to go back.")
 146              ) );
 147          }
 148      }
 149  
 150      /**
 151       * Prints script tags
 152       *
 153       * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
 154       *
 155       * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
 156       * @return array Scripts that have been printed
 157       */
 158  	function print_scripts( $handles = false ) {
 159          global $wp_db_version;
 160  
 161          // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
 162          $handles = false === $handles ? $this->queue : (array) $handles;
 163          $this->all_deps( $handles );
 164  
 165          $to_print = apply_filters( 'print_scripts_array', array_keys($this->to_print) );
 166  
 167          foreach( $to_print as $handle ) {
 168              if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
 169                  if ( $this->scripts[$handle]->src ) { // Else it defines a group.
 170                      $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
 171                      if ( isset($this->args[$handle]) )
 172                          $ver .= '&amp;' . $this->args[$handle];
 173                      $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_option( 'siteurl' ) . $this->scripts[$handle]->src;
 174                      $src = $this->scripts[$handle]->src;
 175  
 176                      if (!preg_match('|^https?://|', $src)) {
 177                          $src = get_option('siteurl') . $src;
 178                      }
 179  
 180                      $src = add_query_arg('ver', $ver, $src);
 181                      $src = clean_url(apply_filters( 'script_loader_src', $src ));
 182                      $this->print_scripts_l10n( $handle );
 183                      echo "<script type='text/javascript' src='$src'></script>\n";
 184                  }
 185                  $this->printed[] = $handle;
 186              }
 187          }
 188  
 189          $this->to_print = array();
 190          return $this->printed;
 191      }
 192  
 193  	function print_scripts_l10n( $handle ) {
 194          if ( empty($this->scripts[$handle]->l10n_object) || empty($this->scripts[$handle]->l10n) || !is_array($this->scripts[$handle]->l10n) )
 195              return;
 196  
 197          $object_name = $this->scripts[$handle]->l10n_object;
 198  
 199          echo "<script type='text/javascript'>\n";
 200          echo "/* <![CDATA[ */\n";
 201          echo "\t$object_name = {\n";
 202          $eol = '';
 203          foreach ( $this->scripts[$handle]->l10n as $var => $val ) {
 204              echo "$eol\t\t$var: \"" . js_escape( $val ) . '"';
 205              $eol = ",\n";
 206          }
 207          echo "\n\t}\n";
 208          echo "/* ]]> */\n";
 209          echo "</script>\n";
 210      }
 211  
 212      /**
 213       * Determines dependencies of scripts
 214       *
 215       * Recursively builds array of scripts to print taking dependencies into account.  Does NOT catch infinite loops.
 216       *
 217       * @param mixed handles Accepts (string) script name or (array of strings) script names
 218       * @param bool recursion Used internally when function calls itself
 219       */
 220  	function all_deps( $handles, $recursion = false ) {
 221          if ( !$handles = (array) $handles )
 222              return false;
 223  
 224          foreach ( $handles as $handle ) {
 225              $handle = explode('?', $handle);
 226              if ( isset($handle[1]) )
 227                  $this->args[$handle[0]] = $handle[1];
 228              $handle = $handle[0];
 229  
 230              if ( isset($this->to_print[$handle]) ) // Already grobbed it and its deps
 231                  continue;
 232  
 233              $keep_going = true;
 234              if ( !isset($this->scripts[$handle]) )
 235                  $keep_going = false; // Script doesn't exist
 236              elseif ( $this->scripts[$handle]->deps && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
 237                  $keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
 238              elseif ( $this->scripts[$handle]->deps && !$this->all_deps( $this->scripts[$handle]->deps, true ) )
 239                  $keep_going = false; // Script requires deps which don't exist
 240  
 241              if ( !$keep_going ) { // Either script or its deps don't exist.
 242                  if ( $recursion )
 243                      return false; // Abort this branch.
 244                  else
 245                      continue; // We're at the top level.  Move on to the next one.
 246              }                    
 247  
 248              $this->to_print[$handle] = true;
 249          }
 250  
 251          return true;
 252      }
 253  
 254      /**
 255       * Adds script
 256       *
 257       * Adds the script only if no script of that name already exists
 258       *
 259       * @param string handle Script name
 260       * @param string src Script url
 261       * @param array deps (optional) Array of script names on which this script depends
 262       * @param string ver (optional) Script version (used for cache busting)
 263       * @return array Hierarchical array of dependencies
 264       */
 265  	function add( $handle, $src, $deps = array(), $ver = false ) {
 266          if ( isset($this->scripts[$handle]) )
 267              return false;
 268          $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver );
 269          return true;
 270      }
 271  
 272      /**
 273       * Localizes a script
 274       *
 275       * Localizes only if script has already been added
 276       *
 277       * @param string handle Script name
 278       * @param string object_name Name of JS object to hold l10n info
 279       * @param array l10n Array of JS var name => localized string
 280       * @return bool Successful localization
 281       */
 282  	function localize( $handle, $object_name, $l10n ) {
 283          if ( !isset($this->scripts[$handle]) )
 284              return false;
 285          return $this->scripts[$handle]->localize( $object_name, $l10n );
 286      }
 287  
 288  	function remove( $handles ) {
 289          foreach ( (array) $handles as $handle )
 290              unset($this->scripts[$handle]);
 291      }
 292  
 293  	function enqueue( $handles ) {
 294          foreach ( (array) $handles as $handle ) {
 295              $handle = explode('?', $handle);
 296              if ( !in_array($handle[0], $this->queue) && isset($this->scripts[$handle[0]]) ) {
 297                  $this->queue[] = $handle[0];
 298                  if ( isset($handle[1]) )
 299                      $this->args[$handle[0]] = $handle[1];
 300              }
 301          }
 302      }
 303  
 304  	function dequeue( $handles ) {
 305          foreach ( (array) $handles as $handle )
 306              unset( $this->queue[$handle] );
 307      }
 308  
 309  	function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
 310          switch ( $list ) :
 311          case 'scripts':
 312              if ( isset($this->scripts[$handle]) )
 313                  return $this->scripts[$handle];
 314              break;
 315          default:
 316              if ( in_array($handle, $this->$list) )
 317                  return true;
 318              break;
 319          endswitch;
 320          return false;
 321      }
 322  
 323  }
 324  
 325  class _WP_Script {
 326      var $handle;
 327      var $src;
 328      var $deps = array();
 329      var $ver = false;
 330      var $l10n_object = '';
 331      var $l10n = array();
 332  
 333  	function _WP_Script() {
 334          @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
 335          if ( !is_array($this->deps) )
 336              $this->deps = array();
 337          if ( !$this->ver )
 338              $this->ver = false;
 339      }
 340  
 341  	function localize( $object_name, $l10n ) {
 342          if ( !$object_name || !is_array($l10n) )
 343              return false;
 344          $this->l10n_object = $object_name;
 345          $this->l10n = $l10n;
 346          return true;
 347      }
 348  }
 349  
 350  /**
 351   * Prints script tags in document head
 352   *
 353   * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load,
 354   * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
 355   * Does make use of already instantiated $wp_scripts if present.
 356   * Use provided wp_print_scripts hook to register/enqueue new scripts.
 357   *
 358   * @see WP_Scripts::print_scripts()
 359   */
 360  function wp_print_scripts( $handles = false ) {
 361      do_action( 'wp_print_scripts' );
 362      if ( '' === $handles ) // for wp_head
 363          $handles = false;
 364  
 365      global $wp_scripts;
 366      if ( !is_a($wp_scripts, 'WP_Scripts') ) {
 367          if ( !$handles )
 368              return array(); // No need to instantiate if nothing's there.
 369          else
 370              $wp_scripts = new WP_Scripts();
 371      }
 372  
 373      return $wp_scripts->print_scripts( $handles );
 374  }
 375  
 376  function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
 377      global $wp_scripts;
 378      if ( !is_a($wp_scripts, 'WP_Scripts') )
 379          $wp_scripts = new WP_Scripts();
 380  
 381      $wp_scripts->add( $handle, $src, $deps, $ver );
 382  }
 383  
 384  /**
 385   * Localizes a script
 386   *
 387   * Localizes only if script has already been added
 388   *
 389   * @see WP_Script::localize()
 390   */
 391  function wp_localize_script( $handle, $object_name, $l10n ) {
 392      global $wp_scripts;
 393      if ( !is_a($wp_scripts, 'WP_Scripts') )
 394          return false;
 395  
 396      return $wp_scripts->localize( $handle, $object_name, $l10n );
 397  }
 398  
 399  function wp_deregister_script( $handle ) {
 400      global $wp_scripts;
 401      if ( !is_a($wp_scripts, 'WP_Scripts') )
 402          $wp_scripts = new WP_Scripts();
 403  
 404      $wp_scripts->remove( $handle );
 405  }
 406  
 407  /**
 408   * Equeues script
 409   *
 410   * Registers the script if src provided (does NOT overwrite) and enqueues.
 411   *
 412   * @see WP_Script::add(), WP_Script::enqueue()
 413  */
 414  function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) {
 415      global $wp_scripts;
 416      if ( !is_a($wp_scripts, 'WP_Scripts') )
 417          $wp_scripts = new WP_Scripts();
 418  
 419      if ( $src ) {
 420          $_handle = explode('?', $handle);
 421          $wp_scripts->add( $_handle[0], $src, $deps, $ver );
 422      }
 423      $wp_scripts->enqueue( $handle );
 424  }
 425  
 426  function wp_prototype_before_jquery( $js_array ) {
 427      if ( false === $jquery = array_search( 'jquery', $js_array ) )
 428          return $js_array;
 429  
 430      if ( false === $prototype = array_search( 'prototype', $js_array ) )
 431          return $js_array;
 432  
 433      if ( $prototype < $jquery )
 434          return $js_array;
 435  
 436      unset($js_array[$prototype]);
 437  
 438      array_splice( $js_array, $jquery, 0, 'prototype' );
 439  
 440      return $js_array;
 441  }
 442  
 443  add_filter( 'print_scripts_array', 'wp_prototype_before_jquery' );
 444  
 445  ?>


Generated Thu Dec 6 06:47:08 2007 for RedAlt XRefs Cross-referenced by PHPXref 0.6 and RedAlt