| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* A class for displaying various tree-like language structures. |
| 8 |
* |
| 9 |
* Extend the `PLL_Walker` class to use it, and implement some of the methods from `Walker`. |
| 10 |
* See: {https://developer.wordpress.org/reference/classes/walker/#methods}. |
| 11 |
* |
| 12 |
* @since 3.4 |
| 13 |
*/ |
| 14 |
class PLL_Walker extends Walker { |
| 15 |
/** |
| 16 |
* Database fields to use. |
| 17 |
* |
| 18 |
* @see https://developer.wordpress.org/reference/classes/walker/#properties Walker::$db_fields. |
| 19 |
* |
| 20 |
* @var string[] |
| 21 |
*/ |
| 22 |
public $db_fields = array( 'parent' => 'parent', 'id' => 'id' ); |
| 23 |
|
| 24 |
/** |
| 25 |
* Overrides Walker::display_element as it expects an object with a parent property. |
| 26 |
* |
| 27 |
* @since 1.2 |
| 28 |
* @since 3.4 Refactored and moved in `PLL_Walker`. |
| 29 |
* |
| 30 |
* @param PLL_Language|stdClass $element Data object. `PLL_language` in our case. |
| 31 |
* @param array $children_elements List of elements to continue traversing. |
| 32 |
* @param int $max_depth Max depth to traverse. |
| 33 |
* @param int $depth Depth of current element. |
| 34 |
* @param array $args An array of arguments. |
| 35 |
* @param string $output Passed by reference. Used to append additional content. |
| 36 |
* @return void |
| 37 |
*/ |
| 38 |
public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
| 39 |
if ( $element instanceof PLL_Language ) { |
| 40 |
$element = $element->to_std_class(); |
| 41 |
|
| 42 |
// Sets the w3c locale as the main locale. |
| 43 |
$element->locale = $element->w3c ?? $element->locale; |
| 44 |
} |
| 45 |
|
| 46 |
// Don't care about this. |
| 47 |
$element->id = 0; |
| 48 |
$element->parent = 0; |
| 49 |
|
| 50 |
parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Sets `PLL_Walker::walk()` arguments as it should |
| 55 |
* and triggers an error in case of misuse of them. |
| 56 |
* |
| 57 |
* @since 3.4 |
| 58 |
* |
| 59 |
* @param array|int $max_depth The maximum hierarchical depth. Passed by reference. |
| 60 |
* @param array $args Additional arguments. Passed by reference. |
| 61 |
* @return void |
| 62 |
*/ |
| 63 |
protected function maybe_fix_walk_args( &$max_depth, &$args ) { |
| 64 |
if ( ! is_array( $max_depth ) ) { |
| 65 |
$args = $args[0] ?? array(); |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
// Backward compatibility with Polylang < 2.6.7 |
| 70 |
_doing_it_wrong( |
| 71 |
self::class . '::walk()', |
| 72 |
'The method expects an integer as second parameter.', |
| 73 |
'2.6.7' |
| 74 |
); |
| 75 |
$args = $max_depth; |
| 76 |
$max_depth = -1; |
| 77 |
} |
| 78 |
} |
| 79 |
|