|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Silk\PostType; |
|
4
|
|
|
|
|
5
|
|
|
use stdClass; |
|
6
|
|
|
use Silk\Type\Type; |
|
7
|
|
|
use Silk\Type\Registerable; |
|
8
|
|
|
use Illuminate\Support\Collection; |
|
9
|
|
|
use Silk\Exception\WP_ErrorException; |
|
10
|
|
|
use Silk\PostType\Exception\NonExistentPostTypeException; |
|
11
|
|
|
|
|
12
|
|
|
class PostType extends Type implements Registerable |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* PostType Constructor |
|
16
|
|
|
* |
|
17
|
|
|
* @param stdClass $object The WordPress post type object |
|
18
|
|
|
* |
|
19
|
|
|
* @throws \InvalidArgumentException |
|
20
|
|
|
*/ |
|
21
|
|
|
public function __construct($object) |
|
22
|
|
|
{ |
|
23
|
|
|
if (! is_object($object) || ! in_array(get_class($object), ['stdClass', 'WP_Post_Type'])) { |
|
24
|
|
|
throw new \InvalidArgumentException(static::class . ' can only be constructed with a Post Type object.'); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
$this->object = $object; |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Create a new instance using the post type slug. |
|
32
|
|
|
* |
|
33
|
|
|
* Loads an existing type, or returns a new builder for registering a new type. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $id The post type identifier |
|
36
|
|
|
* |
|
37
|
|
|
* @return static|Builder If the post type has been registered, a new static instance is returned. |
|
38
|
|
|
* Otherwise a new Builder is created for building a new post type to register. |
|
39
|
|
|
*/ |
|
40
|
|
|
public static function make($id) |
|
41
|
|
|
{ |
|
42
|
|
|
if (static::exists($id)) { |
|
43
|
|
|
return static::load($id); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
return static::build($id); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Create a new instance from an existing type. |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $id The post type identifier |
|
53
|
|
|
* |
|
54
|
|
|
* @return static |
|
55
|
|
|
*/ |
|
56
|
|
|
public static function load($id) |
|
57
|
|
|
{ |
|
58
|
|
|
if (! $object = get_post_type_object($id)) { |
|
59
|
|
|
throw new NonExistentPostTypeException("No post type exists with name '$id'."); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
return new static($object); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Build a new type to be registered. |
|
68
|
|
|
* |
|
69
|
|
|
* @param $id |
|
70
|
|
|
* |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
|
|
static function build($id) |
|
74
|
|
|
{ |
|
75
|
|
|
return new Builder($id); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Get the post type identifier (aka: name/slug). |
|
80
|
|
|
*/ |
|
81
|
|
|
public function id() |
|
82
|
|
|
{ |
|
83
|
|
|
return $this->object->name; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Checks if a post type with this slug has been registered. |
|
88
|
|
|
* |
|
89
|
|
|
* @param string $id The post type identifier |
|
90
|
|
|
* |
|
91
|
|
|
* @return bool |
|
92
|
|
|
*/ |
|
93
|
|
|
public static function exists($id) |
|
94
|
|
|
{ |
|
95
|
|
|
return post_type_exists($id); |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Check for feature support. |
|
100
|
|
|
* |
|
101
|
|
|
* @param string|array $features string - First feature of possible many, |
|
102
|
|
|
* array - Many features to check support for. |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed |
|
105
|
|
|
*/ |
|
106
|
|
|
public function supports($features) |
|
107
|
|
|
{ |
|
108
|
|
|
if (! is_array($features)) { |
|
109
|
|
|
$features = func_get_args(); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
return ! Collection::make($features) |
|
113
|
|
|
->contains(function ($key, $feature) { |
|
114
|
|
|
return ! post_type_supports($this->id(), $feature); |
|
115
|
|
|
}); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* Register support of certain features for an existing post type. |
|
120
|
|
|
* |
|
121
|
|
|
* @param mixed $features string - single feature to add |
|
122
|
|
|
* array - multiple features to add |
|
123
|
|
|
* |
|
124
|
|
|
* @return $this |
|
125
|
|
|
*/ |
|
126
|
|
|
public function addSupportFor($features) |
|
127
|
|
|
{ |
|
128
|
|
|
add_post_type_support($this->id(), is_array($features) ? $features : func_get_args()); |
|
129
|
|
|
|
|
130
|
|
|
return $this; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Un-register support of certain features for an existing post type. |
|
135
|
|
|
* |
|
136
|
|
|
* @param mixed $features string - single feature to remove |
|
137
|
|
|
* array - multiple features to remove |
|
138
|
|
|
* |
|
139
|
|
|
* @return $this |
|
140
|
|
|
*/ |
|
141
|
|
|
public function removeSupportFor($features) |
|
142
|
|
|
{ |
|
143
|
|
|
Collection::make(is_array($features) ? $features : func_get_args()) |
|
144
|
|
|
->each(function ($features) { |
|
145
|
|
|
remove_post_type_support($this->id(), $features); |
|
146
|
|
|
}); |
|
147
|
|
|
|
|
148
|
|
|
return $this; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* Unregister the post type. |
|
153
|
|
|
* |
|
154
|
|
|
* @throws NonExistentPostTypeException |
|
155
|
|
|
* @throws WP_ErrorException |
|
156
|
|
|
* |
|
157
|
|
|
* @return $this |
|
158
|
|
|
*/ |
|
159
|
|
|
public function unregister() |
|
160
|
|
|
{ |
|
161
|
|
|
$id = $this->id(); |
|
162
|
|
|
|
|
163
|
|
|
if (! static::exists($id)) { |
|
164
|
|
|
throw new NonExistentPostTypeException("No post type exists with name '{$id}'."); |
|
165
|
|
|
} |
|
166
|
|
|
|
|
167
|
|
|
if (is_wp_error($error = unregister_post_type($id))) { |
|
168
|
|
|
throw new WP_ErrorException($error); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
return $this; |
|
172
|
|
|
} |
|
173
|
|
|
} |
|
174
|
|
|
|