|
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 |
|
62
|
|
|
*/ |
|
63
|
|
|
public static function make($identifier) |
|
64
|
|
|
{ |
|
65
|
|
|
if (static::exists($identifier)) { |
|
66
|
|
|
return new static(get_taxonomy($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
|
|
|
* Check if the given taxonomy exists. |
|
78
|
|
|
* |
|
79
|
|
|
* @param string $id The taxonomy key/identifier |
|
80
|
|
|
* |
|
81
|
|
|
* @return bool |
|
82
|
|
|
*/ |
|
83
|
|
|
public static function exists($id) |
|
84
|
|
|
{ |
|
85
|
|
|
return taxonomy_exists($id); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Start a new query for terms of this taxonomy. |
|
90
|
|
|
* |
|
91
|
|
|
* @return QueryBuilder |
|
92
|
|
|
*/ |
|
93
|
|
|
public function terms() |
|
94
|
|
|
{ |
|
95
|
|
|
return (new QueryBuilder)->forTaxonomy($this->id); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Get all post types associated with this taxonomy. |
|
100
|
|
|
* |
|
101
|
|
|
* @return Collection |
|
102
|
|
|
*/ |
|
103
|
|
|
public function postTypes() |
|
104
|
|
|
{ |
|
105
|
|
|
return Collection::make($this->object_type) |
|
106
|
|
|
->map(function ($post_type) { |
|
107
|
|
|
return PostType::load($post_type); |
|
108
|
|
|
}); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Unregister the taxonomy. |
|
113
|
|
|
* |
|
114
|
|
|
* @throws \Silk\Taxonomy\Exception\NonExistentTaxonomyException |
|
115
|
|
|
* @throws \Silk\Exception\WP_ErrorException |
|
116
|
|
|
* |
|
117
|
|
|
* @return $this |
|
118
|
|
|
*/ |
|
119
|
|
|
public function unregister() |
|
120
|
|
|
{ |
|
121
|
|
|
if (! $this->exists($this->id)) { |
|
122
|
|
|
throw new NonExistentTaxonomyException; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
if (is_wp_error($error = unregister_taxonomy($this->id))) { |
|
126
|
|
|
throw new WP_ErrorException($error); |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
return $this; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.