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