1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Commands\EnqueueAdminAssets; |
6
|
|
|
use GeminiLabs\SiteReviews\Commands\ExportRatings; |
7
|
|
|
use GeminiLabs\SiteReviews\Commands\ImportRatings; |
8
|
|
|
use GeminiLabs\SiteReviews\Commands\RegisterTinymcePopups; |
9
|
|
|
use GeminiLabs\SiteReviews\Commands\TogglePinned; |
10
|
|
|
use GeminiLabs\SiteReviews\Commands\ToggleStatus; |
11
|
|
|
use GeminiLabs\SiteReviews\Database; |
12
|
|
|
use GeminiLabs\SiteReviews\Defaults\ColumnFilterbyDefaults; |
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
14
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
15
|
|
|
use GeminiLabs\SiteReviews\Install; |
16
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
17
|
|
|
use GeminiLabs\SiteReviews\Modules\Migrate; |
18
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
19
|
|
|
use GeminiLabs\SiteReviews\Modules\Queue; |
20
|
|
|
use GeminiLabs\SiteReviews\Modules\Sanitizer; |
21
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
22
|
|
|
use GeminiLabs\SiteReviews\Request; |
23
|
|
|
use GeminiLabs\SiteReviews\Role; |
24
|
|
|
|
25
|
|
|
class AdminController extends Controller |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @return void |
29
|
|
|
* @action admin_enqueue_scripts |
30
|
|
|
*/ |
31
|
|
|
public function enqueueAssets() |
32
|
|
|
{ |
33
|
|
|
$this->execute(new EnqueueAdminAssets()); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @return array |
38
|
|
|
* @filter plugin_action_links_site-reviews/site-reviews.php |
39
|
|
|
*/ |
40
|
|
|
public function filterActionLinks(array $links) |
41
|
|
|
{ |
42
|
|
|
if (glsr()->hasPermission('settings')) { |
43
|
|
|
$links['settings'] = glsr(Builder::class)->a([ |
44
|
|
|
'href' => glsr_admin_url('settings'), |
45
|
|
|
'text' => _x('Settings', 'admin-text', 'site-reviews'), |
46
|
|
|
]); |
47
|
|
|
} |
48
|
|
|
if (glsr()->hasPermission('documentation')) { |
49
|
|
|
$links['documentation'] = glsr(Builder::class)->a([ |
50
|
|
|
'href' => glsr_admin_url('documentation'), |
51
|
|
|
'text' => _x('Help', 'admin-text', 'site-reviews'), |
52
|
|
|
]); |
53
|
|
|
} |
54
|
|
|
return $links; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $capabilities |
59
|
|
|
* @param string $capability |
60
|
|
|
* @param int $userId |
61
|
|
|
* @param array $args |
62
|
|
|
* @return array |
63
|
|
|
* @filter map_meta_cap |
64
|
|
|
*/ |
65
|
8 |
|
public function filterCapabilities($capabilities, $capability, $userId, $args) |
66
|
|
|
{ |
67
|
8 |
|
if ('respond_to_'.glsr()->post_type !== $capability) { |
68
|
8 |
|
return $capabilities; |
69
|
|
|
} |
70
|
|
|
$review = glsr_get_review(Arr::get($args, 0)); |
71
|
|
|
if (!$review->isValid()) { |
72
|
|
|
return ['do_not_allow']; |
73
|
|
|
} |
74
|
|
|
$capabilities = []; |
75
|
|
|
$respondToReviews = glsr(Role::class)->capability('respond_to_posts'); |
76
|
|
|
if ($userId == $review->author_id) { |
77
|
|
|
$capabilities[] = $respondToReviews; // they are the author of the review |
78
|
|
|
} |
79
|
|
|
foreach ($review->assignedPosts() as $assignedPost) { |
80
|
|
|
if ($userId == $assignedPost->post_author) { |
81
|
|
|
$capabilities[] = $respondToReviews; // they are the author of the post that the review is assigned to |
82
|
|
|
break; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
if (!in_array($respondToReviews, $capabilities)) { |
86
|
|
|
$capabilities[] = glsr(Role::class)->capability('respond_to_others_posts'); |
87
|
|
|
} |
88
|
|
|
return array_unique($capabilities); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param array $items |
93
|
|
|
* @return array |
94
|
|
|
* @filter dashboard_glance_items |
95
|
|
|
*/ |
96
|
|
|
public function filterDashboardGlanceItems($items) |
97
|
|
|
{ |
98
|
|
|
$postCount = wp_count_posts(glsr()->post_type); |
99
|
|
|
if (empty($postCount->publish)) { |
100
|
|
|
return $items; |
101
|
|
|
} |
102
|
|
|
$text = _nx('%s Review', '%s Reviews', $postCount->publish, 'admin-text', 'site-reviews'); |
103
|
|
|
$text = sprintf($text, number_format_i18n($postCount->publish)); |
104
|
|
|
$items = Arr::consolidate($items); |
105
|
|
|
if (glsr()->can('edit_posts')) { |
106
|
|
|
$items[] = glsr(Builder::class)->a($text, [ |
107
|
|
|
'class' => 'glsr-review-count', |
108
|
|
|
'href' => glsr_admin_url(), |
109
|
|
|
]); |
110
|
|
|
} else { |
111
|
|
|
$items[] = glsr(Builder::class)->span($text, [ |
112
|
|
|
'class' => 'glsr-review-count', |
113
|
|
|
]); |
114
|
|
|
} |
115
|
|
|
return $items; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @param array $args |
120
|
|
|
* @return array |
121
|
|
|
* @filter export_args |
122
|
|
|
*/ |
123
|
|
|
public function filterExportArgs($args) |
124
|
|
|
{ |
125
|
|
|
if (in_array(Arr::get($args, 'content'), ['all', glsr()->post_type])) { |
126
|
|
|
$this->execute(new ExportRatings(glsr()->args($args))); |
127
|
|
|
} |
128
|
|
|
return $args; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param array $plugins |
133
|
|
|
* @return array |
134
|
|
|
* @filter mce_external_plugins |
135
|
|
|
*/ |
136
|
|
|
public function filterTinymcePlugins($plugins) |
137
|
|
|
{ |
138
|
|
|
if (glsr()->can('edit_posts')) { |
139
|
|
|
$plugins = Arr::consolidate($plugins); |
140
|
|
|
$plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
141
|
|
|
} |
142
|
|
|
return $plugins; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return void |
147
|
|
|
* @action admin_init |
148
|
|
|
*/ |
149
|
7 |
|
public function onActivation() |
150
|
|
|
{ |
151
|
7 |
|
if (empty(get_option(glsr()->prefix.'activated'))) { |
152
|
7 |
|
glsr(Install::class)->run(); |
153
|
7 |
|
glsr(Migrate::class)->run(); |
154
|
7 |
|
update_option(glsr()->prefix.'activated', true); |
155
|
|
|
} |
156
|
7 |
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return void |
160
|
|
|
* @action import_end |
161
|
|
|
*/ |
162
|
|
|
public function onImportEnd() |
163
|
|
|
{ |
164
|
|
|
$this->execute(new ImportRatings()); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @return void |
169
|
|
|
* @action admin_head |
170
|
|
|
*/ |
171
|
|
|
public function printInlineStyle() |
172
|
|
|
{ |
173
|
|
|
echo '<style type="text/css">a[href="edit.php?post_type=site-review&page='.Str::dashCase(glsr()->prefix).'addons"]:not(.current),a[href="edit.php?post_type=site-review&page='.Str::dashCase(glsr()->prefix).'addons"]:focus,a[href="edit.php?post_type=site-review&page='.Str::dashCase(glsr()->prefix).'addons"]:hover{color:#F6E05E!important;}</style>'; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @return void |
178
|
|
|
* @action admin_init |
179
|
|
|
*/ |
180
|
7 |
|
public function registerTinymcePopups() |
181
|
|
|
{ |
182
|
7 |
|
$this->execute(new RegisterTinymcePopups([ |
183
|
7 |
|
'site_reviews' => _x('Recent Reviews', 'admin-text', 'site-reviews'), |
184
|
7 |
|
'site_reviews_form' => _x('Submit a Review', 'admin-text', 'site-reviews'), |
185
|
7 |
|
'site_reviews_summary' => _x('Summary of Reviews', 'admin-text', 'site-reviews'), |
186
|
|
|
])); |
187
|
7 |
|
} |
188
|
|
|
|
189
|
|
|
/** |
190
|
|
|
* @param string $editorId |
191
|
|
|
* @return void|null |
192
|
|
|
* @action media_buttons |
193
|
|
|
*/ |
194
|
|
|
public function renderTinymceButton($editorId) |
195
|
|
|
{ |
196
|
|
|
$allowedEditors = glsr()->filterArray('tinymce/editor-ids', ['content'], $editorId); |
197
|
|
|
if ('post' !== glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
198
|
|
|
return; |
199
|
|
|
} |
200
|
|
|
$shortcodes = []; |
201
|
|
|
foreach (glsr()->retrieveAs('array', 'mce', []) as $shortcode => $values) { |
202
|
|
|
$shortcodes[$shortcode] = $values; |
203
|
|
|
} |
204
|
|
|
if (empty($shortcodes)) { |
205
|
|
|
return; |
206
|
|
|
} |
207
|
|
|
glsr()->render('partials/editor/tinymce', [ |
208
|
|
|
'shortcodes' => $shortcodes, |
209
|
|
|
]); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* @return void |
214
|
|
|
* @action admin_init |
215
|
|
|
*/ |
216
|
7 |
|
public function scheduleMigration() |
217
|
|
|
{ |
218
|
7 |
|
if ($this->isReviewAdminScreen() |
219
|
7 |
|
&& !defined('GLSR_UNIT_TESTS') |
220
|
7 |
|
&& !glsr(Queue::class)->isPending('queue/migration')) { |
221
|
|
|
if (glsr(Migrate::class)->isMigrationNeeded() || glsr(Database::class)->isMigrationNeeded()) { |
222
|
|
|
glsr(Queue::class)->once(time() + MINUTE_IN_SECONDS, 'queue/migration'); |
223
|
|
|
} |
224
|
|
|
} |
225
|
7 |
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return void |
229
|
|
|
* @action site-reviews/route/ajax/filter-assigned_post |
230
|
|
|
*/ |
231
|
|
|
public function searchAssignedPostsAjax(Request $request) |
232
|
|
|
{ |
233
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
234
|
|
|
$results = glsr(Database::class)->searchAssignedPosts($search)->results(); |
235
|
|
|
wp_send_json_success([ |
236
|
|
|
'items' => $results, |
237
|
|
|
]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return void |
242
|
|
|
* @action site-reviews/route/ajax/filter-assigned_user |
243
|
|
|
*/ |
244
|
|
|
public function searchAssignedUsersAjax(Request $request) |
245
|
|
|
{ |
246
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
247
|
|
|
$results = glsr(Database::class)->searchAssignedUsers($search)->results(); |
248
|
|
|
wp_send_json_success([ |
249
|
|
|
'items' => $results, |
250
|
|
|
]); |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
/** |
254
|
|
|
* @return void |
255
|
|
|
* @action site-reviews/route/ajax/filter-author |
256
|
|
|
*/ |
257
|
|
|
public function searchAuthorsAjax(Request $request) |
258
|
|
|
{ |
259
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
260
|
|
|
$results = glsr(Database::class)->searchUsers($search)->results(); |
261
|
|
|
wp_send_json_success([ |
262
|
|
|
'items' => $results, |
263
|
|
|
]); |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
/** |
267
|
|
|
* @return void |
268
|
|
|
* @action site-reviews/route/ajax/search-posts |
269
|
|
|
*/ |
270
|
|
|
public function searchPostsAjax(Request $request) |
271
|
|
|
{ |
272
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
273
|
|
|
$results = glsr(Database::class)->searchPosts($search)->render(); |
274
|
|
|
wp_send_json_success([ |
275
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
276
|
|
|
'items' => $results, |
277
|
|
|
]); |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* @return void |
282
|
|
|
* @action site-reviews/route/ajax/search-translations |
283
|
|
|
*/ |
284
|
|
|
public function searchTranslationsAjax(Request $request) |
285
|
|
|
{ |
286
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
287
|
|
|
$exclude = glsr(Sanitizer::class)->sanitizeArray($request->exclude); |
288
|
|
|
$results = glsr(Translation::class) |
289
|
|
|
->search($search) |
290
|
|
|
->exclude() |
291
|
|
|
->exclude($exclude) |
292
|
|
|
->renderResults(); |
293
|
|
|
wp_send_json_success([ |
294
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
295
|
|
|
'items' => $results, |
296
|
|
|
]); |
297
|
|
|
} |
298
|
|
|
|
299
|
|
|
/** |
300
|
|
|
* @return void |
301
|
|
|
* @action site-reviews/route/ajax/search-users |
302
|
|
|
*/ |
303
|
|
|
public function searchUsersAjax(Request $request) |
304
|
|
|
{ |
305
|
|
|
$search = glsr(Sanitizer::class)->sanitizeText($request->search); |
306
|
|
|
$results = glsr(Database::class)->searchUsers($search)->render(); |
307
|
|
|
wp_send_json_success([ |
308
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
309
|
|
|
'items' => $results, |
310
|
|
|
]); |
311
|
|
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* @return void |
315
|
|
|
* @action site-reviews/route/ajax/toggle-filters |
316
|
|
|
*/ |
317
|
|
|
public function toggleFiltersAjax(Request $request) |
318
|
|
|
{ |
319
|
|
|
if ($userId = get_current_user_id()) { |
320
|
|
|
$filters = array_keys(glsr(ColumnFilterbyDefaults::class)->defaults()); |
321
|
|
|
$enabled = glsr(Sanitizer::class)->sanitizeArrayString($request->enabled); |
322
|
|
|
$enabled = array_intersect($filters, $enabled); |
323
|
|
|
update_user_meta($userId, 'edit_'.glsr()->post_type.'_filters', $enabled); |
324
|
|
|
} |
325
|
|
|
wp_send_json_success(); |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* @return void |
330
|
|
|
* @action site-reviews/route/ajax/toggle-pinned |
331
|
|
|
*/ |
332
|
|
|
public function togglePinnedAjax(Request $request) |
333
|
|
|
{ |
334
|
|
|
wp_send_json_success([ |
335
|
|
|
'notices' => glsr(Notice::class)->get(), |
336
|
|
|
'pinned' => $this->execute(new TogglePinned($request->toArray())), |
337
|
|
|
]); |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
/** |
341
|
|
|
* @return void |
342
|
|
|
* @action site-reviews/route/ajax/toggle-status |
343
|
|
|
*/ |
344
|
|
|
public function toggleStatusAjax(Request $request) |
345
|
|
|
{ |
346
|
|
|
wp_send_json_success( |
347
|
|
|
$this->execute(new ToggleStatus($request->toArray())) |
348
|
|
|
); |
349
|
|
|
} |
350
|
|
|
} |
351
|
|
|
|