1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Silk\Term; |
4
|
|
|
|
5
|
|
|
use stdClass; |
|
|
|
|
6
|
|
|
use WP_Term; |
7
|
|
|
use Silk\Taxonomy\Taxonomy; |
8
|
|
|
use Silk\Type\Model as BaseModel; |
9
|
|
|
use Illuminate\Support\Collection; |
10
|
|
|
use Silk\Term\Exception\TermNotFoundException; |
11
|
|
|
use Silk\Term\Exception\TaxonomyMismatchException; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @property-read WP_Term $term |
15
|
|
|
* @property int $term_id |
16
|
|
|
* @property string $name |
17
|
|
|
* @property string $slug |
18
|
|
|
* @property string $term_group |
19
|
|
|
* @property int $term_taxonomy_id |
20
|
|
|
* @property string $taxonomy |
21
|
|
|
* @property string $description |
22
|
|
|
* @property int $parent |
23
|
|
|
* @property int $count |
24
|
|
|
*/ |
25
|
|
|
abstract class Model extends BaseModel |
26
|
|
|
{ |
|
|
|
|
27
|
|
|
/** |
|
|
|
|
28
|
|
|
* The term's taxonomy |
|
|
|
|
29
|
|
|
* @var string |
|
|
|
|
30
|
|
|
*/ |
|
|
|
|
31
|
|
|
const TAXONOMY = ''; |
|
|
|
|
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* The object type in WordPress |
|
|
|
|
35
|
|
|
* @var string |
|
|
|
|
36
|
|
|
*/ |
|
|
|
|
37
|
|
|
const OBJECT_TYPE = 'term'; |
|
|
|
|
38
|
|
|
|
39
|
|
|
/** |
|
|
|
|
40
|
|
|
* The primary ID property on the object |
|
|
|
|
41
|
|
|
*/ |
|
|
|
|
42
|
|
|
const ID_PROPERTY = 'term_id'; |
|
|
|
|
43
|
|
|
|
44
|
|
|
/** |
|
|
|
|
45
|
|
|
* Model Constructor. |
|
|
|
|
46
|
|
|
* |
|
|
|
|
47
|
|
|
* @param mixed $term WP_Term to fill data from |
|
|
|
|
48
|
|
|
* |
|
|
|
|
49
|
|
|
* @throws TaxonomyMismatchException |
|
|
|
|
50
|
|
|
*/ |
|
|
|
|
51
|
|
|
public function __construct(WP_Term $term = null) |
|
|
|
|
52
|
|
|
{ |
|
|
|
|
53
|
|
|
if (! $term) { |
|
|
|
|
54
|
|
|
$term = new WP_Term(new stdClass); |
|
|
|
|
55
|
|
|
$term->taxonomy = static::TAXONOMY; |
|
|
|
|
56
|
|
|
} elseif ($term->taxonomy != static::TAXONOMY) { |
|
|
|
|
57
|
|
|
throw new TaxonomyMismatchException(); |
|
|
|
|
58
|
|
|
} |
|
|
|
|
59
|
|
|
|
60
|
|
|
$this->object = $term; |
|
|
|
|
61
|
|
|
} |
|
|
|
|
62
|
|
|
|
63
|
|
|
/** |
|
|
|
|
64
|
|
|
* Create a new instance from a WP_Term object. |
|
|
|
|
65
|
|
|
* |
|
|
|
|
66
|
|
|
* @param WP_Term $term [description] |
|
|
|
|
67
|
|
|
* |
|
|
|
|
68
|
|
|
* @return static |
|
|
|
|
69
|
|
|
*/ |
|
|
|
|
70
|
|
|
public static function fromWpTerm(WP_Term $term) |
|
|
|
|
71
|
|
|
{ |
|
|
|
|
72
|
|
|
return new static($term); |
|
|
|
|
73
|
|
|
} |
|
|
|
|
74
|
|
|
|
75
|
|
|
/** |
|
|
|
|
76
|
|
|
* Create a new instance from a term ID. |
|
|
|
|
77
|
|
|
* |
|
|
|
|
78
|
|
|
* @param int|string $id Term ID |
|
|
|
|
79
|
|
|
* |
|
|
|
|
80
|
|
|
* @throws TermNotFoundException |
|
|
|
|
81
|
|
|
* |
|
|
|
|
82
|
|
|
* @return static |
|
|
|
|
83
|
|
|
*/ |
|
|
|
|
84
|
|
|
public static function fromID($id) |
|
|
|
|
85
|
|
|
{ |
|
|
|
|
86
|
|
|
if (! $term = get_term_by('id', (int) $id, static::TAXONOMY)) { |
|
|
|
|
87
|
|
|
throw new TermNotFoundException("No term found with ID $id."); |
|
|
|
|
88
|
|
|
} |
|
|
|
|
89
|
|
|
|
90
|
|
|
return static::fromWpTerm($term); |
|
|
|
|
91
|
|
|
} |
|
|
|
|
92
|
|
|
|
93
|
|
|
/** |
|
|
|
|
94
|
|
|
* Create a new instance from a slug. |
|
|
|
|
95
|
|
|
* |
|
|
|
|
96
|
|
|
* @param string $slug Term slug |
|
|
|
|
97
|
|
|
* |
|
|
|
|
98
|
|
|
* @throws TermNotFoundException |
|
|
|
|
99
|
|
|
* |
|
|
|
|
100
|
|
|
* @return static |
|
|
|
|
101
|
|
|
*/ |
|
|
|
|
102
|
|
|
public static function fromSlug($slug) |
|
|
|
|
103
|
|
|
{ |
|
|
|
|
104
|
|
|
if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) { |
|
|
|
|
105
|
|
|
throw new TermNotFoundException("No term found with slug '$slug'."); |
|
|
|
|
106
|
|
|
} |
|
|
|
|
107
|
|
|
|
108
|
|
|
return static::fromWpTerm($term); |
|
|
|
|
109
|
|
|
} |
|
|
|
|
110
|
|
|
|
111
|
|
|
/** |
|
|
|
|
112
|
|
|
* Create a new instance from an array of attributes. |
|
|
|
|
113
|
|
|
* |
|
|
|
|
114
|
|
|
* @param array $attributes [description] |
|
|
|
|
115
|
|
|
* |
|
|
|
|
116
|
|
|
* @return static |
|
|
|
|
117
|
|
|
*/ |
|
|
|
|
118
|
|
|
public static function fromArray(array $attributes) |
|
|
|
|
119
|
|
|
{ |
|
|
|
|
120
|
|
|
return new static( |
|
|
|
|
121
|
|
|
new WP_Term((object) $attributes) |
|
|
|
|
122
|
|
|
); |
|
|
|
|
123
|
|
|
} |
|
|
|
|
124
|
|
|
|
125
|
|
|
/** |
|
|
|
|
126
|
|
|
* Create a new term, and get the instance for it. |
|
|
|
|
127
|
|
|
* |
|
|
|
|
128
|
|
|
* @param array $attributes Term attributes |
|
|
|
|
129
|
|
|
* |
|
|
|
|
130
|
|
|
* @return static |
|
|
|
|
131
|
|
|
*/ |
|
|
|
|
132
|
|
|
public static function create(array $attributes = []) |
|
|
|
|
133
|
|
|
{ |
|
|
|
|
134
|
|
|
return static::fromArray( |
|
|
|
|
135
|
|
|
Collection::make($attributes) |
|
|
|
|
136
|
|
|
->except([static::ID_PROPERTY, 'term_taxonomy_id']) |
|
|
|
|
137
|
|
|
->put('taxonomy', static::TAXONOMY) |
|
|
|
|
138
|
|
|
->toArray() |
|
|
|
|
139
|
|
|
)->save(); |
|
|
|
|
140
|
|
|
} |
|
|
|
|
141
|
|
|
|
142
|
|
|
/** |
|
|
|
|
143
|
|
|
* Check if this term exists in the database. |
|
|
|
|
144
|
|
|
* |
|
|
|
|
145
|
|
|
* @return boolean |
|
|
|
|
146
|
|
|
*/ |
|
|
|
|
147
|
|
|
public function exists() |
|
|
|
|
148
|
|
|
{ |
|
|
|
|
149
|
|
|
return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY)); |
|
|
|
|
150
|
|
|
} |
|
|
|
|
151
|
|
|
|
152
|
|
|
/** |
|
|
|
|
153
|
|
|
* Check if this term exists in the database as the child of the given parent. |
|
|
|
|
154
|
|
|
* |
|
|
|
|
155
|
|
|
* @param int|string|object $parent integer Parent term ID |
|
|
|
|
156
|
|
|
* string Parent term slug or name |
|
|
|
|
157
|
|
|
* object The parent term object/model. |
|
|
|
|
158
|
|
|
* |
|
|
|
|
159
|
|
|
* @return boolean True if the this term and the parent |
|
|
|
|
160
|
|
|
* exist in the database, and the instance |
|
|
|
|
161
|
|
|
* is a child of the given parent; |
|
|
|
|
162
|
|
|
* otherwise false |
|
|
|
|
163
|
|
|
*/ |
|
|
|
|
164
|
|
|
public function isChildOf($parent) |
|
|
|
|
165
|
|
|
{ |
|
|
|
|
166
|
|
|
if (isset($parent->term_id)) { |
|
|
|
|
167
|
|
|
$parent = $parent->term_id; |
|
|
|
|
168
|
|
|
} |
|
|
|
|
169
|
|
|
|
170
|
|
|
return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent); |
|
|
|
|
171
|
|
|
} |
|
|
|
|
172
|
|
|
|
173
|
|
|
/** |
|
|
|
|
174
|
|
|
* Get the parent term instance. |
|
|
|
|
175
|
|
|
* |
|
|
|
|
176
|
|
|
* @return static |
|
|
|
|
177
|
|
|
*/ |
|
|
|
|
178
|
|
|
public function parent() |
|
|
|
|
179
|
|
|
{ |
|
|
|
|
180
|
|
|
return static::fromID($this->object->parent); |
|
|
|
|
181
|
|
|
} |
|
|
|
|
182
|
|
|
|
183
|
|
|
/** |
|
|
|
|
184
|
|
|
* Get all ancestors of this term as a collection. |
|
|
|
|
185
|
|
|
* |
|
|
|
|
186
|
|
|
* @return Collection |
|
|
|
|
187
|
|
|
*/ |
|
|
|
|
188
|
|
|
public function ancestors() |
|
|
|
|
189
|
|
|
{ |
|
|
|
|
190
|
|
|
return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy')) |
|
|
|
|
191
|
|
|
->map(function ($term_ID) { |
|
|
|
|
192
|
|
|
return static::fromID($term_ID); |
|
|
|
|
193
|
|
|
}); |
|
|
|
|
194
|
|
|
} |
|
|
|
|
195
|
|
|
|
196
|
|
|
/** |
|
|
|
|
197
|
|
|
* Get the Taxonomy model. |
|
|
|
|
198
|
|
|
* |
|
|
|
|
199
|
|
|
* @return Taxonomy|\Silk\Taxonomy\Builder |
|
|
|
|
200
|
|
|
*/ |
|
|
|
|
201
|
|
|
public static function taxonomy() |
|
|
|
|
202
|
|
|
{ |
|
|
|
|
203
|
|
|
return Taxonomy::make(static::TAXONOMY); |
|
|
|
|
204
|
|
|
} |
|
|
|
|
205
|
|
|
|
206
|
|
|
/** |
|
|
|
|
207
|
|
|
* Start a new query for terms of this type. |
|
|
|
|
208
|
|
|
* |
|
|
|
|
209
|
|
|
* @return QueryBuilder |
|
|
|
|
210
|
|
|
*/ |
|
|
|
|
211
|
|
|
public function newQuery() |
|
|
|
|
212
|
|
|
{ |
|
|
|
|
213
|
|
|
return QueryBuilder::make()->setModel($this); |
|
|
|
|
214
|
|
|
} |
|
|
|
|
215
|
|
|
|
216
|
|
|
/** |
|
|
|
|
217
|
|
|
* Get the array of actions and their respective handler classes. |
|
|
|
|
218
|
|
|
* |
|
|
|
|
219
|
|
|
* @return array |
|
|
|
|
220
|
|
|
*/ |
|
|
|
|
221
|
|
|
protected function actionClasses() |
|
|
|
|
222
|
|
|
{ |
|
|
|
|
223
|
|
|
return [ |
|
|
|
|
224
|
|
|
'save' => Action\TermSaver::class, |
|
|
|
|
225
|
|
|
'load' => Action\TermLoader::class, |
|
|
|
|
226
|
|
|
'delete' => Action\TermDeleter::class, |
|
|
|
|
227
|
|
|
]; |
|
|
|
|
228
|
|
|
} |
|
|
|
|
229
|
|
|
} |
|
|
|
|
230
|
|
|
|