| 1 |
<?php |
| 2 |
|
| 3 |
/* Language Switcher Widget */ |
| 4 |
|
| 5 |
add_action( 'widgets_init', 'bogo_widgets_init', 10, 0 ); |
| 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 |
|
| 18 |
$control_ops = array(); |
| 19 |
|
| 20 |
WP_Widget::__construct( 'bogo_language_switcher', |
| 21 |
__( 'Language Switcher', 'bogo' ), |
| 22 |
$widget_ops, $control_ops |
| 23 |
); |
| 24 |
} |
| 25 |
|
| 26 |
function widget( $args, $instance ) { |
| 27 |
$title = apply_filters( 'widget_title', |
| 28 |
empty( $instance['title'] ) |
| 29 |
? __( 'Language Switcher', 'bogo' ) |
| 30 |
: $instance['title'], |
| 31 |
$instance, $this->id_base |
| 32 |
); |
| 33 |
|
| 34 |
echo wp_kses_post( $args['before_widget'] ); |
| 35 |
|
| 36 |
if ( $title ) { |
| 37 |
echo wp_kses_post( |
| 38 |
$args['before_title'] . $title . $args['after_title'] |
| 39 |
); |
| 40 |
} |
| 41 |
|
| 42 |
echo wp_kses_post( bogo_language_switcher() ); |
| 43 |
|
| 44 |
echo wp_kses_post( $args['after_widget'] ); |
| 45 |
} |
| 46 |
|
| 47 |
function form( $instance ) { |
| 48 |
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); |
| 49 |
$title = wp_strip_all_tags( $instance['title'] ); |
| 50 |
|
| 51 |
$html = sprintf( |
| 52 |
'<p><label %1$s>%2$s</label> <input %3$s /></p>', |
| 53 |
bogo_format_atts( array( |
| 54 |
'for' => $this->get_field_id( 'title' ), |
| 55 |
) ), |
| 56 |
__( 'Title:', 'bogo' ), |
| 57 |
bogo_format_atts( array( |
| 58 |
'class' => 'widefat', |
| 59 |
'id' => $this->get_field_id( 'title' ), |
| 60 |
'name' => $this->get_field_name( 'title' ), |
| 61 |
'type' => 'text', |
| 62 |
'value' => $title, |
| 63 |
) ) |
| 64 |
); |
| 65 |
|
| 66 |
echo wp_kses( $html, 'bogo_form_inside' ); |
| 67 |
} |
| 68 |
|
| 69 |
function update( $new_instance, $old_instance ) { |
| 70 |
$instance = $old_instance; |
| 71 |
|
| 72 |
$new_instance = wp_parse_args( |
| 73 |
(array) $new_instance, |
| 74 |
array( 'title' => '' ) |
| 75 |
); |
| 76 |
|
| 77 |
$instance['title'] = wp_strip_all_tags( $new_instance['title'] ); |
| 78 |
|
| 79 |
return $instance; |
| 80 |
} |
| 81 |
|
| 82 |
} |
| 83 |
|
| 84 |
/* Locale Option */ |
| 85 |
|
| 86 |
add_filter( 'widget_display_callback', 'bogo_widget_display_callback', 10, 3 ); |
| 87 |
|
| 88 |
function bogo_widget_display_callback( $instance, $widget, $args ) { |
| 89 |
if ( isset( $instance['bogo_locales'] ) ) { |
| 90 |
$locale = get_locale(); |
| 91 |
|
| 92 |
if ( ! in_array( $locale, (array) $instance['bogo_locales'] ) ) { |
| 93 |
$instance = false; |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
return $instance; |
| 98 |
} |
| 99 |
|