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