Files
wordpress-theme-aksrt/inc/classes/widgets/class-aksrt-lang-switch-widget.php
T

66 lines
2.7 KiB
PHP

<?php
/**
* AKSRT Language Switch Widget — 语言切换器
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
class AKSRT_Widget_Lang_Switch extends WP_Widget {
public function __construct() {
parent::__construct(
'aksrt_lang_switch',
__('语言切换器', 'aksrt'),
['description' => __('在侧栏显示多语言切换按钮。', 'aksrt')]
);
}
public function widget($args, $instance): void {
$title = !empty($instance['title']) ? $instance['title'] : '';
$style = !empty($instance['style']) ? $instance['style'] : 'dropdown';
if (!function_exists('aksrt_has_multilingual') || !aksrt_has_multilingual()) return;
$html = function_exists('aksrt_render_language_switcher') ? aksrt_render_language_switcher($style) : '';
if (empty($html)) return;
echo $args['before_widget'];
if ($title) {
echo $args['before_title'] . esc_html($title) . $args['after_title'];
}
echo $html; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
echo $args['after_widget'];
}
public function form($instance): void {
$title = !empty($instance['title']) ? $instance['title'] : '';
$style = !empty($instance['style']) ? $instance['style'] : 'dropdown';
?>
<p>
<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('标题(可选):', 'aksrt'); ?></label>
<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($title); ?>">
</p>
<p>
<label for="<?php echo esc_attr($this->get_field_id('style')); ?>"><?php esc_html_e('样式:', 'aksrt'); ?></label>
<select class="widefat" id="<?php echo esc_attr($this->get_field_id('style')); ?>" name="<?php echo esc_attr($this->get_field_name('style')); ?>">
<option value="dropdown" <?php selected($style, 'dropdown'); ?>><?php esc_html_e('下拉菜单', 'aksrt'); ?></option>
<option value="flags" <?php selected($style, 'flags'); ?>><?php esc_html_e('国旗图标', 'aksrt'); ?></option>
</select>
</p>
<p><small><?php esc_html_e('需要安装 Polylang 或 WPML 等多语言插件才能生效。', 'aksrt'); ?></small></p>
<?php
}
public function update($new_instance, $old_instance): array {
$instance = [];
$instance['title'] = sanitize_text_field($new_instance['title'] ?? '');
$instance['style'] = in_array($new_instance['style'] ?? '', ['dropdown', 'flags'], true) ? $new_instance['style'] : 'dropdown';
return $instance;
}
}