PluginProbe
Polylang / 3.4.5
Polylang v3.4.5
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
polylang / include / walker-dropdown.php

walker-dropdown.php in Polylang 3.4.5, at include/walker-dropdown.php

96 lines 3.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * @package Polylang
4 */
5
6 /**
7 * Displays languages in a dropdown list
8 *
9 * @since 1.2
10 * @since 3.4 Extends `PLL_Walker` now.
11 */
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 */
20 public $db_fields = array( 'parent' => 'parent', 'id' => 'id' );
21
22 /**
23 * Outputs one element.
24 *
25 * @since 1.2
26 *
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
33 */
34 public function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
35 $value_type = $args['value'];
36 $output .= sprintf(
37 "\t" . '<option value="%1$s"%2$s%3$s>%4$s</option>' . "\n",
38 'url' === $value_type ? esc_url( $element->$value_type ) : esc_attr( $element->$value_type ),
39 ! empty( $element->locale ) ? sprintf( ' lang="%s"', esc_attr( $element->locale ) ) : '',
40 selected( isset( $args['selected'] ) && $args['selected'] === $element->$value_type, true, false ),
41 esc_html( $element->name )
42 );
43 }
44
45 /**
46 * Starts the output of the dropdown list
47 *
48 * @since 1.2
49 * @since 2.6.7 Use $max_depth and ...$args parameters to follow the move of WP 5.3
50 *
51 * List of parameters accepted in $args:
52 *
53 * flag => display the selected language flag in front of the dropdown if set to 1, defaults to 0
54 * value => the language field to use as value attribute, defaults to 'slug'
55 * selected => the selected value, mandatory
56 * name => the select name attribute, defaults to 'lang_choice'
57 * id => the select id attribute, defaults to $args['name']
58 * class => the class attribute
59 * disabled => disables the dropdown if set to 1
60 *
61 * @param array $elements An array of `PLL_language` or `stdClass` elements.
62 * @param int $max_depth The maximum hierarchical depth.
63 * @param mixed ...$args Additional arguments.
64 * @return string The hierarchical item output.
65 *
66 * @phpstan-param array<PLL_Language|stdClass> $elements
67 */
68 public function walk( $elements, $max_depth, ...$args ) { // // phpcs:ignore WordPressVIPMinimum.Classes.DeclarationCompatibility.DeclarationCompatibility
69 $output = '';
70
71 $this->maybe_fix_walk_args( $max_depth, $args );
72
73 $args = wp_parse_args( $args, array( 'value' => 'slug', 'name' => 'lang_choice' ) );
74
75 if ( ! empty( $args['flag'] ) ) {
76 $current = wp_list_filter( $elements, array( $args['value'] => $args['selected'] ) );
77 $lang = reset( $current );
78 $output = sprintf(
79 '<span class="pll-select-flag">%s</span>',
80 empty( $lang->flag ) ? esc_html( $lang->slug ) : $lang->flag
81 );
82 }
83
84 $output .= sprintf(
85 '<select name="%1$s"%2$s%3$s%4$s>' . "\n" . '%5$s' . "\n" . '</select>' . "\n",
86 esc_attr( $args['name'] ),
87 isset( $args['id'] ) && ! $args['id'] ? '' : ' id="' . ( empty( $args['id'] ) ? esc_attr( $args['name'] ) : esc_attr( $args['id'] ) ) . '"',
88 empty( $args['class'] ) ? '' : ' class="' . esc_attr( $args['class'] ) . '"',
89 disabled( empty( $args['disabled'] ), false, false ),
90 parent::walk( $elements, $max_depth, $args )
91 );
92
93 return $output;
94 }
95 }
96