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