Passed
Push — master ( 97923e...577901 )
by Paul
04:41
created

EditorController::renderPinnedInPublishMetaBox()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 0
dl 0
loc 6
ccs 0
cts 5
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\Controllers\Controller;
7
use GeminiLabs\SiteReviews\Helper;
8
use GeminiLabs\SiteReviews\Modules\Html;
9
use GeminiLabs\SiteReviews\Modules\Rating;
10
use GeminiLabs\SiteReviews\Modules\Editor\Customization;
11
use GeminiLabs\SiteReviews\Modules\Editor\Labels;
12
use GeminiLabs\SiteReviews\Modules\Editor\Metaboxes;
13
use WP_Post;
14
use WP_Screen;
15
16
class EditorController extends Controller
17
{
18
	/**
19
	 * @return void
20
	 * @action admin_enqueue_scripts
21
	 */
22
	public function customizePostStatusLabels()
23
	{
24
		glsr( Labels::class )->customizePostStatusLabels();
25
	}
26
27
	/**
28
	 * @return array
29
	 * @filter wp_editor_settings
30
	 */
31
	public function filterEditorSettings( array $settings )
32
	{
33
		return glsr( Customization::class )->filterEditorSettings( $settings );
34
	}
35
36
	/**
37
	 * Modify the WP_Editor html to allow autosizing without breaking the `editor-expand` script
38
	 * @param string $html
39
	 * @return string
40
	 * @filter the_editor
41
	 */
42
	public function filterEditorTextarea( $html )
43
	{
44
		return glsr( Customization::class )->filterEditorTextarea( $html );
45
	}
46
47
	/**
48
	 * @param string $translation
49
	 * @param string $test
50
	 * @param string $domain
51
	 * @return string
52
	 * @filter gettext
53
	 */
54 7
	public function filterPostStatusLabels( $translation, $text, $domain )
55
	{
56 7
		return glsr( Labels::class )->filterPostStatusLabels( $translation, $text, $domain );
57
	}
58
59
	/**
60
	 * @param string $translation
61
	 * @param string $test
62
	 * @param string $domain
63
	 * @return string
64
	 * @filter gettext_with_context
65
	 */
66 1
	public function filterPostStatusLabelsWithContext( $translation, $text, $context, $domain )
67
	{
68 1
		return glsr( Labels::class )->filterPostStatusLabels( $translation, $text, $domain );
69
	}
70
71
	/**
72
	 * @return array
73
	 * @filter post_updated_messages
74
	 */
75
	public function filterUpdateMessages( array $messages )
76
	{
77
		return glsr( Labels::class )->filterUpdateMessages( $messages );
78
	}
79
80
	/**
81
	 * @param array $postData
82
	 * @param array $meta
83
	 * @param int $postId
84
	 * @return void
85
	 * @action site-reviews/create/review
86
	 */
87
	public function onCreateReview( $postData, $meta, $postId )
88
	{
89
		glsr( Metaboxes::class )->onCreateReview( $postData, $meta, $postId );
90
	}
91
92
	/**
93
	 * @param int $postId
94
	 * @return void
95
	 * @action before_delete_post
96
	 */
97
	public function onDeleteReview( $postId )
98
	{
99
		glsr( Metaboxes::class )->onDeleteReview( $postId );
100
	}
101
102
	/**
103
	 * @param int $postId
104
	 * @return void
105
	 * @action save_post_.static::POST_TYPE
106
	 */
107
	public function onSaveReview( $postId, WP_Post $review )
108
	{
109
		glsr( Metaboxes::class )->onSaveReview( $postId, $review );
110
	}
111
112
	/**
113
	 * @param string $postType
114
	 * @return void
115
	 * @action add_meta_boxes
116
	 */
117
	public function registerMetaBoxes( $postType )
118
	{
119
		if( $postType != Application::POST_TYPE )return;
120
		add_meta_box( Application::ID.'_assigned_to', __( 'Assigned To', 'site-reviews' ), [$this, 'renderAssignedToMetabox'], null, 'side' );
121
		add_meta_box( Application::ID.'_review', __( 'Details', 'site-reviews' ), [$this, 'renderDetailsMetaBox'], null, 'side' );
122
		add_meta_box( Application::ID.'_response', __( 'Respond Publicly', 'site-reviews' ), [$this, 'renderResponseMetaBox'], null, 'normal' );
123
	}
124
125
	/**
126
	 * @return void
127
	 * @action admin_print_scripts
128
	 */
129
	public function removeAutosave()
130
	{
131
		glsr( Customization::class )->removeAutosave();
132
	}
133
134
	/**
135
	 * @return void
136
	 * @action admin_menu
137
	 */
138
	public function removeMetaBoxes()
139
	{
140
		glsr( Customization::class )->removeMetaBoxes();
141
	}
142
143
	/**
144
	 * @return void
145
	 * @callback add_meta_box
146
	 */
147
	public function renderAssignedToMetabox( WP_Post $post )
148
	{
149
		if( !$this->isReviewPostType( $post ))return;
150
		$assignedTo = get_post_meta( $post->ID, 'assigned_to', true );
151
		$template = '';
152
		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

152
		if( $assignedPost = get_post( /** @scrutinizer ignore-type */ $assignedTo )) {
Loading history...
153
			ob_start();
154
			glsr( Html::class )->renderTemplate( 'edit/assigned-post', [
155
				'context' => [
156
					'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

156
					'url' => (string)get_permalink( /** @scrutinizer ignore-type */ $assignedPost ),
Loading history...
157
					'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

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

190
		$pinned = get_post_meta( /** @scrutinizer ignore-type */ get_the_ID(), 'pinned', true );
Loading history...
191
		glsr()->render( 'edit/pinned', [
192
			'pinned' => $pinned,
193
		]);
194
	}
195
196
	/**
197
	 * @return void
198
	 * @callback add_meta_box
199
	 */
200
	public function renderResponseMetaBox( WP_Post $post )
201
	{
202
		if( !$this->isReviewPostType( $post ))return;
203
		$review = glsr( Database::class )->getReview( $post );
204
		wp_nonce_field( 'response', '_nonce-response', false );
205
		glsr()->render( 'edit/metabox-response', [
206
			'response' => $review->response,
207
		]);
208
	}
209
210
	/**
211
	 * @return void
212
	 * @see glsr_categories_meta_box()
213
	 * @callback register_taxonomy
214
	 */
215
	public function renderTaxonomyMetabox( WP_Post $post )
216
	{
217
		if( !$this->isReviewPostType( $post ))return;
218
		glsr()->render( 'edit/metabox-categories', [
219
			'post' => $post,
220
			'tax_name' => esc_attr( Application::TAXONOMY ),
221
			'taxonomy' => get_taxonomy( Application::TAXONOMY ),
222
		]);
223
	}
224
225
	/**
226
	 * @return void
227
	 * @see $this->filterUpdateMessages()
228
	 * @action admin_action_revert
229
	 */
230
	public function revertReview()
231
	{
232
		check_admin_referer( 'revert-review_'.( $postId = $this->getPostId() ));
233
		glsr( Database::class )->revertReview( $postId );
234
		$this->redirect( $postId, 52 );
235
	}
236
237
	/**
238
	 * @param int $postId
239
	 * @return void
240
	 * @action save_post_.Application::POST_TYPE
241
	 */
242
	public function saveMetaboxes( $postId )
243
	{
244
		glsr( Metaboxes::class )->saveAssignedToMetabox( $postId );
245
		glsr( Metaboxes::class )->saveResponseMetabox( $postId );
246
	}
247
248
	/**
249
	 * @param object $review
250
	 * @return array
251
	 */
252
	protected function buildDetailsMetaBox( $review )
253
	{
254
		$reviewer = empty( $review->user_id )
255
			? __( 'Unregistered user', 'site-reviews' )
256
			: $this->generateLink( get_the_author_meta( 'display_name', $review->user_id ), [
0 ignored issues
show
Bug introduced by
The method generateLink() does not exist on GeminiLabs\SiteReviews\C...ollers\EditorController. ( Ignorable by Annotation )

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

256
			: $this->/** @scrutinizer ignore-call */ generateLink( get_the_author_meta( 'display_name', $review->user_id ), [

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
257
				'href' => get_author_posts_url( $review->user_id ),
258
			]);
259
		$email = empty( $review->email )
260
			? '&mdash;'
261
			: $this->generateLink( $review->email, [
262
				'href' => 'mailto:'.$review->email.'?subject='.esc_attr( __( 'RE:', 'site-reviews' ).' '.$review->title ),
263
			]);
264
		$metabox = [
265
			__( 'Rating', 'site-reviews' ) => glsr( Html::class )->renderPartial( 'star-rating', ['rating' => $review->rating] ),
266
			__( 'Type', 'site-reviews' ) => $this->getReviewType( $review ),
267
			__( 'Date', 'site-reviews' ) => get_date_from_gmt( $review->date, 'F j, Y' ),
268
			__( 'Reviewer', 'site-reviews' ) => $reviewer,
269
			__( 'Name', 'site-reviews' ) => $review->author,
270
			__( 'Email', 'site-reviews' ) => $email,
271
			__( 'IP Address', 'site-reviews' ) => $review->ip_address,
272
			__( 'Avatar', 'site-reviews' ) => sprintf( '<img src="%s" width="96">', $review->avatar ),
273
		];
274
		return apply_filters( 'site-reviews/metabox/details', $metabox, $review );
275
	}
276
277
	/**
278
	 * @param object $review
279
	 * @return string
280
	 */
281
	protected function buildDetailsMetaBoxRevertButton( $review, WP_Post $post )
282
	{
283
		$modified = false;
284
		if( $post->post_title !== $review->title
285
			|| $post->post_content !== $review->content
286
			|| $post->post_date !== $review->date ) {
287
			$modified = true;
288
		}
289
		$revertUrl = wp_nonce_url(
290
			admin_url( 'post.php?post='.$post->ID.'&action=revert' ),
291
			'revert-review_'.$post->ID
292
		);
293
		return !$modified
294
			? sprintf( '<button id="revert" class="button button-large" disabled>%s</button>', __( 'Nothing to Revert', 'site-reviews' ))
295
			: sprintf( '<a href="%s" id="revert" class="button button-large">%s</a>', $revertUrl, __( 'Revert Changes', 'site-reviews' ));
296
	}
297
298
	/**
299
	 * @param object $review
300
	 * @return string
301
	 */
302
	protected function getReviewType( $review )
303
	{
304
		$reviewType = $review->review_type;
305
		$reviewTypeFallback = !empty( $review->review_type )
306
			? ucfirst( $review->review_type )
307
			: __( 'Unknown', 'site-reviews' );
308
		if( !empty( $review->url )) {
309
			$reviewType = $this->generateLink( $reviewType, [
310
				'href' => $review->url,
311
				'target' => '_blank',
312
			]);
313
		}
314
		return sprintf( __( '%s review', 'site-reviews' ),
315
			glsr( Strings::class )->review_types( $reviewType, $reviewTypeFallback )
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Controllers\Strings 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...
316
		);
317
	}
318
319
	/**
320
	 * @param mixed $post
321
	 * @return bool
322
	 */
323
	protected function isReviewPostType( $post )
324
	{
325
		return $post instanceof WP_Post && $post->post_type == Application::POST_TYPE;
326
	}
327
328
	/**
329
	 * @param int $postId
330
	 * @param int $messageIndex
331
	 * @return void
332
	 */
333
	protected function redirect( $postId, $messageIndex )
334
	{
335
		$referer = wp_get_referer();
336
		$hasReferer = !$referer
337
			|| strpos( $referer, 'post.php' ) !== false
338
			|| strpos( $referer, 'post-new.php' ) !== false;
339
		$redirectUri = $hasReferer
340
			? remove_query_arg( ['deleted', 'ids', 'trashed', 'untrashed'], $referer )
341
			: 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

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