-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathplugin.php
More file actions
185 lines (155 loc) · 4.57 KB
/
Copy pathplugin.php
File metadata and controls
185 lines (155 loc) · 4.57 KB
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
<?php
class KokenI18n extends KokenPlugin {
private $lang = false;
function __construct()
{
$this->require_setup = true;
$this->register_filter('api.album', 'kokenI18nAlbum');
$this->register_filter('api.content', 'kokenI18nContent');
$this->register_filter('api.text', 'kokenI18nText');
$this->register_filter('site.template', 'kokenI18nTemplate');
$this->register_filter('site.output', 'kokenI18nOutput');
$this->register_hook('before_closing_body', 'kokenI18nSwitcher');
$this->register_hook('before_closing_head', 'kokenI18nSwitcherStyle');
}
private function kokenI18nWrapper($fields, $data, $options)
{
$lang_count = count(explode($this->data->separator, $this->data->lang_string));
if (isset($options['lang']) && !empty($options['lang']))
$this->lang = $options['lang'];
foreach ($fields as $field)
{
if (count(explode($this->data->separator, $data[$field])) === $lang_count)
{
if (substr($data[$field], 0, 2) !== '[[')
$data[$field] = '[['.$data[$field];
if (substr($data[$field], -2, 2) !== ']]')
$data[$field] = $data[$field].']]';
if (!$options['auth'] && $this->lang)
$data[$field] = $this->kokenI18nExpand($data[$field]);
}
}
return $data;
}
private function kokenI18nCookie($setcookie = false)
{
if ($this->lang === false)
$this->lang = isset($_COOKIE['koken_i18n']) ? $_COOKIE['koken_i18n'] : $this->data->lang_default;
if ($setcookie)
setcookie('koken_i18n', $this->lang, 0, '/');
return $this->lang;
}
private function kokenI18nCallback($matches)
{
$lang = explode($this->data->separator, $this->data->lang_string);
$text = explode($this->data->separator, $matches[1]);
if (count($lang) === count($text))
{
$text = array_combine($lang, $text);
$cookie = $this->kokenI18nCookie();
$text = empty($text[$cookie]) ? $text[$this->data->lang_default] : $text[$cookie];
$text = preg_replace('/^\s*(<\/.+?>\s*|<br.*?>\s*)*|(\s*<[^\/]+?>)*\s*$/', '', $text);
return $text;
}
return $matches[0];
}
private function kokenI18nExpand($html)
{
return preg_replace_callback('/\[\[((?:.(?!\[\[))+?)\]\]/s', array($this, 'kokenI18nCallback'), $html);
}
function kokenI18nAlbum($data, $obj, $options)
{
return $this->kokenI18nWrapper(array('title', 'summary', 'description'), $data, $options);
}
function kokenI18nContent($data, $obj, $options)
{
return $this->kokenI18nWrapper(array('title', 'caption'), $data, $options);
}
function kokenI18nText($data, $obj, $options)
{
return $this->kokenI18nWrapper(array('title', 'excerpt', 'content'), $data, $options);
}
function kokenI18nTemplate($tmpl)
{
Koken::$location['lang'] = Koken::$location['parameters']['__overrides']['lang'] = $this->kokenI18nCookie();
$nums = array('singular', 'plural');
foreach (Koken::$site['url_data'] as $key => $value)
{
if (is_array($value))
{
foreach ($nums as $num)
if (isset($this->data->{'labels_'.$key.'_'.$num}))
Koken::$site['url_data'][$key][$num] = $this->kokenI18nExpand('[['.$this->data->{'labels_'.$key.'_'.$num}.']]');
}
else if (is_string($value))
{
if (isset($this->data->{'labels_'.$key}))
Koken::$site['url_data'][$key] = $this->kokenI18nExpand('[['.$this->data->{'labels_'.$key}.']]');
}
}
return $tmpl;
}
function kokenI18nOutput($html)
{
if ($_SERVER['SCRIPT_NAME'] !== "/preview.php")
Koken::$cache_path = str_replace('/cache.', '/cache.'.$this->kokenI18nCookie(true).'.', Koken::$cache_path);
return $this->kokenI18nExpand($html);
}
function kokenI18nSwitcher()
{
if ($this->data->default_switcher)
{
$lang = explode($this->data->separator, $this->data->lang_string);
$li = '';
foreach($lang as $l)
{
$li .= '<li><a href="#" data-lang="'.$l.'">'.$l.'</a></li>';
}
echo <<<OUT
<ul id="lang-switcher">{$li}</ul>
<script>
$('#lang-switcher a[data-lang="'+$.cookie('koken_i18n')+'"]').parent().addClass('current');
$('#lang-switcher a').on('click', function(){
if($.cookie('koken_i18n') != $(this).data('lang')) {
$.cookie('koken_i18n', $(this).data('lang'), { path: '/' });
location.reload(true);
}
return false;
});
</script>
OUT;
}
}
function kokenI18nSwitcherStyle()
{
if ($this->data->default_switcher_style)
{
echo <<<OUT
<style>
#lang-switcher {
position:fixed;
top:4px;
right:4px;
z-index:9999;
}
#lang-switcher li {
display:inline-block;
margin:4px 0;
border-right:1px solid;
}
#lang-switcher li:last-child {
border-right:none;
}
#lang-switcher li.current {
font-weight:bold;
}
#lang-switcher li a {
display:block;
padding:0 4px;
outline-style:none;
}
</style>
OUT;
}
}
}