|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Addons; |
|
4
|
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Controllers\Controller as BaseController; |
|
6
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
|
7
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
8
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
|
9
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Modules\Translator; |
|
12
|
|
|
|
|
13
|
|
|
abstract class Controller extends BaseController |
|
14
|
|
|
{ |
|
15
|
|
|
/** |
|
16
|
|
|
* @var Addon |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $addon; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
|
|
$this->setAddon(); |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @return void |
|
27
|
|
|
* @action admin_enqueue_scripts |
|
28
|
|
|
*/ |
|
29
|
|
|
public function enqueueAdminAssets() |
|
30
|
|
|
{ |
|
31
|
|
|
if (!$this->isReviewAdminPage()) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
$this->enqueueAsset('css', ['suffix' => 'admin']); |
|
35
|
|
|
$this->enqueueAsset('js', ['suffix' => 'admin']); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @return void |
|
40
|
|
|
* @action enqueue_block_editor_assets |
|
41
|
|
|
*/ |
|
42
|
|
|
public function enqueueBlockAssets() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->registerAsset('css', ['suffix' => 'blocks']); |
|
45
|
|
|
$this->registerAsset('js', ['suffix' => 'blocks']); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @return void |
|
50
|
|
|
* @action wp_enqueue_scripts |
|
51
|
|
|
*/ |
|
52
|
|
|
public function enqueuePublicAssets() |
|
53
|
|
|
{ |
|
54
|
|
|
$this->enqueueAsset('css'); |
|
55
|
|
|
$this->enqueueAsset('js'); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @return array |
|
60
|
|
|
* @filter plugin_action_links_{addon_id}/{addon_id}.php |
|
61
|
|
|
*/ |
|
62
|
|
|
public function filterActionLinks(array $links) |
|
63
|
|
|
{ |
|
64
|
|
|
$links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
|
65
|
|
|
'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=settings#tab-addons'), |
|
66
|
|
|
]); |
|
67
|
|
|
return $links; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* @param string $path |
|
72
|
|
|
* @return string |
|
73
|
|
|
* @filter site-reviews/config |
|
74
|
|
|
*/ |
|
75
|
|
|
public function filterConfigPath($path) |
|
76
|
|
|
{ |
|
77
|
|
|
$addonPrefix = $this->addon->id.'/'; |
|
78
|
|
|
return Str::contains($path, $addonPrefix) |
|
79
|
|
|
? $addonPrefix.str_replace($addonPrefix, '', $path) |
|
80
|
|
|
: $path; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return array |
|
85
|
|
|
* @filter site-reviews/addon/documentation |
|
86
|
|
|
*/ |
|
87
|
|
|
public function filterDocumentation(array $documentation) |
|
88
|
|
|
{ |
|
89
|
|
|
$documentation[$this->addon->name] = glsr(Template::class)->build($this->addon->id.'/documentation'); |
|
90
|
|
|
return $documentation; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @param string $path |
|
95
|
|
|
* @param string $file |
|
96
|
|
|
* @return string |
|
97
|
|
|
* @filter site-reviews/path |
|
98
|
|
|
*/ |
|
99
|
|
|
public function filterFilePaths($path, $file) |
|
100
|
|
|
{ |
|
101
|
|
|
$addonPrefix = $this->addon->id.'/'; |
|
102
|
|
|
return Str::startsWith($addonPrefix, $file) |
|
103
|
|
|
? $this->addon->path(Str::replaceFirst($addonPrefix, '', $file)) |
|
104
|
|
|
: $path; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @param string $translation |
|
109
|
|
|
* @param string $text |
|
110
|
|
|
* @return string |
|
111
|
|
|
* @filter site-reviews/gettext/{addon_id} |
|
112
|
|
|
*/ |
|
113
|
|
|
public function filterGettext($translation, $text) |
|
114
|
|
|
{ |
|
115
|
|
|
return glsr(Translator::class)->translate($translation, $this->addon->id, [ |
|
116
|
|
|
'single' => $text, |
|
117
|
|
|
]); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* @param string $translation |
|
122
|
|
|
* @param string $text |
|
123
|
|
|
* @param string $context |
|
124
|
|
|
* @return string |
|
125
|
|
|
* @filter site-reviews/gettext_with_context/{addon_id} |
|
126
|
|
|
*/ |
|
127
|
|
|
public function filterGettextWithContext($translation, $text, $context) |
|
128
|
|
|
{ |
|
129
|
|
|
return glsr(Translator::class)->translate($translation, $this->addon->id, [ |
|
130
|
|
|
'context' => $context, |
|
131
|
|
|
'single' => $text, |
|
132
|
|
|
]); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @param string $translation |
|
137
|
|
|
* @param string $single |
|
138
|
|
|
* @param string $plural |
|
139
|
|
|
* @param int $number |
|
140
|
|
|
* @return string |
|
141
|
|
|
* @filter site-reviews/ngettext/{addon_id} |
|
142
|
|
|
*/ |
|
143
|
|
|
public function filterNgettext($translation, $single, $plural, $number) |
|
144
|
|
|
{ |
|
145
|
|
|
return glsr(Translator::class)->translate($translation, $this->addon->id, [ |
|
146
|
|
|
'number' => $number, |
|
147
|
|
|
'plural' => $plural, |
|
148
|
|
|
'single' => $single, |
|
149
|
|
|
]); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
/** |
|
153
|
|
|
* @param string $translation |
|
154
|
|
|
* @param string $single |
|
155
|
|
|
* @param string $plural |
|
156
|
|
|
* @param int $number |
|
157
|
|
|
* @param string $context |
|
158
|
|
|
* @return string |
|
159
|
|
|
* @filter site-reviews/ngettext_with_context/{addon_id} |
|
160
|
|
|
*/ |
|
161
|
|
|
public function filterNgettextWithContextSiteReviews($translation, $single, $plural, $number, $context) |
|
162
|
|
|
{ |
|
163
|
|
|
return glsr(Translator::class)->translate($translation, $this->addon->id, [ |
|
164
|
|
|
'context' => $context, |
|
165
|
|
|
'number' => $number, |
|
166
|
|
|
'plural' => $plural, |
|
167
|
|
|
'single' => $single, |
|
168
|
|
|
]); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
/** |
|
172
|
|
|
* @return array |
|
173
|
|
|
* @filter site-reviews/addon/settings |
|
174
|
|
|
*/ |
|
175
|
|
|
public function filterSettings(array $settings) |
|
176
|
|
|
{ |
|
177
|
|
|
$settingsFile = $this->addon->path('config/settings.php'); |
|
178
|
|
|
if (file_exists($settingsFile)) { |
|
179
|
|
|
$settings = array_merge((include $settingsFile), $settings); |
|
180
|
|
|
} |
|
181
|
|
|
return $settings; |
|
182
|
|
|
} |
|
183
|
|
|
|
|
184
|
|
|
/** |
|
185
|
|
|
* @return array |
|
186
|
|
|
* @filter site-reviews/addon/system-info |
|
187
|
|
|
*/ |
|
188
|
|
|
public function filterSystemInfo(array $details) |
|
189
|
|
|
{ |
|
190
|
|
|
$version = $this->addon->version; |
|
191
|
|
|
$previousVersion = glsr(OptionManager::class)->get('addons.'.$this->addon->id.'.version_upgraded_from'); |
|
192
|
|
|
if (empty($previousVersion)) { |
|
193
|
|
|
$previousVersion = $version; |
|
194
|
|
|
} |
|
195
|
|
|
$details[$this->addon->name] = sprintf('%s (%s)', $this->addon->version, $previousVersion); |
|
196
|
|
|
return $details; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
/** |
|
200
|
|
|
* @return array |
|
201
|
|
|
* @filter site-reviews/translation/entries |
|
202
|
|
|
*/ |
|
203
|
|
|
public function filterTranslationEntries(array $entries) |
|
204
|
|
|
{ |
|
205
|
|
|
$potFile = $this->addon->path($this->addon->languages.'/'.$this->addon->id.'.pot'); |
|
206
|
|
|
return glsr(Translation::class)->extractEntriesFromPotFile($potFile, $entries); |
|
207
|
|
|
} |
|
208
|
|
|
|
|
209
|
|
|
/** |
|
210
|
|
|
* @return array |
|
211
|
|
|
* @filter site-reviews/translator/domains |
|
212
|
|
|
*/ |
|
213
|
|
|
public function filterTranslatorDomains(array $domains) |
|
214
|
|
|
{ |
|
215
|
|
|
$domains[] = $this->addon->id; |
|
216
|
|
|
return $domains; |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
/** |
|
220
|
|
|
* @return void |
|
221
|
|
|
* @action init |
|
222
|
|
|
*/ |
|
223
|
|
|
public function registerBlocks() |
|
224
|
|
|
{ |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* @return void |
|
229
|
|
|
* @action plugins_loaded |
|
230
|
|
|
*/ |
|
231
|
|
|
public function registerLanguages() |
|
232
|
|
|
{ |
|
233
|
|
|
load_plugin_textdomain($this->addon->id, false, |
|
234
|
|
|
trailingslashit(plugin_basename($this->addon->path()).'/'.$this->addon->languages) |
|
235
|
|
|
); |
|
236
|
|
|
} |
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* @return void |
|
240
|
|
|
* @action init |
|
241
|
|
|
*/ |
|
242
|
|
|
public function registerShortcodes() |
|
243
|
|
|
{ |
|
244
|
|
|
} |
|
245
|
|
|
|
|
246
|
|
|
/** |
|
247
|
|
|
* @return void |
|
248
|
|
|
* @action admin_init |
|
249
|
|
|
*/ |
|
250
|
|
|
public function registerTinymcePopups() |
|
251
|
|
|
{ |
|
252
|
|
|
} |
|
253
|
|
|
|
|
254
|
|
|
/** |
|
255
|
|
|
* @return void |
|
256
|
|
|
* @action widgets_init |
|
257
|
|
|
*/ |
|
258
|
|
|
public function registerWidgets() |
|
259
|
|
|
{ |
|
260
|
|
|
} |
|
261
|
|
|
|
|
262
|
|
|
/** |
|
263
|
|
|
* @param string $rows |
|
264
|
|
|
* @return void |
|
265
|
|
|
* @action site-reviews/addon/settings/{addon_slug} |
|
266
|
|
|
*/ |
|
267
|
|
|
public function renderSettings($rows) |
|
268
|
|
|
{ |
|
269
|
|
|
glsr(Template::class)->render($this->addon->id.'/views/settings', [ |
|
270
|
|
|
'context' => [ |
|
271
|
|
|
'rows' => $rows, |
|
272
|
|
|
'title' => $this->addon->name, |
|
273
|
|
|
], |
|
274
|
|
|
]); |
|
275
|
|
|
} |
|
276
|
|
|
|
|
277
|
|
|
/** |
|
278
|
|
|
* @param string $extension |
|
279
|
|
|
* @return array |
|
280
|
|
|
*/ |
|
281
|
|
|
protected function buildAssetArgs($extension, array $args = []) |
|
282
|
|
|
{ |
|
283
|
|
|
$args = wp_parse_args($args, [ |
|
284
|
|
|
'dependencies' => [], |
|
285
|
|
|
'in_footer' => true, |
|
286
|
|
|
'suffix' => '', |
|
287
|
|
|
]); |
|
288
|
|
|
$path = 'assets/'.$this->addon->id.Str::prefix('-', $args['suffix']).'.'.$extension; |
|
289
|
|
|
if (!file_exists($this->addon->path($path)) || !in_array($extension, ['css', 'js'])) { |
|
290
|
|
|
return []; |
|
291
|
|
|
} |
|
292
|
|
|
$funcArgs = [ |
|
293
|
|
|
$this->addon->id.Str::prefix('/', $args['suffix']), |
|
294
|
|
|
$this->addon->url($path), |
|
295
|
|
|
array_merge([glsr()->id.Str::prefix('/', $args['suffix'])], $args['dependencies']), |
|
296
|
|
|
$this->addon->version, |
|
297
|
|
|
]; |
|
298
|
|
|
if ('js' === $extension && $args['in_footer']) { |
|
299
|
|
|
$funcArgs[] = true; // load script in the footer |
|
300
|
|
|
} |
|
301
|
|
|
return $funcArgs; |
|
302
|
|
|
} |
|
303
|
|
|
|
|
304
|
|
|
/** |
|
305
|
|
|
* @param string $extension |
|
306
|
|
|
* @return void |
|
307
|
|
|
*/ |
|
308
|
|
|
protected function enqueueAsset($extension, array $args = []) |
|
309
|
|
|
{ |
|
310
|
|
|
if ($args = $this->buildAssetArgs($extension, $args)) { |
|
311
|
|
|
$function = 'js' === $extension |
|
312
|
|
|
? 'wp_enqueue_script' |
|
313
|
|
|
: 'wp_enqueue_style'; |
|
314
|
|
|
call_user_func_array($function, $args); |
|
315
|
|
|
} |
|
316
|
|
|
} |
|
317
|
|
|
|
|
318
|
|
|
/** |
|
319
|
|
|
* @param string $extension |
|
320
|
|
|
* @return void |
|
321
|
|
|
*/ |
|
322
|
|
|
protected function registerAsset($extension, array $args = []) |
|
323
|
|
|
{ |
|
324
|
|
|
if ($args = $this->buildAssetArgs($extension, $args)) { |
|
325
|
|
|
$function = 'js' === $extension |
|
326
|
|
|
? 'wp_register_script' |
|
327
|
|
|
: 'wp_register_style'; |
|
328
|
|
|
call_user_func_array($function, $args); |
|
329
|
|
|
} |
|
330
|
|
|
} |
|
331
|
|
|
|
|
332
|
|
|
/** |
|
333
|
|
|
* @return void |
|
334
|
|
|
*/ |
|
335
|
|
|
abstract protected function setAddon(); |
|
336
|
|
|
} |
|
337
|
|
|
|