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