1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\SiteReviews\Controllers; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\SiteReviews\Application; |
6
|
|
|
use GeminiLabs\SiteReviews\Controllers\EditorController\Customization; |
7
|
|
|
use GeminiLabs\SiteReviews\Controllers\EditorController\Labels; |
8
|
|
|
use GeminiLabs\SiteReviews\Controllers\EditorController\Metaboxes; |
9
|
|
|
use GeminiLabs\SiteReviews\Controllers\ListTableController\Columns; |
10
|
|
|
use GeminiLabs\SiteReviews\Database; |
11
|
|
|
use GeminiLabs\SiteReviews\Database\ReviewManager; |
12
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
13
|
|
|
use GeminiLabs\SiteReviews\Helper; |
14
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Builder; |
15
|
|
|
use GeminiLabs\SiteReviews\Modules\Html\Template; |
16
|
|
|
use GeminiLabs\SiteReviews\Modules\Notice; |
17
|
|
|
use GeminiLabs\SiteReviews\Review; |
18
|
|
|
use WP_Post; |
19
|
|
|
|
20
|
|
|
class EditorController extends Controller |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return void |
24
|
|
|
* @action admin_enqueue_scripts |
25
|
|
|
*/ |
26
|
|
|
public function customizePostStatusLabels() |
27
|
|
|
{ |
28
|
|
|
glsr(Labels::class)->customizePostStatusLabels(); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param array $settings |
33
|
|
|
* @return array |
34
|
|
|
* @filter wp_editor_settings |
35
|
|
|
*/ |
36
|
|
|
public function filterEditorSettings($settings) |
37
|
|
|
{ |
38
|
|
|
return glsr(Customization::class)->filterEditorSettings( |
39
|
|
|
glsr(Helper::class)->consolidateArray($settings) |
40
|
|
|
); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script. |
45
|
|
|
* @param string $html |
46
|
|
|
* @return string |
47
|
|
|
* @filter the_editor |
48
|
|
|
*/ |
49
|
|
|
public function filterEditorTextarea($html) |
50
|
|
|
{ |
51
|
|
|
return glsr(Customization::class)->filterEditorTextarea($html); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param bool $protected |
56
|
|
|
* @param string $metaKey |
57
|
|
|
* @param string $metaType |
58
|
|
|
* @return bool |
59
|
|
|
* @filter is_protected_meta |
60
|
|
|
*/ |
61
|
|
|
public function filterIsProtectedMeta($protected, $metaKey, $metaType) |
62
|
|
|
{ |
63
|
|
|
if ('post' == $metaType && Application::POST_TYPE == get_post_type()) { |
64
|
|
|
$values = glsr(CreateReviewDefaults::class)->unguarded(); |
65
|
|
|
$values = glsr(Helper::class)->prefixArrayKeys($values); |
66
|
|
|
if (array_key_exists($metaKey, $values)) { |
67
|
|
|
$protected = false; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
return $protected; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @param string $translation |
75
|
|
|
* @param string $test |
76
|
|
|
* @param string $domain |
77
|
|
|
* @return string |
78
|
|
|
* @filter gettext |
79
|
|
|
*/ |
80
|
7 |
|
public function filterPostStatusLabels($translation, $text, $domain) |
81
|
|
|
{ |
82
|
7 |
|
return glsr(Labels::class)->filterPostStatusLabels($translation, $text, $domain); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $translation |
87
|
|
|
* @param string $test |
88
|
|
|
* @param string $domain |
89
|
|
|
* @return string |
90
|
|
|
* @filter gettext_with_context |
91
|
|
|
*/ |
92
|
2 |
|
public function filterPostStatusLabelsWithContext($translation, $text, $context, $domain) |
93
|
|
|
{ |
94
|
2 |
|
return glsr(Labels::class)->filterPostStatusLabels($translation, $text, $domain); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param array $messages |
99
|
|
|
* @return array |
100
|
|
|
* @filter post_updated_messages |
101
|
|
|
*/ |
102
|
|
|
public function filterUpdateMessages($messages) |
103
|
|
|
{ |
104
|
|
|
return glsr(Labels::class)->filterUpdateMessages( |
105
|
|
|
glsr(Helper::class)->consolidateArray($messages) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return void |
111
|
|
|
* @action add_meta_boxes_{Application::POST_TYPE} |
112
|
|
|
*/ |
113
|
|
|
public function registerMetaBoxes($post) |
114
|
|
|
{ |
115
|
|
|
add_meta_box(Application::ID.'_assigned_to', __('Assigned To', 'site-reviews'), [$this, 'renderAssignedToMetabox'], null, 'side'); |
116
|
|
|
add_meta_box(Application::ID.'_review', __('Details', 'site-reviews'), [$this, 'renderDetailsMetaBox'], null, 'side'); |
117
|
|
|
if ('local' != glsr(Database::class)->get($post->ID, 'review_type')) { |
118
|
|
|
return; |
119
|
|
|
} |
120
|
|
|
add_meta_box(Application::ID.'_response', __('Respond Publicly', 'site-reviews'), [$this, 'renderResponseMetaBox'], null, 'normal'); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @return void |
125
|
|
|
* @action admin_print_scripts |
126
|
|
|
*/ |
127
|
|
|
public function removeAutosave() |
128
|
|
|
{ |
129
|
|
|
glsr(Customization::class)->removeAutosave(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return void |
134
|
|
|
* @action admin_menu |
135
|
|
|
*/ |
136
|
|
|
public function removeMetaBoxes() |
137
|
|
|
{ |
138
|
|
|
glsr(Customization::class)->removeMetaBoxes(); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @return void |
143
|
|
|
*/ |
144
|
1 |
|
public function removePostTypeSupport() |
145
|
|
|
{ |
146
|
1 |
|
glsr(Customization::class)->removePostTypeSupport(); |
147
|
1 |
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param WP_Post $post |
151
|
|
|
* @return void |
152
|
|
|
* @callback add_meta_box |
153
|
|
|
*/ |
154
|
|
|
public function renderAssignedToMetabox($post) |
155
|
|
|
{ |
156
|
|
|
if (!$this->isReviewPostType($post)) { |
157
|
|
|
return; |
158
|
|
|
} |
159
|
|
|
$assignedTo = (string) glsr(Database::class)->get($post->ID, 'assigned_to'); |
160
|
|
|
wp_nonce_field('assigned_to', '_nonce-assigned-to', false); |
161
|
|
|
glsr()->render('partials/editor/metabox-assigned-to', [ |
162
|
|
|
'id' => $assignedTo, |
163
|
|
|
'template' => $this->buildAssignedToTemplate($assignedTo, $post), |
164
|
|
|
]); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param WP_Post $post |
169
|
|
|
* @return void |
170
|
|
|
* @callback add_meta_box |
171
|
|
|
*/ |
172
|
|
|
public function renderDetailsMetaBox($post) |
173
|
|
|
{ |
174
|
|
|
if (!$this->isReviewPostType($post)) { |
175
|
|
|
return; |
176
|
|
|
} |
177
|
|
|
$review = glsr_get_review($post); |
178
|
|
|
glsr()->render('partials/editor/metabox-details', [ |
179
|
|
|
'button' => $this->buildDetailsMetaBoxRevertButton($review, $post), |
180
|
|
|
'metabox' => $this->normalizeDetailsMetaBox($review), |
181
|
|
|
]); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @return void |
186
|
|
|
* @action post_submitbox_misc_actions |
187
|
|
|
*/ |
188
|
|
|
public function renderPinnedInPublishMetaBox() |
189
|
|
|
{ |
190
|
|
|
if (!$this->isReviewPostType(get_post())) { |
191
|
|
|
return; |
192
|
|
|
} |
193
|
|
|
glsr(Template::class)->render('partials/editor/pinned', [ |
194
|
|
|
'context' => [ |
195
|
|
|
'no' => __('No', 'site-reviews'), |
196
|
|
|
'yes' => __('Yes', 'site-reviews'), |
197
|
|
|
], |
198
|
|
|
'pinned' => wp_validate_boolean(glsr(Database::class)->get(get_the_ID(), 'pinned')), |
199
|
|
|
]); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* @param WP_Post $post |
204
|
|
|
* @return void |
205
|
|
|
* @callback add_meta_box |
206
|
|
|
*/ |
207
|
|
|
public function renderResponseMetaBox($post) |
208
|
|
|
{ |
209
|
|
|
if (!$this->isReviewPostType($post)) { |
210
|
|
|
return; |
211
|
|
|
} |
212
|
|
|
wp_nonce_field('response', '_nonce-response', false); |
213
|
|
|
glsr()->render('partials/editor/metabox-response', [ |
214
|
|
|
'response' => glsr(Database::class)->get($post->ID, 'response'), |
215
|
|
|
]); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @param WP_Post $post |
220
|
|
|
* @return void |
221
|
|
|
* @action edit_form_after_title |
222
|
|
|
*/ |
223
|
|
|
public function renderReviewEditor($post) |
224
|
|
|
{ |
225
|
|
|
if (!$this->isReviewPostType($post) || $this->isReviewEditable($post)) { |
226
|
|
|
return; |
227
|
|
|
} |
228
|
|
|
glsr()->render('partials/editor/review', [ |
229
|
|
|
'post' => $post, |
230
|
|
|
'response' => glsr(Database::class)->get($post->ID, 'response'), |
231
|
|
|
]); |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
/** |
235
|
|
|
* @return void |
236
|
|
|
* @action admin_head |
237
|
|
|
*/ |
238
|
|
|
public function renderReviewFields() |
239
|
|
|
{ |
240
|
|
|
$screen = glsr_current_screen(); |
241
|
|
|
if ('post' != $screen->base || Application::POST_TYPE != $screen->post_type) { |
242
|
|
|
return; |
243
|
|
|
} |
244
|
|
|
add_action('edit_form_after_title', [$this, 'renderReviewEditor']); |
245
|
|
|
add_action('edit_form_top', [$this, 'renderReviewNotice']); |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
/** |
249
|
|
|
* @param WP_Post $post |
250
|
|
|
* @return void |
251
|
|
|
* @action edit_form_top |
252
|
|
|
*/ |
253
|
|
|
public function renderReviewNotice($post) |
254
|
|
|
{ |
255
|
|
|
if (!$this->isReviewPostType($post) || $this->isReviewEditable($post)) { |
256
|
|
|
return; |
257
|
|
|
} |
258
|
|
|
glsr(Notice::class)->addWarning(sprintf( |
259
|
|
|
__('%s reviews are read-only.', 'site-reviews'), |
260
|
|
|
glsr(Columns::class)->buildColumnReviewType($post->ID) |
261
|
|
|
)); |
262
|
|
|
glsr(Template::class)->render('partials/editor/notice', [ |
263
|
|
|
'context' => [ |
264
|
|
|
'notices' => glsr(Notice::class)->get(), |
265
|
|
|
], |
266
|
|
|
]); |
267
|
|
|
} |
268
|
|
|
|
269
|
|
|
/** |
270
|
|
|
* @param WP_Post $post |
271
|
|
|
* @return void |
272
|
|
|
* @see glsr_categories_meta_box() |
273
|
|
|
* @callback register_taxonomy |
274
|
|
|
*/ |
275
|
|
|
public function renderTaxonomyMetabox($post) |
276
|
|
|
{ |
277
|
|
|
if (!$this->isReviewPostType($post)) { |
278
|
|
|
return; |
279
|
|
|
} |
280
|
|
|
glsr()->render('partials/editor/metabox-categories', [ |
281
|
|
|
'post' => $post, |
282
|
|
|
'tax_name' => Application::TAXONOMY, |
283
|
|
|
'taxonomy' => get_taxonomy(Application::TAXONOMY), |
284
|
|
|
]); |
285
|
|
|
} |
286
|
|
|
|
287
|
|
|
/** |
288
|
|
|
* @return void |
289
|
|
|
* @see $this->filterUpdateMessages() |
290
|
|
|
* @action admin_action_revert |
291
|
|
|
*/ |
292
|
|
|
public function revertReview() |
293
|
|
|
{ |
294
|
|
|
if (Application::ID != filter_input(INPUT_GET, 'plugin')) { |
295
|
|
|
return; |
296
|
|
|
} |
297
|
|
|
check_admin_referer('revert-review_'.($postId = $this->getPostId())); |
298
|
|
|
glsr(ReviewManager::class)->revert($postId); |
299
|
|
|
$this->redirect($postId, 52); |
300
|
|
|
} |
301
|
|
|
|
302
|
|
|
/** |
303
|
|
|
* @param int $postId |
304
|
|
|
* @return void |
305
|
|
|
* @action save_post_.Application::POST_TYPE |
306
|
|
|
*/ |
307
|
1 |
|
public function saveMetaboxes($postId) |
308
|
|
|
{ |
309
|
1 |
|
glsr(Metaboxes::class)->saveAssignedToMetabox($postId); |
310
|
1 |
|
glsr(Metaboxes::class)->saveResponseMetabox($postId); |
311
|
1 |
|
do_action('site-reviews/review/saved', glsr_get_review($postId)); |
312
|
1 |
|
} |
313
|
|
|
|
314
|
|
|
/** |
315
|
|
|
* @param string $assignedTo |
316
|
|
|
* @return string |
317
|
|
|
*/ |
318
|
|
|
protected function buildAssignedToTemplate($assignedTo, WP_Post $post) |
319
|
|
|
{ |
320
|
|
|
$assignedPost = glsr(Database::class)->getAssignedToPost($post->ID, $assignedTo); |
321
|
|
|
if (!($assignedPost instanceof WP_Post)) { |
322
|
|
|
return; |
323
|
|
|
} |
324
|
|
|
return glsr(Template::class)->build('partials/editor/assigned-post', [ |
325
|
|
|
'context' => [ |
326
|
|
|
'data.url' => (string) get_permalink($assignedPost), |
327
|
|
|
'data.title' => get_the_title($assignedPost), |
328
|
|
|
], |
329
|
|
|
]); |
330
|
|
|
} |
331
|
|
|
|
332
|
|
|
/** |
333
|
|
|
* @return string |
334
|
|
|
*/ |
335
|
|
|
protected function buildDetailsMetaBoxRevertButton(Review $review, WP_Post $post) |
336
|
|
|
{ |
337
|
|
|
$isModified = !glsr(Helper::class)->compareArrays( |
338
|
|
|
[$review->title, $review->content, $review->date], |
339
|
|
|
[ |
340
|
|
|
glsr(Database::class)->get($post->ID, 'title'), |
341
|
|
|
glsr(Database::class)->get($post->ID, 'content'), |
342
|
|
|
glsr(Database::class)->get($post->ID, 'date'), |
343
|
|
|
] |
344
|
|
|
); |
345
|
|
|
if ($isModified) { |
346
|
|
|
$revertUrl = wp_nonce_url( |
347
|
|
|
admin_url('post.php?post='.$post->ID.'&action=revert&plugin='.Application::ID), |
348
|
|
|
'revert-review_'.$post->ID |
349
|
|
|
); |
350
|
|
|
return glsr(Builder::class)->a(__('Revert Changes', 'site-reviews'), [ |
351
|
|
|
'class' => 'button button-large', |
352
|
|
|
'href' => $revertUrl, |
353
|
|
|
'id' => 'revert', |
354
|
|
|
]); |
355
|
|
|
} |
356
|
|
|
return glsr(Builder::class)->button(__('Nothing to Revert', 'site-reviews'), [ |
357
|
|
|
'class' => 'button-large', |
358
|
|
|
'disabled' => true, |
359
|
|
|
'id' => 'revert', |
360
|
|
|
]); |
361
|
|
|
} |
362
|
|
|
|
363
|
|
|
/** |
364
|
|
|
* @param object $review |
365
|
|
|
* @return string|void |
366
|
|
|
*/ |
367
|
|
|
protected function getReviewType($review) |
368
|
|
|
{ |
369
|
|
|
if (count(glsr()->reviewTypes) < 2) { |
370
|
|
|
return; |
371
|
|
|
} |
372
|
|
|
$reviewType = array_key_exists($review->review_type, glsr()->reviewTypes) |
373
|
|
|
? glsr()->reviewTypes[$review->review_type] |
374
|
|
|
: __('Unknown', 'site-reviews'); |
375
|
|
|
if (!empty($review->url)) { |
376
|
|
|
$reviewType = glsr(Builder::class)->a($reviewType, [ |
377
|
|
|
'href' => $review->url, |
378
|
|
|
'target' => '_blank', |
379
|
|
|
]); |
380
|
|
|
} |
381
|
|
|
return $reviewType; |
382
|
|
|
} |
383
|
|
|
|
384
|
|
|
/** |
385
|
|
|
* @return bool |
386
|
|
|
*/ |
387
|
|
|
protected function isReviewEditable($post) |
388
|
|
|
{ |
389
|
|
|
return $this->isReviewPostType($post) |
390
|
|
|
&& post_type_supports(Application::POST_TYPE, 'title') |
391
|
|
|
&& 'local' == glsr(Database::class)->get($post->ID, 'review_type'); |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
/** |
395
|
|
|
* @param mixed $post |
396
|
|
|
* @return bool |
397
|
|
|
*/ |
398
|
|
|
protected function isReviewPostType($post) |
399
|
|
|
{ |
400
|
|
|
return $post instanceof WP_Post && Application::POST_TYPE == $post->post_type; |
401
|
|
|
} |
402
|
|
|
|
403
|
|
|
/** |
404
|
|
|
* @return array |
405
|
|
|
*/ |
406
|
|
|
protected function normalizeDetailsMetaBox(Review $review) |
407
|
|
|
{ |
408
|
|
|
$user = empty($review->user_id) |
409
|
|
|
? __('Unregistered user', 'site-reviews') |
410
|
|
|
: glsr(Builder::class)->a(get_the_author_meta('display_name', $review->user_id), [ |
411
|
|
|
'href' => get_author_posts_url($review->user_id), |
412
|
|
|
]); |
413
|
|
|
$email = empty($review->email) |
414
|
|
|
? '—' |
415
|
|
|
: glsr(Builder::class)->a($review->email, [ |
416
|
|
|
'href' => 'mailto:'.$review->email.'?subject='.esc_attr(__('RE:', 'site-reviews').' '.$review->title), |
417
|
|
|
]); |
418
|
|
|
$metabox = [ |
419
|
|
|
__('Rating', 'site-reviews') => glsr_star_rating($review->rating), |
420
|
|
|
__('Type', 'site-reviews') => $this->getReviewType($review), |
421
|
|
|
__('Date', 'site-reviews') => get_date_from_gmt($review->date, 'F j, Y'), |
422
|
|
|
__('Name', 'site-reviews') => $review->author, |
423
|
|
|
__('Email', 'site-reviews') => $email, |
424
|
|
|
__('User', 'site-reviews') => $user, |
425
|
|
|
__('IP Address', 'site-reviews') => $review->ip_address, |
426
|
|
|
__('Avatar', 'site-reviews') => sprintf('<img src="%s" width="96">', $review->avatar), |
427
|
|
|
]; |
428
|
|
|
return array_filter(apply_filters('site-reviews/metabox/details', $metabox, $review)); |
429
|
|
|
} |
430
|
|
|
|
431
|
|
|
/** |
432
|
|
|
* @param int $postId |
433
|
|
|
* @param int $messageIndex |
434
|
|
|
* @return void |
435
|
|
|
*/ |
436
|
|
|
protected function redirect($postId, $messageIndex) |
437
|
|
|
{ |
438
|
|
|
$referer = wp_get_referer(); |
439
|
|
|
$hasReferer = !$referer |
440
|
|
|
|| false !== strpos($referer, 'post.php') |
441
|
|
|
|| false !== strpos($referer, 'post-new.php'); |
442
|
|
|
$redirectUri = $hasReferer |
443
|
|
|
? remove_query_arg(['deleted', 'ids', 'trashed', 'untrashed'], $referer) |
444
|
|
|
: get_edit_post_link($postId); |
445
|
|
|
wp_safe_redirect(add_query_arg(['message' => $messageIndex], $redirectUri)); |
446
|
|
|
exit; |
447
|
|
|
} |
448
|
|
|
} |
449
|
|
|
|