PluginProbe
Polylang / 3.8.5
Polylang v3.8.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 / src / widget-languages.php

widget-languages.php in Polylang 3.8.5, at src/widget-languages.php

175 lines 5.3 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 * The language switcher widget
8 *
9 * @since 0.1
10 *
11 * @extends WP_Widget<T>
12 * @phpstan-template T of array{
13 * title: string,
14 * dropdown: 0|1,
15 * show_names: 0|1,
16 * show_flags: 0|1,
17 * force_home: 0|1,
18 * hide_current: 0|1,
19 * hide_if_no_translation: 0|1
20 * }
21 */
22 class PLL_Widget_Languages extends WP_Widget {
23
24 /**
25 * Constructor
26 *
27 * @since 0.1
28 */
29 public function __construct() {
30 parent::__construct(
31 'polylang',
32 __( 'Language switcher', 'polylang' ),
33 array(
34 'description' => __( 'Displays a language switcher', 'polylang' ),
35 'customize_selective_refresh' => true,
36 )
37 );
38 }
39
40 /**
41 * Displays the widget
42 *
43 * @since 0.1
44 *
45 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
46 * @param array $instance The settings for the particular instance of the widget
47 * @return void
48 *
49 * @phpstan-param array{
50 * name: string,
51 * id: string,
52 * description: string,
53 * class: string,
54 * before_widget: string,
55 * after_widget: string,
56 * before_title: string,
57 * after_title: string,
58 * before_sidebar: string,
59 * after_sidebar: string,
60 * show_in_rest: boolean,
61 * widget_id: string,
62 * widget_name: string
63 * } $args
64 * @phpstan-param T $instance
65 */
66 public function widget( $args, $instance ) {
67 // Sets a unique id for dropdown.
68 $instance['dropdown'] = empty( $instance['dropdown'] ) ? 0 : $this->id;
69 $instance['echo'] = 0;
70 $instance['raw'] = 0;
71 $list = pll_the_languages( $instance );
72
73 if ( $list ) {
74 $title = empty( $instance['title'] ) ? '' : $instance['title'];
75
76 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
77 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
78
79 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
80
81 if ( $title ) {
82 echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
83 }
84
85 // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
86 $aria_label = trim( wp_strip_all_tags( $title ) );
87 if ( ! $aria_label ) {
88 $aria_label = __( 'Choose a language', 'polylang' );
89 }
90
91 if ( $instance['dropdown'] ) {
92 echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html( $aria_label ) . '</label>';
93 echo $list; // phpcs:ignore WordPress.Security.EscapeOutput
94 } else {
95 $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
96
97 /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
98 $format = apply_filters( 'navigation_widgets_format', $format );
99
100 if ( 'html5' === $format ) {
101 echo '<nav aria-label="' . esc_attr( $aria_label ) . '">';
102 }
103
104 echo "<ul>\n" . $list . "</ul>\n"; // phpcs:ignore WordPress.Security.EscapeOutput
105
106 if ( 'html5' === $format ) {
107 echo '</nav>';
108 }
109 }
110
111 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
112 }
113 }
114
115 /**
116 * Updates the widget options
117 *
118 * @since 0.4
119 *
120 * @param array $new_instance New settings for this instance as input by the user via form()
121 * @param array $old_instance Old settings for this instance
122 * @return array Settings to save or bool false to cancel saving
123 *
124 * @phpstan-param T $new_instance
125 * @phpstan-param T $old_instance
126 */
127 public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
128 $instance = array( 'title' => sanitize_text_field( $new_instance['title'] ) );
129 foreach ( array_keys( PLL_Switcher::get_switcher_options( 'widget' ) ) as $key ) {
130 $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0;
131 }
132
133 return $instance;
134 }
135
136 /**
137 * Displays the widget form.
138 *
139 * @since 0.4
140 *
141 * @param array $instance Current settings.
142 * @return string
143 *
144 * @phpstan-param T $instance
145 */
146 public function form( $instance ) {
147 // Default values
148 $instance = wp_parse_args( (array) $instance, array_merge( array( 'title' => '' ), PLL_Switcher::get_switcher_options( 'widget', 'default' ) ) );
149
150 // Title
151 printf(
152 '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>',
153 esc_attr( $this->get_field_id( 'title' ) ),
154 esc_html__( 'Title:', 'polylang' ),
155 esc_attr( $this->get_field_name( 'title' ) ),
156 esc_attr( $instance['title'] )
157 );
158
159 foreach ( PLL_Switcher::get_switcher_options( 'widget' ) as $key => $str ) {
160 printf(
161 '<div%5$s%6$s><input type="checkbox" class="checkbox %7$s" id="%1$s" name="%2$s"%3$s /><label for="%1$s">%4$s</label></div>',
162 esc_attr( $this->get_field_id( $key ) ),
163 esc_attr( $this->get_field_name( $key ) ),
164 checked( $instance[ $key ], true, false ),
165 esc_html( $str ),
166 in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? sprintf( ' class="no-dropdown-%s"', esc_attr( $this->id ) ) : '',
167 ( ! empty( $instance['dropdown'] ) && in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' style="display:none;"' : '' ),
168 esc_attr( 'pll-' . $key )
169 );
170 }
171
172 return ''; // Because the parent class returns a string, however not used.
173 }
174 }
175