1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Defaults\PostStatusLabelsDefaults; |
6
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Translator; |
10
|
|
|
|
11
|
|
|
class TranslationController |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Translator |
15
|
|
|
*/ |
16
|
|
|
public $translator; |
17
|
|
|
|
18
|
|
|
public function __construct(Translator $translator) |
19
|
|
|
{ |
20
|
|
|
$this->translator = $translator; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param array $messages |
25
|
|
|
* @return array |
26
|
|
|
* @filter bulk_post_updated_messages |
27
|
|
|
*/ |
28
|
|
|
public function filterBulkUpdateMessages($messages, array $counts) |
29
|
|
|
{ |
30
|
|
|
$messages = Arr::consolidate($messages); |
31
|
|
|
$messages[glsr()->post_type] = [ |
32
|
|
|
'updated' => _nx('%s review updated.', '%s reviews updated.', $counts['updated'], 'admin-text', 'site-reviews'), |
33
|
|
|
'locked' => _nx('%s review not updated, somebody is editing it.', '%s reviews not updated, somebody is editing them.', $counts['locked'], 'admin-text', 'site-reviews'), |
34
|
|
|
'deleted' => _nx('%s review permanently deleted.', '%s reviews permanently deleted.', $counts['deleted'], 'admin-text', 'site-reviews'), |
35
|
|
|
'trashed' => _nx('%s review moved to the Trash.', '%s reviews moved to the Trash.', $counts['trashed'], 'admin-text', 'site-reviews'), |
36
|
|
|
'untrashed' => _nx('%s review restored from the Trash.', '%s reviews restored from the Trash.', $counts['untrashed'], 'admin-text', 'site-reviews'), |
37
|
|
|
]; |
38
|
|
|
return $messages; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $translation |
43
|
|
|
* @param string $text |
44
|
|
|
* @return string |
45
|
|
|
* @filter gettext_default |
46
|
|
|
*/ |
47
|
|
|
public function filterEnglishTranslation($translation, $text) |
48
|
|
|
{ |
49
|
|
|
return $text; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* @param string $translation |
54
|
|
|
* @param string $text |
55
|
|
|
* @return string |
56
|
|
|
* @filter gettext_.glsr()->id |
57
|
|
|
*/ |
58
|
|
|
public function filterGettext($translation, $text) |
59
|
|
|
{ |
60
|
|
|
return $this->translator->translate($translation, glsr()->id, [ |
61
|
|
|
'single' => $text, |
62
|
|
|
]); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $translation |
67
|
|
|
* @param string $text |
68
|
|
|
* @param string $context |
69
|
|
|
* @return string |
70
|
|
|
* @filter gettext_with_context_.glsr()->id |
71
|
|
|
*/ |
72
|
|
|
public function filterGettextWithContext($translation, $text, $context) |
73
|
|
|
{ |
74
|
|
|
if (Str::contains($context, Translation::CONTEXT_ADMIN_KEY)) { |
75
|
|
|
return $translation; |
76
|
|
|
} |
77
|
|
|
return $this->translator->translate($translation, glsr()->id, [ |
78
|
|
|
'context' => $context, |
79
|
|
|
'single' => $text, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $translation |
85
|
|
|
* @param string $single |
86
|
|
|
* @param string $plural |
87
|
|
|
* @param int $number |
88
|
|
|
* @return string |
89
|
|
|
* @filter ngettext_.glsr()->id |
90
|
|
|
*/ |
91
|
|
|
public function filterNgettext($translation, $single, $plural, $number) |
92
|
|
|
{ |
93
|
|
|
return $this->translator->translate($translation, glsr()->id, [ |
94
|
|
|
'number' => $number, |
95
|
|
|
'plural' => $plural, |
96
|
|
|
'single' => $single, |
97
|
|
|
]); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string $translation |
102
|
|
|
* @param string $single |
103
|
|
|
* @param string $plural |
104
|
|
|
* @param int $number |
105
|
|
|
* @param string $context |
106
|
|
|
* @return string |
107
|
|
|
* @filter ngettext_with_context_.glsr()->id |
108
|
|
|
*/ |
109
|
|
|
public function filterNgettextWithContext($translation, $single, $plural, $number, $context) |
110
|
|
|
{ |
111
|
|
|
if (Str::contains($context, Translation::CONTEXT_ADMIN_KEY)) { |
112
|
|
|
return $translation; |
113
|
|
|
} |
114
|
|
|
return $this->translator->translate($translation, glsr()->id, [ |
115
|
|
|
'context' => $context, |
116
|
|
|
'number' => $number, |
117
|
|
|
'plural' => $plural, |
118
|
|
|
'single' => $single, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param array $postStates |
124
|
|
|
* @param \WP_Post $post |
125
|
|
|
* @return array |
126
|
|
|
* @filter display_post_states |
127
|
|
|
*/ |
128
|
|
|
public function filterPostStates($postStates, $post) |
129
|
|
|
{ |
130
|
|
|
$postStates = Arr::consolidate($postStates); |
131
|
|
|
if (glsr()->post_type == Arr::get($post, 'post_type') && array_key_exists('pending', $postStates)) { |
132
|
|
|
$postStates['pending'] = _x('Unapproved', 'admin-text', 'site-reviews'); |
133
|
|
|
} |
134
|
|
|
return $postStates; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @param string $translation |
139
|
|
|
* @param string $text |
140
|
|
|
* @return string |
141
|
|
|
* @filter gettext_default |
142
|
|
|
*/ |
143
|
|
|
public function filterPostStatusLabels($translation, $text) |
144
|
|
|
{ |
145
|
|
|
if ($this->canModifyTranslation()) { |
146
|
|
|
$replacements = $this->statusLabels(); |
147
|
|
|
return array_key_exists($text, $replacements) |
148
|
|
|
? $replacements[$text] |
149
|
|
|
: $translation; |
150
|
|
|
} |
151
|
|
|
return $translation; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $translation |
156
|
|
|
* @param string $single |
157
|
|
|
* @param string $plural |
158
|
|
|
* @param int $number |
159
|
|
|
* @return string |
160
|
|
|
* @filter ngettext_default |
161
|
|
|
*/ |
162
|
|
|
public function filterPostStatusText($translation, $single, $plural, $number) |
163
|
|
|
{ |
164
|
|
|
if ($this->canModifyTranslation()) { |
165
|
|
|
$strings = [ |
166
|
|
|
'Published' => _x('Approved', 'admin-text', 'site-reviews'), |
167
|
|
|
'Pending' => _x('Unapproved', 'admin-text', 'site-reviews'), |
168
|
|
|
]; |
169
|
|
|
foreach ($strings as $search => $replace) { |
170
|
|
|
if (!Str::contains($search, $single)) { |
171
|
|
|
continue; |
172
|
|
|
} |
173
|
|
|
return $this->translator->getTranslation([ |
174
|
|
|
'number' => $number, |
175
|
|
|
'plural' => str_replace($search, $replace, $plural), |
176
|
|
|
'single' => str_replace($search, $replace, $single), |
177
|
|
|
]); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
return $translation; |
181
|
|
|
} |
182
|
|
|
|
183
|
|
|
/** |
184
|
|
|
* @return void |
185
|
|
|
* @action admin_print_scripts-post.php |
186
|
|
|
*/ |
187
|
|
|
public function translatePostStatusLabels() |
188
|
|
|
{ |
189
|
|
|
if (!$this->canModifyTranslation()) { |
190
|
|
|
return; |
191
|
|
|
} |
192
|
|
|
$pattern = '/^([^{]+)(.+)([^}]+)$/'; |
193
|
|
|
$script = Arr::get(wp_scripts(), 'registered.post.extra.data'); |
194
|
|
|
preg_match($pattern, $script, $matches); |
195
|
|
|
if (4 === count($matches) && $i10n = json_decode($matches[2], true)) { |
196
|
|
|
$i10n['privatelyPublished'] = _x('Privately Approved', 'admin-text', 'site-reviews'); |
197
|
|
|
$i10n['publish'] = _x('Approve', 'admin-text', 'site-reviews'); |
198
|
|
|
$i10n['published'] = _x('Approved', 'admin-text', 'site-reviews'); |
199
|
|
|
$i10n['publishOn'] = _x('Approve on:', 'admin-text', 'site-reviews'); |
200
|
|
|
$i10n['publishOnPast'] = _x('Approved on:', 'admin-text', 'site-reviews'); |
201
|
|
|
$i10n['savePending'] = _x('Save as Unapproved', 'admin-text', 'site-reviews'); |
202
|
|
|
$script = $matches[1].json_encode($i10n).$matches[3]; |
203
|
|
|
Arr::set(wp_scripts(), 'registered.post.extra.data', $script); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
protected function canModifyTranslation() |
211
|
|
|
{ |
212
|
|
|
$screen = glsr_current_screen(); |
213
|
|
|
return glsr()->post_type == $screen->post_type |
214
|
|
|
&& in_array($screen->base, ['edit', 'post']); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Store the labels to avoid unnecessary loops. |
219
|
|
|
* @return array |
220
|
|
|
*/ |
221
|
|
|
protected function statusLabels() |
222
|
|
|
{ |
223
|
|
|
static $labels; |
224
|
|
|
if (empty($labels)) { |
225
|
|
|
$labels = glsr(PostStatusLabelsDefaults::class)->defaults(); |
226
|
|
|
} |
227
|
|
|
return $labels; |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|