Completed
Pull Request — master (#20)
by Evan
06:01 queued 02:57
created

Taxonomy::unregister()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
cc 3
eloc 6
c 3
b 0
f 1
nc 3
nop 0
dl 0
loc 12
rs 9.4285
1
<?php
2
3
namespace Silk\Taxonomy;
4
5
use Silk\Type\Type;
6
use Silk\PostType\PostType;
7
use Silk\Taxonomy\Builder;
8
use Silk\Term\QueryBuilder;
9
use Illuminate\Support\Collection;
10
use Silk\Exception\WP_ErrorException;
11
use Silk\Taxonomy\Exception\InvalidTaxonomyNameException;
12
use Silk\Taxonomy\Exception\NonExistentTaxonomyException;
13
14
/**
15
 * @property-read bool     $_builtin
16
 * @property-read stdClass $cap
17
 * @property-read string   $description
18
 * @property-read bool     $hierarchical
19
 * @property-read string   $label
20
 * @property-read stdClass $labels
21
 * @property-read callable $meta_box_cb
22
 * @property-read string   $name
23
 * @property-read array    $object_type
24
 * @property-read bool     $public
25
 * @property-read bool     $publicly_queryable
26
 * @property-read string   $query_var
27
 * @property-read array    $rewrite
28
 * @property-read bool     $show_admin_column
29
 * @property-read bool     $show_in_menu
30
 * @property-read bool     $show_in_nav_menus
31
 * @property-read bool     $show_in_quick_edit
32
 * @property-read bool     $show_tagcloud
33
 * @property-read bool     $show_ui
34
 * @property-read callable $update_count_callback
35
 */
36
class Taxonomy extends Type
37
{
38
    /**
39
     * Taxonomy Constructor.
40
     *
41
     * @param object $taxonomy The taxonomy object
42
     *
43
     * @throws \Silk\Taxonomy\Exception\NonExistentTaxonomyException
44
     */
45
    public function __construct($taxonomy)
46
    {
47
        if (empty($taxonomy->name) || ! static::exists($taxonomy->name)) {
48
            throw new NonExistentTaxonomyException;
49
        }
50
51
        $this->object = $taxonomy;
52
    }
53
54
    /**
55
     * Create a new instance using the taxonomy identifier.
56
     *
57
     * @param  string $identifier Taxonomy name/identifier
58
     *
59
     * @throws \Silk\Taxonomy\Exception\InvalidTaxonomyNameException
60
     *
61
     * @return static|Builder
62
     */
63
    public static function make($identifier)
64
    {
65
        if (static::exists($identifier)) {
66
            return static::load($identifier);
67
        }
68
69
        if (! $identifier || strlen($identifier) > 32) {
70
            throw new InvalidTaxonomyNameException('Taxonomy names must be between 1 and 32 characters in length.');
71
        }
72
73
        return new Builder($identifier);
74
    }
75
76
    /**
77
     * Create a new instance from an existing taxonomy.
78
     *
79
     * @param  string $identifier  The taxonomy identifier
80
     *
81
     * @throws NonExistentTaxonomyException
82
     *
83
     * @return static
84
     */
85
    public static function load($identifier)
86
    {
87
        if (! $object = get_taxonomy($identifier)) {
88
            throw new NonExistentTaxonomyException("No taxonomy exists with name '$identifier'.");
89
        }
90
91
        return new static($object);
92
    }
93
94
    /**
95
     * Check if the given taxonomy exists.
96
     *
97
     * @param  string $id The taxonomy key/identifier
98
     *
99
     * @return bool
100
     */
101
    public static function exists($id)
102
    {
103
        return taxonomy_exists($id);
104
    }
105
106
    /**
107
     * Start a new query for terms of this taxonomy.
108
     *
109
     * @return QueryBuilder
110
     */
111
    public function terms()
112
    {
113
        return (new QueryBuilder)->forTaxonomy($this->id);
114
    }
115
116
    /**
117
     * Get all post types associated with this taxonomy.
118
     *
119
     * @return Collection
120
     */
121
    public function postTypes()
122
    {
123
        return Collection::make($this->object_type)
124
            ->map(function ($post_type) {
125
                return PostType::load($post_type);
126
            });
127
    }
128
129
    /**
130
     * Unregister the taxonomy.
131
     *
132
     * @throws \Silk\Taxonomy\Exception\NonExistentTaxonomyException
133
     * @throws \Silk\Exception\WP_ErrorException
134
     *
135
     * @return $this
136
     */
137
    public function unregister()
138
    {
139
        if (! $this->exists($this->id)) {
140
            throw new NonExistentTaxonomyException;
141
        }
142
143
        if (is_wp_error($error = unregister_taxonomy($this->id))) {
144
            throw new WP_ErrorException($error);
145
        }
146
147
        return $this;
148
    }
149
}
150