|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk\PostType; |
|
4
|
|
|
|
|
5
|
|
|
use stdClass; |
|
6
|
|
|
use Illuminate\Support\Collection; |
|
7
|
|
|
use Silk\Exception\WP_ErrorException; |
|
8
|
|
|
use Silk\PostType\Exception\NonExistentPostTypeException; |
|
9
|
|
|
use Silk\PostType\Builder; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* @property-read string $id |
|
13
|
|
|
* @property-read string $slug |
|
14
|
|
|
* @property-read string $one |
|
15
|
|
|
* @property-read string $many |
|
16
|
|
|
*/ |
|
17
|
|
|
class PostType |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Post type object |
|
21
|
|
|
* @var stdClass |
|
22
|
|
|
*/ |
|
23
|
|
|
protected $object; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* PostType Constructor |
|
27
|
|
|
* |
|
28
|
|
|
* @param stdClass $object The WordPress post type object |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct(stdClass $object) |
|
31
|
|
|
{ |
|
32
|
|
|
$this->object = $object; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Create a new instance using the post type slug. |
|
37
|
|
|
* |
|
38
|
|
|
* Loads an existing type, or returns a new builder for registering a new type. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $slug The post type slug |
|
41
|
|
|
* |
|
42
|
|
|
* @return static|PostTypeBuilder If the post type has been registered, a new static instance is returned. |
|
43
|
|
|
* Otherwise a new PostTypeBuilder is created for building a new post type to register. |
|
44
|
|
|
*/ |
|
45
|
|
|
public static function make($slug) |
|
46
|
|
|
{ |
|
47
|
|
|
if (static::exists($slug)) { |
|
48
|
|
|
return static::load($slug); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
return new Builder($slug); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Create a new instance from an existing type. |
|
56
|
|
|
* |
|
57
|
|
|
* @param string $slug The post type slug |
|
58
|
|
|
* |
|
59
|
|
|
* @return static |
|
60
|
|
|
*/ |
|
61
|
|
|
public static function load($slug) |
|
62
|
|
|
{ |
|
63
|
|
|
if (! $object = get_post_type_object($slug)) { |
|
64
|
|
|
throw new NonExistentPostTypeException("No post type exists with name '$slug'."); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return new static($object); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Checks if a post type with this slug has been registered. |
|
72
|
|
|
* |
|
73
|
|
|
* @param string $slug The post type slug |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
|
|
public static function exists($slug) |
|
78
|
|
|
{ |
|
79
|
|
|
return post_type_exists($slug); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Get the post type object. |
|
84
|
|
|
* |
|
85
|
|
|
* @return object |
|
86
|
|
|
*/ |
|
87
|
|
|
public function object() |
|
88
|
|
|
{ |
|
89
|
|
|
return $this->object; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Check for feature support. |
|
94
|
|
|
* |
|
95
|
|
|
* @param string,...|array $features string - First feature of possible many, |
|
|
|
|
|
|
96
|
|
|
* array - Many features to check support for. |
|
97
|
|
|
* |
|
98
|
|
|
* @return mixed |
|
99
|
|
|
*/ |
|
100
|
|
|
public function supports($features) |
|
101
|
|
|
{ |
|
102
|
|
|
if (! is_array($features)) { |
|
103
|
|
|
$features = func_get_args(); |
|
|
|
|
|
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
return ! Collection::make($features) |
|
107
|
|
|
->contains(function ($key, $feature) { |
|
108
|
|
|
return ! post_type_supports($this->slug, $feature); |
|
109
|
|
|
}); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* Register support of certain features for an existing post type. |
|
114
|
|
|
* |
|
115
|
|
|
* @param mixed $features string - single feature to add |
|
116
|
|
|
* array - multiple features to add |
|
117
|
|
|
*/ |
|
118
|
|
|
public function addSupportFor($features) |
|
119
|
|
|
{ |
|
120
|
|
|
add_post_type_support($this->slug, is_array($features) ? $features : func_get_args()); |
|
121
|
|
|
|
|
122
|
|
|
return $this; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Deregister support of certain features for an existing post type. |
|
127
|
|
|
* |
|
128
|
|
|
* @param mixed $features string - single feature to remove |
|
129
|
|
|
* array - multiple features to remove |
|
130
|
|
|
*/ |
|
131
|
|
|
public function removeSupportFor($features) |
|
132
|
|
|
{ |
|
133
|
|
|
Collection::make(is_array($features) ? $features : func_get_args()) |
|
134
|
|
|
->each(function ($features) { |
|
135
|
|
|
remove_post_type_support($this->slug, $features); |
|
136
|
|
|
}); |
|
137
|
|
|
|
|
138
|
|
|
return $this; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* Unregister the post type |
|
143
|
|
|
* |
|
144
|
|
|
* @return $this |
|
145
|
|
|
*/ |
|
146
|
|
|
public function unregister() |
|
147
|
|
|
{ |
|
148
|
|
|
if (! static::exists($this->slug)) { |
|
149
|
|
|
throw new NonExistentPostTypeException("No post type exists with name '{$this->slug}'."); |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
if (is_wp_error($error = unregister_post_type($this->slug))) { |
|
153
|
|
|
throw new WP_ErrorException($error); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
|
|
return $this; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* Magic Getter. |
|
161
|
|
|
* |
|
162
|
|
|
* @param string $property Accessed property name |
|
163
|
|
|
* |
|
164
|
|
|
* @return mixed |
|
165
|
|
|
*/ |
|
166
|
|
|
public function __get($property) |
|
167
|
|
|
{ |
|
168
|
|
|
$default = isset($this->object->$property) |
|
169
|
|
|
? $this->object->$property |
|
170
|
|
|
: null; |
|
171
|
|
|
|
|
172
|
|
|
return Collection::make([ |
|
173
|
|
|
'id' => $this->object->name, |
|
174
|
|
|
'slug' => $this->object->name, |
|
175
|
|
|
'one' => $this->object->labels->singular_name, |
|
176
|
|
|
'many' => $this->object->labels->name, |
|
177
|
|
|
])->get($property, $default); |
|
178
|
|
|
} |
|
179
|
|
|
|
|
180
|
|
|
/** |
|
181
|
|
|
* Magic Isset Check. |
|
182
|
|
|
* |
|
183
|
|
|
* @param string $property Queried property name |
|
184
|
|
|
* |
|
185
|
|
|
* @return boolean |
|
186
|
|
|
*/ |
|
187
|
|
|
public function __isset($property) |
|
188
|
|
|
{ |
|
189
|
|
|
return ! is_null($this->__get($property)); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
|
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.