wp/wp-includes/class-wp-walker.php
author ymh <ymh.work@gmail.com>
Thu, 29 Sep 2022 08:06:27 +0200
changeset 20 7b1b88e27a20
parent 19 3d72ae0968f4
child 21 48c4eec2b7e6
permissions -rw-r--r--
Modified Readme
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
<?php
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
 * A class for displaying various tree-like structures.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
 * Extend the Walker class to use it, see examples below. Child classes
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
 * do not need to implement all of the abstract methods in the class. The child
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
 * only needs to implement the methods that are needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
 * @package WordPress
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
class Walker {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
	 * What the class handles.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    18
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    19
	 * @var string
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    20
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    21
	public $tree_type;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    22
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    23
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    24
	 * DB fields to use.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    25
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    26
	 * @since 2.1.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    27
	 * @var string[]
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    28
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    29
	public $db_fields;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    30
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    31
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    32
	 * Max number of pages walked by the paged walker.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    33
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    34
	 * @since 2.7.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    35
	 * @var int
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    36
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    37
	public $max_pages = 1;
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    38
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    39
	/**
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    40
	 * Whether the current element has children or not.
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    41
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    42
	 * To be used in start_el().
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    43
	 *
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    44
	 * @since 4.0.0
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    45
	 * @var bool
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    46
	 */
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    47
	public $has_children;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    48
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    49
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    50
	 * Starts the list before the elements are added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    51
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    52
	 * The $args parameter holds additional values that may be used with the child
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    53
	 * class methods. This method is called at the start of the output list.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    54
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    55
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    56
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    57
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    58
	 * @param string $output Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    59
	 * @param int    $depth  Depth of the item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    60
	 * @param array  $args   An array of additional arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    61
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    62
	public function start_lvl( &$output, $depth = 0, $args = array() ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    63
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    64
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    65
	 * Ends the list of after the elements are added.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    66
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    67
	 * The $args parameter holds additional values that may be used with the child
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    68
	 * class methods. This method finishes the list at the end of output of the elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    69
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    70
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    71
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    72
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    73
	 * @param string $output Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    74
	 * @param int    $depth  Depth of the item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    75
	 * @param array  $args   An array of additional arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    76
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    77
	public function end_lvl( &$output, $depth = 0, $args = array() ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    78
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    79
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    80
	 * Starts the element output.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    81
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    82
	 * The $args parameter holds additional values that may be used with the child
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    83
	 * class methods. Also includes the element output.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    84
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    85
	 * @since 2.1.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    86
	 * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    88
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    89
	 * @param string $output            Used to append additional content (passed by reference).
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    90
	 * @param object $data_object       The data object.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	 * @param int    $depth             Depth of the item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	 * @param array  $args              An array of additional arguments.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    93
	 * @param int    $current_object_id Optional. ID of the current item. Default 0.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    94
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
    95
	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	 * Ends the element output, if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   100
	 * The $args parameter holds additional values that may be used with the child class methods.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 * @since 2.1.0
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   103
	 * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   104
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   106
	 * @param string $output      Used to append additional content (passed by reference).
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   107
	 * @param object $data_object The data object.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   108
	 * @param int    $depth       Depth of the item.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   109
	 * @param array  $args        An array of additional arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   111
	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   114
	 * Traverses elements to create list from elements.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	 * Display one element if the element doesn't have any children otherwise,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   117
	 * display the element and its children. Will only traverse up to the max
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	 * depth and no ignore elements under that depth. It is possible to set the
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	 * max depth to include all depths, see walk() method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	 * This method should not be called directly, use the walk() method instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   124
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   125
	 * @param object $element           Data object.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   126
	 * @param array  $children_elements List of elements to continue traversing (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	 * @param int    $max_depth         Max depth to traverse.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   128
	 * @param int    $depth             Depth of current element.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 * @param array  $args              An array of arguments.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   130
	 * @param string $output            Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   131
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   132
	public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
		if ( ! $element ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
			return;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   135
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   136
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
		$id_field = $this->db_fields['id'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   138
		$id       = $element->$id_field;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   139
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   140
		// Display this element.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   141
		$this->has_children = ! empty( $children_elements[ $id ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
		if ( isset( $args[0] ) && is_array( $args[0] ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   143
			$args[0]['has_children'] = $this->has_children; // Back-compat.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   144
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   145
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   146
		$this->start_el( $output, $element, $depth, ...array_values( $args ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   148
		// Descend only when the depth is right and there are children for this element.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   149
		if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   150
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   151
			foreach ( $children_elements[ $id ] as $child ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   152
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   153
				if ( ! isset( $newlevel ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
					$newlevel = true;
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   155
					// Start the child delimiter.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   156
					$this->start_lvl( $output, $depth, ...array_values( $args ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   157
				}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   158
				$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   159
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   160
			unset( $children_elements[ $id ] );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   161
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   162
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   163
		if ( isset( $newlevel ) && $newlevel ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   164
			// End the child delimiter.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   165
			$this->end_lvl( $output, $depth, ...array_values( $args ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   166
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   168
		// End this element.
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   169
		$this->end_el( $output, $element, $depth, ...array_values( $args ) );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   170
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   171
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   173
	 * Displays array of elements hierarchically.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	 * Does not assume any existing order of elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	 * $max_depth = -1 means flatly display every element.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	 * $max_depth = 0 means display all levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	 * $max_depth > 0 specifies the number of display levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
	 * @since 2.1.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   182
	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   183
	 *              to the function signature.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   184
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   185
	 * @param array $elements  An array of elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   186
	 * @param int   $max_depth The maximum hierarchical depth.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   187
	 * @param mixed ...$args   Optional additional arguments.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
	 * @return string The hierarchical item output.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   189
	 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   190
	public function walk( $elements, $max_depth, ...$args ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   191
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   192
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   193
		// Invalid parameter or nothing to walk.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   194
		if ( $max_depth < -1 || empty( $elements ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   195
			return $output;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   196
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   197
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   198
		$parent_field = $this->db_fields['parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   199
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   200
		// Flat display.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   201
		if ( -1 == $max_depth ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   202
			$empty_array = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   203
			foreach ( $elements as $e ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   204
				$this->display_element( $e, $empty_array, 1, 0, $args, $output );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   205
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   206
			return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   207
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   208
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   209
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   210
		 * Need to display in hierarchical order.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   211
		 * Separate elements into two buckets: top level and children elements.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   212
		 * Children_elements is two dimensional array. Example:
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   213
		 * Children_elements[10][] contains all sub-elements whose parent is 10.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   214
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   215
		$top_level_elements = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   216
		$children_elements  = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   217
		foreach ( $elements as $e ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   218
			if ( empty( $e->$parent_field ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   219
				$top_level_elements[] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   220
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   221
				$children_elements[ $e->$parent_field ][] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   222
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   223
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   224
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   225
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   226
		 * When none of the elements is top level.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   227
		 * Assume the first one must be root of the sub elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   228
		 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   229
		if ( empty( $top_level_elements ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   230
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   231
			$first = array_slice( $elements, 0, 1 );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   232
			$root  = $first[0];
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   233
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   234
			$top_level_elements = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   235
			$children_elements  = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   236
			foreach ( $elements as $e ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   237
				if ( $root->$parent_field == $e->$parent_field ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   238
					$top_level_elements[] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   239
				} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   240
					$children_elements[ $e->$parent_field ][] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   241
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   242
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   243
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   244
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   245
		foreach ( $top_level_elements as $e ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   246
			$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   247
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   248
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   249
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   250
		 * If we are displaying all levels, and remaining children_elements is not empty,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   251
		 * then we got orphans, which should be displayed regardless.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   252
		 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   253
		if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   254
			$empty_array = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   255
			foreach ( $children_elements as $orphans ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   256
				foreach ( $orphans as $op ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   257
					$this->display_element( $op, $empty_array, 1, 0, $args, $output );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   258
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   259
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   260
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   261
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   262
		return $output;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   263
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   264
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   265
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   266
	 * Produces a page of nested elements.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   267
	 *
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   268
	 * Given an array of hierarchical elements, the maximum depth, a specific page number,
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   269
	 * and number of elements per page, this function first determines all top level root elements
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   270
	 * belonging to that page, then lists them and all of their children in hierarchical order.
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   271
	 *
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   272
	 * $max_depth = 0 means display all levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   273
	 * $max_depth > 0 specifies the number of display levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   274
	 *
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   275
	 * @since 2.7.0
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   276
	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   277
	 *              to the function signature.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   278
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   279
	 * @param array $elements  An array of elements.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   280
	 * @param int   $max_depth The maximum hierarchical depth.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   281
	 * @param int   $page_num  The specific page number, beginning with 1.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   282
	 * @param int   $per_page  Number of elements per page.
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   283
	 * @param mixed ...$args   Optional additional arguments.
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   284
	 * @return string XHTML of the specified page of elements.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   285
	 */
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   286
	public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   287
		if ( empty( $elements ) || $max_depth < -1 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   288
			return '';
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   289
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		$parent_field = $this->db_fields['parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   294
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
		$count = -1;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   296
		if ( -1 == $max_depth ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   297
			$total_top = count( $elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   298
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   299
		if ( $page_num < 1 || $per_page < 0 ) {
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   300
			// No paging.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   301
			$paging = false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   302
			$start  = 0;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   303
			if ( -1 == $max_depth ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
				$end = $total_top;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   305
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
			$this->max_pages = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   307
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   308
			$paging = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   309
			$start  = ( (int) $page_num - 1 ) * (int) $per_page;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   310
			$end    = $start + $per_page;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   311
			if ( -1 == $max_depth ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   312
				$this->max_pages = ceil( $total_top / $per_page );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   313
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   316
		// Flat display.
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
		if ( -1 == $max_depth ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   318
			if ( ! empty( $args[0]['reverse_top_level'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   319
				$elements = array_reverse( $elements );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   320
				$oldstart = $start;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   321
				$start    = $total_top - $end;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   322
				$end      = $total_top - $oldstart;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
			$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   326
			foreach ( $elements as $e ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
				$count++;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   328
				if ( $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   329
					continue;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   330
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   331
				if ( $count >= $end ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
					break;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   333
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
				$this->display_element( $e, $empty_array, 1, 0, $args, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   335
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   336
			return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		 * Separate elements into two buckets: top level and children elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		 * Children_elements is two dimensional array, e.g.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		 * $children_elements[10][] contains all sub-elements whose parent is 10.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   344
		$top_level_elements = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   345
		$children_elements  = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   346
		foreach ( $elements as $e ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   347
			if ( empty( $e->$parent_field ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				$top_level_elements[] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   349
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
				$children_elements[ $e->$parent_field ][] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   351
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   353
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   354
		$total_top = count( $top_level_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   355
		if ( $paging ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   356
			$this->max_pages = ceil( $total_top / $per_page );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   357
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
			$end = $total_top;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   359
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   361
		if ( ! empty( $args[0]['reverse_top_level'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   362
			$top_level_elements = array_reverse( $top_level_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   363
			$oldstart           = $start;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   364
			$start              = $total_top - $end;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   365
			$end                = $total_top - $oldstart;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   366
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   367
		if ( ! empty( $args[0]['reverse_children'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   368
			foreach ( $children_elements as $parent => $children ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   369
				$children_elements[ $parent ] = array_reverse( $children );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   370
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
		foreach ( $top_level_elements as $e ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
			$count++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   375
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
			// For the last page, need to unset earlier children in order to keep track of orphans.
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   377
			if ( $end >= $total_top && $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
					$this->unset_children( $e, $children_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   379
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   381
			if ( $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
				continue;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   383
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   385
			if ( $count >= $end ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
				break;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   387
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
			$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   392
		if ( $end >= $total_top && count( $children_elements ) > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   393
			$empty_array = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   394
			foreach ( $children_elements as $orphans ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   395
				foreach ( $orphans as $op ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   396
					$this->display_element( $op, $empty_array, 1, 0, $args, $output );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   397
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   398
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   402
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   403
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   404
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   405
	 * Calculates the total number of root elements.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   406
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   407
	 * @since 2.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   408
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   409
	 * @param array $elements Elements to list.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   410
	 * @return int Number of root elements.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   411
	 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   412
	public function get_number_of_root_elements( $elements ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   413
		$num          = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   414
		$parent_field = $this->db_fields['parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   415
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   416
		foreach ( $elements as $e ) {
18
be944660c56a Site enmi version 09/2022
ymh <ymh.work@gmail.com>
parents: 16
diff changeset
   417
			if ( empty( $e->$parent_field ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
				$num++;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   419
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
		return $num;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   422
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   423
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   424
	/**
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   425
	 * Unsets all the children for a given top level element.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   426
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   427
	 * @since 2.7.0
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   428
	 *
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   429
	 * @param object $element           The top level element.
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   430
	 * @param array  $children_elements The children elements.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   431
	 */
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   432
	public function unset_children( $element, &$children_elements ) {
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   433
		if ( ! $element || ! $children_elements ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
			return;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   435
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   436
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
		$id_field = $this->db_fields['id'];
19
3d72ae0968f4 upgrade wordpress to 6.0.2
ymh <ymh.work@gmail.com>
parents: 18
diff changeset
   438
		$id       = $element->$id_field;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   439
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   440
		if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   441
			foreach ( (array) $children_elements[ $id ] as $child ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   442
				$this->unset_children( $child, $children_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   443
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   444
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   446
		unset( $children_elements[ $id ] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   448
16
a86126ab1dd4 update enmi-conf
ymh <ymh.work@gmail.com>
parents: 9
diff changeset
   449
}