Taxonomy   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 68
c 3
b 0
f 0
dl 0
loc 155
ccs 0
cts 66
cp 0
rs 10
wmc 21

9 Methods

Rating   Name   Duplication   Size   Complexity  
A normalizePostTypes() 0 3 1
A filterByTaxonomy() 0 11 6
A normalizeMenuName() 0 5 2
A printFilters() 0 15 4
A normalize() 0 10 2
A register() 0 6 2
A normalizeLabels() 0 3 1
A setLabels() 0 15 1
A init() 0 9 2
1
<?php
2
3
namespace GeminiLabs\Pollux\Taxonomy;
4
5
use GeminiLabs\Pollux\Component;
6
use GeminiLabs\Pollux\Helper;
7
use WP_Query;
8
9
class Taxonomy extends Component
10
{
11
	const CUSTOM_KEYS = [
12
		'menu_name', 'plural', 'single',
13
	];
14
15
	const TAXONOMY_DEFAULTS = [
16
		'hierarchical'      => true,
17
		'labels'            => [],
18
		'menu_name'         => '',
19
		'plural'            => '',
20
		'post_types'        => [],
21
		'public'            => true,
22
		'rewrite'           => true,
23
		'show_admin_column' => true,
24
		'show_ui'           => true,
25
		'single'            => '',
26
	];
27
28
	/**
29
	 * @var array
30
	 */
31
	public $taxonomies = [];
32
33
	/**
34
	 * {@inheritdoc}
35
	 */
36
	public function init()
37
	{
38
		if( empty( $this->app->config->taxonomies ))return;
39
40
		$this->normalize();
41
42
		add_action( 'restrict_manage_posts', [ $this, 'printFilters'] );
43
		add_action( 'init',                  [ $this, 'register'] );
44
		add_filter( 'parse_query',           [ $this, 'filterByTaxonomy'] );
45
	}
46
47
	/**
48
	 * @return null|WP_Query
49
	 * @filter parse_query
50
	 */
51
	public function filterByTaxonomy( WP_Query $query )
52
	{
53
		if( !is_admin() || Helper::getCurrentScreen()->base != 'edit' )return;
54
		$vars = &$query->query_vars;
55
		foreach( array_keys( $this->taxonomies ) as $taxonomy ) {
56
			if( !isset( $vars[$taxonomy] ))return;
57
			if( $term = get_term_by( 'id', $vars[$taxonomy], $taxonomy )) {
58
				$vars[$taxonomy] = $term->slug;
59
			}
60
		}
61
		return $query;
62
	}
63
64
	/**
65
	 * @return void
66
	 * @action restrict_manage_posts
67
	 */
68
	public function printFilters()
69
	{
70
		global $wp_query;
71
		foreach( $this->taxonomies as $taxonomy => $args ) {
72
			if( !in_array( get_current_screen()->post_type, $args['post_types'] ))continue;
0 ignored issues
show
Bug introduced by
Are you sure the usage of get_current_screen() is correct as it seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
73
			$selected = isset( $wp_query->query[$taxonomy] )
74
				? $wp_query->query[$taxonomy]
75
				: false;
76
			wp_dropdown_categories([
77
				'hide_if_empty' => true,
78
				'name' => $taxonomy,
79
				'orderby' => 'name',
80
				'selected' => $selected,
81
				'show_option_all' => $args['labels']['all_items'],
82
				'taxonomy' => $taxonomy,
83
			]);
84
		}
85
	}
86
87
	/**
88
	 * @return void
89
	 * @action register
90
	 */
91
	public function register()
92
	{
93
		array_walk( $this->taxonomies, function( $args, $taxonomy ) {
94
			register_taxonomy( $taxonomy, $args['post_types'], array_diff_key( $args, array_flip( static::CUSTOM_KEYS )));
95
			foreach( $args['post_types'] as $type ) {
96
				register_taxonomy_for_object_type( $taxonomy, $type );
97
			}
98
		});
99
	}
100
101
	/**
102
	 * {@inheritdoc}
103
	 */
104
	protected function normalize()
105
	{
106
		foreach( $this->app->config->taxonomies as $taxonomy => $args ) {
107
			$this->taxonomies[$taxonomy] = apply_filters( 'pollux/taxonomy/args',
108
				$this->normalizeThis( $args, static::TAXONOMY_DEFAULTS, $taxonomy )
109
			);
110
		}
111
		$this->taxonomies = array_diff_key(
112
			$this->taxonomies,
113
			get_taxonomies( ['_builtin' => true] )
114
		);
115
	}
116
117
	/**
118
	 * @param mixed $labels
119
	 * @return array
120
	 */
121
	protected function normalizeLabels( $labels, array $args )
122
	{
123
		return wp_parse_args( $labels, $this->setLabels( $args ));
124
	}
125
126
	/**
127
	 * @param string $menuname
128
	 * @return string
129
	 */
130
	protected function normalizeMenuName( $menuname, array $args )
131
	{
132
		return empty( $menuname )
133
			? $args['plural']
134
			: $menuname;
135
	}
136
137
	/**
138
	 * @param mixed $types
139
	 * @return array
140
	 */
141
	protected function normalizePostTypes( $types )
142
	{
143
		return Helper::toArray( $types );
144
	}
145
146
	/**
147
	 * @return array
148
	 */
149
	protected function setLabels( array $args )
150
	{
151
		return apply_filters( 'pollux/taxonomy/labels', [
152
			'add_new_item' => sprintf( _x( 'Add New %s', 'Add new taxonomy', 'pollux' ), $args['single'] ),
153
			'all_items' => sprintf( _x( 'All %s', 'All taxonomies', 'pollux' ), $args['plural'] ),
154
			'edit_item' => sprintf( _x( 'Edit %s', 'Edit taxonomy', 'pollux' ), $args['single'] ),
155
			'menu_name' => $this->normalizeMenuName( $args['menu_name'], $args ),
156
			'name' => $args['plural'],
157
			'new_item_name' => sprintf( _x( 'New %s Name', 'New taxonomy name', 'pollux' ), $args['single'] ),
158
			'not_found' => sprintf( _x( 'No %s found', 'No taxonomies found', 'pollux' ), $args['plural'] ),
159
			'search_items' => sprintf( _x( 'Search %s', 'Search taxonomies', 'pollux' ), $args['plural'] ),
160
			'singular_name' => $args['single'],
161
			'update_item' => sprintf( _x( 'Update %s', 'Update taxonomy', 'pollux' ), $args['single'] ),
162
			'view_item' => sprintf( _x( 'View %s', 'View taxonomy', 'pollux' ), $args['single'] ),
163
		], $args );
164
	}
165
}
166