PluginProbe
Polylang / 2.1.1
Polylang v2.1.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
polylang / include / widget-languages.php

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

158 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * The language switcher widget
5 *
6 * @since 0.1
7 */
8 class PLL_Widget_Languages extends WP_Widget {
9
10 /**
11 * Constructor
12 *
13 * @since 0.1
14 */
15 function __construct() {
16 parent::__construct(
17 'polylang',
18 __( 'Language Switcher', 'polylang' ),
19 array(
20 'description' => __( 'Displays a language switcher', 'polylang' ),
21 'customize_selective_refresh' => true,
22 )
23 );
24 }
25
26 /**
27 * Displays the widget
28 *
29 * @since 0.1
30 *
31 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
32 * @param array $instance The settings for the particular instance of the widget
33 */
34 function widget( $args, $instance ) {
35 // Sets a unique id for dropdown
36 $instance['dropdown'] = empty( $instance['dropdown'] ) ? 0 : $args['widget_id'];
37
38 if ( $list = pll_the_languages( array_merge( $instance, array( 'echo' => 0 ) ) ) ) {
39 $title = empty( $instance['title'] ) ? '' : $instance['title'];
40 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
41 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
42
43 echo $args['before_widget'];
44 if ( $title ) {
45 echo $args['before_title'] . $title . $args['after_title'];
46 }
47 if ( $instance['dropdown'] ) {
48 echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html__( 'Choose a language', 'polylang' ). '</label>';
49 echo $list;
50 } else {
51 echo "<ul>\n" . $list . "</ul>\n";
52 }
53 echo $args['after_widget'];
54 }
55 }
56
57 /**
58 * Updates the widget options
59 *
60 * @since 0.4
61 *
62 * @param array $new_instance New settings for this instance as input by the user via form()
63 * @param array $old_instance Old settings for this instance
64 * @return array Settings to save or bool false to cancel saving
65 */
66 function update( $new_instance, $old_instance ) {
67 $instance['title'] = strip_tags( $new_instance['title'] );
68 foreach ( array_keys( PLL_Switcher::get_switcher_options( 'widget' ) ) as $key ) {
69 $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0;
70 }
71
72 return $instance;
73 }
74
75 /**
76 * Displays the widget form
77 *
78 * @since 0.4
79 *
80 * @param array $instance Current settings
81 */
82 function form( $instance ) {
83 // Default values
84 $instance = wp_parse_args( (array) $instance, array_merge( array( 'title' => '' ), PLL_Switcher::get_switcher_options( 'widget', 'default' ) ) );
85
86 // Title
87 printf(
88 '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>',
89 $this->get_field_id( 'title' ),
90 esc_html__( 'Title:', 'polylang' ),
91 $this->get_field_name( 'title' ),
92 esc_attr( $instance['title'] )
93 );
94
95 $fields = '';
96 foreach ( PLL_Switcher::get_switcher_options( 'widget' ) as $key => $str ) {
97 $fields .= sprintf(
98 '<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>',
99 $this->get_field_id( $key ),
100 $this->get_field_name( $key ),
101 $instance[ $key ] ? ' checked="checked"' : '',
102 esc_html( $str ),
103 in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' class="no-dropdown-' . $this->id . '"' : '',
104 ! empty( $instance['dropdown'] ) && in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' style="display:none;"' : '',
105 'pll-' . $key
106 );
107 }
108
109 echo $fields;
110
111 // FIXME echoing script in form is not very clean
112 // but it does not work if enqueued properly :
113 // clicking save on a widget makes this code unreachable for the just saved widget ( ?! )
114 $this->admin_print_script();
115 }
116
117 /**
118 * Add javascript to control the language switcher options
119 *
120 * @since 1.3
121 */
122 public function admin_print_script() {
123 static $done = false;
124
125 if ( $done ) {
126 return;
127 }
128
129 $done = true; ?>
130 <script type='text/javascript'>
131 //<![CDATA[
132 jQuery( document ).ready( function( $ ) {
133 function pll_toggle( a, test ) {
134 test ? a.show() : a.hide();
135 }
136
137 // Remove all options if dropdown is checked
138 $( '.widgets-sortables,.control-section-sidebar' ).on( 'change', '.pll-dropdown', function() {
139 var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' );
140 pll_toggle( $( '.no-dropdown-' + this_id ), 'checked' != $( this ).attr( 'checked' ) );
141 } );
142
143 // Disallow unchecking both show names and show flags
144 var options = ['-show_flags', '-show_names'];
145 $.each( options, function( i, v ) {
146 $( '.widgets-sortables,.control-section-sidebar' ).on( 'change', '.pll' + v, function() {
147 var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' );
148 if ( 'checked' != $( this ).attr( 'checked' ) ) {
149 $( '#widget-' + this_id + options[ 1-i ] ).prop( 'checked', true );
150 }
151 } );
152 } );
153 } );
154 //]]>
155 </script><?php
156 }
157 }
158