Passed
Push — master ( 3aaa49...566664 )
by Paul
04:28
created

EditorController::buildAssignedToTemplate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 8
ccs 0
cts 6
cp 0
crap 6
rs 9.4285
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\Helper;
9
use GeminiLabs\SiteReviews\Modules\Html;
10
use GeminiLabs\SiteReviews\Modules\Html\Builder;
11
use GeminiLabs\SiteReviews\Modules\Editor\Customization;
12
use GeminiLabs\SiteReviews\Modules\Editor\Labels;
13
use GeminiLabs\SiteReviews\Modules\Editor\Metaboxes;
14
use WP_Post;
15
use WP_Screen;
16
17
class EditorController extends Controller
18
{
19
	/**
20
	 * @return void
21
	 * @action admin_enqueue_scripts
22
	 */
23
	public function customizePostStatusLabels()
24
	{
25
		glsr( Labels::class )->customizePostStatusLabels();
26
	}
27
28
	/**
29
	 * @return array
30
	 * @filter wp_editor_settings
31
	 */
32
	public function filterEditorSettings( array $settings )
33
	{
34
		return glsr( Customization::class )->filterEditorSettings( $settings );
35
	}
36
37
	/**
38
	 * Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script
39
	 * @param string $html
40
	 * @return string
41
	 * @filter the_editor
42
	 */
43
	public function filterEditorTextarea( $html )
44
	{
45
		return glsr( Customization::class )->filterEditorTextarea( $html );
46
	}
47
48
	/**
49
	 * @param string $translation
50
	 * @param string $test
51
	 * @param string $domain
52
	 * @return string
53
	 * @filter gettext
54
	 */
55 7
	public function filterPostStatusLabels( $translation, $text, $domain )
56
	{
57 7
		return glsr( Labels::class )->filterPostStatusLabels( $translation, $text, $domain );
58
	}
59
60
	/**
61
	 * @param string $translation
62
	 * @param string $test
63
	 * @param string $domain
64
	 * @return string
65
	 * @filter gettext_with_context
66
	 */
67 1
	public function filterPostStatusLabelsWithContext( $translation, $text, $context, $domain )
68
	{
69 1
		return glsr( Labels::class )->filterPostStatusLabels( $translation, $text, $domain );
70
	}
71
72
	/**
73
	 * @return array
74
	 * @filter post_updated_messages
75
	 */
76
	public function filterUpdateMessages( array $messages )
77
	{
78
		return glsr( Labels::class )->filterUpdateMessages( $messages );
79
	}
80
81
	/**
82
	 * @param array $postData
83
	 * @param array $meta
84
	 * @param int $postId
85
	 * @return void
86
	 * @action site-reviews/create/review
87
	 */
88
	public function onCreateReview( $postData, $meta, $postId )
89
	{
90
		glsr( Metaboxes::class )->onCreateReview( $postData, $meta, $postId );
91
	}
92
93
	/**
94
	 * @param int $postId
95
	 * @return void
96
	 * @action before_delete_post
97
	 */
98
	public function onDeleteReview( $postId )
99
	{
100
		glsr( Metaboxes::class )->onDeleteReview( $postId );
101
	}
102
103
	/**
104
	 * @param int $postId
105
	 * @return void
106
	 * @action save_post_.static::POST_TYPE
107
	 */
108
	public function onSaveReview( $postId, WP_Post $review )
109
	{
110
		glsr( Metaboxes::class )->onSaveReview( $postId, $review );
111
	}
112
113
	/**
114
	 * @param string $postType
115
	 * @return void
116
	 * @action add_meta_boxes
117
	 */
118
	public function registerMetaBoxes( $postType )
119
	{
120
		if( $postType != Application::POST_TYPE )return;
121
		add_meta_box( Application::ID.'_assigned_to', __( 'Assigned To', 'site-reviews' ), [$this, 'renderAssignedToMetabox'], null, 'side' );
122
		add_meta_box( Application::ID.'_review', __( 'Details', 'site-reviews' ), [$this, 'renderDetailsMetaBox'], null, 'side' );
123
		add_meta_box( Application::ID.'_response', __( 'Respond Publicly', 'site-reviews' ), [$this, 'renderResponseMetaBox'], null, 'normal' );
124
	}
125
126
	/**
127
	 * @return void
128
	 * @action admin_print_scripts
129
	 */
130
	public function removeAutosave()
131
	{
132
		glsr( Customization::class )->removeAutosave();
133
	}
134
135
	/**
136
	 * @return void
137
	 * @action admin_menu
138
	 */
139
	public function removeMetaBoxes()
140
	{
141
		glsr( Customization::class )->removeMetaBoxes();
142
	}
143
144
	/**
145
	 * @return void
146
	 * @callback add_meta_box
147
	 */
148
	public function renderAssignedToMetabox( WP_Post $post )
149
	{
150
		if( !$this->isReviewPostType( $post ))return;
151
		$assignedTo = intval( get_post_meta( $post->ID, 'assigned_to', true ));
152
		wp_nonce_field( 'assigned_to', '_nonce-assigned-to', false );
153
		glsr()->render( 'edit/metabox-assigned-to', [
154
			'id' => $assignedTo,
155
			'template' => $this->buildAssignedToTemplate( $assignedTo ),
156
		]);
157
	}
158
159
	/**
160
	 * @return void
161
	 * @callback add_meta_box
162
	 */
163
	public function renderDetailsMetaBox( WP_Post $post )
164
	{
165
		if( !$this->isReviewPostType( $post ))return;
166
		$review = glsr( Database::class )->getReview( $post );
167
		glsr()->render( 'edit/metabox-details', [
168
			'button' => $this->buildDetailsMetaBoxRevertButton( $review, $post ),
169
			'metabox' => $this->buildDetailsMetaBox( $review ),
170
		]);
171
	}
172
173
	/**
174
	 * @return void
175
	 * @action post_submitbox_misc_actions
176
	 */
177
	public function renderPinnedInPublishMetaBox()
178
	{
179
		if( !$this->isReviewPostType( get_post() ))return;
180
		glsr()->render( 'edit/pinned', [
181
			'pinned' => boolval( get_post_meta( get_the_ID(), 'pinned', true )),
0 ignored issues
show
Bug introduced by
It seems like get_the_ID() can also be of type false; however, parameter $post_id of get_post_meta() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

181
			'pinned' => boolval( get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), 'pinned', true )),
Loading history...
182
		]);
