|
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\ListTableColumns\ColumnValueReviewType; |
|
10
|
|
|
use GeminiLabs\SiteReviews\Database; |
|
11
|
|
|
use GeminiLabs\SiteReviews\Defaults\CreateReviewDefaults; |
|
12
|
|
|
use GeminiLabs\SiteReviews\Helpers\Arr; |
|
13
|
|
|
use GeminiLabs\SiteReviews\Helpers\Str; |
|
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
|
|
|
* @param array $settings |
|
24
|
|
|
* @return array |
|
25
|
|
|
* @filter wp_editor_settings |
|
26
|
|
|
*/ |
|
27
|
|
|
public function filterEditorSettings($settings) |
|
28
|
|
|
{ |
|
29
|
|
|
return glsr(Customization::class)->filterEditorSettings( |
|
30
|
|
|
Arr::consolidate($settings) |
|
31
|
|
|
); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script. |
|
36
|
|
|
* @param string $html |
|
37
|
|
|
* @return string |
|
38
|
|
|
* @filter the_editor |
|
39
|
|
|
*/ |
|
40
|
|
|
public function filterEditorTextarea($html) |
|
41
|
|
|
{ |
|
42
|
|
|
return glsr(Customization::class)->filterEditorTextarea($html); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* @param bool $protected |
|
47
|
|
|
* @param string $metaKey |
|
48
|
|
|
* @param string $metaType |
|
49
|
|
|
* @return bool |
|
50
|
|
|
* @filter is_protected_meta |
|
51
|
|
|
*/ |
|
52
|
|
|
public function filterIsProtectedMeta($protected, $metaKey, $metaType) |
|
53
|
|
|
{ |
|
54
|
|
|
if ('post' == $metaType && glsr()->post_type == get_post_type()) { |
|
55
|
|
|
$values = glsr(CreateReviewDefaults::class)->unguarded(); |
|
56
|
|
|
$values = Arr::prefixKeys($values); |
|
57
|
|
|
if (array_key_exists($metaKey, $values)) { |
|
58
|
|
|
$protected = false; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
return $protected; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param array $messages |
|
66
|
|
|
* @return array |
|
67
|
|
|
* @filter post_updated_messages |
|
68
|
|
|
*/ |
|
69
|
|
|
public function filterUpdateMessages($messages) |
|
70
|
|
|
{ |
|
71
|
|
|
return glsr(Labels::class)->filterUpdateMessages( |
|
72
|
|
|
Arr::consolidate($messages) |
|
73
|
|
|
); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* @return void |
|
78
|
|
|
* @action add_meta_boxes_{Application::POST_TYPE} |
|
79
|
|
|
*/ |
|
80
|
|
|
public function registerMetaBoxes($post) |
|
81
|
|
|
{ |
|
82
|
|
|
add_meta_box(Application::ID.'_assigned_to', _x('Assigned To', 'admin-text', 'site-reviews'), [$this, 'renderAssignedToMetabox'], null, 'side'); |
|
83
|
|
|
add_meta_box(Application::ID.'_review', _x('Details', 'admin-text', 'site-reviews'), [$this, 'renderDetailsMetaBox'], null, 'side'); |
|
84
|
|
|
if ('local' === glsr(Query::class)->review($post->ID)->type) { |
|
|
|
|
|
|
85
|
|
|
add_meta_box(Application::ID.'_response', _x('Respond Publicly', 'admin-text', 'site-reviews'), [$this, 'renderResponseMetaBox'], null, 'normal'); |
|
86
|
|
|
} |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* @return void |
|
91
|
|
|
* @action admin_print_scripts |
|
92
|
|
|
*/ |
|
93
|
|
|
public function removeAutosave() |
|
94
|
|
|
{ |
|
95
|
|
|
glsr(Customization::class)->removeAutosave(); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* @return void |
|
100
|
|
|
* @action admin_menu |
|
101
|
|
|
*/ |
|
102
|
|
|
public function removeMetaBoxes() |
|
103
|
|
|
{ |
|
104
|
|
|
glsr(Customization::class)->removeMetaBoxes(); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return void |
|
109
|
|
|
*/ |
|
110
|
|
|
public function removePostTypeSupport() |
|
111
|
|
|
{ |
|
112
|
|
|
glsr(Customization::class)->removePostTypeSupport(); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @param WP_Post $post |
|
117
|
|
|
* @return void |
|
118
|
|
|
* @callback add_meta_box |
|
119
|
|
|
*/ |
|
120
|
|
|
public function renderAssignedToMetabox($post) |
|
121
|
|
|
{ |
|
122
|
|
|
if (Review::isReview($post)) { |
|
123
|
|
|
$review = glsr(Query::class)->review($post->ID); |
|
124
|
|
|
wp_nonce_field('assigned_to', '_nonce-assigned-to', false); |
|
125
|
|
|
$templates = array_reduce($review->assigned_post_ids, function ($carry, $postId) { |
|
126
|
|
|
return $carry.glsr(Template::class)->build('partials/editor/assigned-post', [ |
|
127
|
|
|
'context' => [ |
|
128
|
|
|
'data.id' => $postId, |
|
129
|
|
|
'data.url' => (string) get_permalink($postId), |
|
130
|
|
|
'data.title' => get_the_title($postId), |
|
131
|
|
|
], |
|
132
|
|
|
]); |
|
133
|
|
|
}); |
|
134
|
|
|
glsr()->render('partials/editor/metabox-assigned-to', [ |
|
135
|
|
|
'templates' => $templates, |
|
136
|
|
|
]); |
|
137
|
|
|
} |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* @param WP_Post $post |
|
142
|
|
|
* @return void |
|
143
|
|
|
* @callback add_meta_box |
|
144
|
|
|
*/ |
|
145
|
|
|
public function renderDetailsMetaBox($post) |
|
146
|
|
|
{ |
|
147
|
|
|
if (Review::isReview($post)) { |
|
148
|
|
|
$review = glsr(Query::class)->review($post); |
|
149
|
|
|
glsr()->render('partials/editor/metabox-details', [ |
|
150
|
|
|
'metabox' => $this->normalizeDetailsMetaBox($review), |
|
151
|
|
|
]); |
|
152
|
|
|
} |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* @return void |
|
157
|
|
|
* @action post_submitbox_misc_actions |
|
158
|
|
|
*/ |
|
159
|
|
|
public function renderPinnedInPublishMetaBox() |
|
160
|
|
|
{ |
|
161
|
|
|
$review = glsr(Query::class)->review(get_post()->ID); |
|
162
|
|
|
if ($review->isValid() && glsr()->can('edit_others_posts')) { |
|
163
|
|
|
glsr(Template::class)->render('partials/editor/pinned', [ |
|
164
|
|
|
'context' => [ |
|
165
|
|
|
'no' => _x('No', 'admin-text', 'site-reviews'), |
|
166
|
|
|
'yes' => _x('Yes', 'admin-text', 'site-reviews'), |
|
167
|
|
|
], |
|
168
|
|
|
'pinned' => $review->is_pinned, |
|
169
|
|
|
]); |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param WP_Post $post |
|
175
|
|
|
* @return void |
|
176
|
|
|
* @callback add_meta_box |
|
177
|
|
|
*/ |
|
178
|
|
|
public function renderResponseMetaBox($post) |
|
179
|
|
|
{ |
|
180
|
|
|
if (Review::isReview($post)) { |
|
181
|
|
|
wp_nonce_field('response', '_nonce-response', false); |
|
182
|
|
|
glsr()->render('partials/editor/metabox-response', [ |
|
183
|
|
|
'response' => glsr(Database::class)->meta($post->ID, 'response'), |
|
184
|
|
|
]); |
|
185
|
|
|
} |
|
186
|
|
|
} |
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* @param WP_Post $post |
|
190
|
|
|
* @return void |
|
191
|
|
|
* @action edit_form_after_title |
|
192
|
|
|
*/ |
|
193
|
|
|
public function renderReviewEditor($post) |
|
194
|
|
|
{ |
|
195
|
|
|
if (Review::isReview($post) && !Review::isEditable($post)) { |
|
196
|
|
|
glsr()->render('partials/editor/review', [ |
|
197
|
|
|
'post' => $post, |
|
198
|
|
|
'response' => glsr(Database::class)->meta($post->ID, 'response'), |
|
199
|
|
|
]); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* @return void |
|
205
|
|
|
* @action admin_head |
|
206
|
|
|
*/ |
|
207
|
|
|
public function renderReviewFields() |
|
208
|
|
|
{ |
|
209
|
|
|
$screen = glsr_current_screen(); |
|
210
|
|
|
if ('post' === $screen->base && glsr()->post_type === $screen->post_type) { |
|
211
|
|
|
add_action('edit_form_after_title', [$this, 'renderReviewEditor']); |
|
212
|
|
|
add_action('edit_form_top', [$this, 'renderReviewNotice']); |
|
213
|
|
|
} |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* @param WP_Post $post |
|
218
|
|
|
* @return void |
|
219
|
|
|
* @action edit_form_top |
|
220
|
|
|
*/ |
|
221
|
|
|
public function renderReviewNotice($post) |
|
222
|
|
|
{ |
|
223
|
|
|
if (Review::isReview($post) && !Review::isEditable($post)) { |
|
224
|
|
|
glsr(Notice::class)->addWarning(sprintf( |
|
225
|
|
|
_x('%s reviews are read-only.', 'admin-text', 'site-reviews'), |
|
226
|
|
|
glsr(ColumnValueReviewType::class)->handle(glsr(Query::class)->review($post->ID)) |
|
227
|
|
|
)); |
|
228
|
|
|
glsr(Template::class)->render('partials/editor/notice', [ |
|
229
|
|
|
'context' => [ |
|
230
|
|
|
'notices' => glsr(Notice::class)->get(), |
|
231
|
|
|
], |
|
232
|
|
|
]); |
|
233
|
|
|
} |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* @param WP_Post $post |
|
238
|
|
|
* @return void |
|
239
|
|
|
* @see glsr_categories_meta_box() |
|
240
|
|
|
* @callback register_taxonomy |
|
241
|
|
|
*/ |
|
242
|
|
|
public function renderTaxonomyMetabox($post) |
|
243
|
|
|
{ |
|
244
|
|
|
if (Review::isReview($post)) { |
|
245
|
|
|
glsr()->render('partials/editor/metabox-categories', [ |
|
246
|
|
|
'post' => $post, |
|
247
|
|
|
'tax_name' => glsr()->taxonomy, |
|
248
|
|
|
'taxonomy' => get_taxonomy(glsr()->taxonomy), |
|
249
|
|
|
]); |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
|
|
/** |
|
254
|
|
|
* @param int $postId |
|
255
|
|
|
* @param \WP_Post $post |
|
256
|
|
|
* @param bool $isUpdate |
|
257
|
|
|
* @return void |
|
258
|
|
|
* @action save_post_.Application::POST_TYPE |
|
259
|
|
|
*/ |
|
260
|
|
|
public function saveMetaboxes($postId, $post, $isUpdating) |
|
261
|
|
|
{ |
|
262
|
|
|
glsr(Metaboxes::class)->saveAssignedToMetabox($postId); |
|
263
|
|
|
glsr(Metaboxes::class)->saveResponseMetabox($postId); |
|
264
|
|
|
if ($isUpdating) { |
|
265
|
|
|
glsr()->action('review/saved', glsr(Query::class)->review($postId)); |
|
266
|
|
|
} |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* @return string|void |
|
271
|
|
|
*/ |
|
272
|
|
|
protected function getReviewType(Review $review) |
|
273
|
|
|
{ |
|
274
|
|
|
if (count(glsr()->reviewTypes) < 2) { |
|
275
|
|
|
return; |
|
276
|
|
|
} |
|
277
|
|
|
$type = $review->type(); |
|
278
|
|
|
if (!empty($review->url)) { |
|
279
|
|
|
return glsr(Builder::class)->a([ |
|
280
|
|
|
'href' => $review->url, |
|
281
|
|
|
'target' => '_blank', |
|
282
|
|
|
'text' => $type, |
|
283
|
|
|
]); |
|
284
|
|
|
} |
|
285
|
|
|
return $type; |
|
286
|
|
|
} |
|
287
|
|
|
|
|
288
|
|
|
/** |
|
289
|
|
|
* @return array |
|
290
|
|
|
*/ |
|
291
|
|
|
protected function normalizeDetailsMetaBox(Review $review) |
|
292
|
|
|
{ |
|
293
|
|
|
$user = empty($review->author_id) |
|
294
|
|
|
? _x('Unregistered user', 'admin-text', 'site-reviews') |
|
295
|
|
|
: glsr(Builder::class)->a(get_the_author_meta('display_name', $review->author_id), [ |
|
296
|
|
|
'href' => get_author_posts_url($review->author_id), |
|
297
|
|
|
]); |
|
298
|
|
|
$email = empty($review->email) |
|
299
|
|
|
? '—' |
|
300
|
|
|
: glsr(Builder::class)->a($review->email, [ |
|
301
|
|
|
'href' => 'mailto:'.$review->email.'?subject='.esc_attr(_x('RE:', 'admin-text', 'site-reviews').' '.$review->title), |
|
302
|
|
|
]); |
|
303
|
|
|
$metabox = [ |
|
304
|
|
|
_x('Rating', 'admin-text', 'site-reviews') => $review->rating(), |
|
305
|
|
|
_x('Type', 'admin-text', 'site-reviews') => $this->getReviewType($review), |
|
306
|
|
|
_x('Date', 'admin-text', 'site-reviews') => $review->date(), |
|
307
|
|
|
_x('Name', 'admin-text', 'site-reviews') => $review->author, |
|
|
|
|
|
|
308
|
|
|
_x('Email', 'admin-text', 'site-reviews') => $email, |
|
309
|
|
|
_x('User', 'admin-text', 'site-reviews') => $user, |
|
310
|
|
|
_x('IP Address', 'admin-text', 'site-reviews') => $review->ip_address, |
|
311
|
|
|
_x('Avatar', 'admin-text', 'site-reviews') => sprintf('<img src="%s" width="96">', $review->avatar), |
|
|
|
|
|
|
312
|
|
|
]; |
|
313
|
|
|
return array_filter(glsr()->filterArray('metabox/details', $metabox, $review)); |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* @param int $postId |
|
318
|
|
|
* @param int $messageIndex |
|
319
|
|
|
* @return void |
|
320
|
|
|
*/ |
|
321
|
|
|
protected function redirect($postId, $messageIndex) |
|
322
|
|
|
{ |
|
323
|
|
|
$referer = wp_get_referer(); |
|
324
|
|
|
$hasReferer = !$referer |
|
325
|
|
|
|| Str::contains($referer, 'post.php') |
|
326
|
|
|
|| Str::contains($referer, 'post-new.php'); |
|
327
|
|
|
$redirectUri = $hasReferer |
|
328
|
|
|
? remove_query_arg(['deleted', 'ids', 'trashed', 'untrashed'], $referer) |
|
329
|
|
|
: get_edit_post_link($postId); |
|
330
|
|
|
wp_safe_redirect(add_query_arg(['message' => $messageIndex], $redirectUri)); |
|
331
|
|
|
exit; |
|
332
|
|
|
} |
|
333
|
|
|
} |
|
334
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths