WP_Dependencies

Core base class extended to register items.

Defined (1)

The class is defined in the following location(s).

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