| @@ -1,8 +1,5 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | 3 | /** |
| 7 | 4 | * Displays languages in a dropdown list |
| 8 | 5 | * |
| @@ -8,15 +5,8 @@ | ||
| 8 | 5 | * |
| 9 | 6 | * @since 1.2 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Walker_Dropdown extends Walker { |
| 12 | - /** | |
| 13 | - * Database fields to use. | |
| 14 | - * | |
| 15 | - * @see https://developer.wordpress.org/reference/classes/walker/#properties Walker::$db_fields. | |
| 16 | - * | |
| 17 | - * @var array | |
| 18 | - */ | |
| 19 | 9 | public $db_fields = array( 'parent' => 'parent', 'id' => 'id' ); |
| 20 | 10 | |
| 21 | 11 | /** |
| 22 | 12 | * Outputs one element |
| @@ -22,22 +12,21 @@ | ||
| 22 | 12 | * Outputs one element |
| 23 | 13 | * |
| 24 | 14 | * @since 1.2 |
| 25 | 15 | * |
| 26 | - * @param string $output Passed by reference. Used to append additional content. | |
| 27 | - * @param stdClass $element The data object. | |
| 28 | - * @param int $depth Depth of the item. | |
| 29 | - * @param array $args An array of additional arguments. | |
| 30 | - * @param int $current_object_id ID of the current item. | |
| 31 | - * @return void | |
| 16 | + * @param string $output Passed by reference. Used to append additional content. | |
| 17 | + * @param object $element The data object. | |
| 18 | + * @param int $depth Depth of the item. | |
| 19 | + * @param array $args An array of additional arguments. | |
| 20 | + * @param int $current_object_id ID of the current item. | |
| 32 | 21 | */ |
| 33 | - public function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 22 | + public function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { | |
| 34 | 23 | $value = $args['value']; |
| 35 | 24 | $output .= sprintf( |
| 36 | 25 | "\t" . '<option value="%1$s"%2$s%3$s>%4$s</option>' . "\n", |
| 37 | 26 | esc_attr( $element->$value ), |
| 38 | 27 | method_exists( $element, 'get_locale' ) ? sprintf( ' lang="%s"', esc_attr( $element->get_locale( 'display' ) ) ) : '', |
| 39 | - selected( isset( $args['selected'] ) && $args['selected'] === $element->$value, true, false ), | |
| 28 | + isset( $args['selected'] ) && $args['selected'] === $element->$value ? ' selected="selected"' : '', | |
| 40 | 29 | esc_html( $element->name ) |
| 41 | 30 | ); |
| 42 | 31 | } |
| 43 | 32 | |
| @@ -45,17 +34,16 @@ | ||
| 45 | 34 | * Overrides Walker::display_element as expects an object with a parent property |
| 46 | 35 | * |
| 47 | 36 | * @since 1.2 |
| 48 | 37 | * |
| 49 | - * @param stdClass $element Data object. | |
| 50 | - * @param array $children_elements List of elements to continue traversing. | |
| 51 | - * @param int $max_depth Max depth to traverse. | |
| 52 | - * @param int $depth Depth of current element. | |
| 53 | - * @param array $args An array of arguments. | |
| 54 | - * @param string $output Passed by reference. Used to append additional content. | |
| 55 | - * @return void | |
| 38 | + * @param object $element Data object. | |
| 39 | + * @param array $children_elements List of elements to continue traversing. | |
| 40 | + * @param int $max_depth Max depth to traverse. | |
| 41 | + * @param int $depth Depth of current element. | |
| 42 | + * @param array $args An array of arguments. | |
| 43 | + * @param string $output Passed by reference. Used to append additional content. | |
| 56 | 44 | */ |
| 57 | - public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { | |
| 45 | + public function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) { | |
| 58 | 46 | $element = (object) $element; // Make sure we have an object |
| 59 | 47 | $element->parent = $element->id = 0; // Don't care about this |
| 60 | 48 | parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 61 | 49 | } |
| @@ -63,9 +51,8 @@ | ||
| 63 | 51 | /** |
| 64 | 52 | * Starts the output of the dropdown list |
| 65 | 53 | * |
| 66 | 54 | * @since 1.2 |
| 67 | - * @since 2.6.7 Use $max_depth and ...$args parameters to follow the move of WP 5.3 | |
| 68 | 55 | * |
| 69 | 56 | * List of parameters accepted in $args: |
| 70 | 57 | * |
| 71 | 58 | * flag => display the selected language flag in front of the dropdown if set to 1, defaults to 0 |
| @@ -75,32 +62,14 @@ | ||
| 75 | 62 | * id => the select id attribute, defaults to $args['name'] |
| 76 | 63 | * class => the class attribute |
| 77 | 64 | * disabled => disables the dropdown if set to 1 |
| 78 | 65 | * |
| 79 | - * @param array $elements An array of elements. | |
| 80 | - * @param int $max_depth The maximum hierarchical depth. | |
| 81 | - * @param mixed ...$args Additional arguments. | |
| 82 | - * @return string The hierarchical item output. | |
| 66 | + * @param array $elements elements to display | |
| 67 | + * @param array $args | |
| 68 | + * @return string | |
| 83 | 69 | */ |
| 84 | - public function walk( $elements, $max_depth, ...$args ) { // // phpcs:ignore WordPressVIPMinimum.Classes.DeclarationCompatibility.DeclarationCompatibility | |
| 70 | + public function walk( $elements, $args = array() ) { | |
| 85 | 71 | $output = ''; |
| 86 | - | |
| 87 | - if ( is_array( $max_depth ) ) { // @phpstan-ignore-line | |
| 88 | - // Backward compatibility with Polylang < 2.6.7 | |
| 89 | - if ( WP_DEBUG ) { | |
| 90 | - trigger_error( // phpcs:ignore WordPress.PHP.DevelopmentFunctions | |
| 91 | - sprintf( | |
| 92 | - '%s was called incorrectly. The method expects an integer as second parameter since Polylang 2.6.7', | |
| 93 | - __METHOD__ | |
| 94 | - ) | |
| 95 | - ); | |
| 96 | - } | |
| 97 | - $args = $max_depth; | |
| 98 | - $max_depth = -1; | |
| 99 | - } else { | |
| 100 | - $args = isset( $args[0] ) ? $args[0] : array(); | |
| 101 | - } | |
| 102 | - | |
| 103 | 72 | $args = wp_parse_args( $args, array( 'value' => 'slug', 'name' => 'lang_choice' ) ); |
| 104 | 73 | |
| 105 | 74 | if ( ! empty( $args['flag'] ) ) { |
| 106 | 75 | $current = wp_list_filter( $elements, array( $args['value'] => $args['selected'] ) ); |
| @@ -112,13 +81,13 @@ | ||
| 112 | 81 | } |
| 113 | 82 | |
| 114 | 83 | $output .= sprintf( |
| 115 | 84 | '<select name="%1$s"%2$s%3$s%4$s>' . "\n" . '%5$s' . "\n" . '</select>' . "\n", |
| 116 | - esc_attr( $args['name'] ), | |
| 117 | - isset( $args['id'] ) && ! $args['id'] ? '' : ' id="' . ( empty( $args['id'] ) ? esc_attr( $args['name'] ) : esc_attr( $args['id'] ) ) . '"', | |
| 85 | + $name = esc_attr( $args['name'] ), | |
| 86 | + isset( $args['id'] ) && ! $args['id'] ? '' : ' id="' . ( empty( $args['id'] ) ? $name : esc_attr( $args['id'] ) ) . '"', | |
| 118 | 87 | empty( $args['class'] ) ? '' : ' class="' . esc_attr( $args['class'] ) . '"', |
| 119 | - disabled( empty( $args['disabled'] ), false, false ), | |
| 120 | - parent::walk( $elements, $max_depth, $args ) | |
| 88 | + empty( $args['disabled'] ) ? '' : ' disabled="disabled"', | |
| 89 | + parent::walk( $elements, -1, $args ) | |
| 121 | 90 | ); |
| 122 | 91 | |
| 123 | 92 | return $output; |
| 124 | 93 | } |