PageRenderTime 43ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/VTTE/sitios-vtte
PHP | 447 lines | 202 code | 50 blank | 195 comment | 58 complexity | a5bb6bf375a4b594650589cdcf300b99 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. $this->start_el( $output, $element, $depth, ...array_values( $args ) );
  134. // Descend only when the depth is right and there are childrens for this element.
  135. if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
  136. foreach ( $children_elements[ $id ] as $child ) {
  137. if ( ! isset( $newlevel ) ) {
  138. $newlevel = true;
  139. // Start the child delimiter.
  140. $this->start_lvl( $output, $depth, ...array_values( $args ) );
  141. }
  142. $this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
  143. }
  144. unset( $children_elements[ $id ] );
  145. }
  146. if ( isset( $newlevel ) && $newlevel ) {
  147. // End the child delimiter.
  148. $this->end_lvl( $output, $depth, ...array_values( $args ) );
  149. }
  150. // End this element.
  151. $this->end_el( $output, $element, $depth, ...array_values( $args ) );
  152. }
  153. /**
  154. * Display array of elements hierarchically.
  155. *
  156. * Does not assume any existing order of elements.
  157. *
  158. * $max_depth = -1 means flatly display every element.
  159. * $max_depth = 0 means display all levels.
  160. * $max_depth > 0 specifies the number of display levels.
  161. *
  162. * @since 2.1.0
  163. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  164. * to the function signature.
  165. *
  166. * @param array $elements An array of elements.
  167. * @param int $max_depth The maximum hierarchical depth.
  168. * @param mixed ...$args Optional additional arguments.
  169. * @return string The hierarchical item output.
  170. */
  171. public function walk( $elements, $max_depth, ...$args ) {
  172. $output = '';
  173. // Invalid parameter or nothing to walk.
  174. if ( $max_depth < -1 || empty( $elements ) ) {
  175. return $output;
  176. }
  177. $parent_field = $this->db_fields['parent'];
  178. // Flat display.
  179. if ( -1 == $max_depth ) {
  180. $empty_array = array();
  181. foreach ( $elements as $e ) {
  182. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  183. }
  184. return $output;
  185. }
  186. /*
  187. * Need to display in hierarchical order.
  188. * Separate elements into two buckets: top level and children elements.
  189. * Children_elements is two dimensional array, eg.
  190. * Children_elements[10][] contains all sub-elements whose parent is 10.
  191. */
  192. $top_level_elements = array();
  193. $children_elements = array();
  194. foreach ( $elements as $e ) {
  195. if ( empty( $e->$parent_field ) ) {
  196. $top_level_elements[] = $e;
  197. } else {
  198. $children_elements[ $e->$parent_field ][] = $e;
  199. }
  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. }
  218. foreach ( $top_level_elements as $e ) {
  219. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  220. }
  221. /*
  222. * If we are displaying all levels, and remaining children_elements is not empty,
  223. * then we got orphans, which should be displayed regardless.
  224. */
  225. if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
  226. $empty_array = array();
  227. foreach ( $children_elements as $orphans ) {
  228. foreach ( $orphans as $op ) {
  229. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  230. }
  231. }
  232. }
  233. return $output;
  234. }
  235. /**
  236. * paged_walk() - produce a page of nested elements
  237. *
  238. * Given an array of hierarchical elements, the maximum depth, a specific page number,
  239. * and number of elements per page, this function first determines all top level root elements
  240. * belonging to that page, then lists them and all of their children in hierarchical order.
  241. *
  242. * $max_depth = 0 means display all levels.
  243. * $max_depth > 0 specifies the number of display levels.
  244. *
  245. * @since 2.7.0
  246. * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
  247. * to the function signature.
  248. *
  249. * @param array $elements
  250. * @param int $max_depth The maximum hierarchical depth.
  251. * @param int $page_num The specific page number, beginning with 1.
  252. * @param int $per_page
  253. * @param mixed ...$args Optional additional arguments.
  254. * @return string XHTML of the specified page of elements
  255. */
  256. public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
  257. if ( empty( $elements ) || $max_depth < -1 ) {
  258. return '';
  259. }
  260. $output = '';
  261. $parent_field = $this->db_fields['parent'];
  262. $count = -1;
  263. if ( -1 == $max_depth ) {
  264. $total_top = count( $elements );
  265. }
  266. if ( $page_num < 1 || $per_page < 0 ) {
  267. // No paging.
  268. $paging = false;
  269. $start = 0;
  270. if ( -1 == $max_depth ) {
  271. $end = $total_top;
  272. }
  273. $this->max_pages = 1;
  274. } else {
  275. $paging = true;
  276. $start = ( (int) $page_num - 1 ) * (int) $per_page;
  277. $end = $start + $per_page;
  278. if ( -1 == $max_depth ) {
  279. $this->max_pages = ceil( $total_top / $per_page );
  280. }
  281. }
  282. // Flat display.
  283. if ( -1 == $max_depth ) {
  284. if ( ! empty( $args[0]['reverse_top_level'] ) ) {
  285. $elements = array_reverse( $elements );
  286. $oldstart = $start;
  287. $start = $total_top - $end;
  288. $end = $total_top - $oldstart;
  289. }
  290. $empty_array = array();
  291. foreach ( $elements as $e ) {
  292. $count++;
  293. if ( $count < $start ) {
  294. continue;
  295. }
  296. if ( $count >= $end ) {
  297. break;
  298. }
  299. $this->display_element( $e, $empty_array, 1, 0, $args, $output );
  300. }
  301. return $output;
  302. }
  303. /*
  304. * Separate elements into two buckets: top level and children elements.
  305. * Children_elements is two dimensional array, e.g.
  306. * $children_elements[10][] contains all sub-elements whose parent is 10.
  307. */
  308. $top_level_elements = array();
  309. $children_elements = array();
  310. foreach ( $elements as $e ) {
  311. if ( 0 == $e->$parent_field ) {
  312. $top_level_elements[] = $e;
  313. } else {
  314. $children_elements[ $e->$parent_field ][] = $e;
  315. }
  316. }
  317. $total_top = count( $top_level_elements );
  318. if ( $paging ) {
  319. $this->max_pages = ceil( $total_top / $per_page );
  320. } else {
  321. $end = $total_top;
  322. }
  323. if ( ! empty( $args[0]['reverse_top_level'] ) ) {
  324. $top_level_elements = array_reverse( $top_level_elements );
  325. $oldstart = $start;
  326. $start = $total_top - $end;
  327. $end = $total_top - $oldstart;
  328. }
  329. if ( ! empty( $args[0]['reverse_children'] ) ) {
  330. foreach ( $children_elements as $parent => $children ) {
  331. $children_elements[ $parent ] = array_reverse( $children );
  332. }
  333. }
  334. foreach ( $top_level_elements as $e ) {
  335. $count++;
  336. // For the last page, need to unset earlier children in order to keep track of orphans.
  337. if ( $end >= $total_top && $count < $start ) {
  338. $this->unset_children( $e, $children_elements );
  339. }
  340. if ( $count < $start ) {
  341. continue;
  342. }
  343. if ( $count >= $end ) {
  344. break;
  345. }
  346. $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
  347. }
  348. if ( $end >= $total_top && count( $children_elements ) > 0 ) {
  349. $empty_array = array();
  350. foreach ( $children_elements as $orphans ) {
  351. foreach ( $orphans as $op ) {
  352. $this->display_element( $op, $empty_array, 1, 0, $args, $output );
  353. }
  354. }
  355. }
  356. return $output;
  357. }
  358. /**
  359. * Calculates the total number of root elements.
  360. *
  361. * @since 2.7.0
  362. *
  363. * @param array $elements Elements to list.
  364. * @return int Number of root elements.
  365. */
  366. public function get_number_of_root_elements( $elements ) {
  367. $num = 0;
  368. $parent_field = $this->db_fields['parent'];
  369. foreach ( $elements as $e ) {
  370. if ( 0 == $e->$parent_field ) {
  371. $num++;
  372. }
  373. }
  374. return $num;
  375. }
  376. /**
  377. * Unset all the children for a given top level element.
  378. *
  379. * @since 2.7.0
  380. *
  381. * @param object $e
  382. * @param array $children_elements
  383. */
  384. public function unset_children( $e, &$children_elements ) {
  385. if ( ! $e || ! $children_elements ) {
  386. return;
  387. }
  388. $id_field = $this->db_fields['id'];
  389. $id = $e->$id_field;
  390. if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
  391. foreach ( (array) $children_elements[ $id ] as $child ) {
  392. $this->unset_children( $child, $children_elements );
  393. }
  394. }
  395. unset( $children_elements[ $id ] );
  396. }
  397. }