1 | <?php |
---|
2 | new CPT_Columns( 'Event', 'Evemts', 'dw-events' ); |
---|
3 | |
---|
4 | class CPT_Columns { |
---|
5 | |
---|
6 | private $single; |
---|
7 | private $plural; |
---|
8 | private $slug; |
---|
9 | private $taxonomies; |
---|
10 | static $instance; |
---|
11 | |
---|
12 | function __construct( $post_type, $plural = '', $slug = '' ) { |
---|
13 | |
---|
14 | self::$instance = $this; |
---|
15 | |
---|
16 | $this->single = $post_type; |
---|
17 | $this->plural = !empty( $plural ) ? $plural : $post_type .'s'; |
---|
18 | $this->slug = !empty( $slug ) ? $slug : sanitize_title( $this->plural ); |
---|
19 | |
---|
20 | add_filter( 'manage_edit-'. $this->slug .'_columns', array( &$this, 'columns' ) ); |
---|
21 | add_action( 'manage_posts_custom_column', array( &$this, 'columns_display' ) ); |
---|
22 | |
---|
23 | } |
---|
24 | |
---|
25 | public function columns( $columns ) { |
---|
26 | $taxonomies = get_object_taxonomies( $this->slug ); |
---|
27 | |
---|
28 | if ( !empty( $taxonomies ) ){ |
---|
29 | |
---|
30 | foreach ( $taxonomies as $key => $tax ) { |
---|
31 | if ( $tax == 'post_tag' || $tax == 'post_category' ) continue; |
---|
32 | |
---|
33 | $object = get_taxonomy( $tax ); |
---|
34 | if ( !$object->hierarchical ) continue; |
---|
35 | |
---|
36 | $this->taxonomies[] = $tax; |
---|
37 | |
---|
38 | |
---|
39 | $columns[$tax.'_column'] = $object->label; |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | return $columns; |
---|
44 | } |
---|
45 | |
---|
46 | public function columns_display( $column ) { |
---|
47 | |
---|
48 | if ( !empty( $this->taxonomies ) ){ |
---|
49 | |
---|
50 | foreach ( $this->taxonomies as $tax ) { |
---|
51 | if ( $column == $tax.'_column' ) { |
---|
52 | $this->taxonomy_column( $tax, ucfirst( str_replace( '-', ' ', $tax ) ) ); |
---|
53 | } |
---|
54 | } |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | public function taxonomy_column( $tax = '', $name = '' ) { |
---|
59 | global $post; |
---|
60 | $id = $post->ID; |
---|
61 | $categories = get_the_terms( $id, $tax ); |
---|
62 | if ( !empty( $categories ) ) { |
---|
63 | $out = array(); |
---|
64 | foreach ( $categories as $c ) { |
---|
65 | $out[] = sprintf( '<a href="%s">%s</a>', |
---|
66 | esc_url( add_query_arg( array( 'post_type' => $post->post_type, $tax => $c->slug ), 'edit.php' ) ), |
---|
67 | esc_html( sanitize_term_field( 'name', $c->name, $c->term_id, 'category', 'display' ) ) |
---|
68 | ); |
---|
69 | } |
---|
70 | echo join( ', ', $out ); |
---|
71 | } else { |
---|
72 | _e( 'No '. $name .' Specified' ); |
---|
73 | } |
---|
74 | |
---|
75 | } |
---|
76 | } |
---|