wp/wp-includes/class-wp-walker.php
changeset 19 3d72ae0968f4
parent 18 be944660c56a
child 21 48c4eec2b7e6
equal deleted inserted replaced
18:be944660c56a 19:3d72ae0968f4
    22 
    22 
    23 	/**
    23 	/**
    24 	 * DB fields to use.
    24 	 * DB fields to use.
    25 	 *
    25 	 *
    26 	 * @since 2.1.0
    26 	 * @since 2.1.0
    27 	 * @var array
    27 	 * @var string[]
    28 	 */
    28 	 */
    29 	public $db_fields;
    29 	public $db_fields;
    30 
    30 
    31 	/**
    31 	/**
    32 	 * Max number of pages walked by the paged walker
    32 	 * Max number of pages walked by the paged walker.
    33 	 *
    33 	 *
    34 	 * @since 2.7.0
    34 	 * @since 2.7.0
    35 	 * @var int
    35 	 * @var int
    36 	 */
    36 	 */
    37 	public $max_pages = 1;
    37 	public $max_pages = 1;
    75 	 * @param array  $args   An array of additional arguments.
    75 	 * @param array  $args   An array of additional arguments.
    76 	 */
    76 	 */
    77 	public function end_lvl( &$output, $depth = 0, $args = array() ) {}
    77 	public function end_lvl( &$output, $depth = 0, $args = array() ) {}
    78 
    78 
    79 	/**
    79 	/**
    80 	 * Start the element output.
    80 	 * Starts the element output.
    81 	 *
    81 	 *
    82 	 * The $args parameter holds additional values that may be used with the child
    82 	 * The $args parameter holds additional values that may be used with the child
    83 	 * class methods. Includes the element output also.
    83 	 * class methods. Also includes the element output.
    84 	 *
    84 	 *
    85 	 * @since 2.1.0
    85 	 * @since 2.1.0
       
    86 	 * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
    86 	 * @abstract
    87 	 * @abstract
    87 	 *
    88 	 *
    88 	 * @param string $output            Used to append additional content (passed by reference).
    89 	 * @param string $output            Used to append additional content (passed by reference).
    89 	 * @param object $object            The data object.
    90 	 * @param object $data_object       The data object.
    90 	 * @param int    $depth             Depth of the item.
    91 	 * @param int    $depth             Depth of the item.
    91 	 * @param array  $args              An array of additional arguments.
    92 	 * @param array  $args              An array of additional arguments.
    92 	 * @param int    $current_object_id ID of the current item.
    93 	 * @param int    $current_object_id Optional. ID of the current item. Default 0.
    93 	 */
    94 	 */
    94 	public function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
    95 	public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
    95 
    96 
    96 	/**
    97 	/**
    97 	 * Ends the element output, if needed.
    98 	 * Ends the element output, if needed.
    98 	 *
    99 	 *
    99 	 * The $args parameter holds additional values that may be used with the child class methods.
   100 	 * The $args parameter holds additional values that may be used with the child class methods.
   100 	 *
   101 	 *
   101 	 * @since 2.1.0
   102 	 * @since 2.1.0
       
   103 	 * @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
   102 	 * @abstract
   104 	 * @abstract
   103 	 *
   105 	 *
   104 	 * @param string $output Used to append additional content (passed by reference).
   106 	 * @param string $output      Used to append additional content (passed by reference).
   105 	 * @param object $object The data object.
   107 	 * @param object $data_object The data object.
   106 	 * @param int    $depth  Depth of the item.
   108 	 * @param int    $depth       Depth of the item.
   107 	 * @param array  $args   An array of additional arguments.
   109 	 * @param array  $args        An array of additional arguments.
   108 	 */
   110 	 */
   109 	public function end_el( &$output, $object, $depth = 0, $args = array() ) {}
   111 	public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
   110 
   112 
   111 	/**
   113 	/**
   112 	 * Traverse elements to create list from elements.
   114 	 * Traverses elements to create list from elements.
   113 	 *
   115 	 *
   114 	 * Display one element if the element doesn't have any children otherwise,
   116 	 * Display one element if the element doesn't have any children otherwise,
   115 	 * display the element and its children. Will only traverse up to the max
   117 	 * display the element and its children. Will only traverse up to the max
   116 	 * depth and no ignore elements under that depth. It is possible to set the
   118 	 * depth and no ignore elements under that depth. It is possible to set the
   117 	 * max depth to include all depths, see walk() method.
   119 	 * max depth to include all depths, see walk() method.
   166 		// End this element.
   168 		// End this element.
   167 		$this->end_el( $output, $element, $depth, ...array_values( $args ) );
   169 		$this->end_el( $output, $element, $depth, ...array_values( $args ) );
   168 	}
   170 	}
   169 
   171 
   170 	/**
   172 	/**
   171 	 * Display array of elements hierarchically.
   173 	 * Displays array of elements hierarchically.
   172 	 *
   174 	 *
   173 	 * Does not assume any existing order of elements.
   175 	 * Does not assume any existing order of elements.
   174 	 *
   176 	 *
   175 	 * $max_depth = -1 means flatly display every element.
   177 	 * $max_depth = -1 means flatly display every element.
   176 	 * $max_depth = 0 means display all levels.
   178 	 * $max_depth = 0 means display all levels.
   205 		}
   207 		}
   206 
   208 
   207 		/*
   209 		/*
   208 		 * Need to display in hierarchical order.
   210 		 * Need to display in hierarchical order.
   209 		 * Separate elements into two buckets: top level and children elements.
   211 		 * Separate elements into two buckets: top level and children elements.
   210 		 * Children_elements is two dimensional array, eg.
   212 		 * Children_elements is two dimensional array. Example:
   211 		 * Children_elements[10][] contains all sub-elements whose parent is 10.
   213 		 * Children_elements[10][] contains all sub-elements whose parent is 10.
   212 		 */
   214 		 */
   213 		$top_level_elements = array();
   215 		$top_level_elements = array();
   214 		$children_elements  = array();
   216 		$children_elements  = array();
   215 		foreach ( $elements as $e ) {
   217 		foreach ( $elements as $e ) {
   259 
   261 
   260 		return $output;
   262 		return $output;
   261 	}
   263 	}
   262 
   264 
   263 	/**
   265 	/**
   264 	 * paged_walk() - produce a page of nested elements
   266 	 * Produces a page of nested elements.
   265 	 *
   267 	 *
   266 	 * Given an array of hierarchical elements, the maximum depth, a specific page number,
   268 	 * Given an array of hierarchical elements, the maximum depth, a specific page number,
   267 	 * and number of elements per page, this function first determines all top level root elements
   269 	 * and number of elements per page, this function first determines all top level root elements
   268 	 * belonging to that page, then lists them and all of their children in hierarchical order.
   270 	 * belonging to that page, then lists them and all of their children in hierarchical order.
   269 	 *
   271 	 *
   272 	 *
   274 	 *
   273 	 * @since 2.7.0
   275 	 * @since 2.7.0
   274 	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
   276 	 * @since 5.3.0 Formalized the existing `...$args` parameter by adding it
   275 	 *              to the function signature.
   277 	 *              to the function signature.
   276 	 *
   278 	 *
   277 	 * @param array $elements
   279 	 * @param array $elements  An array of elements.
   278 	 * @param int   $max_depth The maximum hierarchical depth.
   280 	 * @param int   $max_depth The maximum hierarchical depth.
   279 	 * @param int   $page_num  The specific page number, beginning with 1.
   281 	 * @param int   $page_num  The specific page number, beginning with 1.
   280 	 * @param int   $per_page
   282 	 * @param int   $per_page  Number of elements per page.
   281 	 * @param mixed ...$args   Optional additional arguments.
   283 	 * @param mixed ...$args   Optional additional arguments.
   282 	 * @return string XHTML of the specified page of elements
   284 	 * @return string XHTML of the specified page of elements.
   283 	 */
   285 	 */
   284 	public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
   286 	public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
   285 		if ( empty( $elements ) || $max_depth < -1 ) {
   287 		if ( empty( $elements ) || $max_depth < -1 ) {
   286 			return '';
   288 			return '';
   287 		}
   289 		}
   418 		}
   420 		}
   419 		return $num;
   421 		return $num;
   420 	}
   422 	}
   421 
   423 
   422 	/**
   424 	/**
   423 	 * Unset all the children for a given top level element.
   425 	 * Unsets all the children for a given top level element.
   424 	 *
   426 	 *
   425 	 * @since 2.7.0
   427 	 * @since 2.7.0
   426 	 *
   428 	 *
   427 	 * @param object $e
   429 	 * @param object $element           The top level element.
   428 	 * @param array  $children_elements
   430 	 * @param array  $children_elements The children elements.
   429 	 */
   431 	 */
   430 	public function unset_children( $e, &$children_elements ) {
   432 	public function unset_children( $element, &$children_elements ) {
   431 		if ( ! $e || ! $children_elements ) {
   433 		if ( ! $element || ! $children_elements ) {
   432 			return;
   434 			return;
   433 		}
   435 		}
   434 
   436 
   435 		$id_field = $this->db_fields['id'];
   437 		$id_field = $this->db_fields['id'];
   436 		$id       = $e->$id_field;
   438 		$id       = $element->$id_field;
   437 
   439 
   438 		if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
   440 		if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
   439 			foreach ( (array) $children_elements[ $id ] as $child ) {
   441 			foreach ( (array) $children_elements[ $id ] as $child ) {
   440 				$this->unset_children( $child, $children_elements );
   442 				$this->unset_children( $child, $children_elements );
   441 			}
   443 			}