2010-10-30 03:02:06 -04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* A class for displaying various tree-like structures.
|
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* Extend the Walker class to use it, see examples below. Child classes
|
2010-10-30 03:02:06 -04:00
|
|
|
* do not need to implement all of the abstract methods in the class. The child
|
2013-09-28 17:29:10 -04:00
|
|
|
* only needs to implement the methods that are needed.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2013-10-24 22:29:52 -04:00
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* @package WordPress
|
2010-10-30 03:02:06 -04:00
|
|
|
* @abstract
|
|
|
|
*/
|
Code Modernization: Add `AllowDynamicProperties` attribute to all (parent) classes.
Dynamic (non-explicitly declared) properties are deprecated as of PHP 8.2 and are expected to become a fatal error in PHP 9.0.
There are a number of ways to mitigate this:
* If it is an accidental typo for a declared property: fix the typo.
* For known properties: declare them on the class.
* For unknown properties: add the magic `__get()`, `__set()`, et al. methods to the class or let the class extend `stdClass` which has highly optimized versions of these magic methods built in.
* For unknown ''use'' of dynamic properties, the `#[AllowDynamicProperties]` attribute can be added to the class. The attribute will automatically be inherited by child classes.
Trac ticket #56034 is open to investigate and handle the third and fourth type of situations, however it has become clear this will need more time and will not be ready in time for WP 6.1.
To reduce “noise” in the meantime, both in the error logs of WP users moving onto PHP 8.2, in the test run logs of WP itself, in test runs of plugins and themes, as well as to prevent duplicate tickets from being opened for the same issue, this commit adds the `#[AllowDynamicProperties]` attribute to all “parent” classes in WP.
The logic used for this commit is as follows:
* If a class already has the attribute: no action needed.
* If a class does not `extend`: add the attribute.
* If a class does `extend`:
- If it extends `stdClass`: no action needed (as `stdClass` supports dynamic properties).
- If it extends a PHP native class: add the attribute.
- If it extends a class from one of WP's external dependencies: add the attribute.
* In all other cases: no action — the attribute should not be needed as child classes inherit from the parent.
Whether or not a class contains magic methods has not been taken into account, as a review of the currently existing magic methods has shown that those are generally not sturdy enough and often even set dynamic properties (which they should not). See the [https://www.youtube.com/watch?v=vDZWepDQQVE live stream from August 16, 2022] for more details.
This commit only affects classes in the `src` directory of WordPress core.
* Tests should not get this attribute, but should be fixed to not use dynamic properties instead. Patches for this are already being committed under ticket #56033.
* While a number bundled themes (2014, 2019, 2020, 2021) contain classes, they are not a part of this commit and may be updated separately.
Reference: [https://wiki.php.net/rfc/deprecate_dynamic_properties PHP RFC: Deprecate dynamic properties].
Follow-up to [53922].
Props jrf, hellofromTonya, markjaquith, peterwilsoncc, costdev, knutsp, aristath.
See #56513, #56034.
Built from https://develop.svn.wordpress.org/trunk@54133
git-svn-id: http://core.svn.wordpress.org/trunk@53692 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-12 11:47:14 -04:00
|
|
|
#[AllowDynamicProperties]
|
2010-10-30 03:02:06 -04:00
|
|
|
class Walker {
|
|
|
|
/**
|
|
|
|
* What the class handles.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2014-07-13 19:41:14 -04:00
|
|
|
* @var string
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2014-05-19 02:00:15 -04:00
|
|
|
public $tree_type;
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* DB fields to use.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2022-01-30 14:25:03 -05:00
|
|
|
* @var string[]
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2015-01-10 18:44:23 -05:00
|
|
|
public $db_fields;
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Max number of pages walked by the paged walker.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.7.0
|
2014-07-13 19:41:14 -04:00
|
|
|
* @var int
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2015-01-10 18:44:23 -05:00
|
|
|
public $max_pages = 1;
|
2014-05-19 02:00:15 -04:00
|
|
|
|
2014-06-24 13:06:14 -04:00
|
|
|
/**
|
2014-08-12 23:56:17 -04:00
|
|
|
* Whether the current element has children or not.
|
2014-08-09 15:30:17 -04:00
|
|
|
*
|
|
|
|
* To be used in start_el().
|
2014-06-24 13:06:14 -04:00
|
|
|
*
|
|
|
|
* @since 4.0.0
|
2014-07-13 19:41:14 -04:00
|
|
|
* @var bool
|
2014-06-24 13:06:14 -04:00
|
|
|
*/
|
2015-01-10 20:56:22 -05:00
|
|
|
public $has_children;
|
2014-06-24 13:06:14 -04:00
|
|
|
|
2010-10-30 03:02:06 -04:00
|
|
|
/**
|
|
|
|
* Starts the list before the elements are added.
|
|
|
|
*
|
2013-09-28 17:29:10 -04: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 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-02 18:14:46 -04:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2013-09-28 17:29:10 -04:00
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2014-05-19 02:00:15 -04:00
|
|
|
public function start_lvl( &$output, $depth = 0, $args = array() ) {}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the list of after the elements are added.
|
|
|
|
*
|
2013-09-28 17:29:10 -04: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 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-02 18:14:46 -04:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2013-09-28 17:29:10 -04:00
|
|
|
* @param int $depth Depth of the item.
|
|
|
|
* @param array $args An array of additional arguments.
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2014-05-19 02:00:15 -04:00
|
|
|
public function end_lvl( &$output, $depth = 0, $args = array() ) {}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Starts the element output.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* The $args parameter holds additional values that may be used with the child
|
2022-05-01 17:41:12 -04:00
|
|
|
* class methods. Also includes the element output.
|
2010-10-30 03:02:06 -04: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 11:36:59 -04:00
|
|
|
* @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
|
2010-10-30 03:02:06 -04:00
|
|
|
* @abstract
|
|
|
|
*
|
2017-10-02 18:14:46 -04: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 11:36:59 -04:00
|
|
|
* @param object $data_object The data object.
|
2013-09-28 17:29:10 -04: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 08:39:59 -04:00
|
|
|
* @param int $current_object_id Optional. ID of the current item. Default 0.
|
2010-10-30 03:02:06 -04: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 11:36:59 -04:00
|
|
|
public function start_el( &$output, $data_object, $depth = 0, $args = array(), $current_object_id = 0 ) {}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Ends the element output, if needed.
|
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* The $args parameter holds additional values that may be used with the child class methods.
|
2010-10-30 03:02:06 -04: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 09:03:55 -04:00
|
|
|
* @since 5.9.0 Renamed `$object` (a PHP reserved keyword) to `$data_object` for PHP 8 named parameter support.
|
2010-10-30 03:02:06 -04: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 09:03:55 -04: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 03:02:06 -04: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 09:03:55 -04:00
|
|
|
public function end_el( &$output, $data_object, $depth = 0, $args = array() ) {}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Traverses elements to create list from elements.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
|
|
|
* 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 17:29:10 -04:00
|
|
|
* This method should not be called directly, use the walk() method instead.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.5.0
|
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* @param object $element Data object.
|
2017-10-02 18:14:46 -04:00
|
|
|
* @param array $children_elements List of elements to continue traversing (passed by reference).
|
2013-09-28 17:29:10 -04: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-02 18:14:46 -04:00
|
|
|
* @param string $output Used to append additional content (passed by reference).
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2014-05-19 02:00:15 -04:00
|
|
|
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
|
2015-05-22 13:59:25 -04:00
|
|
|
if ( ! $element ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
return;
|
2015-05-22 13:59:25 -04:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2024-08-07 15:47:10 -04:00
|
|
|
$max_depth = (int) $max_depth;
|
|
|
|
$depth = (int) $depth;
|
|
|
|
|
2010-10-30 03:02:06 -04:00
|
|
|
$id_field = $this->db_fields['id'];
|
2014-06-24 13:06:14 -04:00
|
|
|
$id = $element->$id_field;
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Display this element.
|
2014-06-24 13:06:14 -04:00
|
|
|
$this->has_children = ! empty( $children_elements[ $id ] );
|
|
|
|
if ( isset( $args[0] ) && is_array( $args[0] ) ) {
|
2016-05-13 14:41:31 -04:00
|
|
|
$args[0]['has_children'] = $this->has_children; // Back-compat.
|
2014-06-24 13:06:14 -04:00
|
|
|
}
|
|
|
|
|
2019-09-15 07:50:55 -04:00
|
|
|
$this->start_el( $output, $element, $depth, ...array_values( $args ) );
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2020-11-05 13:46:10 -05:00
|
|
|
// Descend only when the depth is right and there are children for this element.
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( ( 0 === $max_depth || $max_depth > $depth + 1 ) && isset( $children_elements[ $id ] ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $children_elements[ $id ] as $child ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! isset( $newlevel ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$newlevel = true;
|
2020-01-28 19:45:18 -05:00
|
|
|
// Start the child delimiter.
|
2019-09-15 07:50:55 -04:00
|
|
|
$this->start_lvl( $output, $depth, ...array_values( $args ) );
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $output );
|
|
|
|
}
|
|
|
|
unset( $children_elements[ $id ] );
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( isset( $newlevel ) && $newlevel ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// End the child delimiter.
|
2019-09-15 07:50:55 -04:00
|
|
|
$this->end_lvl( $output, $depth, ...array_values( $args ) );
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// End this element.
|
2019-09-15 07:50:55 -04:00
|
|
|
$this->end_el( $output, $element, $depth, ...array_values( $args ) );
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Displays array of elements hierarchically.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
2013-09-28 17:29:10 -04: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 03:02:06 -04:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2019-10-09 00:35:01 -04:00
|
|
|
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
|
|
|
|
* to the function signature.
|
2010-10-30 03:02:06 -04:00
|
|
|
*
|
2013-09-28 17:29:10 -04:00
|
|
|
* @param array $elements An array of elements.
|
|
|
|
* @param int $max_depth The maximum hierarchical depth.
|
2019-10-08 13:56:02 -04:00
|
|
|
* @param mixed ...$args Optional additional arguments.
|
2013-09-28 17:29:10 -04:00
|
|
|
* @return string The hierarchical item output.
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2019-10-08 13:56:02 -04:00
|
|
|
public function walk( $elements, $max_depth, ...$args ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$output = '';
|
|
|
|
|
2024-08-07 15:47:10 -04:00
|
|
|
$max_depth = (int) $max_depth;
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Invalid parameter or nothing to walk.
|
2015-05-22 13:59:25 -04:00
|
|
|
if ( $max_depth < -1 || empty( $elements ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
return $output;
|
2015-05-22 13:59:25 -04:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Flat display.
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( -1 === $max_depth ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$empty_array = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $elements as $e ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 17:29:10 -04:00
|
|
|
* Need to display in hierarchical order.
|
|
|
|
* Separate elements into two buckets: top level and children elements.
|
2021-11-19 09:44:01 -05:00
|
|
|
* Children_elements is two dimensional array. Example:
|
2013-09-28 17:29:10 -04:00
|
|
|
* Children_elements[10][] contains all sub-elements whose parent is 10.
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $elements as $e ) {
|
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$top_level_elements[] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2010-10-30 03:02:06 -04:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 17:29:10 -04:00
|
|
|
* When none of the elements is top level.
|
|
|
|
* Assume the first one must be root of the sub elements.
|
2010-10-30 03:02:06 -04:00
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( empty( $top_level_elements ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$first = array_slice( $elements, 0, 1 );
|
2017-11-30 18:11:00 -05:00
|
|
|
$root = $first[0];
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $elements as $e ) {
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( $root->$parent_field === $e->$parent_field ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$top_level_elements[] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2010-10-30 03:02:06 -04:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $top_level_elements as $e ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
/*
|
2013-09-28 17:29:10 -04: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 03:02:06 -04:00
|
|
|
*/
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( ( 0 === $max_depth ) && count( $children_elements ) > 0 ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$empty_array = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $children_elements as $orphans ) {
|
|
|
|
foreach ( $orphans as $op ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
}
|
2017-11-26 18:57:55 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-26 18:57:55 -05:00
|
|
|
return $output;
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Produces a page of nested elements.
|
2017-11-30 18:11:00 -05:00
|
|
|
*
|
|
|
|
* 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 17:29:10 -04:00
|
|
|
* $max_depth = 0 means display all levels.
|
|
|
|
* $max_depth > 0 specifies the number of display levels.
|
|
|
|
*
|
2017-11-30 18:11:00 -05:00
|
|
|
* @since 2.7.0
|
2019-10-09 00:35:01 -04:00
|
|
|
* @since 5.3.0 Formalized the existing `...$args` parameter by adding it
|
|
|
|
* to the function signature.
|
2013-09-28 17:29:10 -04:00
|
|
|
*
|
2022-02-24 08:33:01 -05:00
|
|
|
* @param array $elements An array of elements.
|
2015-05-22 13:59:25 -04:00
|
|
|
* @param int $max_depth The maximum hierarchical depth.
|
2019-10-08 13:56:02 -04:00
|
|
|
* @param int $page_num The specific page number, beginning with 1.
|
2022-02-24 08:33:01 -05:00
|
|
|
* @param int $per_page Number of elements per page.
|
2019-10-08 13:56:02 -04:00
|
|
|
* @param mixed ...$args Optional additional arguments.
|
2022-05-01 17:41:12 -04:00
|
|
|
* @return string XHTML of the specified page of elements.
|
2015-05-22 13:59:25 -04:00
|
|
|
*/
|
2019-10-08 13:56:02 -04:00
|
|
|
public function paged_walk( $elements, $max_depth, $page_num, $per_page, ...$args ) {
|
2024-08-07 15:47:10 -04:00
|
|
|
$output = '';
|
|
|
|
|
|
|
|
$max_depth = (int) $max_depth;
|
|
|
|
|
2015-05-22 13:59:25 -04:00
|
|
|
if ( empty( $elements ) || $max_depth < -1 ) {
|
2024-08-07 15:47:10 -04:00
|
|
|
return $output;
|
2015-05-22 13:59:25 -04:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
|
|
|
$count = -1;
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( -1 === $max_depth ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$total_top = count( $elements );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
if ( $page_num < 1 || $per_page < 0 ) {
|
2020-01-28 19:45:18 -05:00
|
|
|
// No paging.
|
2010-10-30 03:02:06 -04:00
|
|
|
$paging = false;
|
2017-11-30 18:11:00 -05:00
|
|
|
$start = 0;
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( -1 === $max_depth ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$end = $total_top;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->max_pages = 1;
|
|
|
|
} else {
|
|
|
|
$paging = true;
|
2017-11-30 18:11:00 -05:00
|
|
|
$start = ( (int) $page_num - 1 ) * (int) $per_page;
|
|
|
|
$end = $start + $per_page;
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( -1 === $max_depth ) {
|
2024-02-17 10:24:08 -05:00
|
|
|
$this->max_pages = (int) ceil( $total_top / $per_page );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
2020-01-28 19:45:18 -05:00
|
|
|
// Flat display.
|
2024-03-17 05:56:16 -04:00
|
|
|
if ( -1 === $max_depth ) {
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$elements = array_reverse( $elements );
|
|
|
|
$oldstart = $start;
|
2017-11-30 18:11:00 -05:00
|
|
|
$start = $total_top - $end;
|
|
|
|
$end = $total_top - $oldstart;
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$empty_array = array();
|
|
|
|
foreach ( $elements as $e ) {
|
2023-09-09 05:28:26 -04:00
|
|
|
++$count;
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $count < $start ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
if ( $count >= $end ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
break;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->display_element( $e, $empty_array, 1, 0, $args, $output );
|
|
|
|
}
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-09-28 17:29:10 -04: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 03:02:06 -04:00
|
|
|
*/
|
|
|
|
$top_level_elements = array();
|
|
|
|
$children_elements = array();
|
2017-11-30 18:11:00 -05: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 15:09:00 -04:00
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$top_level_elements[] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2010-10-30 03:02:06 -04:00
|
|
|
$children_elements[ $e->$parent_field ][] = $e;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
$total_top = count( $top_level_elements );
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $paging ) {
|
2024-02-17 10:24:08 -05:00
|
|
|
$this->max_pages = (int) ceil( $total_top / $per_page );
|
2017-11-30 18:11:00 -05:00
|
|
|
} else {
|
2010-10-30 03:02:06 -04:00
|
|
|
$end = $total_top;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! empty( $args[0]['reverse_top_level'] ) ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$top_level_elements = array_reverse( $top_level_elements );
|
2017-11-30 18:11:00 -05:00
|
|
|
$oldstart = $start;
|
|
|
|
$start = $total_top - $end;
|
|
|
|
$end = $total_top - $oldstart;
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! empty( $args[0]['reverse_children'] ) ) {
|
|
|
|
foreach ( $children_elements as $parent => $children ) {
|
|
|
|
$children_elements[ $parent ] = array_reverse( $children );
|
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $top_level_elements as $e ) {
|
2023-09-09 05:28:26 -04:00
|
|
|
++$count;
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2013-09-28 17:29:10 -04:00
|
|
|
// For the last page, need to unset earlier children in order to keep track of orphans.
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $end >= $total_top && $count < $start ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->unset_children( $e, $children_elements );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $count < $start ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
continue;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( $count >= $end ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
break;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $end >= $total_top && count( $children_elements ) > 0 ) {
|
|
|
|
$empty_array = array();
|
2017-11-30 18:11:00 -05:00
|
|
|
foreach ( $children_elements as $orphans ) {
|
|
|
|
foreach ( $orphans as $op ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->display_element( $op, $empty_array, 1, 0, $args, $output );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:59:25 -04:00
|
|
|
/**
|
2015-09-02 15:51:21 -04:00
|
|
|
* Calculates the total number of root elements.
|
2015-05-22 13:59:25 -04:00
|
|
|
*
|
2015-09-02 15:51:21 -04:00
|
|
|
* @since 2.7.0
|
2016-05-13 14:41:31 -04:00
|
|
|
*
|
2015-09-14 11:33:27 -04:00
|
|
|
* @param array $elements Elements to list.
|
|
|
|
* @return int Number of root elements.
|
2015-05-22 13:59:25 -04:00
|
|
|
*/
|
2017-11-30 18:11:00 -05:00
|
|
|
public function get_number_of_root_elements( $elements ) {
|
|
|
|
$num = 0;
|
2010-10-30 03:02:06 -04:00
|
|
|
$parent_field = $this->db_fields['parent'];
|
|
|
|
|
2017-11-30 18:11:00 -05: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 15:09:00 -04:00
|
|
|
if ( empty( $e->$parent_field ) ) {
|
2023-09-09 05:28:26 -04:00
|
|
|
++$num;
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
|
|
|
return $num;
|
|
|
|
}
|
|
|
|
|
2015-05-22 13:59:25 -04:00
|
|
|
/**
|
2022-05-01 17:41:12 -04:00
|
|
|
* Unsets all the children for a given top level element.
|
2015-05-22 13:59:25 -04:00
|
|
|
*
|
2017-01-06 17:14:00 -05:00
|
|
|
* @since 2.7.0
|
|
|
|
*
|
2022-05-01 17:47:10 -04:00
|
|
|
* @param object $element The top level element.
|
|
|
|
* @param array $children_elements The children elements.
|
2015-05-22 13:59:25 -04:00
|
|
|
*/
|
2022-05-01 17:47:10 -04:00
|
|
|
public function unset_children( $element, &$children_elements ) {
|
|
|
|
if ( ! $element || ! $children_elements ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
return;
|
2015-05-22 13:59:25 -04:00
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
|
|
|
$id_field = $this->db_fields['id'];
|
2022-05-01 17:47:10 -04:00
|
|
|
$id = $element->$id_field;
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2017-11-30 18:11:00 -05:00
|
|
|
if ( ! empty( $children_elements[ $id ] ) && is_array( $children_elements[ $id ] ) ) {
|
|
|
|
foreach ( (array) $children_elements[ $id ] as $child ) {
|
2010-10-30 03:02:06 -04:00
|
|
|
$this->unset_children( $child, $children_elements );
|
2017-11-30 18:11:00 -05:00
|
|
|
}
|
|
|
|
}
|
2010-10-30 03:02:06 -04:00
|
|
|
|
2015-05-22 01:47:25 -04:00
|
|
|
unset( $children_elements[ $id ] );
|
2010-10-30 03:02:06 -04:00
|
|
|
}
|
2020-01-28 19:45:18 -05:00
|
|
|
}
|