| 1 |
<?php |
| 2 |
|
| 3 |
use Falang\Model\Falang_Model; |
| 4 |
/** |
| 5 |
* The language switcher widget |
| 6 |
* |
| 7 |
* @since 0.1 |
| 8 |
*/ |
| 9 |
class Falang_Widget_Language_Switcher extends WP_Widget { |
| 10 |
|
| 11 |
/** |
| 12 |
* Holds widget settings defaults options, populated in constructor. |
| 13 |
* |
| 14 |
* @var array |
| 15 |
*/ |
| 16 |
protected $defaults; |
| 17 |
|
| 18 |
|
| 19 |
/** |
| 20 |
* Constructor |
| 21 |
* |
| 22 |
* @since 0.1 |
| 23 |
*/ |
| 24 |
public function __construct() { |
| 25 |
parent::__construct( |
| 26 |
'falang', |
| 27 |
__( 'Falang Language Switcher', 'falang' ), |
| 28 |
array( |
| 29 |
'description' => __( 'Displays a language switcher', 'falang' ), |
| 30 |
'customize_selective_refresh' => true, |
| 31 |
) |
| 32 |
); |
| 33 |
|
| 34 |
//set up default options |
| 35 |
$this->defaults = array( |
| 36 |
'title' => '', |
| 37 |
'display_name' => 1, |
| 38 |
'display_flags' => 0, |
| 39 |
'positioning' => 'h', // vertical|horizontal |
| 40 |
'display_type' => 'list', // types: list: vertical or horizontal list, select: selectbox |
| 41 |
'hide_current' => 1 |
| 42 |
); |
| 43 |
|
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Displays the widget |
| 48 |
* |
| 49 |
* @since 0.1 |
| 50 |
* |
| 51 |
* @param array $args Display arguments including before_title, after_title, before_widget, and after_widget. |
| 52 |
* @param array $instance The settings for the particular instance of the widget |
| 53 |
*/ |
| 54 |
function widget( $args, $instance ) { |
| 55 |
//TODO display message if language switcher not activated (see sublanguage) |
| 56 |
|
| 57 |
extract( $args ); |
| 58 |
|
| 59 |
$fswicher = new \Falang\Core\Language_Switcher($instance); |
| 60 |
|
| 61 |
/** Merge with defaults */ |
| 62 |
$this->instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 63 |
|
| 64 |
|
| 65 |
$title = empty( $instance['title'] ) ? '' : $instance['title']; |
| 66 |
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
| 67 |
$title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 68 |
|
| 69 |
echo $args['before_widget']; |
| 70 |
if ( $title ) { |
| 71 |
echo $args['before_title'] . $title . $args['after_title']; |
| 72 |
} |
| 73 |
|
| 74 |
echo $fswicher->display_switcher(); |
| 75 |
|
| 76 |
echo $args['after_widget']; |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* Displays the widget form |
| 81 |
* |
| 82 |
* @since 1.0 |
| 83 |
* @update 1.3.35 change the initialisation of the default value |
| 84 |
* |
| 85 |
* @param array $instance Current settings |
| 86 |
*/ |
| 87 |
function form( $instance ) { |
| 88 |
$instance = wp_parse_args( (array) $instance, $this->defaults ); |
| 89 |
$title = strip_tags( $instance['title'] ); |
| 90 |
|
| 91 |
?> |
| 92 |
<!-- title --> |
| 93 |
<p> |
| 94 |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php echo esc_html__( 'Title', 'falang' ); ?>:</label> |
| 95 |
<input class="widefat" type="text" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo esc_attr( $instance['title'] ); ?>" class="widefat" /> |
| 96 |
</p> |
| 97 |
|
| 98 |
<!-- display type --> |
| 99 |
<p> |
| 100 |
<label for="<?php echo $this->get_field_id( 'display_type' ); ?>"><?php _e( 'Display as', 'falang' ); ?>:</label> |
| 101 |
<select id="<?php echo $this->get_field_id( 'display_type' ); ?>" name="<?php echo $this->get_field_name( 'display_type' ); ?>"> |
| 102 |
<option value="list" <?php selected( 'list', $instance['display_type'] ); ?>><?php _e( 'List (vertical or horizontal)', 'falang' ); ?></option> |
| 103 |
<!-- <option value="select" --><?php //selected( 'select', $instance['display_type'] ); ?><!-->--><?php //_e( 'Select Box', 'falang' ); ?><!--</option>--> |
| 104 |
</select> |
| 105 |
</p> |
| 106 |
|
| 107 |
<!-- positioning --> |
| 108 |
<p <?php echo ($instance['display_type'] == 'select' ? 'style="display: none;"' : ''); ?>> |
| 109 |
<label for="<?php echo $this->get_field_id( 'positioning' ); ?>"><?php _e( 'Positioning', 'falang' ); ?>:</label> |
| 110 |
<select id="<?php echo $this->get_field_id( 'positioning' ); ?>" name="<?php echo $this->get_field_name( 'positioning' ); ?>"> |
| 111 |
<option value="h" <?php selected( 'h', $instance['positioning'] ); ?>><?php _e( 'Horizontally', 'falang' ); ?></option> |
| 112 |
<option value="v" <?php selected( 'v', $instance['positioning'] ); ?>><?php _e( 'Vertically', 'falang' ); ?></option> |
| 113 |
</select> |
| 114 |
</p> |
| 115 |
|
| 116 |
<!-- display name --> |
| 117 |
<p <?php echo ($instance['display_type'] == 'select' ? 'style="display: none;"' : ''); ?>> |
| 118 |
<input type="checkbox" id="<?php echo $this->get_field_id( 'display_name' ); ?>" name="<?php echo $this->get_field_name( 'display_name' ); ?>" value="1" <?php checked('1',$instance['display_name']); ?>/> |
| 119 |
|
| 120 |
<label for="<?php echo $this->get_field_id( 'display_name' ); ?>"><?php _e( 'Display Language Names', 'falang' ); ?>:</label> |
| 121 |
</p> |
| 122 |
|
| 123 |
<!-- display flag --> |
| 124 |
<p class="option_display_list" <?php echo ($instance['display_type'] == 'select' ? 'style="display: none;"' : ''); ?>> |
| 125 |
<input type="checkbox" id="<?php echo $this->get_field_id( 'display_flags' ); ?>" name="<?php echo $this->get_field_name( 'display_flags' ); ?>" value="1" <?php checked('1',$instance['display_flags']); ?>/> |
| 126 |
|
| 127 |
<label for="<?php echo $this->get_field_id( 'display_flags' ); ?>"><?php _e( 'Display Flags', 'falang' ); ?>:</label> |
| 128 |
</p> |
| 129 |
|
| 130 |
<!-- hide current language --> |
| 131 |
<p> |
| 132 |
<input class="checkbox" type="checkbox" id="<?php echo $this->get_field_id( 'hide_current' ); ?>" name="<?php echo $this->get_field_name( 'hide_current' ); ?>" value="1" <?php checked('1',$instance['hide_current']); ?>/> |
| 133 |
|
| 134 |
<label for="<?php echo $this->get_field_id( 'hide_current' ); ?>"><?php _e( 'Hides the current language', 'falang' ); ?>:</label> |
| 135 |
</p> |
| 136 |
|
| 137 |
|
| 138 |
<?php |
| 139 |
|
| 140 |
// FIXME echoing script in form is not very clean |
| 141 |
// but it does not work if enqueued properly : |
| 142 |
// clicking save on a widget makes this code unreachable for the just saved widget ( ?! ) |
| 143 |
$this->admin_print_script(); |
| 144 |
|
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Updates the widget options |
| 149 |
* |
| 150 |
* @since 1.0 |
| 151 |
* |
| 152 |
* @param array $new_instance New settings for this instance as input by the user via form() |
| 153 |
* @param array $old_instance Old settings for this instance |
| 154 |
* @return array Settings to save or bool false to cancel saving |
| 155 |
*/ |
| 156 |
function update( $new_instance, $old_instance ) { |
| 157 |
$new_instance['title'] = strip_tags( $new_instance['title'] ); |
| 158 |
|
| 159 |
|
| 160 |
$new_instance['positioning'] = strip_tags( $new_instance['positioning'] ); |
| 161 |
$new_instance['display_name'] = strip_tags( $new_instance['display_name'] ); |
| 162 |
$new_instance['display_flags'] = strip_tags( $new_instance['display_flags'] ); |
| 163 |
$new_instance['display_type'] = strip_tags( $new_instance['display_type'] ); |
| 164 |
$new_instance['hide_current'] = strip_tags( $new_instance['hide_current'] ); |
| 165 |
|
| 166 |
return $new_instance; |
| 167 |
} |
| 168 |
|
| 169 |
/** |
| 170 |
* Add javascript to control the switcher widget options |
| 171 |
* |
| 172 |
* @since 1.0 |
| 173 |
*/ |
| 174 |
public function admin_print_script() { |
| 175 |
static $done = false; |
| 176 |
|
| 177 |
if ( $done ) { |
| 178 |
return; |
| 179 |
} |
| 180 |
|
| 181 |
$done = true; |
| 182 |
?> |
| 183 |
<script type='text/javascript'> |
| 184 |
//<![CDATA[ |
| 185 |
jQuery( document ).ready( function( $ ) { |
| 186 |
|
| 187 |
function checkSwitchOn(check1,check2) { |
| 188 |
if (check1.prop('checked') == false && check2.prop('checked') == false) { |
| 189 |
check2.prop('checked',true); |
| 190 |
} |
| 191 |
} |
| 192 |
|
| 193 |
// $('.widgets-sortables,.control-section-sidebar').on('change','.sublanguagesw_display_type',function() { |
| 194 |
// var displayType = $(this).val(); |
| 195 |
// if (displayType == 'select') { |
| 196 |
// $(this).parent().parent().find('.option_display_list').hide(); |
| 197 |
// } else { |
| 198 |
// $(this).parent().parent().find('.option_display_list').show(); |
| 199 |
// } |
| 200 |
// }); |
| 201 |
// |
| 202 |
// $('.widgets-sortables,.control-section-sidebar').on('change','.sublanguagesw_option_display_flags',function() { |
| 203 |
// checkSwitchOn($(this),$(this).parent().parent().find('.sublanguagesw_option_display_name')); |
| 204 |
// }); |
| 205 |
// |
| 206 |
// $('.widgets-sortables,.control-section-sidebar').on('change','.sublanguagesw_option_display_name',function() { |
| 207 |
// checkSwitchOn($(this),$(this).parent().parent().find('.sublanguagesw_option_display_flags')); |
| 208 |
// }); |
| 209 |
|
| 210 |
} ); |
| 211 |
//]]> |
| 212 |
</script> |
| 213 |
<?php |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
} |