PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/class-wp-walker.php

https://gitlab.com/WPonEB/WPonEB
PHP | 425 lines | 186 code | 50 blank | 189 comment | 54 complexity | d1250b485bfa907c4ed10b8e9aaf00f6 MD5 | raw file
  1. <?php
  2. /**
  3. * A class for displaying various tree-like structures.
  4. *
  5. * Extend the Walker class to use it, see examples below. Child classes
  6. * do not need to implement all of the abstract methods in the class. The child
  7. * only needs to implement the methods that are needed.
  8. *
  9. * @since 2.1.0
  10. *
  11. * @package WordPress
  12. * @abstract
  13. */
  14. class Walker {
  15. /**
  16. * What the class handles.
  17. *
  18. * @since 2.1.0
  19. * @var string
  20. */
  21. public $tree_type;
  22. /**
  23. * DB fields to use.
  24. *
  25. * @since 2.1.0
  26. * @var array
  27. */
  28. public $db_fields;
  29. /**
  30. * Max number of pages walked by the paged walker
  31. *
  32. * @since 2.7.0
  33. * @var int
  34. */
  35. public $max_pages = 1;
  36. /**
  37. * Whether the current element has children or not.
  38. *
  39. * To be used in start_el().
  40. *
  41. * @since 4.0.0
  42. * @var bool
  43. */
  44. public $has_children;
  45. /**
  46. * Starts the list before the elements are added.
  47. *
  48. * The $args parameter holds additional values that may be used with the child
  49. * class methods. This method is called at the start of the output list.
  50. *
  51. * @since 2.1.0
  52. * @abstract
  53. *
  54. * @param string $output Used to append additional content (passed by reference).
  55. * @param int $depth Depth of the item.
  56. * @param array $args An array of additional arguments.
  57. */
  58. public function start_lvl( &$output, $depth = 0, $args = array() ) {}
  59. /**
  60. * Ends the list of after the elements are added.
  61. *
  62. * The $args parameter holds additional values that may be used with the child
  63. * class methods. This method finishes the list at the end of output of the elements.
  64. *
  65. * @since 2.1.0
  66. * @abstract
  67. *
  68. * @param string $output Used to append additional content (passed by reference).
  69. * @param int $depth Depth of the item.
  70. * @param array $args An array of additional arguments.
  71. */
  72. public function end_lvl( &$output, $depth = 0, $args = array() ) {}
  73. /**
  74. * Start the element output.
  75. *
  76. * The $args parameter holds additional values that may be used with the child
  77. * class methods. Includes the element output also.
  78. *
  79. * @since 2.1.0
  80. * @abstract
  81. *
  82. * @param string $output Used to append additional content (passed by reference).
  83. * @param object $object The data object.
  84. * @param int $depth Depth of the item.
  85. * @param array $args An array of additional arguments.
  86. * @param int $current_object_id ID of the current item.
  87. */
  88. public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
  89. /**
  90. * Ends the element output, if needed.
  91. *
  92. * The $args parameter holds additional values that may be used with the child class methods.
  93. *
  94. * @since 2.1.0
  95. * @abstract
  96. *
  97. * @param string $output Used to append additional content (passed by reference).
  98. * @param object $object The data object.
  99. * @param int $depth Depth of the item.
  100. * @param array $args An array of additional arguments.
  101. */
  102. public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
  103. /**
  104. * Traverse elements to create list from elements.
  105. *
  106. * Display one element if the element doesn't have any children otherwise,
  107. * display the element and its children. Will only traverse up to the max
  108. * depth and no ignore elements under that depth. It is possible to set the
  109. * max depth to include all depths, see walk() method.
  110. *
  111. * This method should not be called directly, use the walk() method instead.
  112. *
  113. * @since 2.5.0
  114. *
  115. * @param object $element Data object.
  116. * @param array $children_elements List of elements to continue traversing (passed by reference).
  117. * @param int $max_depth Max depth to traverse.
  118. * @param int $depth Depth of current element.
  119. * @param array $args An array of arguments.
  120. * @param string $output Used to append additional content (passed by reference).
  121. */
  122. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  123. if ( ! $element ) {
  124. return;
  125. }
  126. $id_field = $this->db_fields['id'];
  127. $id = $element->$id_field;
  128. //display this element
  129. $this->has_children = ! empty( $children_elements[ $id ] );
  130. if ( isset( $args[0] ) && is_array( $args[0] ) ) {
  131. $args[0]['has_children'] = $this->has_children; // Back-compat.
  132. }
  133. $cb_args = array_merge( array(&$output, $element, $depth), $args);
  134. call_user_func_array(array($this, 'start_el'), $cb_args);
  135. // descend only when the depth is right and there are childrens for this element
  136. if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
  137. foreach ( $children_elements[ $id ] as $child ){
  138. if ( !isset($newlevel) ) {
  139. $newlevel = true;
  140. //start the child delimiter
  141. $cb_args = array_merge( array(&$output, $depth), $args);
  142. call_user_func_array(array($this, 'start_lvl'), $cb_args);
  143. }
  144. $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
  145. }
  146. unset( $children_elements[ $id ] );
  147. }
  148. if ( isset($newlevel) && $newlevel ){
  149. //end the child delimiter
  150. $cb_args = array_merge( array(&$output, $depth), $args);
  151. call_user_func_array(array($this, 'end_lvl'), $cb_args);
  152. }
  153. //end this element
  154. $cb_args = array_merge( array(&$output, $element, $depth), $args);
  155. call_user_func_array(array($this, 'end_el'), $cb_args);
  156. }
  157. /**
  158. * Display array of elements hierarchically.
  159. *
  160. * Does not assume any existing order of elements.
  161. *
  162. * $max_depth = -1 means flatly display every element.
  163. * $max_depth = 0 means display all levels.
  164. * $max_depth > 0 specifies the number of display levels.
  165. *
  166. * @since 2.1.0
  167. *
  168. * @param array $elements An array of elements.
  169. * @param int $max_depth The maximum hierarchical depth.
  170. * @return string The hierarchical item output.
  171. */
  172. public function walk( $elements, $max_depth ) {
  173. $args = array_slice(func_get_args(), 2);
  174. $output = '';
  175. //invalid parameter or nothing to walk
  176. if ( $max_depth < -1 || empty( $elements ) ) {
  177. return $output;
  178. }
  179. $parent_field = $this->db_fields['parent'];
  180. // flat display
  181. if ( -1 == $max_depth ) {
  182. $empty_array = array();
  183. foreach ( $elements as $e )
  184. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  185. return $output;
  186. }
  187. /*
  188. * Need to display in hierarchical order.
  189. * Separate elements into two buckets: top level and children elements.
  190. * Children_elements is two dimensional array, eg.
  191. * Children_elements[10][] contains all sub-elements whose parent is 10.
  192. */
  193. $top_level_elements = array();
  194. $children_elements = array();
  195. foreach ( $elements as $e) {
  196. if ( empty( $e->$parent_field ) )
  197. $top_level_elements[] = $e;
  198. else
  199. $children_elements[ $e->$parent_field ][] = $e;
  200. }
  201. /*
  202. * When none of the elements is top level.
  203. * Assume the first one must be root of the sub elements.
  204. */
  205. if ( empty($top_level_elements) ) {
  206. $first = array_slice( $elements, 0, 1 );
  207. $root = $first[0];
  208. $top_level_elements = array();
  209. $children_elements = array();
  210. foreach ( $elements as $e) {
  211. if ( $root->$parent_field == $e->$parent_field )
  212. $top_level_elements[] = $e;
  213. else
  214. $children_elements[ $e->$parent_field ][] = $e;
  215. }
  216. }
  217. foreach ( $top_level_elements as $e )
  218. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  219. /*
  220. * If we are displaying all levels, and remaining children_elements is not empty,
  221. * then we got orphans, which should be displayed regardless.
  222. */
  223. if ( ( $max_depth == 0 ) && count( $children_elements ) > 0 ) {
  224. $empty_array = array();
  225. foreach ( $children_elements as $orphans )
  226. foreach ( $orphans as $op )
  227. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  228. }
  229. return $output;
  230. }
  231. /**
  232. * paged_walk() - produce a page of nested elements
  233. *
  234. * Given an array of hierarchical elements, the maximum depth, a specific page number,
  235. * and number of elements per page, this function first determines all top level root elements
  236. * belonging to that page, then lists them and all of their children in hierarchical order.
  237. *
  238. * $max_depth = 0 means display all levels.
  239. * $max_depth > 0 specifies the number of display levels.
  240. *
  241. * @since 2.7.0
  242. *
  243. * @param array $elements
  244. * @param int $max_depth The maximum hierarchical depth.
  245. * @param int $page_num The specific page number, beginning with 1.
  246. * @param int $per_page
  247. * @return string XHTML of the specified page of elements
  248. */
  249. public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
  250. if ( empty( $elements ) || $max_depth < -1 ) {
  251. return '';
  252. }
  253. $args = array_slice( func_get_args(), 4 );
  254. $output = '';
  255. $parent_field = $this->db_fields['parent'];
  256. $count = -1;
  257. if ( -1 == $max_depth )
  258. $total_top = count( $elements );
  259. if ( $page_num < 1 || $per_page < 0 ) {
  260. // No paging
  261. $paging = false;
  262. $start = 0;
  263. if ( -1 == $max_depth )
  264. $end = $total_top;
  265. $this->max_pages = 1;
  266. } else {
  267. $paging = true;
  268. $start = ( (int)$page_num - 1 ) * (int)$per_page;
  269. $end = $start + $per_page;
  270. if ( -1 == $max_depth )
  271. $this->max_pages = ceil($total_top / $per_page);
  272. }
  273. // flat display
  274. if ( -1 == $max_depth ) {
  275. if ( !empty($args[0]['reverse_top_level']) ) {
  276. $elements = array_reverse( $elements );
  277. $oldstart = $start;
  278. $start = $total_top - $end;
  279. $end = $total_top - $oldstart;
  280. }
  281. $empty_array = array();
  282. foreach ( $elements as $e ) {
  283. $count++;
  284. if ( $count < $start )
  285. continue;
  286. if ( $count >= $end )
  287. break;
  288. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  289. }
  290. return $output;
  291. }
  292. /*
  293. * Separate elements into two buckets: top level and children elements.
  294. * Children_elements is two dimensional array, e.g.
  295. * $children_elements[10][] contains all sub-elements whose parent is 10.
  296. */
  297. $top_level_elements = array();
  298. $children_elements = array();
  299. foreach ( $elements as $e) {
  300. if ( 0 == $e->$parent_field )
  301. $top_level_elements[] = $e;
  302. else
  303. $children_elements[ $e->$parent_field ][] = $e;
  304. }
  305. $total_top = count( $top_level_elements );
  306. if ( $paging )
  307. $this->max_pages = ceil($total_top / $per_page);
  308. else
  309. $end = $total_top;
  310. if ( !empty($args[0]['reverse_top_level']) ) {
  311. $top_level_elements = array_reverse( $top_level_elements );
  312. $oldstart = $start;
  313. $start = $total_top - $end;
  314. $end = $total_top - $oldstart;
  315. }
  316. if ( !empty($args[0]['reverse_children']) ) {
  317. foreach ( $children_elements as $parent => $children )
  318. $children_elements[$parent] = array_reverse( $children );
  319. }
  320. foreach ( $top_level_elements as $e ) {
  321. $count++;
  322. // For the last page, need to unset earlier children in order to keep track of orphans.
  323. if ( $end >= $total_top && $count < $start )
  324. $this->unset_children( $e, $children_elements );
  325. if ( $count < $start )
  326. continue;
  327. if ( $count >= $end )
  328. break;
  329. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  330. }
  331. if ( $end >= $total_top && count( $children_elements ) > 0 ) {
  332. $empty_array = array();
  333. foreach ( $children_elements as $orphans )
  334. foreach ( $orphans as $op )
  335. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  336. }
  337. return $output;
  338. }
  339. /**
  340. * Calculates the total number of root elements.
  341. *
  342. * @since 2.7.0
  343. *
  344. * @param array $elements Elements to list.
  345. * @return int Number of root elements.
  346. */
  347. public function get_number_of_root_elements( $elements ){
  348. $num = 0;
  349. $parent_field = $this->db_fields['parent'];
  350. foreach ( $elements as $e) {
  351. if ( 0 == $e->$parent_field )
  352. $num++;
  353. }
  354. return $num;
  355. }
  356. /**
  357. * Unset all the children for a given top level element.
  358. *
  359. * @since 2.7.0
  360. *
  361. * @param object $e
  362. * @param array $children_elements
  363. */
  364. public function unset_children( $e, &$children_elements ){
  365. if ( ! $e || ! $children_elements ) {
  366. return;
  367. }
  368. $id_field = $this->db_fields['id'];
  369. $id = $e->$id_field;
  370. if ( !empty($children_elements[$id]) && is_array($children_elements[$id]) )
  371. foreach ( (array) $children_elements[$id] as $child )
  372. $this->unset_children( $child, $children_elements );
  373. unset( $children_elements[ $id ] );
  374. }
  375. } // Walker