Passed
Push — master ( 0de278...836c6f )
by Paul
04:15
created

Columns::renderValues()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 2
dl 0
loc 7
ccs 0
cts 7
cp 0
crap 12
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace GeminiLabs\SiteReviews\Modules\ListTable;
4
5
use GeminiLabs\SiteReviews\Application;
6
use GeminiLabs\SiteReviews\Database;
7
use GeminiLabs\SiteReviews\Modules\Html;
8
9
class Columns
10
{
11
	/**
12
	 * @param string $postType
13
	 * @return void
14
	 */
15
	public function renderFilters( $postType )
16
	{
17
		if( $postType !== Application::POST_TYPE )return;
18
		if( !( $status = filter_input( INPUT_GET, 'post_status' ))) {
19
			$status = 'publish';
20
		}
21
		$ratings = glsr( Database::class )->getReviewsMeta( 'rating', $status );
22
		$types = glsr( Database::class )->getReviewsMeta( 'type', $status );
23
		$this->renderFilterRatings( $ratings );
24
		$this->renderFilterTypes( $types );
25
	}
26
27
	/**
28
	 * @param string $column
29
	 * @param int $postId
30
	 * @return void
31
	 */
32
	public function renderValues( $column, $postId )
33
	{
34
		if( glsr_current_screen()->id != Application::POST_TYPE )return;
35
		$method = glsr( Helper::class )->buildMethodName( $column, 'buildColumn' );
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Modules\ListTable\Helper 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...
36
		echo method_exists( $this, $method )
37
			? call_user_func( [$this, $method], $postId )
38
			: apply_filters( 'site-reviews/columns/'.$column, '', $postId );
39
	}
40
41
	/**
42
	 * @param int $postId
43
	 * @return string
44
	 */
45
	protected function buildColumnAssignedTo( $postId )
46
	{
47
		$post = get_post( glsr( Database::class )->getReviewMeta( $postId )->assigned_to );
48
		if( !( $post instanceof WP_Post ) || $post->post_status != 'publish' ) {
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Modules\ListTable\WP_Post was not found. Did you mean WP_Post? If so, make sure to prefix the type with \.
Loading history...
49
			return '&mdash;';
50
		}
51
		return glsr( Builder::class )->a( get_the_title( $post->ID ), [
0 ignored issues
show
Bug introduced by
The type GeminiLabs\SiteReviews\Modules\ListTable\Builder 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...
52
			'href' => (string)get_the_permalink( $post->ID ),
53
		]);
54
	}
55
56
	/**
57
	 * @param int $postId
58
	 * @return string
59
	 */
60
	protected function buildColumnReviewer( $postId )
61
	{
62
		return glsr( Database::class )->getReviewMeta( $postId )->author;
63
	}
64
65
	/**
66
	 * @param int $postId
67
	 * @return string
68
	 */
69
	protected function buildColumnStars( $postId )
70
	{
71
		return glsr( Html::class )->buildPartial( 'star-rating', [
72
			'rating' => glsr( Database::class )->getReviewMeta( $postId )->rating,
73
		]);
74
	}
75
76
	/**
77
	 * @param int $postId
78
	 * @return string
79
	 */
80
	protected function buildColumnSticky( $postId )
81
	{
82
		$pinned = glsr( Database::class )->getReviewMeta( $postId )->pinned
83
			? ' pinned'
84
			: '';
85
		return glsr( Builder::class )->i([
86
			'class' => trim( 'dashicons dashicons-sticky '.$pinned ),
87
			'data-id' => $postId,
88
		]);
89
	}
90
91
	/**
92
	 * @param int $postId
93
	 * @return string
94
	 */
95
	protected function buildColumnType( $postId )
96
	{
97
		$reviewMeta = glsr( Database::class )->getReviewMeta( $postId );
98
		return isset( glsr()->reviewTypes[$reviewMeta->review_type] )
99
			? glsr()->reviewTypes[$reviewMeta->review_type]
100
			: $reviewMeta->review_type;
101
	}
102
103
	/**
104
	 * @param array $ratings
105
	 * @return void
106
	 */
107
	protected function renderFilterRatings( $ratings )
108
	{
109
		if( empty( $ratings )
110
			|| apply_filters( 'site-reviews/disable/filter/ratings', false )
111
		)return;
112
		$ratings = array_flip( array_reverse( $ratings ));
113
		array_walk( $ratings, function( &$value, $key ) {
114
			$label = _n( '%s star', '%s stars', $key, 'site-reviews' );
115
			$value = sprintf( $label, $key );
116
		});
117
		$ratings = [__( 'All ratings', 'site-reviews' )] + $ratings;
118
		printf( '<label class="screen-reader-text" for="rating">%s</label>', __( 'Filter by rating', 'site-reviews' ));
119
		glsr( Html::class )->renderPartial( 'filterby', [
120
			'name' => 'rating',
121
			'values' => $ratings,
122
		]);
123
	}
124
125
	/**
126
	 * @param array $types
127
	 * @return void
128
	 */
129
	protected function renderFilterTypes( $types )
130
	{
131
		if( count( glsr()->reviewTypes ) < 2 )return;
132
		$reviewTypes = ['' => __( 'All types', 'site-reviews' )] + glsr()->reviewTypes;
133
		printf( '<label class="screen-reader-text" for="type">%s</label>', __( 'Filter by type', 'site-reviews' ));
134
		glsr( Html::class )->renderPartial( 'filterby', [
135
			'name' => 'review_type',
136
			'options' => $reviewTypes,
137
		]);
138
	}
139
}
140