Passed
Branch master (3d135a)
by Paul
02:50
created

Columns   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 111
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 12
lcom 1
cbo 1
dl 0
loc 111
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A initColumns() 0 11 3
A printColumnValue() 0 7 2
A getColumnImage() 0 15 3
A getColumnMedia() 0 7 1
A getColumnSlug() 0 4 1
A normalizeColumns() 0 6 1
A setColumns() 0 11 1
1
<?php
2
3
namespace GeminiLabs\Pollux\PostType;
4
5
use GeminiLabs\Pollux\PostMeta;
6
7
trait Columns
8
{
9
	/**
10
	 * @var array
11
	 */
12
	public $columns = [];
13
14
	/**
15
	 * @var array
16
	 */
17
	public $types = [];
18
19
	/**
20
	 * @var Application
21
	 */
22
	protected $app;
23
24
	/**
25
	 * @var void
26
	 */
27
	public function initColumns()
28
	{
29
		foreach( $this->types as $type => $args ) {
30
			add_action( "manage_{$type}_posts_custom_column", [$this, 'printColumnValue'], 10, 2 );
31
			add_filter( "manage_{$type}_posts_columns", function( $columns ) use( $args ) {
32
				return count( $args['columns'] ) > 1
33
					? $args['columns']
34
					: $columns;
35
			});
36
		}
37
	}
38
39
	/**
40
	 * @param string $name
41
	 * @param int $postId
42
	 * @return void
43
	 * @action manage_{$type}_posts_custom_column
44
	 */
45
	public function printColumnValue( $name, $postId )
46
	{
47
		$method = $this->app->buildMethodName( $name, 'getColumn' );
48
		echo method_exists( $this, $method )
49
			? $this->$method( $postId )
50
			: apply_filters( "pollux/post_type/column/{$name}", '' );
51
	}
52
53
	/**
54
	 * @param int $postId
55
	 * @return string
56
	 */
57
	protected function getColumnImage( $postId )
58
	{
59
		if( has_post_thumbnail( $postId ) ) {
60
			list( $src, $width, $height ) = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), [96, 48] );
61
			$image = sprintf( '<img src="%s" alt="%s" width="%s" height="%s">',
62
				esc_url( set_url_scheme( $src )),
63
				esc_attr( get_the_title( $postId )),
64
				$width,
65
				$height
66
			);
67
		}
68
		return empty( $image )
69
			? '&mdash;'
70
			: $image;
71
	}
72
73
	/**
74
	 * @return int
75
	 */
76
	protected function getColumnMedia()
77
	{
78
		return count( (new PostMeta)->get( 'media', [
79
			'fallback' => [],
80
			'single' => false,
81
		]));
82
	}
83
84
	/**
85
	 * @param int $postId
86
	 * @return string
87
	 */
88
	protected function getColumnSlug( $postId )
89
	{
90
		return get_post( $postId )->post_name;
91
	}
92
93
	/**
94
	 * @return array
95
	 */
96
	protected function normalizeColumns( array $columns )
97
	{
98
		$columns = array_flip( $columns );
99
		$columns = array_merge( $columns, array_intersect_key( $this->columns, $columns ));
100
		return ['cb' => '<input type="checkbox">'] + $columns;
101
	}
102
103
	/**
104
	 * @return void
105
	 */
106
	protected function setColumns()
107
	{
108
		$comments = sprintf(
109
			'<span class="vers comment-grey-bubble" title="%1$s"><span class="screen-reader-text">%1$s</span></span>',
110
			$this->app->config['columns']['comments']
111
		);
112
		$columns = wp_parse_args( $this->app->config['columns'], [
113
			'comments' => $comments,
114
		]);
115
		$this->columns = apply_filters( 'pollux/post_type/columns', $columns );
116
	}
117
}
118