Passed
Push — master ( eeeeb5...9c97a1 )
by Paul
03:55
created

TranslationController::filterBulkUpdateMessages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 8
nc 1
nop 2
dl 0
loc 11
ccs 0
cts 9
cp 0
crap 2
rs 10
c 1
b 0
f 1
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Controllers;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Controllers\EditorController\Labels;
7
use GeminiLabs\SiteReviews\Helpers\Arr;
8
use GeminiLabs\SiteReviews\Helpers\Str;
9
use GeminiLabs\SiteReviews\Modules\Translation;
10
use GeminiLabs\SiteReviews\Modules\Translator;
11
12
class TranslationController
13
{
14
    /**
15
     * @var Translator
16
     */
17
    public $translator;
18
19
    public function __construct(Translator $translator)
20
    {
21
        $this->translator = $translator;
22
    }
23
24
    /**
25
     * @return void
26
     * @action plugins_loaded
27
     */
28
    public function addTranslationFilters()
29
    {
30
        if (empty(glsr(Translation::class)->translations())) {
31
            return;
32
        }
33
        add_filter('gettext',                                         [$this, 'filterGettext'], 9, 3);
34
        add_filter('site-reviews/gettext/site-reviews',               [$this, 'filterGettextSiteReviews'], 10, 2);
35
        add_filter('gettext_with_context',                            [$this, 'filterGettextWithContext'], 9, 4);
36
        add_filter('site-reviews/gettext_with_context/site-reviews',  [$this, 'filterGettextWithContextSiteReviews'], 10, 3);
37
        add_filter('ngettext',                                        [$this, 'filterNgettext'], 9, 5);
38
        add_filter('site-reviews/ngettext/site-reviews',              [$this, 'filterNgettextSiteReviews'], 10, 4);
39
        add_filter('ngettext_with_context',                           [$this, 'filterNgettextWithContext'], 9, 6);
40
        add_filter('site-reviews/ngettext_with_context/site-reviews', [$this, 'filterNgettextWithContextSiteReviews'], 10, 5);
41
    }
42
43
    /**
44
     * @param array $messages
45
     * @return array
46
     * @filter bulk_post_updated_messages
47
     */
48
    public function filterBulkUpdateMessages($messages, array $counts)
49
    {
50
        $messages = Arr::consolidateArray($messages);
51
        $messages[Application::POST_TYPE] = [
52
            'updated' => _n('%s review updated.', '%s reviews updated.', $counts['updated'], 'site-reviews'),
53
            'locked' => _n('%s review not updated, somebody is editing it.', '%s reviews not updated, somebody is editing them.', $counts['locked'], 'site-reviews'),
54
            'deleted' => _n('%s review permanently deleted.', '%s reviews permanently deleted.', $counts['deleted'], 'site-reviews'),
55
            'trashed' => _n('%s review moved to the Trash.', '%s reviews moved to the Trash.', $counts['trashed'], 'site-reviews'),
56
            'untrashed' => _n('%s review restored from the Trash.', '%s reviews restored from the Trash.', $counts['untrashed'], 'site-reviews'),
57
        ];
58
        return $messages;
59
    }
60
61
    /**
62
     * @param string $translation
63
     * @param string $text
64
     * @param string $domain
65
     * @return string
66
     * @filter gettext
67
     */
68
    public function filterGettext($translation, $text, $domain)
69
    {
70
        return apply_filters('site-reviews/gettext/'.$domain, $translation, $text);
71
    }
72
73
    /**
74
     * @param string $translation
75
     * @param string $text
76
     * @return string
77
     * @filter site-reviews/gettext/site-reviews
78
     */
79
    public function filterGettextSiteReviews($translation, $text)
80
    {
81
        return $this->translator->translate($translation, Application::ID, [
82
            'single' => $text,
83
        ]);
84
    }
85
86
    /**
87
     * @param string $translation
88
     * @param string $text
89
     * @param string $context
90
     * @param string $domain
91
     * @return string
92
     * @filter gettext_with_context
93
     */
94
    public function filterGettextWithContext($translation, $text, $context, $domain)
95
    {
96
        return apply_filters('site-reviews/gettext_with_context/'.$domain, $translation, $text, $context);
97
    }
98
99
    /**
100
     * @param string $translation
101
     * @param string $text
102
     * @param string $context
103
     * @return string
104
     * @filter site-reviews/gettext_with_context/site-reviews
105
     */
106
    public function filterGettextWithContextSiteReviews($translation, $text, $context)
107
    {
108
        return $this->translator->translate($translation, Application::ID, [
109
            'context' => $context,
110
            'single' => $text,
111
        ]);
112
    }
113
114
    /**
115
     * @param string $translation
116
     * @param string $single
117
     * @param string $plural
118
     * @param int $number
119
     * @param string $domain
120
     * @return string
121
     * @filter ngettext
122
     */
123
    public function filterNgettext($translation, $single, $plural, $number, $domain)
124
    {
125
        return apply_filters('site-reviews/ngettext/'.$domain, $translation, $single, $plural, $number);
126
    }
127
128
    /**
129
     * @param string $translation
130
     * @param string $single
131
     * @param string $plural
132
     * @param int $number
133
     * @return string
134
     * @filter site-reviews/ngettext/site-reviews
135
     */
136
    public function filterNgettextSiteReviews($translation, $single, $plural, $number)
137
    {
138
        return $this->translator->translate($translation, Application::ID, [
139
            'number' => $number,
140
            'plural' => $plural,
141
            'single' => $single,
142
        ]);
143
    }
144
145
    /**
146
     * @param string $translation
147
     * @param string $single
148
     * @param string $plural
149
     * @param int $number
150
     * @param string $context
151
     * @param string $domain
152
     * @return string
153
     * @filter ngettext_with_context
154
     */
155
    public function filterNgettextWithContext($translation, $single, $plural, $number, $context, $domain)
156
    {
157
        return apply_filters('site-reviews/ngettext_with_context/'.$domain, $translation, $single, $plural, $number, $context);
158
    }
159
160
    /**
161
     * @param string $translation
162
     * @param string $single
163
     * @param string $plural
164
     * @param int $number
165
     * @param string $context
166
     * @return string
167
     * @filter site-reviews/ngettext_with_context/site-reviews
168
     */
169
    public function filterNgettextWithContextSiteReviews($translation, $single, $plural, $number, $context)
170
    {
171
        return $this->translator->translate($translation, Application::ID, [
172
            'context' => $context,
173
            'number' => $number,
174
            'plural' => $plural,
175
            'single' => $single,
176
        ]);
177
    }
178
179
    /**
180
     * @param array $postStates
181
     * @param \WP_Post $post
182
     * @return array
183
     * @filter display_post_states
184
     */
185
    public function filterPostStates($postStates, $post)
186
    {
187
        $postStates = Arr::consolidateArray($postStates);
188
        if (Application::POST_TYPE == Arr::get($post, 'post_type') && array_key_exists('pending', $postStates)) {
189
            $postStates['pending'] = __('Unapproved', 'site-reviews');
190
        }
191
        return $postStates;
192
    }
193
194
    /**
195
     * @param string $translation
196
     * @param string $text
197
     * @return string
198
     * @filter site-reviews/gettext/default
199
     * @filter site-reviews/gettext_with_context/default
200
     */
201
    public function filterPostStatusLabels($translation, $text)
202
    {
203
        return $this->canModifyTranslation()
204
            ? glsr(Labels::class)->filterPostStatusLabels($translation, $text)
205
            : $translation;
206
    }
207
208
    /**
209
     * @param string $translation
210
     * @param string $single
211
     * @param string $plural
212
     * @param int $number
213
     * @return string
214
     * @filter site-reviews/ngettext/default
215
     */
216
    public function filterPostStatusText($translation, $single, $plural, $number)
217
    {
218
        if ($this->canModifyTranslation()) {
219
            $strings = [
220
                'Published' => __('Approved', 'site-reviews'),
221
                'Pending' => __('Unapproved', 'site-reviews'),
222
            ];
223
            foreach ($strings as $search => $replace) {
224
                if (!Str::contains($single, $search)) {
225
                    continue;
226
                }
227
                return $this->translator->getTranslation([
228
                    'number' => $number,
229
                    'plural' => str_replace($search, $replace, $plural),
230
                    'single' => str_replace($search, $replace, $single),
231
                ]);
232
            }
233
        }
234
        return $translation;
235
    }
236
237
    /**
238
     * @return void
239
     * @action admin_enqueue_scripts
240
     */
241
    public function translatePostStatusLabels()
242
    {
243
        if ($this->canModifyTranslation()) {
244
            glsr(Labels::class)->translatePostStatusLabels();
245
        }
246
    }
247
248
    /**
249
     * @return bool
250
     */
251
    protected function canModifyTranslation()
252
    {
253
        $screen = glsr_current_screen();
254
        return Application::POST_TYPE == $screen->post_type 
255
            && in_array($screen->base, ['edit', 'post']);
256
    }
257
}
258