1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Taxonomy; |
4
|
|
|
|
5
|
|
|
use stdClass; |
6
|
|
|
use Silk\Type\Type; |
7
|
|
|
use Silk\Type\Registerable; |
8
|
|
|
use Silk\PostType\PostType; |
9
|
|
|
use Silk\Term\QueryBuilder; |
10
|
|
|
use Illuminate\Support\Collection; |
11
|
|
|
use Silk\Exception\WP_ErrorException; |
12
|
|
|
use Silk\Taxonomy\Exception\InvalidTaxonomyNameException; |
13
|
|
|
use Silk\Taxonomy\Exception\NonExistentTaxonomyException; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @property-read bool $_builtin |
17
|
|
|
* @property-read stdClass $cap |
18
|
|
|
* @property-read string $description |
19
|
|
|
* @property-read bool $hierarchical |
20
|
|
|
* @property-read string $label |
21
|
|
|
* @property-read stdClass $labels |
22
|
|
|
* @property-read callable $meta_box_cb |
23
|
|
|
* @property-read string $name |
24
|
|
|
* @property-read array $object_type |
25
|
|
|
* @property-read bool $public |
26
|
|
|
* @property-read bool $publicly_queryable |
27
|
|
|
* @property-read string $query_var |
28
|
|
|
* @property-read array $rewrite |
29
|
|
|
* @property-read bool $show_admin_column |
30
|
|
|
* @property-read bool $show_in_menu |
31
|
|
|
* @property-read bool $show_in_nav_menus |
32
|
|
|
* @property-read bool $show_in_quick_edit |
33
|
|
|
* @property-read bool $show_tagcloud |
34
|
|
|
* @property-read bool $show_ui |
35
|
|
|
* @property-read callable $update_count_callback |
36
|
|
|
*/ |
37
|
|
|
class Taxonomy extends Type implements Registerable |
38
|
|
|
{ |
39
|
|
|
/** |
40
|
|
|
* Taxonomy Constructor. |
41
|
|
|
* |
42
|
|
|
* @param object $taxonomy The taxonomy object |
43
|
|
|
* |
44
|
|
|
* @throws NonExistentTaxonomyException |
45
|
|
|
*/ |
46
|
|
|
public function __construct($taxonomy) |
47
|
|
|
{ |
48
|
|
|
if (empty($taxonomy->name) || ! static::exists($taxonomy->name)) { |
49
|
|
|
throw new NonExistentTaxonomyException; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$this->object = $taxonomy; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new instance using the taxonomy identifier. |
57
|
|
|
* |
58
|
|
|
* @param string $id Taxonomy name/identifier |
59
|
|
|
* |
60
|
|
|
* @throws InvalidTaxonomyNameException |
61
|
|
|
* |
62
|
|
|
* @return static|Builder |
63
|
|
|
*/ |
64
|
|
|
public static function make($id) |
65
|
|
|
{ |
66
|
|
|
if (static::exists($id)) { |
67
|
|
|
return static::load($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
if ( ! $id || strlen($id) > 32) { |
71
|
|
|
throw new InvalidTaxonomyNameException('Taxonomy names must be between 1 and 32 characters in length.'); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
return static::build($id); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Create a new instance from an existing taxonomy. |
79
|
|
|
* |
80
|
|
|
* @param string $id The taxonomy identifier |
81
|
|
|
* |
82
|
|
|
* @throws NonExistentTaxonomyException |
83
|
|
|
* |
84
|
|
|
* @return static |
85
|
|
|
*/ |
86
|
|
|
public static function load($id) |
87
|
|
|
{ |
88
|
|
|
if (! $object = get_taxonomy($id)) { |
89
|
|
|
throw new NonExistentTaxonomyException("No taxonomy exists with name '$id'."); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
return new static($object); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Build a new Taxonomy to be registered. |
97
|
|
|
* |
98
|
|
|
* @param $id |
99
|
|
|
* |
100
|
|
|
* @return Builder |
101
|
|
|
*/ |
102
|
|
|
public static function build($id) |
103
|
|
|
{ |
104
|
|
|
return new Builder($id); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Check if a Taxonomy exists for the given identifier. |
109
|
|
|
* |
110
|
|
|
* @param string $id The taxonomy key/identifier |
111
|
|
|
* |
112
|
|
|
* @return bool |
113
|
|
|
*/ |
114
|
|
|
public static function exists($id) |
115
|
|
|
{ |
116
|
|
|
return taxonomy_exists($id); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @return mixed |
121
|
|
|
*/ |
122
|
|
|
public function id() |
123
|
|
|
{ |
124
|
|
|
return $this->object->name; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Start a new query for terms of this taxonomy. |
129
|
|
|
* |
130
|
|
|
* @return QueryBuilder |
131
|
|
|
*/ |
132
|
|
|
public function terms() |
133
|
|
|
{ |
134
|
|
|
return (new QueryBuilder)->forTaxonomy($this->id()); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Get all post types associated with this taxonomy. |
139
|
|
|
* |
140
|
|
|
* @return Collection |
141
|
|
|
*/ |
142
|
|
|
public function postTypes() |
143
|
|
|
{ |
144
|
|
|
return Collection::make($this->object_type) |
145
|
|
|
->map(function ($post_type) { |
146
|
|
|
return PostType::load($post_type); |
147
|
|
|
}); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Unregister the taxonomy. |
152
|
|
|
* |
153
|
|
|
* @throws NonExistentTaxonomyException |
154
|
|
|
* @throws WP_ErrorException |
155
|
|
|
* |
156
|
|
|
* @return $this |
157
|
|
|
*/ |
158
|
|
|
public function unregister() |
159
|
|
|
{ |
160
|
|
|
if (! $this->exists($this->id())) { |
161
|
|
|
throw new NonExistentTaxonomyException; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
if (is_wp_error($error = unregister_taxonomy($this->id()))) { |
165
|
|
|
throw new WP_ErrorException($error); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|