2010-10-30 09:02:06 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class for displaying various tree-like structures.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* Extend the Walker class to use it, see examples below. Child classes
|
2010-10-30 09:02:06 +02:00
|
|
|
* do not need to implement all of the abstract methods in the class. The child
|
2013-09-28 23:29:10 +02:00
|
|
|
* only needs to implement the methods that are needed.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2013-10-25 04:29:52 +02:00
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* @package WordPress
|
2010-10-30 09:02:06 +02:00
|
|
|
* @abstract
|
|
|
|
*/
|
|
|
|
class Walker {
|
|
|
|
/**
|
|
|
|
* What the class handles.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2014-07-14 01:41:14 +02:00
|
|
|
* @var string
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2014-05-19 08:00:15 +02:00
|
|
|
public $tree_type;
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DB fields to use.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2014-07-14 01:41:14 +02:00
|
|
|
* @var array
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2015-01-11 00:44:23 +01:00
|
|
|
public $db_fields;
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Max number of pages walked by the paged walker
|
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2014-07-14 01:41:14 +02:00
|
|
|
* @var int
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2015-01-11 00:44:23 +01:00
|
|
|
public $max_pages = 1;
|
2014-05-19 08:00:15 +02:00
|
|
|
|
2014-06-24 19:06:14 +02:00
|
|
|
/**
|
2014-08-13 05:56:17 +02:00
|
|
|
* Whether the current element has children or not.
|
2014-08-09 21:30:17 +02:00
|
|
|
*
|
|
|
|
* To be used in start_el().
|
2014-06-24 19:06:14 +02:00
|
|
|
*
|
|
|
|
* @since 4.0.0
|
2014-07-14 01:41:14 +02:00
|
|
|
* @var bool
|
2014-06-24 19:06:14 +02:00
|
|
|
*/
|
2015-01-11 02:56:22 +01:00
|
|
|
public $has_children;
|
2014-06-24 19:06:14 +02:00
|
|
|
|
2010-10-30 09:02:06 +02:00
|
|
|
/**
|
|
|
|
* Starts the list before the elements are added.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* The $args parameter holds additional values that may be used with the child
|
|
|
|
* class methods. This method is called at the start of the output list.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2014-05-19 08:00:15 +02:00
|
|
|
public function start_lvl( &$output, $depth = 0, $args = array() ) {}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the list of after the elements are added.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* The $args parameter holds additional values that may be used with the child
|
|
|
|
* class methods. This method finishes the list at the end of output of the elements.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2014-05-19 08:00:15 +02:00
|
|
|
public function end_lvl( &$output, $depth = 0, $args = array() ) {}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start the element output.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* The $args parameter holds additional values that may be used with the child
|
|
|
|
* class methods. Includes the element output also.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::start_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword.
In each child class: renames the corresponding parameter to match the parent's method signature.
Why?
PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- in methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
- in cases where the original parameter name was too generic, renamed (when reassigning) to a more descriptive name for use within the method.
Follow-up to [7737], [8900], [8970], [14248], [15077], [16100], [25642], [25644], [37051], [37054], [37056], [46271], [47189].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51739
git-svn-id: http://core.svn.wordpress.org/trunk@51347 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-08 17:36:59 +02:00
|
|
|
* @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
|
2010-10-30 09:02:06 +02:00
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::start_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword.
In each child class: renames the corresponding parameter to match the parent's method signature.
Why?
PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- in methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
- in cases where the original parameter name was too generic, renamed (when reassigning) to a more descriptive name for use within the method.
Follow-up to [7737], [8900], [8970], [14248], [15077], [16100], [25642], [25644], [37051], [37054], [37056], [46271], [47189].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51739
git-svn-id: http://core.svn.wordpress.org/trunk@51347 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-08 17:36:59 +02:00
|
|
|
* @param object $data_object The data object.
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
Code Modernization: Fix last parameter name mismatches for parent/child classes in `Walker::start_el()`.
The parent class uses `$current_object_id` while most of the child classes use `$id`. As the parent class' is more descriptive, renaming the last parameter in each of child class.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- In methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
- In cases where the original parameter name was too generic or misleading, renamed (when reassigning) to a more descriptive name for use within the method.
Follow-up to [7737], [8900], [8970], [14248], [15077], [16100], [25642], [25644], [37051], [37054], [37056], [46271], [47189], [51739].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51779
git-svn-id: http://core.svn.wordpress.org/trunk@51386 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 14:39:59 +02:00
|
|
|
* @param int $current_object_id Optional. ID of the current item. Default 0.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::start_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword.
In each child class: renames the corresponding parameter to match the parent's method signature.
Why?
PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
- in methods longer than a single line, the generic parameter is reassigned to the original parameter restoring it for context for use within the method. An inline comment is added to explain why this reassignment is made.
- in cases where the original parameter name was too generic, renamed (when reassigning) to a more descriptive name for use within the method.
Follow-up to [7737], [8900], [8970], [14248], [15077], [16100], [25642], [25644], [37051], [37054], [37056], [46271], [47189].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51739
git-svn-id: http://core.svn.wordpress.org/trunk@51347 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-08 17:36:59 +02:00
|
|
|
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the element output, if needed.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* The $args parameter holds additional values that may be used with the child class methods.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::end_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword. The parameter name is selected for consistency with `Walker::start_el()`.
In each child class: renames the parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
Follow-up to [7737], [8900], [8970], [14248], [16100], [25642], [25644], [37051], [37056].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51780
git-svn-id: http://core.svn.wordpress.org/trunk@51387 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 15:03:55 +02:00
|
|
|
* @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
|
2010-10-30 09:02:06 +02:00
|
|
|
* @abstract
|
|
|
|
*
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::end_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword. The parameter name is selected for consistency with `Walker::start_el()`.
In each child class: renames the parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
Follow-up to [7737], [8900], [8970], [14248], [16100], [25642], [25644], [37051], [37056].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51780
git-svn-id: http://core.svn.wordpress.org/trunk@51387 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 15:03:55 +02:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
|
|
|
* @param object $data_object The data object.
|
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
Code Modernization: Fix reserved keyword and parameter name mismatches for parent/child classes in `Walker::end_el()`.
In the parent class, renames the parameter `$object` to `$data_object`.
Why? `object` is a PHP reserved keyword. The parameter name is selected for consistency with `Walker::start_el()`.
In each child class: renames the parameter to match the parent's method signature.
Why? PHP 8 introduces the ability to pass named arguments to function/method calls. This means the child and parent method signatures (i.e. parameter names) need to match.
Changes for readability:
- `@since` clearly specifies the original parameter name and its new name as well as why the change happened.
Follow-up to [7737], [8900], [8970], [14248], [16100], [25642], [25644], [37051], [37056].
Props jrf, hellofromTonya, sergeybiryukov, azaozz, desrosj, johnbillion.
See #51553.
Built from https://develop.svn.wordpress.org/trunk@51780
git-svn-id: http://core.svn.wordpress.org/trunk@51387 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-09-09 15:03:55 +02:00
|
|
|
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Traverse elements to create list from elements.
|
|
|
|
*
|
|
|
|
* Display one element if the element doesn't have any children otherwise,
|
|
|
|
* display the element and its children. Will only traverse up to the max
|
|
|
|
* depth and no ignore elements under that depth. It is possible to set the
|
|
|
|
* max depth to include all depths, see walk() method.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* This method should not be called directly, use the walk() method instead.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param object $element Data object.
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param array $children_elements List of elements to continue traversing (passed by reference).
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param int $max_depth Max depth to traverse.
|
|
|
|
* @param int $depth Depth of current element.
|
|
|
|
* @param array $args An array of arguments.
|
2017-10-03 00:14:46 +02:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2014-05-19 08:00:15 +02:00
|
|
|
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
|
2015-05-22 19:59:25 +02:00
|
|
|
if ( ! $element ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
return;
|
2015-05-22 19:59:25 +02:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$id_field = $this->db_fields['id'];
|
2014-06-24 19:06:14 +02:00
|
|
|
$id = $element->$id_field;
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Display this element.
|
2014-06-24 19:06:14 +02:00
|
|
|
$this->has_children = ! empty( $children_elements[ $id ] );
|
|
|
|
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
|
2016-05-13 20:41:31 +02:00
|
|
|
$args[0]['has_children'] = $this->has_children; // Back-compat.
|
2014-06-24 19:06:14 +02:00
|
|
|
}
|
|
|
|
|
2019-09-15 13:50:55 +02:00
|
|
|
$this->start_el( $output, $element, $depth, ...array_values( $args ) );
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2020-11-05 19:46:10 +01:00
|
|
|
// Descend only when the depth is right and there are children for this element.
|
2020-02-09 17:55:09 +01:00
|
|
|
if ( ( 0 == $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $children_elements[ $id ] as $child ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $newlevel ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$newlevel = true;
|
2020-01-29 01:45:18 +01:00
|
|
|
// Start the child delimiter.
|
2019-09-15 13:50:55 +02:00
|
|
|
$this->start_lvl( $output, $depth, ...array_values( $args ) );
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
|
|
|
|
}
|
|
|
|
unset( $children_elements[ $id ] );
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $newlevel ) && $newlevel ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// End the child delimiter.
|
2019-09-15 13:50:55 +02:00
|
|
|
$this->end_lvl( $output, $depth, ...array_values( $args ) );
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// End this element.
|
2019-09-15 13:50:55 +02:00
|
|
|
$this->end_el( $output, $element, $depth, ...array_values( $args ) );
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Display array of elements hierarchically.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* Does not assume any existing order of elements.
|
|
|
|
*
|
|
|
|
* $max_depth = -1 means flatly display every element.
|
|
|
|
* $max_depth = 0 means display all levels.
|
|
|
|
* $max_depth > 0 specifies the number of display levels.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2019-10-09 06:35:01 +02:00
|
|
|
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
|
|
|
|
* to the function signature.
|
2010-10-30 09:02:06 +02:00
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* @param array $elements An array of elements.
|
|
|
|
* @param int $max_depth The maximum hierarchical depth.
|
2019-10-08 19:56:02 +02:00
|
|
|
* @param mixed ...$args Optional additional arguments.
|
2013-09-28 23:29:10 +02:00
|
|
|
* @return string The hierarchical item output.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2019-10-08 19:56:02 +02:00
|
|
|
public function walk( $elements, $max_depth, ...$args ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$output = '';
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Invalid parameter or nothing to walk.
|
2015-05-22 19:59:25 +02:00
|
|
|
if ( $max_depth < -1 || empty( $elements ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
return $output;
|
2015-05-22 19:59:25 +02:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Flat display.
|
2010-10-30 09:02:06 +02:00
|
|
|
if ( -1 == $max_depth ) {
|
|
|
|
$empty_array = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $elements as $e ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 23:29:10 +02:00
|
|
|
* Need to display in hierarchical order.
|
|
|
|
* Separate elements into two buckets: top level and children elements.
|
2021-11-19 15:44:01 +01:00
|
|
|
* Children_elements is two dimensional array. Example:
|
2013-09-28 23:29:10 +02:00
|
|
|
* Children_elements[10][] contains all sub-elements whose parent is 10.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $elements as $e ) {
|
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$top_level_elements[] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2010-10-30 09:02:06 +02:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 23:29:10 +02:00
|
|
|
* When none of the elements is top level.
|
|
|
|
* Assume the first one must be root of the sub elements.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $top_level_elements ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$first = array_slice( $elements, 0, 1 );
|
2017-12-01 00:11:00 +01:00
|
|
|
$root = $first[0];
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $elements as $e ) {
|
|
|
|
if ( $root->$parent_field == $e->$parent_field ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$top_level_elements[] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2010-10-30 09:02:06 +02:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $top_level_elements as $e ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
/*
|
2013-09-28 23:29:10 +02:00
|
|
|
* If we are displaying all levels, and remaining children_elements is not empty,
|
|
|
|
* then we got orphans, which should be displayed regardless.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
2020-02-09 17:55:09 +01:00
|
|
|
if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$empty_array = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $children_elements as $orphans ) {
|
|
|
|
foreach ( $orphans as $op ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
2017-11-27 00:57:55 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-11-27 00:57:55 +01:00
|
|
|
return $output;
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-12-01 00:11:00 +01:00
|
|
|
* paged_walk() - produce a page of nested elements
|
|
|
|
*
|
|
|
|
* Given an array of hierarchical elements, the maximum depth, a specific page number,
|
|
|
|
* and number of elements per page, this function first determines all top level root elements
|
|
|
|
* belonging to that page, then lists them and all of their children in hierarchical order.
|
|
|
|
*
|
2013-09-28 23:29:10 +02:00
|
|
|
* $max_depth = 0 means display all levels.
|
|
|
|
* $max_depth > 0 specifies the number of display levels.
|
|
|
|
*
|
2017-12-01 00:11:00 +01:00
|
|
|
* @since 2.7.0
|
2019-10-09 06:35:01 +02:00
|
|
|
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
|
|
|
|
* to the function signature.
|
2013-09-28 23:29:10 +02:00
|
|
|
*
|
2015-05-22 19:59:25 +02:00
|
|
|
* @param array $elements
|
|
|
|
* @param int $max_depth The maximum hierarchical depth.
|
2019-10-08 19:56:02 +02:00
|
|
|
* @param int $page_num The specific page number, beginning with 1.
|
2015-05-22 19:59:25 +02:00
|
|
|
* @param int $per_page
|
2019-10-08 19:56:02 +02:00
|
|
|
* @param mixed ...$args Optional additional arguments.
|
2015-05-22 19:59:25 +02:00
|
|
|
* @return string XHTML of the specified page of elements
|
|
|
|
*/
|
2019-10-08 19:56:02 +02:00
|
|
|
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
|
2015-05-22 19:59:25 +02:00
|
|
|
if ( empty( $elements ) || $max_depth < -1 ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
return '';
|
2015-05-22 19:59:25 +02:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$output = '';
|
|
|
|
|
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
|
|
|
$count = -1;
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( -1 == $max_depth ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$total_top = count( $elements );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
if ( $page_num < 1 || $per_page < 0 ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// No paging.
|
2010-10-30 09:02:06 +02:00
|
|
|
$paging = false;
|
2017-12-01 00:11:00 +01:00
|
|
|
$start = 0;
|
|
|
|
if ( -1 == $max_depth ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$end = $total_top;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->max_pages = 1;
|
|
|
|
} else {
|
|
|
|
$paging = true;
|
2017-12-01 00:11:00 +01:00
|
|
|
$start = ( (int) $page_num - 1 ) * (int) $per_page;
|
|
|
|
$end = $start + $per_page;
|
|
|
|
if ( -1 == $max_depth ) {
|
|
|
|
$this->max_pages = ceil( $total_top / $per_page );
|
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Flat display.
|
2010-10-30 09:02:06 +02:00
|
|
|
if ( -1 == $max_depth ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$elements = array_reverse( $elements );
|
|
|
|
$oldstart = $start;
|
2017-12-01 00:11:00 +01:00
|
|
|
$start = $total_top - $end;
|
|
|
|
$end = $total_top - $oldstart;
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$empty_array = array();
|
|
|
|
foreach ( $elements as $e ) {
|
|
|
|
$count++;
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $count < $start ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
continue;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
if ( $count >= $end ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
break;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 23:29:10 +02:00
|
|
|
* Separate elements into two buckets: top level and children elements.
|
|
|
|
* Children_elements is two dimensional array, e.g.
|
|
|
|
* $children_elements[10][] contains all sub-elements whose parent is 10.
|
2010-10-30 09:02:06 +02:00
|
|
|
*/
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $elements as $e ) {
|
Code Modernization: Use a consistent check for parent items in `WP_Walker`.
This affects the `::walk()`, `::paged_walk()`, and `::get_number_of_root_elements()` methods.
PHP 8 changes the way string to number comparisons are performed: https://wiki.php.net/rfc/string_to_number_comparison
In particular, checking if an empty string is equal to zero in PHP 8 evaluates to `false`, not `true`.
For the `WP_Walker` class, this resulted in an incorrect handling of parent items in a few methods.
By explicitly checking for an `empty()` value instead, we make sure the check works as expected in PHP 8 and earlier versions.
Follow-up to [35876], [48960], [49043], [49076].
Props sunxiyuan, aristath, SergeyBiryukov.
Fixes #53474.
Built from https://develop.svn.wordpress.org/trunk@51204
git-svn-id: http://core.svn.wordpress.org/trunk@50813 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-06-22 21:09:00 +02:00
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$top_level_elements[] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
} else {
|
2010-10-30 09:02:06 +02:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$total_top = count( $top_level_elements );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $paging ) {
|
|
|
|
$this->max_pages = ceil( $total_top / $per_page );
|
|
|
|
} else {
|
2010-10-30 09:02:06 +02:00
|
|
|
$end = $total_top;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$top_level_elements = array_reverse( $top_level_elements );
|
2017-12-01 00:11:00 +01:00
|
|
|
$oldstart = $start;
|
|
|
|
$start = $total_top - $end;
|
|
|
|
$end = $total_top - $oldstart;
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! empty( $args[0]['reverse_children'] ) ) {
|
|
|
|
foreach ( $children_elements as $parent => $children ) {
|
|
|
|
$children_elements[ $parent ] = array_reverse( $children );
|
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $top_level_elements as $e ) {
|
|
|
|
$count++;
|
|
|
|
|
2013-09-28 23:29:10 +02:00
|
|
|
// For the last page, need to unset earlier children in order to keep track of orphans.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $end >= $total_top && $count < $start ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->unset_children( $e, $children_elements );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $count < $start ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
continue;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $count >= $end ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
break;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $end >= $total_top && count( $children_elements ) > 0 ) {
|
|
|
|
$empty_array = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $children_elements as $orphans ) {
|
|
|
|
foreach ( $orphans as $op ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-05-22 19:59:25 +02:00
|
|
|
/**
|
2015-09-02 21:51:21 +02:00
|
|
|
* Calculates the total number of root elements.
|
2015-05-22 19:59:25 +02:00
|
|
|
*
|
2015-09-02 21:51:21 +02:00
|
|
|
* @since 2.7.0
|
2016-05-13 20:41:31 +02:00
|
|
|
*
|
2015-09-14 17:33:27 +02:00
|
|
|
* @param array $elements Elements to list.
|
|
|
|
* @return int Number of root elements.
|
2015-05-22 19:59:25 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
public function get_number_of_root_elements( $elements ) {
|
|
|
|
$num = 0;
|
2010-10-30 09:02:06 +02:00
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $elements as $e ) {
|
Code Modernization: Use a consistent check for parent items in `WP_Walker`.
This affects the `::walk()`, `::paged_walk()`, and `::get_number_of_root_elements()` methods.
PHP 8 changes the way string to number comparisons are performed: https://wiki.php.net/rfc/string_to_number_comparison
In particular, checking if an empty string is equal to zero in PHP 8 evaluates to `false`, not `true`.
For the `WP_Walker` class, this resulted in an incorrect handling of parent items in a few methods.
By explicitly checking for an `empty()` value instead, we make sure the check works as expected in PHP 8 and earlier versions.
Follow-up to [35876], [48960], [49043], [49076].
Props sunxiyuan, aristath, SergeyBiryukov.
Fixes #53474.
Built from https://develop.svn.wordpress.org/trunk@51204
git-svn-id: http://core.svn.wordpress.org/trunk@50813 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-06-22 21:09:00 +02:00
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$num++;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
|
|
|
return $num;
|
|
|
|
}
|
|
|
|
|
2015-05-22 19:59:25 +02:00
|
|
|
/**
|
|
|
|
* Unset all the children for a given top level element.
|
|
|
|
*
|
2017-01-06 23:14:00 +01:00
|
|
|
* @since 2.7.0
|
|
|
|
*
|
2015-05-22 19:59:25 +02:00
|
|
|
* @param object $e
|
2020-07-23 23:11:05 +02:00
|
|
|
* @param array $children_elements
|
2015-05-22 19:59:25 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
public function unset_children( $e, &$children_elements ) {
|
2015-05-22 19:59:25 +02:00
|
|
|
if ( ! $e || ! $children_elements ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
return;
|
2015-05-22 19:59:25 +02:00
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
|
|
|
$id_field = $this->db_fields['id'];
|
2017-12-01 00:11:00 +01:00
|
|
|
$id = $e->$id_field;
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
|
|
|
|
foreach ( (array) $children_elements[ $id ] as $child ) {
|
2010-10-30 09:02:06 +02:00
|
|
|
$this->unset_children( $child, $children_elements );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 09:02:06 +02:00
|
|
|
|
2015-05-22 07:47:25 +02:00
|
|
|
unset( $children_elements[ $id ] );
|
2010-10-30 09:02:06 +02:00
|
|
|
}
|
2013-09-28 23:29:10 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
}
|