Completed
Pull Request — master (#23)
by Evan
06:37 queued 03:21
created

Model::actionClasses()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 8
rs 9.4285
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\Exception\WP_ErrorException;
11
use Silk\Term\Exception\TermNotFoundException;
12
use Silk\Term\Exception\TaxonomyMismatchException;
13
14
/**
15
 * @property-read WP_Term $term
16
 * @property int    $term_id
17
 * @property string $name
18
 * @property string $slug
19
 * @property string $term_group
20
 * @property int    $term_taxonomy_id
21
 * @property string $taxonomy
22
 * @property string $description
23
 * @property int    $parent
24
 * @property int    $count
25
 */
26
abstract class Model extends BaseModel
27
{
28
    /**
29
     * The term's taxonomy
30
     * @var string
31
     */
32
    const TAXONOMY = '';
33
34
    /**
35
     * The object type in WordPress
36
     * @var string
37
     */
38
    const OBJECT_TYPE = 'term';
39
40
    /**
41
     * The primary ID property on the object
42
     */
43
    const ID_PROPERTY = 'term_id';
44
45
    /**
46
     * Model Constructor.
47
     *
48
     * @param array|WP_Term $term  WP_Term
49
     *
50
     * @throws TaxonomyMismatchException
51
     */
52 View Code Duplication
    public function __construct($term = [])
1 ignored issue
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
53
    {
54
        $attributes = is_array($term) ? $term : [];
55
56
        if (! $term instanceof WP_Term) {
0 ignored issues
show
Bug introduced by
The class WP_Term does not exist. Is this class maybe located in a folder that is not analyzed, or in a newer version of your dependencies than listed in your composer.lock/composer.json?
Loading history...
57
            $term = new WP_Term(new stdClass);
58
            $term->taxonomy = static::TAXONOMY;
59
        } elseif ($term->taxonomy != static::TAXONOMY) {
60
            throw new TaxonomyMismatchException();
61
        }
62
63
        $this->setObject($term);
64
65
        $this->fill($attributes);
66
    }
67
68
    /**
69
     * Create a new instance from a WP_Term object.
70
     * @deprecated - use static::make()
71
     *
72
     * @param  WP_Term $term [description]
73
     *
74
     * @return static
75
     */
76
    public static function fromWpTerm(WP_Term $term)
77
    {
78
        return new static($term);
79
    }
80
81
    /**
82
     * Create a new instance from a term ID.
83
     *
84
     * @param  int|string $id  Term ID
85
     *
86
     * @throws TermNotFoundException
87
     *
88
     * @return static
89
     */
90
    public static function fromID($id)
91
    {
92
        if (! $term = WP_Term::get_instance($id, static::TAXONOMY)) {
93
            throw new TermNotFoundException("No term found with ID $id.");
94
        }
95
96
        return new static($term);
97
    }
98
99
    /**
100
     * Create a new instance from a slug.
101
     *
102
     * @param  string $slug  Term slug
103
     *
104
     * @throws TermNotFoundException
105
     *
106
     * @return static
107
     */
108
    public static function fromSlug($slug)
109
    {
110
        if (! $term = get_term_by('slug', $slug, static::TAXONOMY)) {
111
            throw new TermNotFoundException("No term found with slug '$slug'.");
112
        }
113
114
        return new static($term);
115
    }
116
117
    /**
118
     * Check if this term exists in the database.
119
     *
120
     * @return boolean
121
     */
122
    public function exists()
123
    {
124
        return $this->id && ((bool) term_exists((int) $this->id, static::TAXONOMY));
125
    }
126
127
    /**
128
     * Check if this term exists in the database as the child of the given parent.
129
     *
130
     * @param  int|string|object  $parent  integer Parent term ID
131
     *                                     string  Parent term slug or name
132
     *                                     object  The parent term object/model.
133
     *
134
     * @return boolean                     True if the this term and the parent
135
     *                                     exist in the database, and the instance
136
     *                                     is a child of the given parent;
137
     *                                     otherwise false
138
     */
139
    public function isChildOf($parent)
140
    {
141
        if (isset($parent->term_id)) {
142
            $parent = $parent->term_id;
143
        }
144
145
        return (bool) term_exists((int) $this->id, static::TAXONOMY, $parent);
146
    }
147
148
    /**
149
     * Get the parent term instance.
150
     *
151
     * @return static
152
     */
153
    public function parent()
154
    {
155
        return static::fromID($this->object->parent);
156
    }
157
158
    /**
159
     * Get all ancestors of this term as a collection.
160
     *
161
     * @return Collection
162
     */
163
    public function ancestors()
164
    {
165
        return Collection::make(get_ancestors($this->id, static::TAXONOMY, 'taxonomy'))
166
            ->map([static::class, 'fromID']);
167
    }
168
169
    /**
170
     * Get all children of this term as a collection.
171
     *
172
     * @return Collection
173
     */
174
    public function children()
175
    {
176
        return Collection::make(get_term_children($this->id, static::TAXONOMY))
177
             ->map([static::class, 'fromID']);
178
    }
179
180
    /**
181
     * Get the Taxonomy model.
182
     *
183
     * @return Taxonomy|\Silk\Taxonomy\Builder
184
     */
185
    public static function taxonomy()
186
    {
187
        return Taxonomy::make(static::TAXONOMY);
188
    }
189
190
    /**
191
     * Get the URL for this term.
192
     *
193
     * @return string|bool
194
     */
195
    public function url()
196
    {
197
        $url = get_term_link($this->id, $this->taxonomy);
198
199
        if (is_wp_error($url)) {
200
            throw new WP_ErrorException($url);
201
        }
202
203
        return $url;
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
     * Save the term to the database.
218
     *
219
     * @throws WP_ErrorException
220
     *
221
     * @return $this
222
     */
223
    public function save()
224
    {
225
        if ($this->id) {
226
            $ids = wp_update_term($this->id, $this->taxonomy, $this->object->to_array());
227
        } else {
228
            $ids = wp_insert_term($this->name, $this->taxonomy, $this->object->to_array());
229
        }
230
231
        if (is_wp_error($ids)) {
232
            throw new WP_ErrorException($ids);
233
        }
234
235
        $this->setId($ids['term_id'])->refresh();
236
237
        return $this;
238
    }
239
240
    /**
241
     * Delete the term from the database.
242
     *
243
     * @return $this
244
     */
245
    public function delete()
246
    {
247
        if (wp_delete_term($this->id, $this->taxonomy)) {
248
            $this->setObject(new WP_Term(new stdClass));
249
        }
250
251
        return $this;
252
    }
253
254
    /**
255
     * Reload the term object from the database.
256
     *
257
     * @return $this
258
     */
259
    public function refresh()
260
    {
261
        $this->setObject(WP_Term::get_instance($this->id, $this->taxonomy));
262
263
        return $this;
264
    }
265
}
266