Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

EditorController::onSaveReview()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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