Passed
Push — master ( 203c84...bd5512 )
by Paul
04:35
created

Columns::buildColumnStars()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 0
cts 4
cp 0
crap 2
rs 10
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\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, $rating = null )
60
	{
61
		if( is_null( $rating )) {
62
			$rating = glsr( Database::class )->getReviewMeta( $postId )->rating;
63
		}
64
		ob_start();
65
		wp_star_rating([
66
			'rating' => $rating,
67
		]);
68
		return ob_get_clean();
69
	}
70
71
	/**
72
	 * @param int $postId
73
	 * @return string
74
	 */
75
	public function buildColumnType( $postId )
76
	{
77
		$reviewMeta = glsr( Database::class )->getReviewMeta( $postId );
78
		return isset( glsr()->reviewTypes[$reviewMeta->review_type] )
79
			? glsr()->reviewTypes[$reviewMeta->review_type]
80
			: $reviewMeta->review_type;
81
	}
82
83
	/**
84
	 * @param string $postType
85
	 * @return void
86
	 */
87
	public function renderFilters( $postType )
88
	{
89
		if( $postType !== Application::POST_TYPE )return;
90
		if( !( $status = filter_input( INPUT_GET, 'post_status' ))) {
91
			$status = 'publish';
92
		}
93
		$ratings = glsr( Database::class )->getReviewsMeta( 'rating', $status );
94
		$types = glsr( Database::class )->getReviewsMeta( 'type', $status );
95
		$this->renderFilterRatings( $ratings );
96
		$this->renderFilterTypes( $types );
97
	}
98
99
	/**
100
	 * @param string $column
101
	 * @param int $postId
102
	 * @return void
103
	 */
104
	public function renderValues( $column, $postId )
105
	{
106
		if( glsr_current_screen()->post_type != Application::POST_TYPE )return;
107
		$method = glsr( Helper::class )->buildMethodName( $column, 'buildColumn' );
108
		echo method_exists( $this, $method )
109
			? call_user_func( [$this, $method], $postId )
110
			: apply_filters( 'site-reviews/columns/'.$column, '', $postId );
111
	}
112
113
	/**
114
	 * @param array $ratings
115
	 * @return void
116
	 */
117
	protected function renderFilterRatings( $ratings )
118
	{
119
		if( empty( $ratings ))return;
120
		$ratings = array_flip( array_reverse( $ratings ));
121
		array_walk( $ratings, function( &$value, $key ) {
122
			$label = _n( '%s star', '%s stars', $key, 'site-reviews' );
123
			$value = sprintf( $label, $key );
124
		});
125
		echo glsr( Builder::class )->label( __( 'Filter by rating', 'site-reviews' ), [
126
			'class' => 'screen-reader-text',
127
			'for' => 'rating',
128
		]);
129
		echo glsr( Builder::class )->select([
130
			'name' => 'rating',
131
			'options' => ['' => __( 'All ratings', 'site-reviews' )] + $ratings,
132
			'value' => filter_input( INPUT_GET, 'rating' ),
133
		]);
134
	}
135
136
	/**
137
	 * @param array $types
138
	 * @return void
139
	 */
140
	protected function renderFilterTypes( $types )
141
	{
142
		if( count( glsr()->reviewTypes ) < 2 )return;
143
		echo glsr( Builder::class )->label( __( 'Filter by type', 'site-reviews' ), [
144
			'class' => 'screen-reader-text',
145
			'for' => 'review_type',
146
		]);
147
		echo glsr( Builder::class )->select([
148
			'name' => 'review_type',
149
			'options' => ['' => __( 'All types', 'site-reviews' )] + glsr()->reviewTypes,
150
			'value' => filter_input( INPUT_GET, 'review_type' ),
151
		]);
152
	}
153
}
154