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