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