[ Index ]

WordPress Source Cross Reference

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 $printed = array();
   6      var $args = array();
   7  
   8  	function WP_Scripts() {
   9          $this->default_scripts();
  10      }
  11  
  12  	function default_scripts() {
  13          $this->add( 'dbx', '/wp-includes/js/dbx.js', false, '2.02' );
  14          $this->add( 'fat', '/wp-includes/js/fat.js', false, '1.0-RC1_3660' );
  15          $this->add( 'sack', '/wp-includes/js/tw-sack.js', false, '1.6.1' );
  16          $this->add( 'quicktags', '/wp-includes/js/quicktags.js', false, '3517' );
  17          $this->add( 'colorpicker', '/wp-includes/js/colorpicker.js', false, '3517' );
  18          $this->add( 'tiny_mce', '/wp-includes/js/tinymce/tiny_mce_gzip.php', false, '04162006' );
  19          $this->add( 'wp_tiny_mce', '/wp-includes/js/tinymce/tiny_mce_config.php', array('tiny_mce'), '04162006' );
  20          if ( is_admin() ) {
  21              $this->add( 'dbx-admin-key', '/wp-admin/dbx-admin-key-js.php', array('dbx'), '3651' );
  22              $this->add( 'listman', '/wp-admin/list-manipulation-js.php', array('sack', 'fat'), '3850' ); // Make changeset # the correct one
  23              $this->add( 'ajaxcat', '/wp-admin/cat-js.php', array('listman'), '3684' );
  24              $this->add( 'admin-categories', '/wp-admin/categories.js', array('listman'), '3684' );
  25              $this->add( 'admin-custom-fields', '/wp-admin/custom-fields.js', array('listman'), '3733' );
  26              $this->add( 'admin-comments', '/wp-admin/edit-comments.js', array('listman'), '3850' ); // Make changeset # the correct one
  27              $this->add( 'admin-users', '/wp-admin/users.js', array('listman'), '3684' );
  28              $this->add( 'xfn', '/wp-admin/xfn.js', false, '3517' );
  29          }
  30      }
  31  
  32      /**
  33       * Prints script tags
  34       *
  35       * Prints the scripts passed to it or the print queue.  Also prints all necessary dependencies.
  36       *
  37       * @param mixed handles (optional) Scripts to be printed.  (void) prints queue, (string) prints that script, (array of strings) prints those scripts.
  38       * @return array Scripts that have been printed
  39       */
  40  	function print_scripts( $handles = false ) {
  41          // Print the queue if nothing is passed.  If a string is passed, print that script.  If an array is passed, print those scripts.
  42          $handles = false === $handles ? $this->queue : (array) $handles;
  43          $handles = $this->all_deps( $handles );
  44          $this->_print_scripts( $handles );
  45          return $this->printed;
  46      }
  47  
  48      /**
  49       * Internally used helper function for printing script tags
  50       *
  51       * @param array handles Hierarchical array of scripts to be printed
  52       * @see WP_Scripts::all_deps()
  53       */
  54  	function _print_scripts( $handles ) {
  55          global $wp_db_version;
  56  
  57          foreach( array_keys($handles) as $handle ) {
  58              if ( !$handles[$handle] )
  59                  return;
  60              elseif ( is_array($handles[$handle]) )
  61                  $this->_print_scripts( $handles[$handle] );
  62              if ( !in_array($handle, $this->printed) && isset($this->scripts[$handle]) ) {
  63                  $ver = $this->scripts[$handle]->ver ? $this->scripts[$handle]->ver : $wp_db_version;
  64                  if ( isset($this->args[$handle]) )
  65                      $ver .= '&amp;' . $this->args[$handle];
  66                  $src = 0 === strpos($this->scripts[$handle]->src, 'http://') ? $this->scripts[$handle]->src : get_settings( 'siteurl' ) . $this->scripts[$handle]->src;
  67                  echo "<script type='text/javascript' src='$src?ver=$ver'></script>\n";
  68                  $this->printed[] = $handle;
  69              }
  70          }
  71      }
  72                  
  73  
  74      /**
  75       * Determines dependencies of scripts
  76       *
  77       * Recursively builds hierarchical array of script dependencies.  Does NOT catch infinite loops.
  78       *
  79       * @param mixed handles Accepts (string) script name or (array of strings) script names
  80       * @param bool recursion Used internally when function calls itself
  81       * @return array Hierarchical array of dependencies
  82       */
  83  	function all_deps( $handles, $recursion = false ) {
  84          if ( ! $handles = (array) $handles )
  85              return array();
  86          $return = array();
  87          foreach ( $handles as $handle ) {
  88              $handle = explode('?', $handle);
  89              if ( isset($handle[1]) )
  90                  $this->args[$handle[0]] = $handle[1];
  91              $handle = $handle[0];
  92              if ( is_null($return[$handle]) ) // Prime the return array with $handles
  93                  $return[$handle] = true;
  94              if ( $this->scripts[$handle]->deps ) {
  95                  if ( false !== $return[$handle] && array_diff($this->scripts[$handle]->deps, array_keys($this->scripts)) )
  96                      $return[$handle] = false; // Script required deps which don't exist
  97                  else
  98                      $return[$handle] = $this->all_deps( $this->scripts[$handle]->deps, true ); // Build the hierarchy
  99              }
 100              if ( $recursion && false === $return[$handle] )
 101                  return false; // Cut the branch
 102          }
 103          return $return;
 104      }
 105  
 106      /**
 107       * Adds script
 108       *
 109       * Adds the script only if no script of that name already exists
 110       *
 111       * @param string handle Script name
 112       * @param string src Script url
 113       * @param array deps (optional) Array of script names on which this script depends
 114       * @param string ver (optional) Script version (used for cache busting)
 115       * @return array Hierarchical array of dependencies
 116       */
 117  	function add( $handle, $src, $deps = array(), $ver = false ) {
 118          if ( isset($this->scripts[$handle]) )
 119              return false;
 120          $this->scripts[$handle] = new _WP_Script( $handle, $src, $deps, $ver );
 121          return true;
 122      }
 123  
 124  	function remove( $handles ) {
 125          foreach ( (array) $handles as $handle )
 126              unset($this->scripts[$handle]);
 127      }
 128  
 129  	function enqueue( $handles ) {
 130          foreach ( (array) $handles as $handle ) {
 131              $handle = explode('?', $handle);
 132              if ( !in_array($handle[0], $this->queue) && isset($this->scripts[$handle[0]]) ) {
 133                  $this->queue[] = $handle[0];
 134                  if ( isset($handle[1]) )
 135                      $this->args[$handle[0]] = $handle[1];
 136              }
 137          }
 138      }
 139  
 140  	function dequeue( $handles ) {
 141          foreach ( (array) $handles as $handle )
 142              unset( $this->queue[$handle] );
 143      }
 144  
 145  	function query( $handle, $list = 'scripts' ) { // scripts, queue, or printed
 146          switch ( $list ) :
 147          case 'scripts':
 148              if ( isset($this->scripts[$handle]) )
 149                  return $this->scripts[$handle];
 150              break;
 151          default:
 152              if ( in_array($handle, $this->$list) )
 153                  return true;
 154              break;
 155          endswitch;
 156          return false;
 157      }
 158              
 159  }
 160  
 161  class _WP_Script {
 162      var $handle;
 163      var $src;
 164      var $deps = array();
 165      var $ver = false;
 166      var $args = false;
 167  
 168  	function _WP_Script() {
 169          @list($this->handle, $this->src, $this->deps, $this->ver) = func_get_args();
 170          if ( !is_array($this->deps) )
 171              $this->deps = array();
 172          if ( !$this->ver )
 173              $this->ver = false;
 174      }
 175  }
 176  
 177  /**
 178   * Prints script tags in document head
 179   *
 180   * Called by admin-header.php and by wp_head hook. Since it is called by wp_head on every page load,
 181   * the function does not instantiate the WP_Scripts object unless script names are explicitly passed.
 182   * Does make use of already instantiated $wp_scripts if present.
 183   * Use provided wp_print_scripts hook to register/enqueue new scripts.
 184   *
 185   * @see WP_Scripts::print_scripts()
 186   */
 187  function wp_print_scripts( $handles = false ) {
 188      do_action( 'wp_print_scripts' );
 189  
 190      global $wp_scripts;
 191      if ( !is_a($wp_scripts, 'WP_Scripts') ) {
 192          if ( !$handles )
 193              return array(); // No need to instantiate if nothing's there.
 194          else
 195              $wp_scripts = new WP_Scripts();
 196      }
 197  
 198      return $wp_scripts->print_scripts( $handles );
 199  }
 200  
 201  function wp_register_script( $handle, $src, $deps = array(), $ver = false ) {
 202      global $wp_scripts;
 203      if ( !is_a($wp_scripts, 'WP_Scripts') )
 204          $wp_scripts = new WP_Scripts();
 205  
 206      $wp_scripts->add( $handle, $src, $deps, $ver );
 207  }
 208  
 209  function wp_deregister_script( $handle ) {
 210      global $wp_scripts;
 211      if ( !is_a($wp_scripts, 'WP_Scripts') )
 212          $wp_scripts = new WP_Scripts();
 213  
 214      $wp_scripts->remove( $handle );
 215  }
 216  
 217  /**
 218   * Equeues script
 219   *
 220   * Registers the script if src provided (does NOT overwrite) and enqueues.
 221   *
 222   * @see WP_Script::add(), WP_Script::enqueue()
 223  */
 224  function wp_enqueue_script( $handle, $src = false, $deps = array(), $ver = false ) {
 225      global $wp_scripts;
 226      if ( !is_a($wp_scripts, 'WP_Scripts') )
 227          $wp_scripts = new WP_Scripts();
 228  
 229      if ( $src ) {
 230          $_handle = explode('?', $handle);
 231          $wp_scripts->add( $_handle[0], $src, $deps, $ver );
 232      }
 233      $wp_scripts->enqueue( $handle );
 234  }
 235  ?>

Your comment here...

Name: Location:
Comments:


List: Classes | Functions | Variables | Constants | Tables

Generated: Sat Jul 15 11:57:04 2006 Courtesy of Taragana