| 1 |
<?php |
| 2 |
|
| 3 |
add_action( 'in_widget_form', 'bogo_in_widget_form', 10, 3 ); |
| 4 |
|
| 5 |
function bogo_in_widget_form( $widget, $return, $instance ) { |
| 6 |
$available_languages = bogo_available_languages( array( |
| 7 |
'orderby' => 'value', |
| 8 |
) ); |
| 9 |
|
| 10 |
if ( empty( $available_languages ) ) { |
| 11 |
return; |
| 12 |
} |
| 13 |
|
| 14 |
$return = null; |
| 15 |
|
| 16 |
$selected_languages = isset( $instance['bogo_locales'] ) |
| 17 |
? (array) $instance['bogo_locales'] |
| 18 |
: array_keys( $available_languages ); |
| 19 |
|
| 20 |
?> |
| 21 |
<fieldset class="bogo-locale-options"> |
| 22 |
<legend><?php echo esc_html( __( 'Displayed on pages in', 'bogo' ) ); ?></legend> |
| 23 |
<?php |
| 24 |
|
| 25 |
foreach ( $available_languages as $locale => $language ) { |
| 26 |
$checked = in_array( $locale, $selected_languages ); |
| 27 |
|
| 28 |
$id_attr = sprintf( |
| 29 |
'%s-%s', |
| 30 |
$widget->get_field_id( 'bogo_locales' ), |
| 31 |
$locale |
| 32 |
); |
| 33 |
|
| 34 |
$checkbox = sprintf( |
| 35 |
'<label %1$s><input %2$s />%3$s</label>', |
| 36 |
bogo_format_atts( array( |
| 37 |
'class' => 'bogo-locale-option' . ( $checked ? ' checked' : '' ), |
| 38 |
'for' => $id_attr, |
| 39 |
) ), |
| 40 |
bogo_format_atts( array( |
| 41 |
'type' => 'checkbox', |
| 42 |
'id' => $id_attr, |
| 43 |
'name' => sprintf( '%s[]', $widget->get_field_name( 'bogo_locales' ) ), |
| 44 |
'value' => $locale, |
| 45 |
'checked' => $checked, |
| 46 |
) ), |
| 47 |
$language |
| 48 |
); |
| 49 |
|
| 50 |
echo wp_kses( $checkbox, 'bogo_form_inside' ) . "\n"; |
| 51 |
} |
| 52 |
|
| 53 |
?> |
| 54 |
</fieldset> |
| 55 |
<?php |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
add_filter( 'widget_update_callback', 'bogo_widget_update_callback', 10, 4 ); |
| 60 |
|
| 61 |
function bogo_widget_update_callback( $instance, $new_instance, $old_instance, $widget ) { |
| 62 |
if ( |
| 63 |
isset( $new_instance['bogo_locales'] ) and |
| 64 |
is_array( $new_instance['bogo_locales'] ) |
| 65 |
) { |
| 66 |
$instance['bogo_locales'] = $new_instance['bogo_locales']; |
| 67 |
} else { |
| 68 |
$instance['bogo_locales'] = array(); |
| 69 |
} |
| 70 |
|
| 71 |
return $instance; |
| 72 |
} |
| 73 |
|