Completed
Pull Request — master (#23)
by Evan
04:00
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 View Code Duplication
    public static function fromID($id)
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...
91
    {
92
        if (! $term = get_term_by('id', (int) $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 View Code Duplication
    public static function fromSlug($slug)
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...
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(function ($term_ID) {
167
                return static::fromID($term_ID);
168
            });
169
    }
170
171
    /**
172
     * Get the Taxonomy model.
173
     *
174
     * @return Taxonomy|\Silk\Taxonomy\Builder
175
     */
176
    public static function taxonomy()
177
    {
178
        return Taxonomy::make(static::TAXONOMY);
179
    }
180
181
    /**
182
     * Get the URL for this term.
183
     *
184
     * @return string|bool
185
     */
186
    public function url()
187
    {
188
        $url = get_term_link($this->id, $this->taxonomy);
189
190
        if (is_wp_error($url)) {
191
            throw new WP_ErrorException($url);
192
        }
193
194
        return $url;
195
    }
196
197
    /**
198
     * Start a new query for terms of this type.
199
     *
200
     * @return QueryBuilder
201
     */
202
    public function newQuery()
203
    {
204
        return QueryBuilder::make()->setModel($this);
205
    }
206
207
    /**
208
     * Save the term to the database.
209
     *
210
     * @throws WP_ErrorException
211
     *
212
     * @return $this
213
     */
214
    public function save()
215
    {
216
        if ($this->id) {
217
            $ids = wp_update_term($this->id, $this->taxonomy, $this->object->to_array());
218
        } else {
219
            $ids = wp_insert_term($this->name, $this->taxonomy, $this->object->to_array());
220
        }
221
222
        if (is_wp_error($ids)) {
223
            throw new WP_ErrorException($ids);
224
        }
225
226
        $this->setId($ids['term_id'])->refresh();
227
228
        return $this;
229
    }
230
231
    /**
232
     * Delete the term from the database.
233
     *
234
     * @return $this
235
     */
236
    public function delete()
237
    {
238
        if (wp_delete_term($this->id, $this->taxonomy)) {
239
            $this->setObject(new WP_Term(new stdClass));
240
        }
241
242
        return $this;
243
    }
244
245
    /**
246
     * Reload the term object from the database.
247
     *
248
     * @return $this
249
     */
250
    public function refresh()
251
    {
252
        $this->setObject(WP_Term::get_instance($this->id, $this->taxonomy));
253
254
        return $this;
255
    }
256
}
257