183
	}
184
185
	/**
186
	 * @return void
187
	 * @callback add_meta_box
188
	 */
189
	public function renderResponseMetaBox( WP_Post $post )
190
	{
191
		if( !$this->isReviewPostType( $post ))return;
192
		wp_nonce_field( 'response', '_nonce-response', false );
193
		glsr()->render( 'edit/metabox-response', [
194
			'response' => glsr( Database::class )->getReview( $post )->response,
195
		]);
196
	}
197
198
	/**
199
	 * @return void
200
	 * @see glsr_categories_meta_box()
201
	 * @callback register_taxonomy
202
	 */
203
	public function renderTaxonomyMetabox( WP_Post $post )
204
	{
205
		if( !$this->isReviewPostType( $post ))return;
206
		glsr()->render( 'edit/metabox-categories', [
207
			'post' => $post,
208
			'tax_name' => Application::TAXONOMY,
209
			'taxonomy' => get_taxonomy( Application::TAXONOMY ),
210
		]);
211
	}
212
213
	/**
214
	 * @return void
215
	 * @see $this->filterUpdateMessages()
216
	 * @action admin_action_revert
217
	 */
218
	public function revertReview()
219
	{
220
		check_admin_referer( 'revert-review_'.( $postId = $this->getPostId() ));
221
		glsr( Database::class )->revertReview( $postId );
222
		$this->redirect( $postId, 52 );
223
	}
224
225
	/**
226
	 * @param int $postId
227
	 * @return void
228
	 * @action save_post_.Application::POST_TYPE
229
	 */
230
	public function saveMetaboxes( $postId )
231
	{
232
		glsr( Metaboxes::class )->saveAssignedToMetabox( $postId );
233
		glsr( Metaboxes::class )->saveResponseMetabox( $postId );
234
	}
235
236
	/**
237
	 * @param int $assignedTo
238
	 * @return string
239
	 */
240
	protected function buildAssignedToTemplate( $assignedTo )
241
	{
242
		$assignedPost = get_post( $assignedTo );
243
		if( !( $assignedPost instanceof WP_Post ))return;
244
		return glsr( Html::class )->buildTemplate( 'edit/assigned-post', [
245
			'context' => [
246
				'url' => (string)get_permalink( $assignedPost ),
247
				'title' => get_the_title( $assignedPost ),
248
			],
249
		]);
250
	}
