-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-post-types.php
392 lines (352 loc) · 10.5 KB
/
custom-post-types.php
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
<?php
/**
* Abstract class for defining custom post types.
*
**/
abstract class CustomPostType{
public
$name = 'custom_post_type',
$plural_name = 'Custom Posts',
$singular_name = 'Custom Post',
$add_new_item = 'Add New Custom Post',
$edit_item = 'Edit Custom Post',
$new_item = 'New Custom Post',
$public = True, # I dunno...leave it true
$use_title = True, # Title field
$use_editor = True, # WYSIWYG editor, post content field
$use_revisions = True, # Revisions on post content and titles
$use_thumbnails = False, # Featured images
$use_order = False, # Wordpress built-in order meta data
$use_metabox = False, # Enable if you have custom fields to display in admin
$use_shortcode = False, # Auto generate a shortcode for the post type
# (see also objectsToHTML and toHTML methods)
$taxonomies = array('post_tag'),
$built_in = False,
# Optional default ordering for generic shortcode if not specified by user.
$default_orderby = null,
$default_order = null;
/**
* Wrapper for get_posts function, that predefines post_type for this
* custom post type. Any options valid in get_posts can be passed as an
* option array. Returns an array of objects.
**/
public function get_objects($options=array()){
$defaults = array(
'numberposts' => -1,
'orderby' => 'title',
'order' => 'ASC',
'post_type' => $this->options('name'),
);
$options = array_merge($defaults, $options);
$objects = get_posts($options);
return $objects;
}
/**
* Similar to get_objects, but returns array of key values mapping post
* title to id if available, otherwise it defaults to id=>id.
**/
public function get_objects_as_options($options=array()){
$objects = $this->get_objects($options);
$opt = array();
foreach($objects as $o){
switch(True){
case $this->options('use_title'):
$opt[$o->post_title] = $o->ID;
break;
default:
$opt[$o->ID] = $o->ID;
break;
}
}
return $opt;
}
/**
* Return the instances values defined by $key.
**/
public function options($key){
$vars = get_object_vars($this);
return $vars[$key];
}
/**
* Additional fields on a custom post type may be defined by overriding this
* method on an descendant object.
**/
public function fields(){
return array();
}
/**
* Using instance variables defined, returns an array defining what this
* custom post type supports.
**/
public function supports(){
#Default support array
$supports = array();
if ($this->options('use_title')){
$supports[] = 'title';
}
if ($this->options('use_order')){
$supports[] = 'page-attributes';
}
if ($this->options('use_thumbnails')){
$supports[] = 'thumbnail';
}
if ($this->options('use_editor')){
$supports[] = 'editor';
}
if ($this->options('use_revisions')){
$supports[] = 'revisions';
}
return $supports;
}
/**
* Creates labels array, defining names for admin panel.
**/
public function labels(){
return array(
'name' => __($this->options('plural_name')),
'singular_name' => __($this->options('singular_name')),
'add_new_item' => __($this->options('add_new_item')),
'edit_item' => __($this->options('edit_item')),
'new_item' => __($this->options('new_item')),
);
}
/**
* Creates metabox array for custom post type. Override method in
* descendants to add or modify metaboxes.
**/
public function metabox(){
if ($this->options('use_metabox')){
return array(
'id' => $this->options('name').'_metabox',
'title' => __($this->options('singular_name').' Fields'),
'page' => $this->options('name'),
'context' => 'normal',
'priority' => 'high',
'fields' => $this->fields(),
);
}
return null;
}
/**
* Registers metaboxes defined for custom post type.
**/
public function register_metaboxes(){
if ($this->options('use_metabox')){
$metabox = $this->metabox();
add_meta_box(
$metabox['id'],
$metabox['title'],
'show_meta_boxes',
$metabox['page'],
$metabox['context'],
$metabox['priority']
);
}
}
/**
* Registers the custom post type and any other ancillary actions that are
* required for the post to function properly.
**/
public function register(){
$registration = array(
'labels' => $this->labels(),
'supports' => $this->supports(),
'public' => $this->options('public'),
'taxonomies' => $this->options('taxonomies'),
'_builtin' => $this->options('built_in')
);
if ($this->options('use_order')){
$registration = array_merge($registration, array('hierarchical' => True,));
}
register_post_type($this->options('name'), $registration);
if ($this->options('use_shortcode')){
add_shortcode($this->options('name').'-list', array($this, 'shortcode'));
}
}
/**
* Shortcode for this custom post type. Can be overridden for descendants.
* Defaults to just outputting a list of objects outputted as defined by
* toHTML method.
**/
public function shortcode($attr){
$default = array(
'type' => $this->options('name'),
);
if (is_array($attr)){
$attr = array_merge($default, $attr);
}else{
$attr = $default;
}
return sc_object_list($attr);
}
/**
* Handles output for a list of objects, can be overridden for descendants.
* If you want to override how a list of objects are outputted, override
* this, if you just want to override how a single object is outputted, see
* the toHTML method.
**/
public function objectsToHTML($objects, $css_classes){
if (count($objects) < 1){ return '';}
$class = get_custom_post_type($objects[0]->post_type);
$class = new $class;
ob_start();
?>
<ul class="<?php if($css_classes):?><?=$css_classes?><?php else:?><?=$class->options('name')?>-list<?php endif;?>">
<?php foreach($objects as $o):?>
<li>
<?=$class->toHTML($o)?>
</li>
<?php endforeach;?>
</ul>
<?php
$html = ob_get_clean();
return $html;
}
/**
* Outputs this item in HTML. Can be overridden for descendants.
**/
public function toHTML($object){
$html = '<a href="'.get_permalink($object->ID).'">'.$object->post_title.'</a>';
return $html;
}
}
class Page extends CustomPostType {
public
$name = 'page',
$plural_name = 'Pages',
$singular_name = 'Page',
$add_new_item = 'Add New Page',
$edit_item = 'Edit Page',
$new_item = 'New Page',
$public = True,
$use_editor = True,
$use_thumbnails = False,
$use_order = True,
$use_title = True,
$use_metabox = True,
$built_in = True;
public function fields() {
$prefix = $this->options('name').'_';
return array(
array(
'name' => 'Featured image "Desktop" size override',
'desc' => 'Overrides the automatically generated "Desktop" size of the featured image for this page. Recommended image size: 1199x750px.',
'id' => $prefix.'image_d',
'type' => 'file',
),
array(
'name' => 'Featured image "Tablet" size override',
'desc' => 'Overrides the automatically generated "Tablet" size of the featured image for this page. Recommended image size: 767x775px.',
'id' => $prefix.'image_t',
'type' => 'file',
),
array(
'name' => 'Featured image "Mobile" size override',
'desc' => 'Overrides the automatically generated "Mobile" size of the featured image for this page. Recommended image size: 480x475px.',
'id' => $prefix.'image_m',
'type' => 'file',
),
);
}
}
class Post extends CustomPostType {
public
$name = 'post',
$plural_name = 'Posts',
$singular_name = 'Post',
$add_new_item = 'Add New Post',
$edit_item = 'Edit Post',
$new_item = 'New Post',
$public = True,
$use_editor = True,
$use_thumbnails = False,
$use_order = True,
$use_title = True,
$use_metabox = True,
$taxonomies = array('post_tag', 'category'),
$built_in = True;
public function fields() {
$prefix = $this->options('name').'_';
return array();
}
}
class ParallaxFeature extends CustomPostType {
public
$name = 'parallax_feature',
$plural_name = 'Parallax Features',
$singular_name = 'Parallax Feature',
$add_new_item = 'Add New Parallax Feature',
$edit_item = 'Edit Parallax Feature',
$new_item = 'New Parallax Feature',
$public = True,
$use_editor = True,
$use_thumbnails = True,
$use_order = True,
$use_title = True,
$use_metabox = True,
$taxonomies = array();
/**
* Grab array of pages for CTA link options
**/
public function get_page_options() {
$pages = get_posts(array('posts_per_page' => -1, 'post_type' => 'page'));
$pages_array = array();
foreach ($pages as $page) {
$pages_array[$page->post_title] = $page->ID;
}
return $pages_array;
}
public function fields() {
$prefix = $this->options('name').'_';
return array(
array(
'name' => 'Callout box position',
'desc' => 'Position of the yellow box inside the parallax feature.',
'id' => $prefix.'callout_position',
'type' => 'radio',
'options' => array(
'Left' => 'left',
'Right' => 'right'
),
),
array(
'name' => 'Featured image "Desktop" size override',
'desc' => 'Overrides the automatically generated "Desktop" size of the featured image for this parallax feature. Recommended image size: 1199x925px.',
'id' => $prefix.'image_d',
'type' => 'file',
),
array(
'name' => 'Featured image "Tablet" size override',
'desc' => 'Overrides the automatically generated "Tablet" size of the featured image for this parallax feature. Recommended image size: 767x775px.',
'id' => $prefix.'image_t',
'type' => 'file',
),
array(
'name' => 'Featured image "Mobile" size override',
'desc' => 'Overrides the automatically generated "Mobile" size of the featured image for this parallax feature. Recommended image size: 480x475px.',
'id' => $prefix.'image_m',
'type' => 'file',
),
array(
'name' => 'Display CTA',
'desc' => 'Display a call to action button in the bottom-center edge of the feature.',
'id' => $prefix.'display_cta',
'type' => 'checkbox'
),
array(
'name' => 'CTA Text',
'desc' => 'Text within the call to action button.',
'id' => $prefix.'cta_text',
'type' => 'text'
),
array(
'name' => 'CTA Link to Page',
'desc' => 'Page that the call to action button links to.',
'id' => $prefix.'cta_link',
'type' => 'select',
'options' => $this->get_page_options(),
),
);
}
}
?>