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