| 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 |
global $polylang; |
| 29 |
if (!(isset($polylang) && $polylang->model->get_languages_list() && $list = pll_the_languages(array_merge($instance, array('echo' => 0))))) |
| 30 |
return; |
| 31 |
|
| 32 |
extract($args); |
| 33 |
extract($instance); |
| 34 |
|
| 35 |
echo "$before_widget\n"; |
| 36 |
if ($title = apply_filters('widget_title', $title, $instance, $this->id_base)) |
| 37 |
echo $before_title . $title . $after_title; |
| 38 |
echo $dropdown ? $list : "<ul>\n" . $list . "</ul>\n"; |
| 39 |
echo "$after_widget\n"; |
| 40 |
|
| 41 |
// javascript to switch the language when using a dropdown list |
| 42 |
if ($dropdown) { |
| 43 |
foreach ($polylang->model->get_languages_list() as $language) { |
| 44 |
$url = $force_home || ($url = $polylang->links->get_translation_url($language)) == null ? $polylang->links->get_home_url($language) : $url; |
| 45 |
$urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"'; |
| 46 |
} |
| 47 |
|
| 48 |
$urls = implode(',', $urls); |
| 49 |
|
| 50 |
$js = " |
| 51 |
<script type='text/javascript'> |
| 52 |
//<![CDATA[ |
| 53 |
var urls = {{$urls}}; |
| 54 |
var d = document.getElementById('lang_choice'); |
| 55 |
d.onchange = function() { |
| 56 |
for (var i in urls) { |
| 57 |
if (this.value == i) |
| 58 |
location.href = urls[i]; |
| 59 |
} |
| 60 |
} |
| 61 |
//]]> |
| 62 |
</script>"; |
| 63 |
|
| 64 |
echo $js; |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/* |
| 69 |
* updates the widget options |
| 70 |
* |
| 71 |
* @since 0.4 |
| 72 |
* |
| 73 |
* @param array $new_instance New settings for this instance as input by the user via form() |
| 74 |
* @param array $old_instance Old settings for this instance |
| 75 |
* @return array Settings to save or bool false to cancel saving |
| 76 |
*/ |
| 77 |
function update( $new_instance, $old_instance ) { |
| 78 |
$instance['title'] = strip_tags($new_instance['title']); |
| 79 |
foreach (array_keys(PLL_Switcher::get_switcher_options('widget')) as $key) |
| 80 |
$instance[$key] = !empty($new_instance[$key]) ? 1 : 0; |
| 81 |
|
| 82 |
return $instance; |
| 83 |
} |
| 84 |
|
| 85 |
/* |
| 86 |
* displays the widget form |
| 87 |
* |
| 88 |
* @since 0.4 |
| 89 |
* |
| 90 |
* @param array $instance Current settings |
| 91 |
*/ |
| 92 |
function form($instance) { |
| 93 |
// default values |
| 94 |
$instance = wp_parse_args( (array)$instance, array_merge(array('title' => ''), PLL_Switcher::get_switcher_options('widget', 'default')) ); |
| 95 |
|
| 96 |
// title |
| 97 |
$title = sprintf( |
| 98 |
'<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', |
| 99 |
$this->get_field_id('title'), |
| 100 |
__('Title:', 'polylang'), |
| 101 |
$this->get_field_name('title'), |
| 102 |
esc_attr($instance['title']) |
| 103 |
); |
| 104 |
|
| 105 |
$fields = ''; |
| 106 |
foreach (PLL_Switcher::get_switcher_options('widget') as $key => $str) |
| 107 |
$fields .= sprintf( |
| 108 |
'<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>', |
| 109 |
$this->get_field_id($key), |
| 110 |
$this->get_field_name($key), |
| 111 |
$instance[$key] ? 'checked="checked"' : '', |
| 112 |
esc_html($str), |
| 113 |
'dropdown' == $key ? '' : 'no-dropdown-' . $this->id, |
| 114 |
'dropdown' == $key || empty($instance['dropdown']) ? '' : 'style="display:none;"' |
| 115 |
); |
| 116 |
|
| 117 |
|
| 118 |
echo $title.'<p>'.$fields.'</p>'; |
| 119 |
|
| 120 |
// FIXME echoing script in form is not very clean |
| 121 |
// but it does not work if enqueued properly : |
| 122 |
// clicking save on a widget makes this code unreachable for the just saved widget (?!) |
| 123 |
$this->admin_print_script(); |
| 124 |
} |
| 125 |
|
| 126 |
/* |
| 127 |
* add javascript to control the language switcher options |
| 128 |
* |
| 129 |
* @since 1.3 |
| 130 |
*/ |
| 131 |
public function admin_print_script() { |
| 132 |
static $js = ''; |
| 133 |
|
| 134 |
if ($js) |
| 135 |
return; |
| 136 |
|
| 137 |
$js = " |
| 138 |
<script type='text/javascript'> |
| 139 |
//<![CDATA[ |
| 140 |
jQuery(document).ready(function($) { |
| 141 |
function pll_toggle(a, test) { |
| 142 |
test ? a.show() : a.hide(); |
| 143 |
} |
| 144 |
|
| 145 |
var widgets = new Array(); |
| 146 |
$('.widget-id').each( function(){ |
| 147 |
var this_id = $(this).attr('value'); |
| 148 |
|
| 149 |
// remove all options if dropdown is checked |
| 150 |
$('#widget-'+this_id+'-dropdown').change(function() { |
| 151 |
pll_toggle($('.no-dropdown-'+this_id), 'checked' != $(this).attr('checked')); |
| 152 |
}); |
| 153 |
|
| 154 |
// disallow unchecking both show names and show flags |
| 155 |
$('#widget-'+this_id+'-show_flags').change(function() { |
| 156 |
if ('checked' != $(this).attr('checked')) |
| 157 |
$('#widget-'+this_id+'-show_names').prop('checked', true); |
| 158 |
}); |
| 159 |
|
| 160 |
}); |
| 161 |
}); |
| 162 |
//]]> |
| 163 |
</script>"; |
| 164 |
|
| 165 |
echo $js; |
| 166 |
} |
| 167 |
} |
| 168 |
|