PluginProbe
Polylang / 3.4
Polylang v3.4
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 / widget-languages.php

widget-languages.php in Polylang 3.4, at include/widget-languages.php

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