wp/wp-includes/class-wp-walker.php
author ymh <ymh.work@gmail.com>
Tue, 22 Oct 2019 16:11:46 +0200
changeset 15 3d4e9c994f10
parent 9 177826044cd9
child 16 a86126ab1dd4
permissions -rw-r--r--
Upgrade jquery-ui in in-motion theme version from 1.8.14 to 1.8.22 to avoid 'a.curCSS is not a function' errors in console that caused problems with circles and navigation.
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    27
	 * @var array
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
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    32
	 * Max number of pages walked by the paged walker
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
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    80
	 * Start the element output.
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    83
	 * class methods. Includes the element output also.
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    86
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    87
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
    88
	 * @param string $output            Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    89
	 * @param object $object            The data object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    90
	 * @param int    $depth             Depth of the item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    91
	 * @param array  $args              An array of additional arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    92
	 * @param int    $current_object_id ID of the current item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    93
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
    94
	public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    95
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    96
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    97
	 * Ends the element output, if needed.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    98
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
    99
	 * 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
   100
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   101
	 * @since 2.1.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   102
	 * @abstract
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   103
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   104
	 * @param string $output Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   105
	 * @param object $object The data object.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   106
	 * @param int    $depth  Depth of the item.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   107
	 * @param array  $args   An array of additional arguments.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   108
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   109
	public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   110
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   111
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   112
	 * Traverse elements to create list from elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   113
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   114
	 * Display one element if the element doesn't have any children otherwise,
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   115
	 * display the element and its children. Will only traverse up to the max
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   116
	 * 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
   117
	 * max depth to include all depths, see walk() method.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   118
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   119
	 * This method should not be called directly, use the walk() method instead.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   120
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   121
	 * @since 2.5.0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   122
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   123
	 * @param object $element           Data object.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   124
	 * @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
   125
	 * @param int    $max_depth         Max depth to traverse.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   126
	 * @param int    $depth             Depth of current element.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   127
	 * @param array  $args              An array of arguments.
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   128
	 * @param string $output            Used to append additional content (passed by reference).
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   129
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   130
	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
   131
		if ( ! $element ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   132
			return;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   133
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   134
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   135
		$id_field = $this->db_fields['id'];
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   136
		$id       = $element->$id_field;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   137
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   138
		//display this element
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   139
		$this->has_children = ! empty( $children_elements[ $id ] );
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   140
		if ( isset( $args[0] ) && is_array( $args[0] ) ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   141
			$args[0]['has_children'] = $this->has_children; // Back-compat.
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   142
		}
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   143
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   144
		$cb_args = array_merge( array( &$output, $element, $depth ), $args );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   145
		call_user_func_array( array( $this, 'start_el' ), $cb_args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   146
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   147
		// descend only when the depth is right and there are childrens for this element
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   148
		if ( ( $max_depth == 0 || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   149
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   150
			foreach ( $children_elements[ $id ] as $child ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   151
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   152
				if ( ! isset( $newlevel ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   153
					$newlevel = true;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   154
					//start the child delimiter
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   155
					$cb_args = array_merge( array( &$output, $depth ), $args );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   156
					call_user_func_array( array( $this, 'start_lvl' ), $cb_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 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   164
			//end the child delimiter
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   165
			$cb_args = array_merge( array( &$output, $depth ), $args );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   166
			call_user_func_array( array( $this, 'end_lvl' ), $cb_args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   167
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   168
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   169
		//end this element
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   170
		$cb_args = array_merge( array( &$output, $element, $depth ), $args );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   171
		call_user_func_array( array( $this, 'end_el' ), $cb_args );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   172
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   173
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   174
	/**
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   175
	 * Display array of elements hierarchically.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   176
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   177
	 * Does not assume any existing order of elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   178
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   179
	 * $max_depth = -1 means flatly display every element.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   180
	 * $max_depth = 0 means display all levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   181
	 * $max_depth > 0 specifies the number of display levels.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   182
	 *
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   183
	 * @since 2.1.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.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   187
	 * @return string The hierarchical item output.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   188
	 */
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   189
	public function walk( $elements, $max_depth ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   190
		$args   = array_slice( func_get_args(), 2 );
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
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   193
		//invalid parameter or nothing to walk
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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   200
		// flat display
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.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   212
		 * Children_elements is two dimensional array, eg.
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
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   253
		if ( ( $max_depth == 0 ) && count( $children_elements ) > 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
	/**
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   266
	 * paged_walk() - produce a page of nested elements
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
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   276
	 *
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   277
	 * @param array $elements
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   278
	 * @param int   $max_depth The maximum hierarchical depth.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   279
	 * @param int   $page_num The specific page number, beginning with 1.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   280
	 * @param int   $per_page
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   281
	 * @return string XHTML of the specified page of elements
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   282
	 */
5
5e2f62d02dcd upgrade wordpress + plugins
ymh <ymh.work@gmail.com>
parents: 0
diff changeset
   283
	public function paged_walk( $elements, $max_depth, $page_num, $per_page ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   284
		if ( empty( $elements ) || $max_depth < -1 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   285
			return '';
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   286
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   287
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   288
		$args   = array_slice( func_get_args(), 4 );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   289
		$output = '';
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   290
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   291
		$parent_field = $this->db_fields['parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   292
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   293
		$count = -1;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   294
		if ( -1 == $max_depth ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   295
			$total_top = count( $elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   296
		}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   297
		if ( $page_num < 1 || $per_page < 0 ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   298
			// No paging
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   299
			$paging = false;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   300
			$start  = 0;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   301
			if ( -1 == $max_depth ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   302
				$end = $total_top;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   303
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   304
			$this->max_pages = 1;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   305
		} else {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   306
			$paging = true;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   307
			$start  = ( (int) $page_num - 1 ) * (int) $per_page;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   308
			$end    = $start + $per_page;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   309
			if ( -1 == $max_depth ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   310
				$this->max_pages = ceil( $total_top / $per_page );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   311
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   312
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   313
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   314
		// flat display
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   315
		if ( -1 == $max_depth ) {
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   316
			if ( ! empty( $args[0]['reverse_top_level'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   317
				$elements = array_reverse( $elements );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   318
				$oldstart = $start;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   319
				$start    = $total_top - $end;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   320
				$end      = $total_top - $oldstart;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   321
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   322
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   323
			$empty_array = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   324
			foreach ( $elements as $e ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   325
				$count++;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   326
				if ( $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   327
					continue;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   328
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   329
				if ( $count >= $end ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   330
					break;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   331
				}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   332
				$this->display_element( $e, $empty_array, 1, 0, $args, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   333
			}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   334
			return $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
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   337
		/*
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   338
		 * Separate elements into two buckets: top level and children elements.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   339
		 * Children_elements is two dimensional array, e.g.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   340
		 * $children_elements[10][] contains all sub-elements whose parent is 10.
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   341
		 */
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   342
		$top_level_elements = array();
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   343
		$children_elements  = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   344
		foreach ( $elements as $e ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   345
			if ( 0 == $e->$parent_field ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   346
				$top_level_elements[] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   347
			} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   348
				$children_elements[ $e->$parent_field ][] = $e;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   349
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   350
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   351
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   352
		$total_top = count( $top_level_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   353
		if ( $paging ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   354
			$this->max_pages = ceil( $total_top / $per_page );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   355
		} else {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   356
			$end = $total_top;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   357
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   358
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   359
		if ( ! empty( $args[0]['reverse_top_level'] ) ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   360
			$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
   361
			$oldstart           = $start;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   362
			$start              = $total_top - $end;
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   363
			$end                = $total_top - $oldstart;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   364
		}
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   365
		if ( ! empty( $args[0]['reverse_children'] ) ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   366
			foreach ( $children_elements as $parent => $children ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   367
				$children_elements[ $parent ] = array_reverse( $children );
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   368
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   369
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   370
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   371
		foreach ( $top_level_elements as $e ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   372
			$count++;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   373
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   374
			// 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
   375
			if ( $end >= $total_top && $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   376
					$this->unset_children( $e, $children_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   377
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   378
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   379
			if ( $count < $start ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   380
				continue;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   381
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   382
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   383
			if ( $count >= $end ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   384
				break;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   385
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   386
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   387
			$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   388
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   389
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   390
		if ( $end >= $total_top && count( $children_elements ) > 0 ) {
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   391
			$empty_array = array();
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   392
			foreach ( $children_elements as $orphans ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   393
				foreach ( $orphans as $op ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   394
					$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
   395
				}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   396
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   397
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   398
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   399
		return $output;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   400
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   401
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   402
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   403
	 * Calculates the total number of root elements.
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
	 * @since 2.7.0
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
	 * @param array $elements Elements to list.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   408
	 * @return int Number of root elements.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   409
	 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   410
	public function get_number_of_root_elements( $elements ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   411
		$num          = 0;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   412
		$parent_field = $this->db_fields['parent'];
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   413
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   414
		foreach ( $elements as $e ) {
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   415
			if ( 0 == $e->$parent_field ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   416
				$num++;
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   417
			}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   418
		}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   419
		return $num;
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   420
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   421
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   422
	/**
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   423
	 * Unset all the children for a given top level element.
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   424
	 *
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   425
	 * @since 2.7.0
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
	 * @param object $e
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   428
	 * @param array $children_elements
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   429
	 */
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   430
	public function unset_children( $e, &$children_elements ) {
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   431
		if ( ! $e || ! $children_elements ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   432
			return;
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   433
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   434
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   435
		$id_field = $this->db_fields['id'];
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   436
		$id       = $e->$id_field;
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   437
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   438
		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
   439
			foreach ( (array) $children_elements[ $id ] as $child ) {
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   440
				$this->unset_children( $child, $children_elements );
9
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   441
			}
177826044cd9 upgrade wordpress to 5.2.3
ymh <ymh.work@gmail.com>
parents: 7
diff changeset
   442
		}
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   443
7
cf61fcea0001 resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents: 5
diff changeset
   444
		unset( $children_elements[ $id ] );
0
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   445
	}
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   446
d970ebf37754 first import
ymh <ymh.work@gmail.com>
parents:
diff changeset
   447
} // Walker