-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmkalias.php
More file actions
144 lines (123 loc) · 4.5 KB
/
Copy pathmkalias.php
File metadata and controls
144 lines (123 loc) · 4.5 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
<?php
defined('_JEXEC') or die;
use Joomla\CMS\Filter\OutputFilter;
use Joomla\CMS\Plugin\CMSPlugin;
/**
* Universal alias generator (MK Cyrillic -> Latin transliteration).
*
* v1.6.0: fixes mixed-script titles where Joomla auto-generates alias only from Latin part.
*/
class PlgContentMkalias extends CMSPlugin
{
public function onContentBeforeSave($context, &$item, $isNew, $data = []): bool
{
$titleField = $this->pickFirstField($item, ['title', 'name']);
if ($titleField === null) {
return true;
}
$title = trim((string) $item->{$titleField});
if ($title === '') {
return true;
}
$aliasField = $this->pickFirstField($item, ['alias', 'slug']);
if ($aliasField === null) {
return true;
}
// Submitted alias (if available)
$submittedAlias = null;
if (is_array($data)) {
if (array_key_exists('alias', $data)) {
$submittedAlias = trim((string) $data['alias']);
} elseif (array_key_exists('slug', $data)) {
$submittedAlias = trim((string) $data['slug']);
}
}
// If user provided a non-empty alias, never overwrite.
if ($submittedAlias !== null && $submittedAlias !== '') {
return true;
}
$currentAlias = trim((string) ($item->{$aliasField} ?? ''));
// Desired alias from transliterated title
$latin = $this->mkToLatin($title);
$desired = OutputFilter::stringURLSafe($latin);
if ($desired === '') {
return true;
}
// If submitted alias is explicitly empty => user left it blank => force our desired alias.
if ($submittedAlias !== null && $submittedAlias === '') {
$item->{$aliasField} = $desired;
return true;
}
// submitted alias not available (null) => override only when current alias looks auto-generated
if ($submittedAlias === null) {
if ($currentAlias === '' || $this->looksLikeDateAlias($currentAlias)) {
$item->{$aliasField} = $desired;
return true;
}
$autoFromOriginal = OutputFilter::stringURLSafe($title);
if ($autoFromOriginal !== '' && $currentAlias === $autoFromOriginal) {
$item->{$aliasField} = $desired;
return true;
}
return true;
}
return true;
}
private function looksLikeDateAlias(string $alias): bool
{
return (bool) preg_match('/^\d{4}-\d{2}-\d{2}-\d{2}-\d{2}(-\d{2})?$/', $alias);
}
private function pickFirstField($obj, array $candidates): ?string
{
foreach ($candidates as $field) {
if (is_object($obj) && property_exists($obj, $field)) {
return $field;
}
}
return null;
}
private function mkToLatin(string $text): string
{
$text = str_replace(['ѐ', 'Ѐ', 'ѝ', 'Ѝ'], ['е', 'Е', 'и', 'И'], $text);
// Normalize common dash characters to hyphen
$text = str_replace(
["\xE2\x80\x90", "\xE2\x80\x91", "\xE2\x80\x92", "\xE2\x80\x93", "\xE2\x80\x94", "\xE2\x80\x95"],
'-',
$text
);
$map = [
'ѓ' => 'gj', 'Ѓ' => 'GJ',
'ж' => 'zh', 'Ж' => 'ZH',
'ќ' => 'kj', 'Ќ' => 'KJ',
'ч' => 'ch', 'Ч' => 'CH',
'љ' => 'lj', 'Љ' => 'LJ',
'њ' => 'nj', 'Њ' => 'NJ',
'ѕ' => 'dz', 'Ѕ' => 'DZ',
'џ' => 'dzj','Џ' => 'DZJ',
'ш' => 'sh', 'Ш' => 'SH',
'а' => 'a', 'А' => 'A',
'б' => 'b', 'Б' => 'B',
'в' => 'v', 'В' => 'V',
'г' => 'g', 'Г' => 'G',
'д' => 'd', 'Д' => 'D',
'е' => 'e', 'Е' => 'E',
'з' => 'z', 'З' => 'Z',
'и' => 'i', 'И' => 'I',
'ј' => 'j', 'Ј' => 'J',
'к' => 'k', 'К' => 'K',
'л' => 'l', 'Л' => 'L',
'м' => 'm', 'М' => 'M',
'н' => 'n', 'Н' => 'N',
'о' => 'o', 'О' => 'O',
'п' => 'p', 'П' => 'P',
'р' => 'r', 'Р' => 'R',
'с' => 's', 'С' => 'S',
'т' => 't', 'Т' => 'T',
'у' => 'u', 'У' => 'U',
'ф' => 'f', 'Ф' => 'F',
'х' => 'h', 'Х' => 'H',
'ц' => 'c', 'Ц' => 'C',
];
return strtr($text, $map);
}
}