251
252
	/**
253
	 * @param object $review
254
	 * @return array
255
	 */
256
	protected function buildDetailsMetaBox( $review )
257
	{
258
		$reviewer = empty( $review->user_id )
259
			? __( 'Unregistered user', 'site-reviews' )
260
			: glsr( Builder::class )->a( get_the_author_meta( 'display_name', $review->user_id ), [
261
				'href' => get_author_posts_url( $review->user_id ),
262
			]);
263
		$email = empty( $review->email )
264
			? '&mdash;'
265
			: glsr( Builder::class )->a( $review->email, [
266
				'href' => 'mailto:'.$review->email.'?subject='.esc_attr( __( 'RE:', 'site-reviews' ).' '.$review->title ),
267
			]);
268
		$metabox = [
269
			__( 'Rating', 'site-reviews' ) => glsr( Html::class )->renderPartial( 'star-rating', ['rating' => $review->rating] ),
270
			__( 'Type', 'site-reviews' ) => $this->getReviewType( $review ),
271
			__( 'Date', 'site-reviews' ) => get_date_from_gmt( $review->date, 'F j, Y' ),
272
			__( 'Reviewer', 'site-reviews' ) => $reviewer,
273
			__( 'Name', 'site-reviews' ) => $review->author,
274
			__( 'Email', 'site-reviews' ) => $email,
275
			__( 'IP Address', 'site-reviews' ) => $review->ip_address,
276
			__( 'Avatar', 'site-reviews' ) => sprintf( '<img src="%s" width="96">', $review->avatar ),
277
		];
278
		return apply_filters( 'site-reviews/metabox/details', $metabox, $review );
279
	}
280
281
	/**
282
	 * @param object $review
283
	 * @return string
284
	 */
285
	protected function buildDetailsMetaBoxRevertButton( $review, WP_Post $post )
286
	{
287
		$modified = false;
288
		if( $post->post_title !== $review->title
289
			|| $post->post_content !== $review->content
290
			|| $post->post_date !== $review->date ) {
291
			$modified = true;
292
		}
293
		$revertUrl = wp_nonce_url(
294
			admin_url( 'post.php?post='.$post->ID.'&action=revert' ),
295
			'revert-review_'.$post->ID
296
		);
297
		return !$modified
298
			? sprintf( '<button id="revert" class="button button-large" disabled>%s</button>', __( 'Nothing to Revert', 'site-reviews' ))
299
			: sprintf( '<a href="%s" id="revert" class="button button-large">%s</a>', $revertUrl, __( 'Revert Changes', 'site-reviews' ));
300
	}
301
302
	/**
303
	 * @param object $review
304
	 * @return string
305
	 */
306
	protected function getReviewType( $review )
307
	{
308
		$reviewType = $review->review_type;
309
		if( !empty( $review->url )) {
310
			$reviewType = glsr( Builder::class )->a( $reviewType, [
311
				'href' => $review->url,
312
				'target' => '_blank',
313
			]);
314
		}
315
		return in_array( $reviewType, glsr()->reviewTypes )
316
			? glsr()->reviewTypes[$reviewType]
317
			: __( 'Unknown', 'site-reviews' );
318
	}
319
320
	/**
321
	 * @param mixed $post
322
	 * @return bool
323
	 */
324
	protected function isReviewPostType( $post )
325
	{
326
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
327
	}
328
329
	/**
330
	 * @param int $postId
331
	 * @param int $messageIndex
332
	 * @return void
333
	 */
334
	protected function redirect( $postId, $messageIndex )
335
	{
336
		$referer = wp_get_referer();
337
		$hasReferer = !$referer
338
			|| strpos( $referer, 'post.php' ) !== false
339
			|| strpos( $referer, 'post-new.php' ) !== false;
340
		$redirectUri = $hasReferer
341
			? remove_query_arg( ['deleted', 'ids', 'trashed', 'untrashed'], $referer )
342
			: get_edit_post_link( $postId, false );
0 ignored issues
show
Bug introduced by
false of type false is incompatible with the type string expected by parameter $context of get_edit_post_link(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

342
			: get_edit_post_link( $postId, /** @scrutinizer ignore-type */ false );
Loading history...
343
		wp_safe_redirect( add_query_arg( ['message' => $messageIndex], $redirectUri ));
344
		exit;
1 ignored issue
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
345
	}
346
}
347