1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeminiLabs\Pollux; |
4
|
|
|
|
5
|
|
|
use GeminiLabs\Pollux\Component; |
6
|
|
|
use WP_Query; |
7
|
|
|
|
8
|
|
|
class Taxonomy extends Component |
9
|
|
|
{ |
10
|
|
|
const CUSTOM_KEYS = [ |
11
|
|
|
'menu_name', 'plural', 'single', |
12
|
|
|
]; |
13
|
|
|
|
14
|
|
|
const TAXONOMY_DEFAULTS = [ |
15
|
|
|
'hierarchical' => true, |
16
|
|
|
'labels' => [], |
17
|
|
|
'menu_name' => '', |
18
|
|
|
'plural' => '', |
19
|
|
|
'post_types' => [], |
20
|
|
|
'public' => true, |
21
|
|
|
'rewrite' => true, |
22
|
|
|
'show_admin_column' => true, |
23
|
|
|
'show_ui' => true, |
24
|
|
|
'single' => '', |
25
|
|
|
]; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
public $taxonomies = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
|
|
public function init() |
36
|
|
|
{ |
37
|
|
|
$this->normalize(); |
38
|
|
|
|
39
|
|
|
add_action( 'restrict_manage_posts', [ $this, 'printFilters'] ); |
40
|
|
|
add_action( 'init', [ $this, 'register'] ); |
41
|
|
|
add_filter( 'parse_query', [ $this, 'filterByTaxonomy'] ); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @return null|WP_Query |
46
|
|
|
* @filter parse_query |
47
|
|
|
*/ |
48
|
|
|
public function filterByTaxonomy( WP_Query $query ) |
49
|
|
|
{ |
50
|
|
|
if( !is_admin() || $this->app->screen()->base != 'edit' )return; |
51
|
|
|
$vars = &$query->query_vars; |
52
|
|
|
foreach( array_keys( $this->taxonomies ) as $taxonomy ) { |
53
|
|
|
if( !isset( $vars[$taxonomy] ))return; |
54
|
|
|
if( $term = get_term_by( 'id', $vars[$taxonomy], $taxonomy )) { |
55
|
|
|
$vars[$taxonomy] = $term->slug; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
return $query; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return void |
63
|
|
|
* @action restrict_manage_posts |
64
|
|
|
*/ |
65
|
|
|
public function printFilters() |
66
|
|
|
{ |
67
|
|
|
global $wp_query; |
68
|
|
|
foreach( $this->taxonomies as $taxonomy => $args ) { |
69
|
|
|
if( !in_array( get_current_screen()->post_type, $args['post_types'] ))continue; |
70
|
|
|
$selected = isset( $wp_query->query[$taxonomy] ) |
71
|
|
|
? $wp_query->query[$taxonomy] |
72
|
|
|
: false; |
73
|
|
|
wp_dropdown_categories([ |
74
|
|
|
'hide_if_empty' => true, |
75
|
|
|
'name' => $taxonomy, |
76
|
|
|
'orderby' => 'name', |
77
|
|
|
'selected' => $selected, |
78
|
|
|
'show_option_all' => $args['labels']['all_items'], |
79
|
|
|
'taxonomy' => $taxonomy, |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return void |
86
|
|
|
* @action register |
87
|
|
|
*/ |
88
|
|
|
public function register() |
89
|
|
|
{ |
90
|
|
|
array_walk( $this->taxonomies, function( $args, $taxonomy ) { |
91
|
|
|
register_taxonomy( $taxonomy, $args['post_types'], array_diff_key( $args, array_flip( static::CUSTOM_KEYS ))); |
92
|
|
|
foreach( $args['post_types'] as $type ) { |
93
|
|
|
register_taxonomy_for_object_type( $taxonomy, $type ); |
94
|
|
|
} |
95
|
|
|
}); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* {@inheritdoc} |
100
|
|
|
*/ |
101
|
|
|
protected function normalize() |
102
|
|
|
{ |
103
|
|
View Code Duplication |
foreach( $this->app->config['taxonomies'] as $taxonomy => $args ) { |
104
|
|
|
$this->taxonomies[$taxonomy] = apply_filters( 'pollux/taxonomy/args', |
105
|
|
|
$this->normalizeThis( $args, static::TAXONOMY_DEFAULTS, $taxonomy ) |
106
|
|
|
); |
107
|
|
|
} |
108
|
|
|
$this->taxonomies = array_diff_key( |
109
|
|
|
$this->taxonomies, |
110
|
|
|
get_taxonomies( ['_builtin' => true] ) |
111
|
|
|
); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @param mixed $labels |
116
|
|
|
* @return array |
117
|
|
|
*/ |
118
|
|
|
protected function normalizeLabels( $labels, array $args ) |
119
|
|
|
{ |
120
|
|
|
return wp_parse_args( $labels, $this->setLabels( $args )); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param string $menuname |
125
|
|
|
* @return string |
126
|
|
|
*/ |
127
|
|
|
protected function normalizeMenuName( $menuname, array $args ) |
128
|
|
|
{ |
129
|
|
|
return empty( $menuname ) |
130
|
|
|
? $args['plural'] |
131
|
|
|
: $menuname; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param mixed $types |
136
|
|
|
* @return array |
137
|
|
|
*/ |
138
|
|
|
protected function normalizePostTypes( $types ) |
139
|
|
|
{ |
140
|
|
|
return $this->toArray( $types ); |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return array |
145
|
|
|
*/ |
146
|
|
|
protected function setLabels( array $args ) |
147
|
|
|
{ |
148
|
|
|
return apply_filters( 'pollux/taxonomy/labels', [ |
149
|
|
|
'add_new_item' => sprintf( _x( 'Add New %s', 'Add new taxonomy', 'pollux' ), $args['single'] ), |
150
|
|
|
'all_items' => sprintf( _x( 'All %s', 'All taxonomies', 'pollux' ), $args['plural'] ), |
151
|
|
|
'edit_item' => sprintf( _x( 'Edit %s', 'Edit taxonomy', 'pollux' ), $args['single'] ), |
152
|
|
|
'menu_name' => $this->normalizeMenuName( $args['menu_name'], $args ), |
153
|
|
|
'name' => $args['plural'], |
154
|
|
|
'new_item_name' => sprintf( _x( 'New %s Name', 'New taxonomy name', 'pollux' ), $args['single'] ), |
155
|
|
|
'not_found' => sprintf( _x( 'No %s found', 'No taxonomies found', 'pollux' ), $args['plural'] ), |
156
|
|
|
'search_items' => sprintf( _x( 'Search %s', 'Search taxonomies', 'pollux' ), $args['plural'] ), |
157
|
|
|
'singular_name' => $args['single'], |
158
|
|
|
'update_item' => sprintf( _x( 'Update %s', 'Update taxonomy', 'pollux' ), $args['single'] ), |
159
|
|
|
'view_item' => sprintf( _x( 'View %s', 'View taxonomy', 'pollux' ), $args['single'] ), |
160
|
|
|
], $args ); |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|