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