-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPostType.php
More file actions
315 lines (268 loc) · 7.4 KB
/
PostType.php
File metadata and controls
315 lines (268 loc) · 7.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
namespace Vanilla;
use Vanilla\Query\Builder;
abstract class PostType {
/**
* @var \WP_Post
*/
protected $wp_post;
/**
* @var array
*/
protected static $instances = [];
/**
* Post Type name
*
* @var string
*/
protected $name;
/**
* You can specify `singular`, `plural` and `slug` for a post type in $names array
* The default values are generated from the post type name.
*
* @var array overrides
*/
protected $names = [];
/**
* Define if the post type have a single page or archive page.
* If $hasPages = false, $templates and $archiveTemplate properties are ignored
*
* @see https://codex.wordpress.org/Function_Reference/register_post_type#publicly_queryable
*
* @var bool
*/
protected $hasPublicPages = true;
/**
* Define if the post type has archive page
* If $hasArchivePage = false, $archiveTemplate property is ignored
* Change in this property requires `wp flush-rewrites` call for the changes to be applied
*
* @see https://codex.wordpress.org/Function_Reference/register_post_type#has_archive
*
* @var bool
*/
protected $hasArchivePage = true;
/**
* Archive page object used to access meta data and custom fields
*
* @var
*/
protected $archivePage;
/**
* Define a template for index page
*
* @var string
*/
protected $archiveTemplate = 'default';
/**
* List of available templates for a PostType
*
* @var array
*/
protected $templates = [];
/**
* List of custom fields
*
* @example ['sub_heading' => ['label' => 'Sub Heading', 'type' => 'text']]
*
* @var array
*/
protected $fields = [];
/**
* Post Type settings. For available options
*
* @see https://github.com/johnbillion/extended-cpts register_extended_post_type()
* @see https://codex.wordpress.org/Function_Reference/register_post_type register_post_type()
*
* @return void|array
*/
abstract protected function args();
public function __construct(\WP_Post $wp_post = null)
{
$this->wp_post = $wp_post ?: get_post();
}
public static function find($id)
{
return new static(get_post($id));
}
public function field($name)
{
return get_field($name, $this->id);
}
/**
* Post id
*
* @return null|int
*/
public function id()
{
return object_get($this->wp_post, 'ID');
}
/**
* @see the_title();
*
* @param string $before
* @param string $after
*
* @return string
*/
public function title()
{
return object_get($this->wp_post, 'post_title');
}
/**
* @param null $more
* @param bool $strip
*
* @return string
*/
public function content($more = null, $strip = false)
{
ob_start();
the_content($more, $strip);
return ob_get_clean();
}
public function slug()
{
return isset($this->names['slug']) ? $this->names['slug'] : str_slug(str_plural($this->name()));
}
/**
* Register post type
*/
public function register()
{
$existing = get_post_type_object($this->name());
if (!$existing || !$existing->_builtin) {
register_extended_post_type($this->name(), $this->arguments(), $this->names);
} else {
extend_post_type($this->name(), $this->arguments(), $this->names);
add_rewrite_rule("{$this->slug()}/([^/]+)/?$", "index.php?&post_type={$this->name()}&name=\$matches[1]", 'top');
add_rewrite_rule("{$this->slug()}/?$", "index.php?&post_type={$this->name()}", 'top');
}
$this->registerTemplates();
$this->registerCustomFields();
return $this;
}
protected function registerTemplates()
{
if (count($this->templates) === 0) {
return null;
}
$themeTemplates = app()->getRegisteredTemplates();
$existing = isset($themeTemplates[$this->name]) ? $themeTemplates[$this->name] : [];
$postTemplates = $this->templates;
unset($postTemplates['Default']);
$themeTemplates[$this->name] = array_merge($existing, array_flip($postTemplates));
wp_cache_set('post_templates-' . app()->cacheHash(), $themeTemplates, 'themes', 1800);
}
protected function registerCustomFields()
{
if(!count($this->fields)) {
return;
}
$fieldCreator = app()->fields()->forPostType($this);
foreach ($this->fields as $key => $args) {
$fieldCreator->add($key, $args);
}
$fieldCreator->create();
}
/**
* @return string
*/
public function name()
{
return $this->name ? : class_basename($this);
}
/**
* @return string
*/
public function singularName()
{
return isset($this->names['singular']) ? $this->names['singular'] : ucfirst($this->name());
}
/**
* Arguments for register_extended_post_type function
*
* @return array
*/
public function arguments()
{
return array_merge([
'exclude_from_search' => $this->isExcludedFromSearch(),
'className' => static::class,
'defaultTemplate' => isset($this->templates['Default']) ? $this->templates['Default'] : null,
'publicly_queryable' => !! $this->hasPublicPages,
'has_archive' => !! $this->hasArchivePage,
'archiveTemplate' => $this->archiveTemplate,
], $this->args() ?: []);
}
protected function isExcludedFromSearch()
{
$blackList = app()->config('search.excluded_types', []);
return in_array($this->name, $blackList) || in_array(get_class($this), $blackList);
}
public function hasTaxonomy($class)
{
return collect(wp_get_post_terms($this->id, (new $class)->name()));
}
public function archive()
{
if(!$this->archivePage) {
$this->archivePage = app()->query()->type('page')->slug($this->slug())->take(1)->get()->pop();
}
return $this->archivePage;
}
/**
* @return Builder
*/
protected function newQuery()
{
return (new Builder())->type($this->name());
}
/**
* Create a query builder for a post type
*
* @return Builder
*/
public static function query()
{
return (new static)->newQuery();
}
/**
* Get all posts of a given post type
*
* @return \Generator
*/
public static function all()
{
return static::query()->get();
}
/**
* If there is no custom parameter on a PostType
* Object, user might want to access parameter from WP_Post object
*
* @param $name
*
* @return mixed
*/
public function __get($name)
{
if(method_exists($this, $name)) {
return $this->$name();
}
return object_get($this->wp_post, $name);
}
/**
* This allows calling Query Builder methods statically on
* PostType class. Post::all(), Page::paginate() etc.
*
* @param $name
* @param $arguments
*
* @return mixed
*/
public static function __callStatic($name, $arguments)
{
return call_user_func_array([static::query(), $name], $arguments);
}
}