Passed
Push — master ( a406f9...3ba855 )
by Paul
08:12 queued 04:00
created

TranslationController::translatePostStatusLabels()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

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