PluginProbe
Polylang / 1.8
Polylang v1.8
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 1.8, at include/widget-languages.php

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