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