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