-
Notifications
You must be signed in to change notification settings - Fork 147
/
Copy pathqtranslate_utils.php
295 lines (269 loc) · 8.3 KB
/
qtranslate_utils.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
<?php // encoding: utf-8
/*
Copyright 2014 qTranslate Team (email : [email protected] )
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
// Exit if accessed directly
if ( !defined( 'ABSPATH' ) ) exit;
/* qTranslate-X Utilities */
// /*
if(defined('WP_DEBUG')&&WP_DEBUG){
if(!function_exists('qtranxf_dbg_log')){
function qtranxf_dbg_log($msg,$var='novar',$bt=false){
//$d=ABSPATH.'/wp-logs';
//if(!file_exists($d)) mkdir($d);
//$f=$d.'/qtranslate.log';
$f=WP_CONTENT_DIR.'/debug-qtranslate.log';
if( $var !== 'novar' )
$msg .= var_export($var,true);
if($bt){
$msg .= PHP_EOL.'backtrace:'.PHP_EOL.var_export(debug_backtrace(),true);
}
error_log($msg.PHP_EOL,3,$f);
}
}
if(!function_exists('qtranxf_dbg_echo')){
function qtranxf_dbg_echo($msg,$var='novar',$bt=false){
if( $var !== 'novar' )
$msg .= var_export($var,true);
echo $msg."<br>\n";
if($bt){
debug_print_backtrace();
}
}
}
assert_options(ASSERT_BAIL,true);
}else{
if(!function_exists('qtranxf_dbg_log')){ function qtranxf_dbg_log($msg,$var=null,$bt=false){} }
if(!function_exists('qtranxf_dbg_echo')){ function qtranxf_dbg_echo($msg,$var=null,$bt=false){} }
//assert_options(ASSERT_ACTIVE,false);
//assert_options(ASSERT_WARNING,false);
//assert_options(ASSERT_QUIET_EVAL,true);
}// */
function qtranxf_parseURL($url) {
//this is not the same as native parse_url and so it is in use
$r = '!(?:(\w+)://)?(?:(\w+)\:(\w+)@)?([^/:]+)?';
$r .= '(?:\:(\d*))?([^#?]+)?(?:\?([^#]+))?(?:#(.+$))?!i';
preg_match ( $r, $url, $out );
$result = @array(
"scheme" => $out[1],
"host" => $out[4].(($out[5]=='')?'':':'.$out[5]),
"user" => $out[2],
"pass" => $out[3],
"path" => $out[6],
"query" => $out[7],
"fragment" => $out[8]
);
// }
/*
$result = parse_url($url) + array(
'scheme' => '',
'host' => '',
'user' => '',
'pass' => '',
'path' => '',
'query' => '',
'fragment' => ''
);
isset($result['port']) and $result['host'] .= ':'. $result['port'];
*/
return $result;
}
function qtranxf_stripSlashesIfNecessary($str) {
if(1==get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return $str;
}
function qtranxf_insertDropDownElement($language, $url, $id){
global $q_config;
$html ="
var sb = document.getElementById('qtranxs_select_".$id."');
var o = document.createElement('option');
var l = document.createTextNode('".$q_config['language_name'][$language]."');
";
if($q_config['language']==$language)
$html .= "o.selected = 'selected';";
$html .= "
o.value = '".addslashes(htmlspecialchars_decode($url, ENT_NOQUOTES))."';
o.appendChild(l);
sb.appendChild(o);
";
return $html;
}
if (!function_exists('qtranxf_getLanguage')){
function qtranxf_getLanguage() {
global $q_config;
return $q_config['language'];
}
}
function qtranxf_getLanguageName($lang = '') {
global $q_config;
if($lang=='' || !qtranxf_isEnabled($lang)) $lang = $q_config['language'];
return $q_config['language_name'][$lang];
}
function qtranxf_isEnabled($lang) {
global $q_config;
return in_array($lang, $q_config['enabled_languages']);
}
function qtranxf_startsWith($s, $n) {
if(strlen($n)>strlen($s)) return false;
if($n == substr($s,0,strlen($n))) return true;
return false;
}
function qtranxf_getAvailableLanguages($text) {
global $q_config;
$result = array();
$content = qtranxf_split($text);
foreach($content as $language => $lang_text) {
$lang_text = trim($lang_text);
if(!empty($lang_text)) $result[] = $language;
}
if(sizeof($result)==0) {
// add default language to keep default URL
$result[] = $q_config['language'];
}
return $result;
}
function qtranxf_isAvailableIn($post_id, $language='') {
global $q_config;
if($language == '') $language = $q_config['default_language'];
$p = get_post($post_id); $post = &$p;
$languages = qtranxf_getAvailableLanguages($post->post_content);
return in_array($language,$languages);
}
function qtranxf_convertDateFormatToStrftimeFormat($format) {
$mappings = array(
'd' => '%d',
'D' => '%a',
'j' => '%E',
'l' => '%A',
'N' => '%u',
'S' => '%q',
'w' => '%f',
'z' => '%F',
'W' => '%V',
'F' => '%B',
'm' => '%m',
'M' => '%b',
'n' => '%i',
't' => '%J',
'L' => '%k',
'o' => '%G',
'Y' => '%Y',
'y' => '%y',
'a' => '%P',
'A' => '%p',
'B' => '%K',
'g' => '%l',
'G' => '%L',
'h' => '%I',
'H' => '%H',
'i' => '%M',
's' => '%S',
'u' => '%N',
'e' => '%Q',
'I' => '%o',
'O' => '%O',
'P' => '%s',
'T' => '%v',
'Z' => '%1',
'c' => '%2',
'r' => '%3',
'U' => '%4'
);
$date_parameters = array();
$strftime_parameters = array();
$date_parameters[] = '#%#'; $strftime_parameters[] = '%';
foreach($mappings as $df => $sf) {
$date_parameters[] = '#(([^%\\\\])'.$df.'|^'.$df.')#'; $strftime_parameters[] = '${2}'.$sf;
}
// convert everything
$format = preg_replace($date_parameters, $strftime_parameters, $format);
// remove single backslashes from dates
$format = preg_replace('#\\\\([^\\\\]{1})#','${1}',$format);
// remove double backslashes from dates
$format = preg_replace('#\\\\\\\\#','\\\\',$format);
return $format;
}
function qtranxf_convertFormat($format, $default_format) {
global $q_config;
// if timestamp is requested, don't replace it
if('U'==$format) return qtranxf_convertDateFormatToStrftimeFormat($format);
// check for multilang formats
$format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage($format);
$default_format = qtranxf_useCurrentLanguageIfNotFoundUseDefaultLanguage($default_format);
switch($q_config['use_strftime']) {
case QTX_DATE:
if($format=='') $format = $default_format;
return qtranxf_convertDateFormatToStrftimeFormat($format);
case QTX_DATE_OVERRIDE:
return qtranxf_convertDateFormatToStrftimeFormat($default_format);
case QTX_STRFTIME:
return $format;
case QTX_STRFTIME_OVERRIDE:
return $default_format;
}
}
function qtranxf_convertDateFormat($format) {
global $q_config;
if(isset($q_config['date_format'][$q_config['language']])) {
$default_format = $q_config['date_format'][$q_config['language']];
} elseif(isset($q_config['date_format'][$q_config['default_language']])) {
$default_format = $q_config['date_format'][$q_config['default_language']];
} else {
$default_format = '';
}
return qtranxf_convertFormat($format, $default_format);
}
function qtranxf_convertTimeFormat($format) {
global $q_config;
if(isset($q_config['time_format'][$q_config['language']])) {
$default_format = $q_config['time_format'][$q_config['language']];
} elseif(isset($q_config['time_format'][$q_config['default_language']])) {
$default_format = $q_config['time_format'][$q_config['default_language']];
} else {
$default_format = '';
}
return qtranxf_convertFormat($format, $default_format);
}
function qtranxf_formatCommentDateTime($format) {
global $comment;
return qtranxf_strftime(qtranxf_convertFormat($format, $format), mysql2date('U',$comment->comment_date), '', $before, $after);
}
function qtranxf_formatPostDateTime($format) {
global $post;
return qtranxf_strftime(qtranxf_convertFormat($format, $format), mysql2date('U',$post->post_date), '', $before, $after);
}
function qtranxf_formatPostModifiedDateTime($format) {
global $post;
return qtranxf_strftime(qtranxf_convertFormat($format, $format), mysql2date('U',$post->post_modified), '', $before, $after);
}
//not in use
//function qtranxf_realURL($url = '') {
// global $q_config;
// return $q_config['url_info']['original_url'];
//}
function qtranxf_getSortedLanguages($reverse = false) {
global $q_config;
$languages = $q_config['enabled_languages'];
ksort($languages);
// fix broken order
$clean_languages = array();
foreach($languages as $lang) {
$clean_languages[] = $lang;
}
if($reverse) krsort($clean_languages);
return $clean_languages;
}
?>