| 1 | <?php |
||
| 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) |
||
|
1 ignored issue
–
show
|
|||
| 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) |
||
| 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) |
||
| 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 = []) |
||
| 141 | |||
| 142 | /** |
||
| 143 | * Check if this term exists in the database. |
||
| 144 | * |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | public function exists() |
||
| 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) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Get the parent term instance. |
||
| 175 | * |
||
| 176 | * @return static |
||
| 177 | */ |
||
| 178 | public function parent() |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Get all ancestors of this term as a collection. |
||
| 185 | * |
||
| 186 | * @return Collection |
||
| 187 | */ |
||
| 188 | public function ancestors() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Get the Taxonomy model. |
||
| 198 | * |
||
| 199 | * @return Taxonomy|\Silk\Taxonomy\Builder |
||
| 200 | */ |
||
| 201 | public static function taxonomy() |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Start a new query for terms of this type. |
||
| 208 | * |
||
| 209 | * @return QueryBuilder |
||
| 210 | */ |
||
| 211 | public function newQuery() |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Get the array of actions and their respective handler classes. |
||
| 218 | * |
||
| 219 | * @return array |
||
| 220 | */ |
||
| 221 | protected function actionClasses() |
||
| 229 | } |
||
| 230 |