[ XREF Home ] [ Index ]

PHP Cross Reference of WordPress Trunk

Provided by Yoast

title

Body

[close]

/wp-includes/ -> class.wp-dependencies.php (source)

   1  <?php
   2  /**
   3   * BackPress Scripts enqueue.
   4   *
   5   * These classes were refactored from the WordPress WP_Scripts and WordPress
   6   * script enqueue API.
   7   *
   8   * @package BackPress
   9   * @since r74
  10   */
  11  
  12  /**
  13   * BackPress enqueued dependiences class.
  14   *
  15   * @package BackPress
  16   * @uses _WP_Dependency
  17   * @since r74
  18   */
  19  class WP_Dependencies {
  20      var $registered = array();
  21      var $queue = array();
  22      var $to_do = array();
  23      var $done = array();
  24      var $args = array();
  25      var $groups = array();
  26      var $group = 0;
  27  
  28      /**
  29       * Do the dependencies
  30       *
  31       * Process the items passed to it or the queue.  Processes all dependencies.
  32       *
  33       * @param mixed $handles (optional) items to be processed. (void) processes queue, (string) process that item, (array of strings) process those items
  34       * @return array Items that have been processed
  35       */
  36  	function do_items( $handles = false, $group = false ) {
  37          // Print the queue if nothing is passed. If a string is passed, print that script. If an array is passed, print those scripts.
  38          $handles = false === $handles ? $this->queue : (array) $handles;
  39          $this->all_deps( $handles );
  40  
  41          foreach( $this->to_do as $key => $handle ) {
  42              if ( !in_array($handle, $this->done) && isset($this->registered[$handle]) ) {
  43  
  44                  if ( ! $this->registered[$handle]->src ) { // Defines a group.
  45                      $this->done[] = $handle;
  46                      continue;
  47                  }
  48  
  49                  if ( $this->do_item( $handle, $group ) )
  50                      $this->done[] = $handle;
  51  
  52                  unset( $this->to_do[$key] );
  53              }
  54          }
  55  
  56          return $this->done;
  57      }
  58  
  59  	function do_item( $handle ) {
  60          return isset($this->registered[$handle]);
  61      }
  62  
  63      /**
  64       * Determines dependencies
  65       *
  66       * Recursively builds array of items to process taking dependencies into account.  Does NOT catch infinite loops.
  67       *
  68       *
  69       * @param mixed $handles Accepts (string) dep name or (array of strings) dep names
  70       * @param bool $recursion Used internally when function calls itself
  71       */
  72  	function all_deps( $handles, $recursion = false, $group = false ) {
  73          if ( !$handles = (array) $handles )
  74              return false;
  75  
  76          foreach ( $handles as $handle ) {
  77              $handle_parts = explode('?', $handle);
  78              $handle = $handle_parts[0];
  79              $queued = in_array($handle, $this->to_do, true);
  80  
  81              if ( in_array($handle, $this->done, true) ) // Already done
  82                  continue;
  83  
  84              $moved = $this->set_group( $handle, $recursion, $group );
  85  
  86              if ( $queued && !$moved ) // already queued and in the right group
  87                  continue;
  88  
  89              $keep_going = true;
  90              if ( !isset($this->registered[$handle]) )
  91                  $keep_going = false; // Script doesn't exist
  92              elseif ( $this->registered[$handle]->deps && array_diff($this->registered[$handle]->deps, array_keys($this->registered)) )
  93                  $keep_going = false; // Script requires deps which don't exist (not a necessary check.  efficiency?)
  94              elseif ( $this->registered[$handle]->deps && !$this->all_deps( $this->registered[$handle]->deps, true, $group ) )
  95                  $keep_going = false; // Script requires deps which don't exist
  96  
  97              if ( !$keep_going ) { // Either script or its deps don't exist.
  98                  if ( $recursion )
  99                      return false; // Abort this branch.
 100                  else
 101                      continue; // We're at the top level.  Move on to the next one.
 102              }
 103  
 104              if ( $queued ) // Already grobbed it and its deps
 105                  continue;
 106  
 107              if ( isset($handle_parts[1]) )
 108                  $this->args[$handle] = $handle_parts[1];
 109  
 110              $this->to_do[] = $handle;
 111          }
 112  
 113          return true;
 114      }
 115  
 116      /**
 117       * Adds item
 118       *
 119       * Adds the item only if no item of that name already exists
 120       *
 121       * @param string $handle Script name
 122       * @param string $src Script url
 123       * @param array $deps (optional) Array of script names on which this script depends
 124       * @param string $ver (optional) Script version (used for cache busting)
 125       * @return array Hierarchical array of dependencies
 126       */
 127  	function add( $handle, $src, $deps = array(), $ver = false, $args = null ) {
 128          if ( isset($this->registered[$handle]) )
 129              return false;
 130          $this->registered[$handle] = new _WP_Dependency( $handle, $src, $deps, $ver, $args );
 131          return true;
 132      }
 133  
 134      /**
 135       * Adds extra data
 136       *
 137       * Adds data only if script has already been added
 138       *
 139       * @param string $handle Script name
 140       * @param string $data_name Name of object in which to store extra data
 141       * @param array $data Array of extra data
 142       * @return bool success
 143       */
 144  	function add_data( $handle, $data_name, $data ) {
 145          if ( !isset($this->registered[$handle]) )
 146              return false;
 147          return $this->registered[$handle]->add_data( $data_name, $data );
 148      }
 149  
 150  	function remove( $handles ) {
 151          foreach ( (array) $handles as $handle )
 152              unset($this->registered[$handle]);
 153      }
 154  
 155  	function enqueue( $handles ) {
 156          foreach ( (array) $handles as $handle ) {
 157              $handle = explode('?', $handle);
 158              if ( !in_array($handle[0], $this->queue) && isset($this->registered[$handle[0]]) ) {
 159                  $this->queue[] = $handle[0];
 160                  if ( isset($handle[1]) )
 161                      $this->args[$handle[0]] = $handle[1];
 162              }
 163          }
 164      }
 165  
 166  	function dequeue( $handles ) {
 167          foreach ( (array) $handles as $handle ) {
 168              $handle = explode('?', $handle);
 169              $key = array_search($handle[0], $this->queue);
 170              if ( false !== $key ) {
 171                  unset($this->queue[$key]);
 172                  unset($this->args[$handle[0]]);
 173              }
 174          }
 175      }
 176  
 177  	function query( $handle, $list = 'registered' ) { // registered, queue, done, to_do
 178          switch ( $list ) :
 179          case 'registered':
 180          case 'scripts': // back compat
 181              if ( isset($this->registered[$handle]) )
 182                  return $this->registered[$handle];
 183              break;
 184          case 'to_print': // back compat
 185          case 'printed': // back compat
 186              if ( 'to_print' == $list )
 187                  $list = 'to_do';
 188              else
 189                  $list = 'printed';
 190          default:
 191              if ( in_array($handle, $this->$list) )
 192                  return true;
 193              break;
 194          endswitch;
 195          return false;
 196      }
 197  
 198  	function set_group( $handle, $recursion, $group ) {
 199          $group = (int) $group;
 200  
 201          if ( $recursion )
 202              $group = min($this->group, $group);
 203          else
 204              $this->group = $group;
 205  
 206          if ( isset($this->groups[$handle]) && $this->groups[$handle] <= $group )
 207              return false;
 208  
 209          $this->groups[$handle] = $group;
 210          return true;
 211      }
 212  
 213  }
 214  
 215  class _WP_Dependency {
 216      var $handle;
 217      var $src;
 218      var $deps = array();
 219      var $ver = false;
 220      var $args = null;
 221  
 222      var $extra = array();
 223  
 224  	function __construct() {
 225          @list($this->handle, $this->src, $this->deps, $this->ver, $this->args) = func_get_args();
 226          if ( !is_array($this->deps) )
 227              $this->deps = array();
 228      }
 229  
 230  	function add_data( $name, $data ) {
 231          if ( !is_scalar($name) )
 232              return false;
 233          $this->extra[$name] = $data;
 234          return true;
 235      }
 236  }


Generated: Wed Jun 1 08:30:02 2011 Cross-referenced by PHPXref 0.7
Provided by Yoast and awesome WordPress Hosting