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