-
Notifications
You must be signed in to change notification settings - Fork 0
/
drago-widget.php
193 lines (155 loc) · 5.13 KB
/
drago-widget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
<?php
/**
* @package Drago
*/
class Drago_Widget extends WP_Widget {
protected $meta;
protected $languageTitles = [
'en' => 'English',
'es' => 'Español',
'ru' => 'По-русски',
'zh' => '中国普通话'
];
protected $currentUrl;
protected $currentLanguage;
function __construct()
{
global $drago, $wp;
load_plugin_textdomain( 'drago' );
parent::__construct(
'drago_widget',
__( 'Drago Widget' , 'drago'),
array( 'description' => __( 'Language selector for your website' , 'drago') )
);
if (is_active_widget( false, false, $this->id_base)) {
add_action('wp_head', array( $this, 'css'));
}
if (isset($drago))
$this->meta = $drago->getMetaData();
$this->currentUrl = home_url(add_query_arg(NULL, NULL));
}
/**
* ToDo: add ugly links support
*
* @param $language
*
* @return string
*/
protected function getLink($language)
{
if (!$this->meta['status']) return null;
if ($this->currentLanguage != $this->meta['original_language']) {
$pathElements = explode('/', trim(parse_url($this->currentUrl, PHP_URL_PATH), '/'));
array_pop($pathElements);
$path = implode('/', $pathElements);
$nudeUrl = parse_url($this->currentUrl, PHP_URL_SCHEME) . '://' .
parse_url($this->currentUrl, PHP_URL_HOST) .
':' . parse_url($this->currentUrl, PHP_URL_PORT) . '/' .
$path .
parse_url($this->currentUrl, PHP_URL_QUERY);
} else {
$nudeUrl = $this->currentUrl;
}
if ($language == $this->meta['original_language']) return $nudeUrl . '/';
if ($nudeUrl[strlen($nudeUrl) - 1] != '/') $nudeUrl .= '/';
return $nudeUrl . $language . '/';
}
/**
*
*/
function css() {
?>
<style type="text/css">
/* basic menu code 1.0 */
.language-dropmenu ul {
margin: 0;
padding: 0;
}
.language-dropmenu li {
float: left;
list-style: none;
background: #eeeeee;
position: relative;
}
.language-dropmenu li ul li {
float: none;
}
.language-dropmenu li ul {
display: none;
position: absolute;
left: 0; top: 100%;
width: 10em;
box-shadow: 0 0 2px rgba(0,0,0,0.2);
z-index: 999;
}
.language-dropmenu li a {
text-decoration: none;
display: block;
padding: 0.5em 1em;
}
.language-dropmenu li:hover > ul {
display: block;
}
.language-dropmenu li:hover {
background: #d8d8d8;
}
.language-dropmenu:after {
content: "";
display: table;
clear: both;
}
</style>
<?php
}
function form($instance) {
if ($instance) {
$title = $instance['title'];
} else {
$title = __( 'Our site in' , 'drago');
}
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:' , 'drago'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
function update($new_instance, $old_instance)
{
$instance['title'] = strip_tags($new_instance['title']);
return $instance;
}
function widget($args, $instance)
{
global $drago;
$this->currentLanguage = $drago->getCurrentLanguage();
echo $args['before_widget'];
if (!empty($instance['title'])) {
echo $args['before_title'];
echo esc_html($instance['title']);
echo $args['after_title'];
}
if ($this->meta['status']) {
?>
<ul class="language-dropmenu">
<li class="dir"><a href="#"><?php echo $this->languageTitles[$this->currentLanguage]; ?></a>
<ul>
<?php
foreach (array_merge($this->meta['extra_languages'], [$this->meta['original_language']]) as $language) {
if ($language !== $this->currentLanguage) {
?>
<li class="dir"><a href="<?php echo $this->getLink($language); ?>"><?php echo $this->languageTitles[$language]; ?></a></li>
<?php } } ?>
</ul>
</li>
</ul>
<?php
}
echo $args['after_widget'];
}
}
function drago_register_widgets()
{
register_widget( 'Drago_Widget' );
}
add_action( 'widgets_init', 'drago_register_widgets' );