PluginProbe
Polylang / 3.7.1
Polylang v3.7.1
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
← All changes | include/walker-dropdown.php +24 -44 3.0.33.7.1 View file →
@@ -6,21 +6,22 @@
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 {
12 13 /**
13 14 * Database fields to use.
14 15 *
15 16 * @see https://developer.wordpress.org/reference/classes/walker/#properties Walker::$db_fields.
16 17 *
17 - * @var array
18 + * @var string[]
18 19 */
19 20 public $db_fields = array( 'parent' => 'parent', 'id' => 'id' );
20 21
21 22 /**
22 - * Outputs one element
23 + * Outputs one element.
23 24 *
24 25 * @since 1.2
25 26 *
26 27 * @param string $output Passed by reference. Used to append additional content.
@@ -30,38 +31,29 @@
30 31 * @param int $current_object_id ID of the current item.
31 32 * @return void
32 33 */
33 34 public function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
34 - $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 +
35 45 $output .= sprintf(
36 - "\t" . '<option value="%1$s"%2$s%3$s>%4$s</option>' . "\n",
37 - esc_attr( $element->$value ),
38 - 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 ),
40 - 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 ) )
41 52 );
42 53 }
43 54
44 55 /**
45 - * Overrides Walker::display_element as expects an object with a parent property
46 - *
47 - * @since 1.2
48 - *
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
56 - */
57 - public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) {
58 - $element = (object) $element; // Make sure we have an object
59 - $element->parent = $element->id = 0; // Don't care about this
60 - parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
61 - }
62 -
63 - /**
64 56 * Starts the output of the dropdown list
65 57 *
66 58 * @since 1.2
67 59 * @since 2.6.7 Use $max_depth and ...$args parameters to follow the move of WP 5.3
@@ -75,31 +67,19 @@
75 67 * id => the select id attribute, defaults to $args['name']
76 68 * class => the class attribute
77 69 * disabled => disables the dropdown if set to 1
78 70 *
79 - * @param array $elements An array of elements.
71 + * @param array $elements An array of `PLL_language` or `stdClass` elements.
80 72 * @param int $max_depth The maximum hierarchical depth.
81 73 * @param mixed ...$args Additional arguments.
82 74 * @return string The hierarchical item output.
75 + *
76 + * @phpstan-param array<PLL_Language|stdClass> $elements
83 77 */
84 78 public function walk( $elements, $max_depth, ...$args ) { // // phpcs:ignore WordPressVIPMinimum.Classes.DeclarationCompatibility.DeclarationCompatibility
85 79 $output = '';
86 80
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 - }
81 + $this->maybe_fix_walk_args( $max_depth, $args );
102 82
103 83 $args = wp_parse_args( $args, array( 'value' => 'slug', 'name' => 'lang_choice' ) );
104 84
105 85 if ( ! empty( $args['flag'] ) ) {