| 1 |
<?php |
| 2 |
|
| 3 |
/* Language Switcher Widget */ |
| 4 |
|
| 5 |
add_action( 'widgets_init', 'bogo_widgets_init' ); |
| 6 |
|
| 7 |
function bogo_widgets_init() { |
| 8 |
register_widget( 'Bogo_Widget_Language_Switcher' ); |
| 9 |
} |
| 10 |
|
| 11 |
class Bogo_Widget_Language_Switcher extends WP_Widget { |
| 12 |
|
| 13 |
function __construct() { |
| 14 |
$widget_ops = array( |
| 15 |
'description' => __( 'Language switcher widget by Bogo plugin', 'bogo' ) ); |
| 16 |
|
| 17 |
$control_ops = array(); |
| 18 |
|
| 19 |
WP_Widget::__construct( 'bogo_language_switcher', |
| 20 |
__( 'Language Switcher', 'bogo' ), |
| 21 |
$widget_ops, $control_ops ); |
| 22 |
} |
| 23 |
|
| 24 |
function widget( $args, $instance ) { |
| 25 |
extract( $args ); |
| 26 |
|
| 27 |
$title = apply_filters( 'widget_title', |
| 28 |
empty( $instance['title'] ) ? __( 'Language Switcher', 'bogo' ) : $instance['title'], |
| 29 |
$instance, $this->id_base ); |
| 30 |
|
| 31 |
echo $before_widget; |
| 32 |
|
| 33 |
if ( $title ) |
| 34 |
echo $before_title . $title . $after_title; |
| 35 |
|
| 36 |
echo bogo_language_switcher(); |
| 37 |
|
| 38 |
echo $after_widget; |
| 39 |
} |
| 40 |
|
| 41 |
function form( $instance ) { |
| 42 |
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
| 43 |
$title = strip_tags( $instance['title'] ); |
| 44 |
|
| 45 |
echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . esc_html( __( 'Title:', 'bogo' ) ) . '</label> <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . esc_attr( $title ) . '" /></p>'; |
| 46 |
} |
| 47 |
|
| 48 |
function update( $new_instance, $old_instance ) { |
| 49 |
$instance = $old_instance; |
| 50 |
$new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '' ) ); |
| 51 |
$instance['title'] = strip_tags( $new_instance['title'] ); |
| 52 |
|
| 53 |
return $instance; |
| 54 |
} |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
/* Locale Option */ |
| 59 |
|
| 60 |
add_filter( 'widget_display_callback', 'bogo_widget_display_callback', 10, 3 ); |
| 61 |
|
| 62 |
function bogo_widget_display_callback( $instance, $widget, $args ) { |
| 63 |
if ( isset( $instance['bogo_locales'] ) ) { |
| 64 |
$locale = get_locale(); |
| 65 |
|
| 66 |
if ( ! in_array( $locale, (array) $instance['bogo_locales'] ) ) |
| 67 |
$instance = false; |
| 68 |
} |
| 69 |
|
| 70 |
return $instance; |
| 71 |
} |
| 72 |
|
| 73 |
add_action( 'in_widget_form', 'bogo_in_widget_form', 10, 3 ); |
| 74 |
|
| 75 |
function bogo_in_widget_form( $widget, $return, $instance ) { |
| 76 |
$available_languages = bogo_available_languages( 'orderby=value' ); |
| 77 |
|
| 78 |
$selected_languages = ! isset( $instance['bogo_locales'] ) |
| 79 |
? array_keys( $available_languages ) : (array) $instance['bogo_locales']; |
| 80 |
|
| 81 |
?> |
| 82 |
<fieldset style="padding: .4em 1em .6em; border: 1px solid #ccc;"> |
| 83 |
<legend style="padding: 0 1em;"><?php echo esc_html( __( 'Displayed on pages in:', 'bogo' ) ); ?></legend> |
| 84 |
<?php foreach ( $available_languages as $locale => $language ) : ?> |
| 85 |
<input type="checkbox" id="<?php echo $widget->get_field_id( 'bogo_locales' ); ?>" name="<?php echo $widget->get_field_name( 'bogo_locales' ); ?>[]" value="<?php echo esc_attr( $locale ); ?>"<?php echo in_array( $locale, $selected_languages ) ? ' checked="checked"' : ''; ?> /> |
| 86 |
<?php echo esc_html( $language ); ?><br /> |
| 87 |
<?php endforeach; ?> |
| 88 |
</fieldset> |
| 89 |
<?php |
| 90 |
|
| 91 |
$return = null; |
| 92 |
} |
| 93 |
|
| 94 |
add_filter( 'widget_update_callback', 'bogo_widget_update_callback', 10, 4 ); |
| 95 |
|
| 96 |
function bogo_widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { |
| 97 |
if ( isset( $new_instance['bogo_locales'] ) && is_array( $new_instance['bogo_locales'] ) ) |
| 98 |
$instance['bogo_locales'] = $new_instance['bogo_locales']; |
| 99 |
else |
| 100 |
$instance['bogo_locales'] = array(); |
| 101 |
|
| 102 |
return $instance; |
| 103 |
} |
| 104 |
|
| 105 |
?> |