1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Commands\EnqueueAdminAssets; |
7
|
|
|
use GeminiLabs\SiteReviews\Commands\RegisterTinymcePopups; |
8
|
|
|
use GeminiLabs\SiteReviews\Database\CountsManager; |
9
|
|
|
use GeminiLabs\SiteReviews\Database\OptionManager; |
10
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
11
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
12
|
|
|
use GeminiLabs\SiteReviews\Modules\Console; |
13
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
15
|
|
|
use GeminiLabs\SiteReviews\Modules\System; |
16
|
|
|
use GeminiLabs\SiteReviews\Modules\Upgrader\Upgrade_4_0_2; |
17
|
|
|
|
18
|
|
|
class AdminController extends Controller |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return void |
22
|
|
|
* @action admin_enqueue_scripts |
23
|
|
|
*/ |
24
|
|
|
public function enqueueAssets() |
25
|
|
|
{ |
26
|
|
|
$command = new EnqueueAdminAssets([ |
27
|
|
|
'pointers' => [[ |
28
|
|
|
'content' => __('You can pin exceptional reviews so that they are always shown first.', 'site-reviews'), |
29
|
|
|
'id' => 'glsr-pointer-pinned', |
30
|
|
|
'position' => [ |
31
|
|
|
'edge' => 'right', |
32
|
|
|
'align' => 'middle', |
33
|
|
|
], |
34
|
|
|
'screen' => Application::POST_TYPE, |
35
|
|
|
'target' => '#misc-pub-pinned', |
36
|
|
|
'title' => __('Pin Your Reviews', 'site-reviews'), |
37
|
|
|
]], |
38
|
|
|
]); |
39
|
|
|
$this->execute($command); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return array |
44
|
|
|
* @filter plugin_action_links_site-reviews/site-reviews.php |
45
|
|
|
*/ |
46
|
|
|
public function filterActionLinks(array $links) |
47
|
|
|
{ |
48
|
|
|
$links['documentation'] = glsr(Builder::class)->a(__('Help', 'site-reviews'), [ |
49
|
|
|
'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=documentation'), |
50
|
|
|
]); |
51
|
|
|
$links['settings'] = glsr(Builder::class)->a(__('Settings', 'site-reviews'), [ |
52
|
|
|
'href' => admin_url('edit.php?post_type='.Application::POST_TYPE.'&page=settings'), |
53
|
|
|
]); |
54
|
|
|
return $links; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array $capabilities |
59
|
|
|
* @param string $capability |
60
|
|
|
* @return array |
61
|
|
|
* @filter map_meta_cap |
62
|
|
|
*/ |
63
|
1 |
|
public function filterCreateCapability($capabilities, $capability) |
64
|
|
|
{ |
65
|
1 |
|
if ($capability == 'create_'.Application::POST_TYPE) { |
66
|
|
|
$capabilities[] = 'do_not_allow'; |
67
|
|
|
} |
68
|
1 |
|
return $capabilities; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param array $items |
73
|
|
|
* @return array |
74
|
|
|
* @filter dashboard_glance_items |
75
|
|
|
*/ |
76
|
|
|
public function filterDashboardGlanceItems($items) |
77
|
|
|
{ |
78
|
|
|
$postCount = wp_count_posts(Application::POST_TYPE); |
79
|
|
|
if (empty($postCount->publish)) { |
80
|
|
|
return $items; |
81
|
|
|
} |
82
|
|
|
$text = _n('%s Review', '%s Reviews', $postCount->publish, 'site-reviews'); |
83
|
|
|
$text = sprintf($text, number_format_i18n($postCount->publish)); |
84
|
|
|
$items = Arr::consolidateArray($items); |
85
|
|
|
$items[] = current_user_can(get_post_type_object(Application::POST_TYPE)->cap->edit_posts) |
86
|
|
|
? glsr(Builder::class)->a($text, [ |
87
|
|
|
'class' => 'glsr-review-count', |
88
|
|
|
'href' => 'edit.php?post_type='.Application::POST_TYPE, |
89
|
|
|
]) |
90
|
|
|
: glsr(Builder::class)->span($text, [ |
91
|
|
|
'class' => 'glsr-review-count', |
92
|
|
|
]); |
93
|
|
|
return $items; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param array $plugins |
98
|
|
|
* @return array |
99
|
|
|
* @filter mce_external_plugins |
100
|
|
|
*/ |
101
|
|
|
public function filterTinymcePlugins($plugins) |
102
|
|
|
{ |
103
|
|
|
if (current_user_can('edit_posts') || current_user_can('edit_pages')) { |
104
|
|
|
$plugins = Arr::consolidateArray($plugins); |
105
|
|
|
$plugins['glsr_shortcode'] = glsr()->url('assets/scripts/mce-plugin.js'); |
106
|
|
|
} |
107
|
|
|
return $plugins; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @return void |
112
|
|
|
* @action admin_init |
113
|
|
|
*/ |
114
|
1 |
|
public function registerTinymcePopups() |
115
|
|
|
{ |
116
|
1 |
|
$command = new RegisterTinymcePopups([ |
117
|
1 |
|
'site_reviews' => esc_html__('Recent Reviews', 'site-reviews'), |
118
|
1 |
|
'site_reviews_form' => esc_html__('Submit a Review', 'site-reviews'), |
119
|
1 |
|
'site_reviews_summary' => esc_html__('Summary of Reviews', 'site-reviews'), |
120
|
|
|
]); |
121
|
1 |
|
$this->execute($command); |
122
|
1 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param string $editorId |
126
|
|
|
* @return void|null |
127
|
|
|
* @action media_buttons |
128
|
|
|
*/ |
129
|
|
|
public function renderTinymceButton($editorId) |
130
|
|
|
{ |
131
|
|
|
$allowedEditors = apply_filters('site-reviews/tinymce/editor-ids', ['content'], $editorId); |
132
|
|
|
if ('post' != glsr_current_screen()->base || !in_array($editorId, $allowedEditors)) { |
133
|
|
|
return; |
134
|
|
|
} |
135
|
|
|
$shortcodes = []; |
136
|
|
|
foreach (glsr()->mceShortcodes as $shortcode => $values) { |
137
|
|
|
$shortcodes[$shortcode] = $values; |
138
|
|
|
} |
139
|
|
|
if (empty($shortcodes)) { |
140
|
|
|
return; |
141
|
|
|
} |
142
|
|
|
glsr()->render('partials/editor/tinymce', [ |
143
|
|
|
'shortcodes' => $shortcodes, |
144
|
|
|
]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* @return void |
149
|
|
|
*/ |
150
|
|
|
public function routerClearConsole() |
151
|
|
|
{ |
152
|
|
|
glsr(Console::class)->clear(); |
153
|
|
|
glsr(Notice::class)->addSuccess(__('Console cleared.', 'site-reviews')); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @return void |
158
|
|
|
*/ |
159
|
|
|
public function routerFetchConsole() |
160
|
|
|
{ |
161
|
|
|
glsr(Notice::class)->addSuccess(__('Console reloaded.', 'site-reviews')); |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* @param bool $showNotice |
166
|
|
|
* @return void |
167
|
|
|
*/ |
168
|
1 |
|
public function routerCountReviews($showNotice = true) |
169
|
|
|
{ |
170
|
1 |
|
glsr(CountsManager::class)->updateAll(); |
171
|
1 |
|
glsr(OptionManager::class)->set('last_review_count', current_time('timestamp')); |
172
|
1 |
|
if ($showNotice) { |
173
|
|
|
glsr(Notice::class)->clear()->addSuccess(__('Recalculated rating counts.', 'site-reviews')); |
174
|
|
|
} |
175
|
1 |
|
} |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* @return void |
179
|
|
|
*/ |
180
|
|
|
public function routerMigrateReviews() |
181
|
|
|
{ |
182
|
|
|
glsr(Upgrade_4_0_2::class)->protectMetaKeys(); |
183
|
|
|
glsr(Notice::class)->clear()->addSuccess(__('All reviews have been migrated.', 'site-reviews')); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* @return void |
188
|
|
|
*/ |
189
|
|
|
public function routerDownloadConsole() |
190
|
|
|
{ |
191
|
|
|
$this->download(Application::ID.'-console.txt', glsr(Console::class)->get()); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* @return void |
196
|
|
|
*/ |
197
|
|
|
public function routerDownloadSystemInfo() |
198
|
|
|
{ |
199
|
|
|
$this->download(Application::ID.'-system-info.txt', glsr(System::class)->get()); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @return void |
204
|
|
|
*/ |
205
|
|
|
public function routerExportSettings() |
206
|
|
|
{ |
207
|
|
|
$this->download(Application::ID.'-settings.json', glsr(OptionManager::class)->json()); |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* @return void |
212
|
|
|
*/ |
213
|
|
|
public function routerImportSettings() |
214
|
|
|
{ |
215
|
|
|
$file = $_FILES['import-file']; |
216
|
|
|
if (UPLOAD_ERR_OK !== $file['error']) { |
217
|
|
|
return glsr(Notice::class)->addError($this->getUploadError($file['error'])); |
218
|
|
|
} |
219
|
|
|
if ('application/json' !== $file['type'] || !Str::endsWith('.json', $file['name'])) { |
220
|
|
|
return glsr(Notice::class)->addError(__('Please use a valid Site Reviews settings file.', 'site-reviews')); |
221
|
|
|
} |
222
|
|
|
$settings = json_decode(file_get_contents($file['tmp_name']), true); |
223
|
|
|
if (empty($settings)) { |
224
|
|
|
return glsr(Notice::class)->addWarning(__('There were no settings found to import.', 'site-reviews')); |
225
|
|
|
} |
226
|
|
|
glsr(OptionManager::class)->set(glsr(OptionManager::class)->normalize($settings)); |
227
|
|
|
glsr(Notice::class)->addSuccess(__('Settings imported.', 'site-reviews')); |
228
|
|
|
} |
229
|
|
|
|
230
|
|
|
/** |
231
|
|
|
* @param int $errorCode |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
protected function getUploadError($errorCode) |
235
|
|
|
{ |
236
|
|
|
$errors = [ |
237
|
|
|
UPLOAD_ERR_INI_SIZE => __('The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'site-reviews'), |
238
|
|
|
UPLOAD_ERR_FORM_SIZE => __('The uploaded file is too big.', 'site-reviews'), |
239
|
|
|
UPLOAD_ERR_PARTIAL => __('The uploaded file was only partially uploaded.', 'site-reviews'), |
240
|
|
|
UPLOAD_ERR_NO_FILE => __('No file was uploaded.', 'site-reviews'), |
241
|
|
|
UPLOAD_ERR_NO_TMP_DIR => __('Missing a temporary folder.', 'site-reviews'), |
242
|
|
|
UPLOAD_ERR_CANT_WRITE => __('Failed to write file to disk.', 'site-reviews'), |
243
|
|
|
UPLOAD_ERR_EXTENSION => __('A PHP extension stopped the file upload.', 'site-reviews'), |
244
|
|
|
]; |
245
|
|
|
return Arr::get($errors, $errorCode, __('Unknown upload error.', 'site-reviews')); |
246
|
|
|
} |
247
|
|
|
} |
248
|
|
|
|