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\Helpers\Arr; |
13
|
|
|
use GeminiLabs\SiteReviews\Install; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
15
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
16
|
|
|
use GeminiLabs\SiteReviews\Modules\Translation; |
17
|
|
|
use GeminiLabs\SiteReviews\Request; |
18
|
|
|
|
19
|
|
|
class AdminController extends Controller |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* @return void |
23
|
|
|
* @action site-reviews/export/cleanup |
24
|
|
|
*/ |
25
|
|
|
public function cleanupAfterExport() |
26
|
|
|
{ |
27
|
|
|
glsr(Database::class)->deleteMeta(glsr()->export_key); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @return void |
32
|
|
|
* @action admin_enqueue_scripts |
33
|
|
|
*/ |
34
|
|
|
public function enqueueAssets() |
35
|
|
|
{ |
36
|
|
|
$this->execute(new EnqueueAdminAssets()); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return array |
41
|
|
|
* @filter plugin_action_links_site-reviews/site-reviews.php |
42
|
|
|
*/ |
43
|
|
|
public function filterActionLinks(array $links) |
44
|
|
|
{ |
45
|
|
|
if (glsr()->hasPermission('settings')) { |
46
|
|
|
$links['settings'] = glsr(Builder::class)->a([ |
47
|
|
|
'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=settings'), |
|
|
|
|
48
|
|
|
'text' => _x('Settings', 'admin-text', 'site-reviews'), |
|
|
|
|
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
if (glsr()->hasPermission('documentation')) { |
52
|
|
|
$links['documentation'] = glsr(Builder::class)->a([ |
53
|
|
|
'href' => admin_url('edit.php?post_type='.glsr()->post_type.'&page=documentation'), |
54
|
|
|
'text' => _x('Help', 'admin-text', 'site-reviews'), |
55
|
|
|
]); |
56
|
|
|
} |
57
|
|
|
return $links; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param array $capabilities |
62
|
|
|
* @param string $capability |
63
|
|
|
* @return array |
64
|
|
|
* @filter map_meta_cap |
65
|
|
|
*/ |
66
|
8 |
|
public function filterCreateCapability($capabilities, $capability) |
67
|
|
|
{ |
68
|
8 |
|
if ($capability == 'create_'.glsr()->post_type) { |
69
|
|
|
$capabilities[] = 'do_not_allow'; |
70
|
|
|
} |
71
|
8 |
|
return $capabilities; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param array $items |
76
|
|
|
* @return array |
77
|
|
|
* @filter dashboard_glance_items |
78
|
|
|
*/ |
79
|
|
|
public function filterDashboardGlanceItems($items) |
80
|
|
|
{ |
81
|
|
|
$postCount = wp_count_posts(glsr()->post_type); |
|
|
|
|
82
|
|
|
if (empty($postCount->publish)) { |
83
|
|
|
return $items; |
84
|
|
|
} |
85
|
|
|
$text = _nx('%s Review', '%s Reviews', $postCount->publish, 'admin-text', 'site-reviews'); |
|
|
|
|
86
|
|
|
$text = sprintf($text, number_format_i18n($postCount->publish)); |
|
|
|
|
87
|
|
|
$items = Arr::consolidate($items); |
88
|
|
|
if (glsr()->can('edit_posts')) { |
89
|
|
|
$items[] = glsr(Builder::class)->a($text, [ |
90
|
|
|
'class' => 'glsr-review-count', |
91
|
|
|
'href' => 'edit.php?post_type='.glsr()->post_type, |
92
|
|
|
]); |
93
|
|
|
} else { |
94
|
|
|
$items[] = glsr(Builder::class)->span($text, [ |
95
|
|
|
'class' => 'glsr-review-count', |
96
|
|
|
]); |
97
|
|
|
} |
98
|
|
|
return $items; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @param array $args |
103
|
|
|
* @return array |
104
|
|
|
* @filter export_args |
105
|
|
|
*/ |
106
|
|
|
public function filterExportArgs($args) |
107
|
|
|
{ |
108
|
|
|
if (in_array(Arr::get($args, 'content'), ['all', glsr()->post_type])) { |
109
|
|
|
$this->execute(new ExportRatings(glsr()->args($args))); |
110
|
|
|
} |
111
|
|
|
return $args; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param array $plugins |
116
|
|
|
* @return array |
117
|
|
|
* @filter mce_external_plugins |
118
|
|
|
*/ |
119
|
|
|
public function filterTinymcePlugins($plugins) |
120
|
|
|
{ |
121
|
|
|
if (glsr()->can('edit_posts')) { |
122
|
|
|
$plugins = Arr::consolidate($plugins); |
123
|
|
|
$plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
124
|
|
|
} |
125
|
|
|
return $plugins; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* @return void |
130
|
|
|
* @action admin_init |
131
|
|
|
*/ |
132
|
7 |
|
public function onActivation() |
133
|
|
|
{ |
134
|
7 |
|
if (empty(get_option(glsr()->prefix.'activated'))) { |
|
|
|
|
135
|
7 |
|
glsr(Install::class)->run(); |
136
|
7 |
|
update_option(glsr()->prefix.'activated', true); |
|
|
|
|
137
|
|
|
} |
138
|
7 |
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @return void |
142
|
|
|
* @action import_end |
143
|
|
|
*/ |
144
|
|
|
public function onImportEnd() |
145
|
|
|
{ |
146
|
|
|
$this->execute(new ImportRatings()); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return void |
151
|
|
|
* @action admin_head |
152
|
|
|
*/ |
153
|
|
|
public function printInlineStyle() |
154
|
|
|
{ |
155
|
|
|
echo '<style type="text/css">a[href="edit.php?post_type=site-review&page=addons"]:not(.current):not(:hover) { color:#F6E05E!important; }</style>'; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @return void |
160
|
|
|
* @action admin_init |
161
|
|
|
*/ |
162
|
7 |
|
public function registerTinymcePopups() |
163
|
|
|
{ |
164
|
7 |
|
$this->execute(new RegisterTinymcePopups([ |
165
|
7 |
|
'site_reviews' => _x('Recent Reviews', 'admin-text', 'site-reviews'), |
|
|
|
|
166
|
7 |
|
'site_reviews_form' => _x('Submit a Review', 'admin-text', 'site-reviews'), |
167
|
7 |
|
'site_reviews_summary' => _x('Summary of Reviews', 'admin-text', 'site-reviews'), |
168
|
|
|
])); |
169
|
7 |
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* @param string $editorId |
173
|
|
|
* @return void|null |
174
|
|
|
* @action media_buttons |
175
|
|
|
*/ |
176
|
|
|
public function renderTinymceButton($editorId) |
177
|
|
|
{ |
178
|
|
|
$allowedEditors = glsr()->filterArray('tinymce/editor-ids', ['content'], $editorId); |
179
|
|
|
if ('post' !== glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
180
|
|
|
return; |
181
|
|
|
} |
182
|
|
|
$shortcodes = []; |
183
|
|
|
foreach (glsr()->retrieve('mce', []) as $shortcode => $values) { |
184
|
|
|
$shortcodes[$shortcode] = $values; |
185
|
|
|
} |
186
|
|
|
if (empty($shortcodes)) { |
187
|
|
|
return; |
188
|
|
|
} |
189
|
|
|
glsr()->render('partials/editor/tinymce', [ |
190
|
|
|
'shortcodes' => $shortcodes, |
191
|
|
|
]); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return void |
196
|
|
|
* @action site-reviews/route/ajax/search-posts |
197
|
|
|
*/ |
198
|
|
|
public function searchPostsAjax(Request $request) |
199
|
|
|
{ |
200
|
|
|
$results = glsr(Database::class)->searchPosts($request->search); |
201
|
|
|
wp_send_json_success([ |
|
|
|
|
202
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
|
|
|
203
|
|
|
'items' => $results, |
204
|
|
|
]); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* @return void |
209
|
|
|
* @action site-reviews/route/ajax/search-translations |
210
|
|
|
*/ |
211
|
|
|
public function searchTranslationsAjax(Request $request) |
212
|
|
|
{ |
213
|
|
|
if (empty($request->exclude)) { |
214
|
|
|
$request->exclude = []; |
215
|
|
|
} |
216
|
|
|
$results = glsr(Translation::class) |
217
|
|
|
->search($request->search) |
218
|
|
|
->exclude() |
219
|
|
|
->exclude($request->exclude) |
220
|
|
|
->renderResults(); |
221
|
|
|
wp_send_json_success([ |
|
|
|
|
222
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
|
|
|
223
|
|
|
'items' => $results, |
224
|
|
|
]); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* @return void |
229
|
|
|
* @action site-reviews/route/ajax/search-users |
230
|
|
|
*/ |
231
|
|
|
public function searchUsersAjax(Request $request) |
232
|
|
|
{ |
233
|
|
|
$results = glsr(Database::class)->searchUsers($request->search); |
234
|
|
|
wp_send_json_success([ |
|
|
|
|
235
|
|
|
'empty' => '<div>'._x('Nothing found.', 'admin-text', 'site-reviews').'</div>', |
|
|
|
|
236
|
|
|
'items' => $results, |
237
|
|
|
]); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* @return void |
242
|
|
|
* @action site-reviews/route/ajax/toggle-pinned |
243
|
|
|
*/ |
244
|
|
|
public function togglePinnedAjax(Request $request) |
245
|
|
|
{ |
246
|
|
|
wp_send_json_success([ |
|
|
|
|
247
|
|
|
'notices' => glsr(Notice::class)->get(), |
248
|
|
|
'pinned' => $this->execute(new TogglePinned($request->toArray())), |
249
|
|
|
]); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
/** |
253
|
|
|
* @return void |
254
|
|
|
* @action site-reviews/route/ajax/toggle-status |
255
|
|
|
*/ |
256
|
|
|
public function toggleStatusAjax(Request $request) |
257
|
|
|
{ |
258
|
|
|
wp_send_json_success( |
|
|
|
|
259
|
|
|
$this->execute(new ToggleStatus($request->post_id, $request->status)) |
260
|
|
|
); |
261
|
|
|
} |
262
|
|
|
} |
263
|
|
|
|