Test Failed
Push — master ( 3b4aec...d6370d )
by Paul
03:25
created

buildDetailsMetaBoxRevertButton()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 20
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 14
nc 2
nop 2
dl 0
loc 20
ccs 0
cts 9
cp 0
crap 6
rs 9.7998
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\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;
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...
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 6
	public function filterPostStatusLabels( $translation, $text, $domain )
59
	{
60 6
		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 1
	public function filterPostStatusLabelsWithContext( $translation, $text, $context, $domain )
71
	{
72 1
		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 string $postType
86
	 * @return void
87
	 * @action add_meta_boxes
88
	 */
89
	public function registerMetaBoxes( $postType )
90
	{
91
		if( $postType != Application::POST_TYPE )return;
92
		add_meta_box( Application::ID.'_assigned_to', __( 'Assigned To', 'site-reviews' ), [$this, 'renderAssignedToMetabox'], null, 'side' );
93
		add_meta_box( Application::ID.'_review', __( 'Details', 'site-reviews' ), [$this, 'renderDetailsMetaBox'], null, 'side' );
94
		add_meta_box( Application::ID.'_response', __( 'Respond Publicly', 'site-reviews' ), [$this, 'renderResponseMetaBox'], null, 'normal' );
95
	}
96
97
	/**
98
	 * @return void
99
	 * @action admin_print_scripts
100
	 */
101
	public function removeAutosave()
102
	{
103
		glsr( Customization::class )->removeAutosave();
104
	}
105
106
	/**
107
	 * @return void
108
	 * @action admin_menu
109
	 */
110
	public function removeMetaBoxes()
111
	{
112
		glsr( Customization::class )->removeMetaBoxes();
113
	}
114
115
	/**
116
	 * @return void
117
	 * @callback add_meta_box
118
	 */
119
	public function renderAssignedToMetabox( WP_Post $post )
120
	{
121
		if( !$this->isReviewPostType( $post ))return;
122
		$assignedTo = (string)get_post_meta( $post->ID, 'assigned_to', true );
123
		wp_nonce_field( 'assigned_to', '_nonce-assigned-to', false );
124
		glsr()->render( 'partials/editor/metabox-assigned-to', [
125
			'id' => $assignedTo,
126
			'template' => $this->buildAssignedToTemplate( $assignedTo, $post ),
127
		]);
128
	}
129
130
	/**
131
	 * @return void
132
	 * @callback add_meta_box
133
	 */
134
	public function renderDetailsMetaBox( WP_Post $post )
135
	{
136
		if( !$this->isReviewPostType( $post ))return;
137
		$review = glsr( ReviewManager::class )->single( $post );
138
		glsr()->render( 'partials/editor/metabox-details', [
139
			'button' => $this->buildDetailsMetaBoxRevertButton( $review, $post ),
140
			'metabox' => $this->normalizeDetailsMetaBox( $review ),
141
		]);
142
	}
143
144
	/**
145
	 * @return void
146
	 * @action post_submitbox_misc_actions
147
	 */
148
	public function renderPinnedInPublishMetaBox()
149
	{
150
		if( !$this->isReviewPostType( get_post() ))return;
151
		glsr( Html::class )->renderTemplate( 'partials/editor/pinned', [
152
			'context' => [
153
				'no' => __( 'No', 'site-reviews' ),
154
				'yes' => __( 'Yes', 'site-reviews' ),
155
			],
156
			'pinned' => wp_validate_boolean( get_post_meta( intval( get_the_ID() ), 'pinned', true )),
157
		]);
158
	}
159
160
	/**
161
	 * @return void
162
	 * @callback add_meta_box
163
	 */
164
	public function renderResponseMetaBox( WP_Post $post )
165
	{
166
		if( !$this->isReviewPostType( $post ))return;
167
		wp_nonce_field( 'response', '_nonce-response', false );
168
		glsr()->render( 'partials/editor/metabox-response', [
169
			'response' => get_post_meta( $post->ID, 'response', true ),
170
		]);
171
	}
172
173
	/**
174
	 * @return void
175
	 * @see glsr_categories_meta_box()
176
	 * @callback register_taxonomy
177
	 */
178
	public function renderTaxonomyMetabox( WP_Post $post )
179
	{
180
		if( !$this->isReviewPostType( $post ))return;
181
		glsr()->render( 'partials/editor/metabox-categories', [
182
			'post' => $post,
183
			'tax_name' => Application::TAXONOMY,
184
			'taxonomy' => get_taxonomy( Application::TAXONOMY ),
185
		]);
186
	}
187
188
	/**
189
	 * @return void
190
	 * @see $this->filterUpdateMessages()
191
	 * @action admin_action_revert
192
	 */
193
	public function revertReview()
194
	{
195
		check_admin_referer( 'revert-review_'.( $postId = $this->getPostId() ));
196
		glsr( ReviewManager::class )->revert( $postId );
197
		$this->redirect( $postId, 52 );
198
	}
199
200
	/**
201
	 * @param int $postId
202
	 * @return void
203
	 * @action save_post_.Application::POST_TYPE
204
	 */
205
	public function saveMetaboxes( $postId )
206
	{
207
		glsr( Metaboxes::class )->saveAssignedToMetabox( $postId );
208
		glsr( Metaboxes::class )->saveResponseMetabox( $postId );
209
	}
210
211
	/**
212
	 * @param string $assignedTo
213
	 * @return string
214
	 */
215
	protected function buildAssignedToTemplate( $assignedTo, WP_Post $post )
216
	{
217
		$assignedPost = glsr( Database::class )->getAssignedToPost( $post->ID, $assignedTo );
218
		if( !( $assignedPost instanceof WP_Post ))return;
219
		return glsr( Html::class )->buildTemplate( 'partials/editor/assigned-post', [
220
			'context' => [
221
				'data.url' => (string)get_permalink( $assignedPost ),
222
				'data.title' => get_the_title( $assignedPost ),
223
			],
224
		]);
225
	}
226
227
	/**
228
	 * @return string
229
	 */
230
	protected function buildDetailsMetaBoxRevertButton( Review $review, WP_Post $post )
231
	{
232
		$isModified = !glsr( Helper::class )->compareArrays(
233
			[$review->title, $review->content, $review->date],
234
			[$post->post_title, $post->post_content, $post->post_date]
235
		);
236
		if( $isModified ) {
237
			$revertUrl = wp_nonce_url( admin_url( 'post.php?post='.$post->ID.'&action=revert' ),
238
				'revert-review_'.$post->ID
239
			);
240
			return glsr( Builder::class )->a( __( 'Revert Changes', 'site-reviews' ), [
241
				'class' => 'button button-large',
242
				'href' => $revertUrl,
243
				'id' => 'revert',
244
			]);
245
		}
246
		return glsr( Builder::class )->button( __( 'Nothing to Revert', 'site-reviews' ), [
247
			'class' => 'button button-large',
248
			'disabled' => true,
249
			'id' => 'revert',
250
		]);
251
	}
252
253
	/**
254
	 * @param object $review
255
	 * @return string|void
256
	 */
257
	protected function getReviewType( $review )
258
	{
259
		if( count( glsr()->reviewTypes ) < 2 )return;
260
		$reviewType = array_key_exists( $review->review_type, glsr()->reviewTypes )
261
			? glsr()->reviewTypes[$review->review_type]
262
			: __( 'Unknown', 'site-reviews' );
263
		if( !empty( $review->url )) {
264
			$reviewType = glsr( Builder::class )->a( $reviewType, [
265
				'href' => $review->url,
266
				'target' => '_blank',
267
			]);
268
		}
269
		return $reviewType;
270
	}
271
272
	/**
273
	 * @param mixed $post
274
	 * @return bool
275
	 */
276
	protected function isReviewPostType( $post )
277
	{
278
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
279
	}
280
281
	/**
282
	 * @return array
283
	 */
284
	protected function normalizeDetailsMetaBox( Review $review )
285
	{
286
		$user = empty( $review->user_id )
287
			? __( 'Unregistered user', 'site-reviews' )
288
			: glsr( Builder::class )->a( get_the_author_meta( 'display_name', $review->user_id ), [
289
				'href' => get_author_posts_url( $review->user_id ),
290
			]);
291
		$email = empty( $review->email )
292
			? '&mdash;'
293
			: glsr( Builder::class )->a( $review->email, [
294
				'href' => 'mailto:'.$review->email.'?subject='.esc_attr( __( 'RE:', 'site-reviews' ).' '.$review->title ),
295
			]);
296
		$metabox = [
297
			__( 'Rating', 'site-reviews' ) => glsr( Html::class )->buildPartial( 'star-rating', ['rating' => $review->rating] ),
298
			__( 'Type', 'site-reviews' ) => $this->getReviewType( $review ),
299
			__( 'Date', 'site-reviews' ) => get_date_from_gmt( $review->date, 'F j, Y' ),
300
			__( 'Name', 'site-reviews' ) => $review->author,
301
			__( 'Email', 'site-reviews' ) => $email,
302
			__( 'User', 'site-reviews' ) => $user,
303
			__( 'IP Address', 'site-reviews' ) => $review->ip_address,
304
			__( 'Avatar', 'site-reviews' ) => sprintf( '<img src="%s" width="96">', $review->avatar ),
305
		];
306
		return array_filter( apply_filters( 'site-reviews/metabox/details', $metabox, $review ));
307
	}
308
309
	/**
310
	 * @param int $postId
311
	 * @param int $messageIndex
312
	 * @return void
313
	 */
314
	protected function redirect( $postId, $messageIndex )
315
	{
316
		$referer = wp_get_referer();
317
		$hasReferer = !$referer
318
			|| strpos( $referer, 'post.php' ) !== false
319
			|| strpos( $referer, 'post-new.php' ) !== false;
320
		$redirectUri = $hasReferer
321
			? remove_query_arg( ['deleted', 'ids', 'trashed', 'untrashed'], $referer )
322
			: get_edit_post_link( $postId );
323
		wp_safe_redirect( add_query_arg( ['message' => $messageIndex], $redirectUri ));
324
		exit;
325
	}
326
}
327