Test Failed
Push — master ( 87eb8d...25783a )
by Paul
03:58
created

EditorController   B

Complexity

Total Complexity 49

Size/Duplication

Total Lines 367
Duplicated Lines 0 %

Test Coverage

Coverage 11.41%

Importance

Changes 0
Metric Value
eloc 113
dl 0
loc 367
ccs 17
cts 149
cp 0.1141
rs 8.48
c 0
b 0
f 0
wmc 49

27 Methods

Rating   Name   Duplication   Size   Complexity  
A isReviewPostType() 0 3 2
A filterPostStatusLabelsWithContext() 0 3 1
A customizePostStatusLabels() 0 3 1
A normalizeDetailsMetaBox() 0 23 3
A removeAutosave() 0 3 1
A buildDetailsMetaBoxRevertButton() 0 20 2
A redirect() 0 11 4
A registerMetaBoxes() 0 6 2
A buildAssignedToTemplate() 0 8 2
A filterEditorTextarea() 0 3 1
A getReviewType() 0 13 4
A removeMetaBoxes() 0 3 1
A onReviewStatusChange() 0 6 3
A saveMetaboxes() 0 4 1
A renderAssignedToMetabox() 0 8 2
A revertReview() 0 5 1
A filterEditorSettings() 0 3 1
A filterPostStatusLabels() 0 3 1
A renderPinnedInPublishMetaBox() 0 9 2
A filterUpdateMessages() 0 3 1
A renderResponseMetaBox() 0 6 2
A renderDetailsMetaBox() 0 7 2
A renderTaxonomyMetabox() 0 7 2
A onSaveReview() 0 3 1
A onBeforeDeleteReview() 0 3 1
A onCreateReview() 0 3 1
A onBeforeUpdateReview() 0 7 4

How to fix   Complexity   

Complex Class

Complex classes like EditorController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

While breaking up the class, it is a good idea to analyze how other classes use EditorController, and based on these observations, apply Extract Interface, too.

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