Test Failed
Push — master ( 59b255...755753 )
by Paul
04:04
created

EditorController::onReviewStatusChange()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 4
nc 2
nop 3
dl 0
loc 6
ccs 0
cts 1
cp 0
crap 12
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;
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\C...torController\Metaboxes was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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 int $metaId
84
	 * @param int $postId
85
	 * @param string $metaKey
86
	 * @param mixed $metaValue
87
	 * @return void
88
	 * @action update_postmeta
89 1
	 */
90
	public function onBeforeUpdateReview( $metaId, $postId, $metaKey, $metaValue )
91 1
	{
92 1
		if( !in_array( $metaKey, ['rating', 'review_type'] )
93
			|| get_post_field( 'post_type', $postId ) != Application::POST_TYPE
94
			|| get_post_field( 'post_status', $postId ) != 'publish' // only fire on an existing published post
95
 		)return;
96
		glsr( Metaboxes::class )->onBeforeUpdateReview( get_post( $postId ), $metaKey, $metaValue );
97
	}
98
99
	/**
100
	 * @param array $postData
101
	 * @param array $meta
102
	 * @param int $postId
103
	 * @return void
104
	 * @action site-reviews/create/review
105
	 */
106
	public function onCreateReview( $postData, $meta, $postId )
107
	{
108
		glsr( Metaboxes::class )->onCreateReview( $postData, $meta, $postId );
109 1
	}
110
111 1
	/**
112 1
	 * @param int $postId
113
	 * @return void
114
	 * @action before_delete_post
115
	 */
116
	public function onDeleteReview( $postId )
117
	{
118
		glsr( Metaboxes::class )->onDeleteReview( $postId );
119
	}
120
121
	/**
122
	 * @param string $oldStatus
123
	 * @param string $newStatus
124
	 * @return void
125
	 */
126
	public function onReviewStatusChange( $newStatus, $oldStatus, WP_Post $post )
127
	{
128
		if( in_array( $oldStatus, ['new', $newStatus] )
129
			|| $post->post_type != Application::POST_TYPE
130
		)return;
131
		glsr( Metaboxes::class )->onReviewStatusChange( $newStatus, $post );
132
	}
133
134
	/**
135
	 * @param int $postId
136
	 * @return void
137
	 * @action save_post_.static::POST_TYPE
138
	 */
139
	public function onSaveReview( $postId, WP_Post $review )
140
	{
141
		glsr( Metaboxes::class )->onSaveReview( $postId, $review );
142
	}
143
144
	/**
145
	 * @param string $postType
146
	 * @return void
147
	 * @action add_meta_boxes
148
	 */
149
	public function registerMetaBoxes( $postType )
150
	{
151
		if( $postType != Application::POST_TYPE )return;
152
		add_meta_box( Application::ID.'_assigned_to', __( 'Assigned To', 'site-reviews' ), [$this, 'renderAssignedToMetabox'], null, 'side' );
153
		add_meta_box( Application::ID.'_review', __( 'Details', 'site-reviews' ), [$this, 'renderDetailsMetaBox'], null, 'side' );
154
		add_meta_box( Application::ID.'_response', __( 'Respond Publicly', 'site-reviews' ), [$this, 'renderResponseMetaBox'], null, 'normal' );
155
	}
156
157
	/**
158
	 * @return void
159
	 * @action admin_print_scripts
160
	 */
161
	public function removeAutosave()
162
	{
163
		glsr( Customization::class )->removeAutosave();
164
	}
165
166
	/**
167
	 * @return void
168
	 * @action admin_menu
169
	 */
170
	public function removeMetaBoxes()
171
	{
172
		glsr( Customization::class )->removeMetaBoxes();
173
	}
174
175
	/**
176
	 * @return void
177
	 * @callback add_meta_box
178
	 */
179
	public function renderAssignedToMetabox( WP_Post $post )
180
	{
181
		if( !$this->isReviewPostType( $post ))return;
182
		$assignedTo = (string)get_post_meta( $post->ID, 'assigned_to', true );
183
		wp_nonce_field( 'assigned_to', '_nonce-assigned-to', false );
184
		glsr()->render( 'partials/editor/metabox-assigned-to', [
185
			'id' => $assignedTo,
186
			'template' => $this->buildAssignedToTemplate( $assignedTo, $post ),
187
		]);
188
	}
189
190
	/**
191
	 * @return void
192
	 * @callback add_meta_box
193
	 */
194
	public function renderDetailsMetaBox( WP_Post $post )
195
	{
196
		if( !$this->isReviewPostType( $post ))return;
197
		$review = glsr( Database::class )->getReview( $post );
198
		glsr()->render( 'partials/editor/metabox-details', [
199
			'button' => $this->buildDetailsMetaBoxRevertButton( $review, $post ),
200
			'metabox' => $this->normalizeDetailsMetaBox( $review ),
201
		]);
202
	}
203
204
	/**
205
	 * @return void
206
	 * @action post_submitbox_misc_actions
207
	 */
