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