1 | <?php |
||
25 | abstract class Model extends ActiveRecord |
||
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 | use QueryBuilder; |
||
45 | |||
46 | /** |
||
47 | * Model Constructor. |
||
48 | * |
||
49 | * @param mixed $term WP_Term to fill data from |
||
50 | * |
||
51 | * @throws TaxonomyMismatchException |
||
52 | */ |
||
53 | public function __construct(WP_Term $term = null) |
||
54 | { |
||
55 | if (! $term) { |
||
56 | $term = new WP_Term(new stdClass); |
||
57 | $term->taxonomy = static::TAXONOMY; |
||
58 | } elseif ($term->taxonomy != static::TAXONOMY) { |
||
59 | throw new TaxonomyMismatchException(); |
||
60 | } |
||
61 | |||
62 | $this->object = $term; |
||
63 | } |
||
64 | |||
65 | /** |
||
66 | * Create a new instance from a WP_Term object. |
||
67 | * |
||
68 | * @param WP_Term $term [description] |
||
69 | * |
||
70 | * @return static |
||
71 | */ |
||
72 | public static function fromWpTerm(WP_Term $term) |
||
73 | { |
||
74 | return new static($term); |
||
75 | } |
||
76 | |||
77 | /** |
||
78 | * Create a new instance from a term ID. |
||
79 | * |
||
80 | * @param int|string $id Term ID |
||
81 | * |
||
82 | * @throws TermNotFoundException |
||
83 | * |
||
84 | * @return static |
||
85 | */ |
||
86 | public static function fromID($id) |
||
94 | |||
95 | /** |
||
96 | * Create a new instance from a slug. |
||
97 | * |
||
98 | * @param string $slug Term slug |
||
99 | * |
||
100 | * @throws TermNotFoundException |
||
101 | * |
||
102 | * @return static |
||
103 | */ |
||
104 | public static function fromSlug($slug) |
||
112 | |||
113 | /** |
||
114 | * Create a new instance from an array of attributes. |
||
115 | * |
||
116 | * @param array $attributes [description] |
||
117 | * |
||
118 | * @return static |
||
119 | */ |
||
120 | public static function fromArray(array $attributes) |
||
126 | |||
127 | /** |
||
128 | * Create a new term, and get the instance for it. |
||
129 | * |
||
130 | * @param array $attributes Term attributes |
||
131 | * |
||
132 | * @return static |
||
133 | */ |
||
134 | public static function create(array $attributes = []) |
||
143 | |||
144 | /** |
||
145 | * Check if this term exists in the database. |
||
146 | * |
||
147 | * @return boolean |
||
148 | */ |
||
149 | public function exists() |
||
153 | |||
154 | /** |
||
155 | * Check if this term exists in the database as the child of the given parent. |
||
156 | * |
||
157 | * @param int|string|object $parent integer Parent term ID |
||
158 | * string Parent term slug or name |
||
159 | * object The parent term object/model. |
||
160 | * |
||
161 | * @return boolean True if the this term and the parent |
||
162 | * exist in the database, and the instance |
||
163 | * is a child of the given parent; |
||
164 | * otherwise false |
||
165 | */ |
||
166 | public function isChildOf($parent) |
||
174 | |||
175 | /** |
||
176 | * Get the parent term instance. |
||
177 | * |
||
178 | * @return static |
||
179 | */ |
||
180 | public function parent() |
||
184 | |||
185 | /** |
||
186 | * Get all ancestors of this term as a collection. |
||
187 | * |
||
188 | * @return Collection |
||
189 | */ |
||
190 | public function ancestors() |
||
197 | |||
198 | /** |
||
199 | * Get the Taxonomy model. |
||
200 | * |
||
201 | * @return Taxonomy |
||
202 | */ |
||
203 | public function taxonomy() |
||
207 | |||
208 | /** |
||
209 | * Start a new query for terms of this type. |
||
210 | * |
||
211 | * @return TermQueryBuilder |
||
212 | */ |
||
213 | public function newQuery() |
||
217 | |||
218 | /** |
||
219 | * Get the array of actions and their respective handler classes. |
||
220 | * |
||
221 | * @return array |
||
222 | */ |
||
223 | protected function actionClasses() |
||
231 | } |
||
232 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.