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