Passed
Push — master ( 78492a...4f268f )
by Paul
04:26
created

Columns::buildColumnReviewer()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

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