208
	public function renderPinnedInPublishMetaBox()
209
	{
210
		if( !$this->isReviewPostType( get_post() ))return;
211
		glsr( Html::class )->renderTemplate( 'partials/editor/pinned', [
212
			'context' => [
213
				'no' => __( 'No', 'site-reviews' ),
214
				'yes' => __( 'Yes', 'site-reviews' ),
215
			],
216
			'pinned' => wp_validate_boolean( get_post_meta( intval( get_the_ID() ), 'pinned', true )),
217
		]);
218
	}
219
220
	/**
221
	 * @return void
222
	 * @callback add_meta_box
223
	 */
224
	public function renderResponseMetaBox( WP_Post $post )
225
	{
226
		if( !$this->isReviewPostType( $post ))return;
227
		wp_nonce_field( 'response', '_nonce-response', false );
228
		glsr()->render( 'partials/editor/metabox-response', [
229
			'response' => glsr( Database::class )->getReview( $post )->response,
230
		]);
231
	}
232
233
	/**
234
	 * @return void
235 1
	 * @see glsr_categories_meta_box()
236
	 * @callback register_taxonomy
237 1
	 */
238 1
	public function renderTaxonomyMetabox( WP_Post $post )
239 1
	{
240
		if( !$this->isReviewPostType( $post ))return;
241
		glsr()->render( 'partials/editor/metabox-categories', [
242
			'post' => $post,
243
			'tax_name' => Application::TAXONOMY,
244
			'taxonomy' => get_taxonomy( Application::TAXONOMY ),
245
		]);
246
	}
247
248
	/**
249
	 * @return void
250
	 * @see $this->filterUpdateMessages()
251
	 * @action admin_action_revert
252
	 */
253
	public function revertReview()
254
	{
255
		check_admin_referer( 'revert-review_'.( $postId = $this->getPostId() ));
256
		glsr( Database::class )->revertReview( $postId );
257
		$this->redirect( $postId, 52 );
258
	}
259
260
	/**
261
	 * @param int $postId
262
	 * @return void
263
	 * @action save_post_.Application::POST_TYPE
264
	 */
265
	public function saveMetaboxes( $postId )
266
	{
267
		glsr( Metaboxes::class )->saveAssignedToMetabox( $postId );
268
		glsr( Metaboxes::class )->saveResponseMetabox( $postId );
269
	}
270
271
	/**
272
	 * @param string $assignedTo
273
	 * @return string
274
	 */
275
	protected function buildAssignedToTemplate( $assignedTo, WP_Post $post )
276
	{
277
		$assignedPost = glsr( Database::class )->getAssignedToPost( $post, $assignedTo );
278
		if( !( $assignedPost instanceof WP_Post ))return;
279
		return glsr( Html::class )->buildTemplate( 'partials/editor/assigned-post', [
280
			'context' => [
281
				'data.url' => (string)get_permalink( $assignedPost ),
282
				'data.title' => get_the_title( $assignedPost ),
283
			],
284
		]);
285
	}
286
287
	/**
288
	 * @param object $review
289
	 * @return string
290
	 */
291
	protected function buildDetailsMetaBoxRevertButton( $review, WP_Post $post )
292
	{
293
		$isModified = !glsr( Helper::class )->compareArrays(
294
			[$review->title, $review->content, $review->date],
295
			[$post->post_title, $post->post_content, $post->post_date]
296
		);
297
		if( $isModified ) {
298
			$revertUrl = wp_nonce_url( admin_url( 'post.php?post='.$post->ID.'&action=revert' ),
299
				'revert-review_'.$post->ID
300
			);
301
			return glsr( Builder::class )->a( __( 'Revert Changes', 'site-reviews' ), [
302
				'class' => 'button button-large',
303
				'href' => $revertUrl,
304
				'id' => 'revert',
305
			]);
306
		}
307
		return glsr( Builder::class )->button( __( 'Nothing to Revert', 'site-reviews' ), [
308
			'class' => 'button button-large',
309
			'disabled' => true,
310
			'id' => 'revert',
311
		]);
312
	}
313
314
	/**
315
	 * @param object $review
316
	 * @return string|void
317
	 */
318
	protected function getReviewType( $review )
319
	{
320
		if( count( glsr()->reviewTypes ) < 2 )return;
321
		$reviewType = array_key_exists( $review->review_type, glsr()->reviewTypes )
322
			? glsr()->reviewTypes[$review->review_type]
323
			: __( 'Unknown', 'site-reviews' );
324
		if( !empty( $review->url )) {
325
			$reviewType = glsr( Builder::class )->a( $reviewType, [
326
				'href' => $review->url,
327
				'target' => '_blank',
328
			]);
329
		}
330
		return $reviewType;
331
	}
332
333
	/**
334
	 * @param mixed $post
335
	 * @return bool
336
	 */
337
	protected function isReviewPostType( $post )
338
	{
339
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
340
	}
341
342
	/**
343
	 * @param object $review
344
	 * @return array
345
	 */
346
	protected function normalizeDetailsMetaBox( $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