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

https://gitlab.com/campus-academy/krowkaramel · PHP · 449 lines · 202 code · 50 blank · 197 comment · 56 complexity · e2b6427c944dada58326ad540d296991 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. * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
  81. * @abstract
  82. *
  83. * @param string $output Used to append additional content (passed by reference).
  84. * @param object $data_object The data object.
  85. * @param int $depth Depth of the item.
  86. * @param array $args An array of additional arguments.
  87. * @param int $current_object_id Optional. ID of the current item. Default 0.
  88. */
  89. public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
  90. /**
  91. * Ends the element output, if needed.
  92. *
  93. * The $args parameter holds additional values that may be used with the child class methods.
  94. *
  95. * @since 2.1.0
  96. * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
  97. * @abstract
  98. *
  99. * @param string $output Used to append additional content (passed by reference).
  100. * @param object $data_object The data object.
  101. * @param int $depth Depth of the item.
  102. * @param array $args An array of additional arguments.
  103. */
  104. public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
  105. /**
  106. * Traverse elements to create list from elements.
  107. *
  108. * Display one element if the element doesn't have any children otherwise,
  109. * display the element and its children. Will only traverse up to the max
  110. * depth and no ignore elements under that depth. It is possible to set the
  111. * max depth to include all depths, see walk() method.
  112. *
  113. * This method should not be called directly, use the walk() method instead.
  114. *
  115. * @since 2.5.0
  116. *
  117. * @param object $element Data object.
  118. * @param array $children_elements List of elements to continue traversing (passed by reference).
  119. * @param int $max_depth Max depth to traverse.
  120. * @param int $depth Depth of current element.
  121. * @param array $args An array of arguments.
  122. * @param string $output Used to append additional content (passed by reference).
  123. */
  124. public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
  125. if ( ! $element ) {
  126. return;
  127. }
  128. $id_field = $this->db_fields['id'];
  129. $id = $element->$id_field;
  130. // Display this element.
  131. $this->has_children = ! empty( $children_elements[ $id ] );
  132. if ( isset( $args[0] ) && is_array( $args[0] ) ) {
  133. $args[0]['has_children'] = $this->has_children; // Back-compat.
  134. }
  135. $this->start_el( $output, $element, $depth, ...array_values( $args ) );
  136. // Descend only when the depth is right and there are children for this element.
  137. if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
  138. foreach ( $children_elements[ $id ] as $child ) {
  139. if ( ! isset( $newlevel ) ) {
  140. $newlevel = true;
  141. // Start the child delimiter.
  142. $this->start_lvl( $output, $depth, ...array_values( $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. $this->end_lvl( $output, $depth, ...array_values( $args ) );
  151. }
  152. // End this element.
  153. $this->end_el( $output, $element, $depth, ...array_values( $args ) );
  154. }
  155. /**
  156. * Display array of elements hierarchically.
  157. *
  158. * Does not assume any existing order of elements.
  159. *
  160. * $max_depth = -1 means flatly display every element.
  161. * $max_depth = 0 means display all levels.
  162. * $max_depth > 0 specifies the number of display levels.
  163. *
  164. * @since 2.1.0
  165. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  166. * to the function signature.
  167. *
  168. * @param array $elements An array of elements.
  169. * @param int $max_depth The maximum hierarchical depth.
  170. * @param mixed ...$args Optional additional arguments.
  171. * @return string The hierarchical item output.
  172. */
  173. public function walk( $elements, $max_depth, ...$args ) {
  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. }
  186. return $output;
  187. }
  188. /*
  189. * Need to display in hierarchical order.
  190. * Separate elements into two buckets: top level and children elements.
  191. * Children_elements is two dimensional array. Example:
  192. * Children_elements[10][] contains all sub-elements whose parent is 10.
  193. */
  194. $top_level_elements = array();
  195. $children_elements = array();
  196. foreach ( $elements as $e ) {
  197. if ( empty( $e->$parent_field ) ) {
  198. $top_level_elements[] = $e;
  199. } else {
  200. $children_elements[ $e->$parent_field ][] = $e;
  201. }
  202. }
  203. /*
  204. * When none of the elements is top level.
  205. * Assume the first one must be root of the sub elements.
  206. */
  207. if ( empty( $top_level_elements ) ) {
  208. $first = array_slice( $elements, 0, 1 );
  209. $root = $first[0];
  210. $top_level_elements = array();
  211. $children_elements = array();
  212. foreach ( $elements as $e ) {
  213. if ( $root->$parent_field == $e->$parent_field ) {
  214. $top_level_elements[] = $e;
  215. } else {
  216. $children_elements[ $e->$parent_field ][] = $e;
  217. }
  218. }
  219. }
  220. foreach ( $top_level_elements as $e ) {
  221. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  222. }
  223. /*
  224. * If we are displaying all levels, and remaining children_elements is not empty,
  225. * then we got orphans, which should be displayed regardless.
  226. */
  227. if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
  228. $empty_array = array();
  229. foreach ( $children_elements as $orphans ) {
  230. foreach ( $orphans as $op ) {
  231. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  232. }
  233. }
  234. }
  235. return $output;
  236. }
  237. /**
  238. * paged_walk() - produce a page of nested elements
  239. *
  240. * Given an array of hierarchical elements, the maximum depth, a specific page number,
  241. * and number of elements per page, this function first determines all top level root elements
  242. * belonging to that page, then lists them and all of their children in hierarchical order.
  243. *
  244. * $max_depth = 0 means display all levels.
  245. * $max_depth > 0 specifies the number of display levels.
  246. *
  247. * @since 2.7.0
  248. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  249. * to the function signature.
  250. *
  251. * @param array $elements
  252. * @param int $max_depth The maximum hierarchical depth.
  253. * @param int $page_num The specific page number, beginning with 1.
  254. * @param int $per_page
  255. * @param mixed ...$args Optional additional arguments.
  256. * @return string XHTML of the specified page of elements
  257. */
  258. public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
  259. if ( empty( $elements ) || $max_depth < -1 ) {
  260. return '';
  261. }
  262. $output = '';
  263. $parent_field = $this->db_fields['parent'];
  264. $count = -1;
  265. if ( -1 == $max_depth ) {
  266. $total_top = count( $elements );
  267. }
  268. if ( $page_num < 1 || $per_page < 0 ) {
  269. // No paging.
  270. $paging = false;
  271. $start = 0;
  272. if ( -1 == $max_depth ) {
  273. $end = $total_top;
  274. }
  275. $this->max_pages = 1;
  276. } else {
  277. $paging = true;
  278. $start = ( (int) $page_num - 1 ) * (int) $per_page;
  279. $end = $start + $per_page;
  280. if ( -1 == $max_depth ) {
  281. $this->max_pages = ceil( $total_top / $per_page );
  282. }
  283. }
  284. // Flat display.
  285. if ( -1 == $max_depth ) {
  286. if ( ! empty( $args[0]['reverse_top_level'] ) ) {
  287. $elements = array_reverse( $elements );
  288. $oldstart = $start;
  289. $start = $total_top - $end;
  290. $end = $total_top - $oldstart;
  291. }
  292. $empty_array = array();
  293. foreach ( $elements as $e ) {
  294. $count++;
  295. if ( $count < $start ) {
  296. continue;
  297. }
  298. if ( $count >= $end ) {
  299. break;
  300. }
  301. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  302. }
  303. return $output;
  304. }
  305. /*
  306. * Separate elements into two buckets: top level and children elements.
  307. * Children_elements is two dimensional array, e.g.
  308. * $children_elements[10][] contains all sub-elements whose parent is 10.
  309. */
  310. $top_level_elements = array();
  311. $children_elements = array();
  312. foreach ( $elements as $e ) {
  313. if ( empty( $e->$parent_field ) ) {
  314. $top_level_elements[] = $e;
  315. } else {
  316. $children_elements[ $e->$parent_field ][] = $e;
  317. }
  318. }
  319. $total_top = count( $top_level_elements );
  320. if ( $paging ) {
  321. $this->max_pages = ceil( $total_top / $per_page );
  322. } else {
  323. $end = $total_top;
  324. }
  325. if ( ! empty( $args[0]['reverse_top_level'] ) ) {
  326. $top_level_elements = array_reverse( $top_level_elements );
  327. $oldstart = $start;
  328. $start = $total_top - $end;
  329. $end = $total_top - $oldstart;
  330. }
  331. if ( ! empty( $args[0]['reverse_children'] ) ) {
  332. foreach ( $children_elements as $parent => $children ) {
  333. $children_elements[ $parent ] = array_reverse( $children );
  334. }
  335. }
  336. foreach ( $top_level_elements as $e ) {
  337. $count++;
  338. // For the last page, need to unset earlier children in order to keep track of orphans.
  339. if ( $end >= $total_top && $count < $start ) {
  340. $this->unset_children( $e, $children_elements );
  341. }
  342. if ( $count < $start ) {
  343. continue;
  344. }
  345. if ( $count >= $end ) {
  346. break;
  347. }
  348. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  349. }
  350. if ( $end >= $total_top && count( $children_elements ) > 0 ) {
  351. $empty_array = array();
  352. foreach ( $children_elements as $orphans ) {
  353. foreach ( $orphans as $op ) {
  354. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  355. }
  356. }
  357. }
  358. return $output;
  359. }
  360. /**
  361. * Calculates the total number of root elements.
  362. *
  363. * @since 2.7.0
  364. *
  365. * @param array $elements Elements to list.
  366. * @return int Number of root elements.
  367. */
  368. public function get_number_of_root_elements( $elements ) {
  369. $num = 0;
  370. $parent_field = $this->db_fields['parent'];
  371. foreach ( $elements as $e ) {
  372. if ( empty( $e->$parent_field ) ) {
  373. $num++;
  374. }
  375. }
  376. return $num;
  377. }
  378. /**
  379. * Unset all the children for a given top level element.
  380. *
  381. * @since 2.7.0
  382. *
  383. * @param object $e
  384. * @param array $children_elements
  385. */
  386. public function unset_children( $e, &$children_elements ) {
  387. if ( ! $e || ! $children_elements ) {
  388. return;
  389. }
  390. $id_field = $this->db_fields['id'];
  391. $id = $e->$id_field;
  392. if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
  393. foreach ( (array) $children_elements[ $id ] as $child ) {
  394. $this->unset_children( $child, $children_elements );
  395. }
  396. }
  397. unset( $children_elements[ $id ] );
  398. }
  399. }