Passed
Push — master ( 577901...9b3d8a )
by Paul
04:24
created

EditorController::saveMetaboxes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 3
cp 0
crap 2
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\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 = get_post_meta( $post->ID, 'assigned_to', true );
152
		$template = '';
153
		if( $assignedPost = get_post( $assignedTo )) {
0 ignored issues
show
Bug introduced by
It seems like $assignedTo can also be of type false and string; however, parameter $post of get_post() does only seem to accept null|integer|WP_Post, 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

153
		if( $assignedPost = get_post( /** @scrutinizer ignore-type */ $assignedTo )) {
Loading history...
154
			ob_start();
155
			glsr( Html::class )->renderTemplate( 'edit/assigned-post', [
156
				'context' => [
157
					'url' => (string)get_permalink( $assignedPost ),
0 ignored issues
show
Bug introduced by
It seems like $assignedPost can also be of type array; however, parameter $post of get_permalink() does only seem to accept integer|WP_Post, 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

157
					'url' => (string)get_permalink( /** @scrutinizer ignore-type */ $assignedPost ),
Loading history...
158
					'title' => get_the_title( $assignedPost ),
0 ignored issues
show
Bug introduced by
It seems like $assignedPost can also be of type array; however, parameter $post of get_the_title() does only seem to accept integer|WP_Post, 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

158
					'title' => get_the_title( /** @scrutinizer ignore-type */ $assignedPost ),
Loading history...
159
				],
160
			]);
161
			$template = ob_get_clean();
162
		}
163
		wp_nonce_field( 'assigned_to', '_nonce-assigned-to', false );
164
		glsr()->render( 'edit/metabox-assigned-to', [
165
			'id' => $assignedTo,
166
			'template' => $template,
167
		]);
168
	}
169
170
	/**
171
	 * @return void
172
	 * @callback add_meta_box
173
	 */
174
	public function renderDetailsMetaBox( WP_Post $post )
175
	{
176
		if( !$this->isReviewPostType( $post ))return;
177
		$review = glsr( Database::class )->getReview( $post );
178
		glsr()->render( 'edit/metabox-details', [
179
			'button' => $this->buildDetailsMetaBoxRevertButton( $review, $post ),
180
			'metabox' => $this->buildDetailsMetaBox( $review ),
181
		]);
182
	}
183
184
	/**
185
	 * @return void
186
	 * @action post_submitbox_misc_actions
187
	 */
188
	public function renderPinnedInPublishMetaBox()
189
	{
190
		if( !$this->isReviewPostType( get_post() ))return;
191
		$pinned = 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

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

339
			: get_edit_post_link( $postId, /** @scrutinizer ignore-type */ false );
Loading history...
340
		wp_safe_redirect( add_query_arg( ['message' => $messageIndex], $redirectUri ));
341
		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...
342
	}
343
}
344