-
Notifications
You must be signed in to change notification settings - Fork 50
/
widget.php
157 lines (131 loc) · 5.3 KB
/
widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
add_action( 'widgets_init', 'babble_widget' );
function babble_widget() {
register_widget( 'Babble_Widget' );
}
class Babble_Widget extends WP_Widget {
public $defaults = array(
'show_if_unavailable' => 'off',
'show_as' => 'dropdown',
);
function __construct() {
parent::__construct(
'bbl_widget', // Base ID
__('Language Switcher','babble'), // Name
array( 'description' => __('Displays a list of links to translations / equivalent pages.', 'babble') ) // Args
);
}
function widget( $args, $instance ) {
$args = array_merge( array(
'show_as' => 'dropdown',
), $args );
echo $args['before_widget'];
echo $args['before_title'] . __( 'Languages', 'babble' ) . $args['after_title'];
$list = bbl_get_switcher_links();
switch ( $instance['show_as'] ) {
case 'dropdown':
echo '<select onchange="document.location.href=this.options[this.selectedIndex].value;">';
foreach ( $list as $item ) :
if ( $item['active'] ) {
$selected = 'selected="selected" ';
} else {
$selected = '';
}
if ( in_array( 'bbl-add',$item['classes'] ) ) {
/*
We're logged in, there's no translation
of this page as yet, but the user has the
ability to create one; so here's a link
to allow him/her to do so
*/
echo '<option ' . $selected . 'class="' . esc_attr( $item[ 'class' ] ) . '" value="' . esc_url( $item[ 'href' ] ) . '">' . esc_html( $item[ 'lang' ]->display_name ) . ' [' . __('Add') . ']</option>';
}
elseif ( $item[ 'href'] ) {
/*
Means there is a translation of this page
into the language in question
*/
echo '<option ' . $selected . 'class="' . esc_attr( $item[ 'class' ] ) . '" value="' . esc_url( $item[ 'href' ] ) . '">' . esc_html( $item[ 'lang' ]->display_name ) . '</option>';
}
elseif ( 'on' === $instance['show_if_unavailable'] ) {
/*
We're on the front end, but there is
no translation into this language as yet;
the user has said 'Show if unavailable'
in the widget management screen.
Applies a no-translation class to the <li>,
and a bbl-no-translation class to the <div>
in case you want to 'grey it out' somehow
*/
echo '<option disabled class="' . esc_attr( $item[ 'class' ] ) . '" value="">' . esc_html( $item[ 'lang' ]->display_name ) . '</option>';
}
endforeach;
echo '</select>';
break;
case 'list':
echo '<ul class="languages_list">';
foreach ( $list as $item ) :
if ( in_array( 'bbl-add',$item['classes'] ) ) {
/*
We're logged in, there's no translation
of this page as yet, but the user has the
ability to create one; so here's a link
to allow him/her to do so
*/
echo '<li><a href="' . esc_url( $item[ 'href' ] ) . '" class="' . esc_attr( $item[ 'class' ] ) . '">' . esc_html( $item[ 'lang' ]->display_name ) . ' [' . __( 'Add', 'babble' ) . ']</a></li>';
}
elseif ( $item[ 'href'] ) {
/*
Means there is a translation of this page
into the language in question
*/
echo '<li><a href="' . esc_url( $item[ 'href' ] ) . '" class="' . esc_attr( $item[ 'class' ] ) . '">' . esc_html( $item[ 'lang' ]->display_name ) . '</a></li>';
}
elseif ( 'on' === $instance['show_if_unavailable'] ) {
/*
We're on the front end, but there is
no translation into this language as yet;
the user has said 'Show if unavailable'
in the widget management screen.
Applies a no-translation class to the <li>,
and a bbl-no-translation class to the <div>
in case you want to 'grey it out' somehow
*/
echo '<li class="no-translation"><div class="bbl-no-translation">' . esc_html( $item[ 'lang' ]->display_name ) . '</div></li>';
}
endforeach;
echo '</ul>';
break;
}
echo $args['after_widget'];
}
function update( $new_instance, $old_instance ) {
$new_instance = array_merge( $this->defaults, $new_instance );
$new_instance['show_as'] = strip_tags( $new_instance['show_as'] );
$new_instance['show_if_unavailable'] = strip_tags( $new_instance['show_if_unavailable'] );
return $new_instance;
}
function form( $instance ) {
global $wpdb;
$instance = wp_parse_args( $instance, $this->defaults );
?>
<p>
<?php _e('Show as:','babble'); ?>
<select id="<?php echo $this->get_field_id('show_as'); ?>" name="<?php echo $this->get_field_name('show_as'); ?>">
<option value="dropdown" <?php selected( $instance['show_as'],'dropdown' ); ?>><?php _e('Dropdown','babble'); ?></option>
<option value="list" <?php selected( $instance['show_as'],'list' ); ?>><?php _e('List','babble'); ?></option>
</select>
</p>
<p>
<input id="<?php echo $this->get_field_id('show_if_unavailable'); ?>" name="<?php echo $this->get_field_name('show_if_unavailable'); ?>" type="checkbox" <?php checked( 'on', $instance['show_if_unavailable'] ); ?> />
<label for="<?php echo $this->get_field_id('show_if_unavailable'); ?>"><?php _e('Show all languages in widget, even if there is no translation', 'babble'); ?></label>
</p>
<p class="description">
<?php _e("Don't worry: if there's no equivalent page, the link won't be clickable.","babble"); ?>
</p>
<p class="description">
<?php _e("Links allowing logged-in administrators to add translations will always be shown.","babble"); ?>
</p>
<?php
}
}