Passed
Branch develop (f3ed2c)
by Paul
02:52
created

Columns   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
dl 0
loc 101
rs 10
c 0
b 0
f 0
wmc 12
lcom 1
cbo 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\Facades\PostMeta;
6
7
trait Columns
8
{
9
	/**
10
	 * @var array
11
	 */
12
	public $columns = [];
13
14
	/**
15
	 * @var void
16
	 */
17
	public function initColumns()
18
	{
19
		foreach( $this->types as $type => $args ) {
0 ignored issues
show
Bug introduced by
The property types does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
20
			add_action( "manage_{$type}_posts_custom_column", [$this, 'printColumnValue'], 10, 2 );
21
			add_filter( "manage_{$type}_posts_columns", function( $columns ) use( $args ) {
22
				return count( $args['columns'] ) > 1
23
					? $args['columns']
24
					: $columns;
25
			});
26
		}
27
	}
28
29
	/**
30
	 * @param string $name
31
	 * @param int $postId
32
	 * @return void
33
	 * @action manage_{$type}_posts_custom_column
34
	 */
35
	public function printColumnValue( $name, $postId )
36
	{
37
		$method = $this->app->buildMethodName( $name, 'getColumn' );
0 ignored issues
show
Bug introduced by
The property app does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
		echo method_exists( $this, $method )
39
			? $this->$method( $postId )
40
			: apply_filters( "pollux/post_type/column/{$name}", '' );
41
	}
42
43
	/**
44
	 * @param int $postId
45
	 * @return string
46
	 */
47
	protected function getColumnImage( $postId )
48
	{
49
		if( has_post_thumbnail( $postId ) ) {
50
			list( $src, $width, $height ) = wp_get_attachment_image_src( get_post_thumbnail_id( $postId ), [96, 48] );
51
			$image = sprintf( '<img src="%s" alt="%s" width="%s" height="%s">',
52
				esc_url( set_url_scheme( $src )),
53
				esc_attr( get_the_title( $postId )),
54
				$width,
55
				$height
56
			);
57
		}
58
		return empty( $image )
59
			? '&mdash;'
60
			: $image;
61
	}
62
63
	/**
64
	 * @return int
65
	 */
66
	protected function getColumnMedia()
67
	{
68
		return count( PostMeta::get( 'media', [
69
			'fallback' => [],
70
			'single' => false,
71
		]));
72
	}
73
74
	/**
75
	 * @param int $postId
76
	 * @return string
77
	 */
78
	protected function getColumnSlug( $postId )
79
	{
80
		return get_post( $postId )->post_name;
81
	}
82
83
	/**
84
	 * @return array
85
	 */
86
	protected function normalizeColumns( array $columns )
87
	{
88
		$columns = array_flip( $columns );
89
		$columns = array_merge( $columns, array_intersect_key( $this->columns, $columns ));
90
		return ['cb' => '<input type="checkbox">'] + $columns;
91
	}
92
93
	/**
94
	 * @return void
95
	 */
96
	protected function setColumns()
97
	{
98
		$comments = sprintf(
99
			'<span class="vers comment-grey-bubble" title="%1$s"><span class="screen-reader-text">%1$s</span></span>',
100
			$this->app->config['columns']['comments']
101
		);
102
		$columns = wp_parse_args( $this->app->config['columns'], [
103
			'comments' => $comments,
104
		]);
105
		$this->columns = apply_filters( 'pollux/post_type/columns', $columns );
106
	}
107
}
108