-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfunctions.php
More file actions
545 lines (492 loc) · 14.4 KB
/
functions.php
File metadata and controls
545 lines (492 loc) · 14.4 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
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
<?php
if (!defined('WPINC')) {
die();
}
/**
* Set of important functions
*
* @link http://infinitumform.com/
* @since 1.0.7
* @package Serbian_Transliteration
***********************************************/
// Indicate functions file for the extended plugins
if (!defined('RSTR_FUNCTIONS')) {
define('RSTR_FUNCTIONS', true);
}
/*
* Is transliteration excluded
* @return bool
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('transliteration_excluded')) :
function transliteration_excluded(): bool
{
return Transliteration_Utilities::exclude_transliteration();
}
endif;
/*
* Get the latin URL
* @param $url (optional)
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('get_latin_url')) :
function get_latin_url(string $url = null)
{
if ($url) {
return add_query_arg(get_rstr_option('url-selector', 'rstr'), 'lat', $url);
}
return add_query_arg(get_rstr_option('url-selector', 'rstr'), 'lat');
}
endif;
/*
* Get the cyrillic URL
* @param $url (optional)
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('get_cyrillic_url')) :
function get_cyrillic_url(string $url = null)
{
if ($url) {
return add_query_arg(get_rstr_option('url-selector', 'rstr'), 'cyr', $url);
}
return add_query_arg(get_rstr_option('url-selector', 'rstr'), 'cyr');
}
endif;
/*
* Get current URL
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('get_current_url')) :
function get_current_url()
{
$parse_url = Transliteration_Utilities::parse_url();
return $parse_url['url'];
}
endif;
/*
* Get current script
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('get_script')) :
function get_script()
{
return Transliteration_Utilities::get_current_script();
} elseif (!function_exists('rstr_get_script')):
function rstr_get_script()
{
_doing_it_wrong('rstr_get_script', __('This function is deprecated and will be removed. Replace it with the `get_script()` function', 'serbian-transliteration'), '1.10.5');
return Transliteration_Utilities::get_current_script();
}
endif;
/*
* Get the active transliteration
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('get_active_transliteration')) :
function get_active_transliteration()
{
return (function_exists('rstr_get_script') ? rstr_get_script() : get_script());
}
endif;
/*
* Check is latin text
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_latin_text')) :
function is_latin_text($content)
{
return Transliteration_Utilities::is_lat($content);
}
endif;
/*
* Check is cyrillic text
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_cyrillic_text')) :
function is_cyrillic_text($content)
{
return Transliteration_Utilities::is_cyr($content);
}
endif;
/*
* Check is site on latin
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_latin')) :
function is_latin(): bool
{
$script = function_exists('get_script') ? get_script() : rstr_get_script();
return in_array($script, ['cyr_to_lat', 'lat']);
}
endif;
/*
* Check is site on cyrillic
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_cyrillic')) :
function is_cyrillic(): bool
{
$script = function_exists('get_script') ? get_script() : rstr_get_script();
return in_array($script, ['lat_to_cyr', 'cyr']);
}
endif;
/*
* Check is already cyrillic
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_already_cyrillic')) :
function is_already_cyrillic()
{
return Transliteration_Utilities::already_cyrillic();
}
endif;
/*
* Check is Serbian language
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_serbian')) :
function is_serbian()
{
return Transliteration_Utilities::get_locale('sr_RS');
}
endif;
/*
* Check is Russian language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_russian')) :
function is_russian()
{
return Transliteration_Utilities::get_locale('ru_RU');
}
endif;
/*
* Check is Georgian language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_georgian')) :
function is_georgian()
{
return Transliteration_Utilities::get_locale('ka_GE');
}
endif;
/*
* Check is Belarusian language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_belarusian')) :
function is_belarusian()
{
return Transliteration_Utilities::get_locale('bel');
}
endif;
/*
* Check is Bulgarian language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_bulgarian')) :
function is_bulgarian()
{
return Transliteration_Utilities::get_locale('bg_BG');
}
endif;
/*
* Check is Macedonian language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_macedonian')) :
function is_macedonian()
{
return Transliteration_Utilities::get_locale('mk_MK');
}
endif;
/*
* Check is Kazakh language
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_kazakh')) :
function is_kazakh()
{
return Transliteration_Utilities::get_locale('kk');
}
endif;
/*
* Greece (Elini'ka) transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_greece')) :
function is_greece()
{
return Transliteration_Utilities::get_locale('el');
}
endif;
/*
* Arabic transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_arabic')) :
function is_arabic()
{
return Transliteration_Utilities::get_locale('ar');
}
endif;
/*
* Armenian transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_armenian')) :
function is_armenian()
{
return Transliteration_Utilities::get_locale('hy');
}
endif;
/*
* Mongolian transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_mongolian')) :
function is_mongolian()
{
return Transliteration_Utilities::get_locale('mn');
}
endif;
/*
* Bashkir transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_bashkir')) :
function is_bashkir()
{
return Transliteration_Utilities::get_locale('ba');
}
endif;
/*
* Uzbek transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_uzbek')) :
function is_uzbek()
{
return Transliteration_Utilities::get_locale('uz_UZ');
}
endif;
/*
* Kyrgyz transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_kyrgyz')) :
function is_kyrgyz()
{
return Transliteration_Utilities::get_locale('kir');
}
endif;
/*
* Tajik transliteration
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_tajik')) :
function is_tajik()
{
return Transliteration_Utilities::get_locale('tg');
}
endif;
/*
* Elini'ka is natural Greece language, alias of function is_greece()
* @return boolean
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('is_elinika')) :
function is_elinika()
{
return is_greece();
}
endif;
/*
* Transliterate content
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('transliterate')) :
function transliterate($content, string $type = 'cyr_to_lat', bool $fix_html = true)
{
return Transliteration_Controller::get()->transliterate($content, $type, $fix_html);
}
endif;
/*
* Translate from Cyrillic to Latin
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('cyr_to_lat')) :
function cyr_to_lat($content, bool $sanitize_html = true)
{
return Transliteration_Controller::get()->cyr_to_lat($content, $sanitize_html);
}
endif;
/*
* Translate from Latin to Cyrillic
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('lat_to_cyr')) :
function lat_to_cyr($content, bool $sanitize_html = true, bool $fix_diacritics = true)
{
return Transliteration_Controller::get()->lat_to_cyr($content, $sanitize_html, $fix_diacritics);
}
endif;
/*
* Translate from Latin to Cyrillic in Sanitize way
* @return string
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('cyr_to_ascii_lat')) :
function cyr_to_ascii_lat($content)
{
return Transliteration_Controller::get()->cyr_to_lat_sanitize($content);
}
endif;
if (!function_exists('cyr_to_simplified_lat')) : function cyr_to_simplified_lat($content)
{
return cyr_to_ascii_lat($content);
} endif;
/*
* Script selector
* @return HTML string/echo/object/array
* @author Ivijan-Stefan Stipic
*/
if (!function_exists('script_selector')) :
function script_selector($args)
{
$ID = uniqid('script_selector_');
$args = (object) wp_parse_args($args, [
'id' => $ID,
'display_type' => 'inline',
'echo' => false,
'separator' => ' | ',
'cyr_caption' => __('Cyrillic', 'serbian-transliteration'),
'lat_caption' => __('Latin', 'serbian-transliteration'),
]);
/*
* Ensure captions are always rendered in correct scripts without using inline tags,
* to avoid conflicts with menu/title filters from other plugins.
*/
if (function_exists('lat_to_cyr')) {
$args->cyr_caption = lat_to_cyr((string) $args->cyr_caption, false, false);
}
if (function_exists('cyr_to_lat')) {
$args->lat_caption = cyr_to_lat((string) $args->lat_caption, false);
}
$options = (object) [
'active' => function_exists('rstr_get_script') ? rstr_get_script() : get_script(),
'cyr' => add_query_arg(get_rstr_option('url-selector', 'rstr'), 'cyr'),
'lat' => add_query_arg(get_rstr_option('url-selector', 'rstr'), 'lat'),
];
$activeClasses = [
'cyr' => in_array($options->active, ['lat_to_cyr', 'cyr']) ? ' active' : ' inactive',
'lat' => in_array($options->active, ['cyr_to_lat', 'lat']) ? ' active' : ' inactive',
];
$return = '';
$templateHandlers = get_script_selector_template_handlers($options, $args, $ID, $activeClasses);
if (isset($templateHandlers[$args->display_type])) {
$return = call_user_func($templateHandlers[$args->display_type]);
} else {
$return = sprintf(__('Choose one of the display types: "%1$s", "%2$s", "%3$s", "%4$s", "%5$s" or "%6$s"', 'serbian-transliteration'), 'inline', 'select', 'list', 'list_items', 'array', 'object');
}
if ($args->echo) {
echo $return;
} else {
return $return;
}
return;
}
function get_script_selector_template_handlers($options, $args, $ID, $activeClasses): array
{
return [
'inline' => fn (): string => sprintf(
'<a href="%1$s" class="rstr-script-selector%5$s">%2$s</a>%3$s<a href="%4$s" class="rstr-script-selector%6$s">%7$s</a>',
esc_url($options->lat),
esc_html($args->lat_caption),
esc_html($args->separator),
esc_url($options->cyr),
$activeClasses['lat'],
$activeClasses['cyr'],
esc_html($args->cyr_caption)
),
'select' => fn (): string => sprintf(
'<script type="text/javascript">/*<![CDATA[*/function rstr_%1$s(redirect) {document.location.href = redirect.value;}/*]]>*/</script><select class="rstr-script-selector" onchange="rstr_%1$s(this);" id="rstr_%1$s"><option value="%2$s"%6$s>%3$s</option><option value="%4$s"%7$s>%5$s</option></select>',
$ID,
esc_url($options->lat),
esc_html($args->lat_caption),
esc_url($options->cyr),
esc_html($args->cyr_caption),
$activeClasses['lat'],
$activeClasses['cyr']
),
'list' => fn (): string => sprintf(
'<ul class="rstr-script-selector" id="rstr_%1$s"><li class="rstr-script-selector-item%5$s"><a href="%2$s" class="rstr-script-selector-item-link%5$s">%3$s</a></li><li class="rstr-script-selector-item%6$s"><a href="%4$s" class="rstr-script-selector-item-link%6$s">%7$s</a></li></ul>',
$ID,
esc_url($options->lat),
esc_html($args->lat_caption),
esc_url($options->cyr),
esc_html($args->cyr_caption),
$activeClasses['lat'],
$activeClasses['cyr']
),
'list_items' => fn (): string => sprintf(
'<li class="rstr-script-selector-item%5$s"><a href="%1$s" class="rstr-script-selector-item-link%5$s">%2$s</a></li><li class="rstr-script-selector-item%6$s"><a href="%3$s" class="rstr-script-selector-item-link%6$s">%4$s</a></li>',
esc_url($options->lat),
esc_html($args->lat_caption),
esc_url($options->cyr),
esc_html($args->cyr_caption),
$activeClasses['lat'],
$activeClasses['cyr']
),
'array' => fn (): array => [
'cyr' => [
'caption' => $args->cyr_caption,
'url' => $options->cyr,
],
'lat' => [
'caption' => $args->lat_caption,
'url' => $options->lat,
],
],
'object' => fn () => (object) [
'cyr' => (object) [
'caption' => $args->cyr_caption,
'url' => $options->cyr,
],
'lat' => (object) [
'caption' => $args->lat_caption,
'url' => $options->lat,
],
],
// Add other display types here if needed.
];
}
endif;