-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcustom-post-types.php
More file actions
248 lines (214 loc) · 6.24 KB
/
custom-post-types.php
File metadata and controls
248 lines (214 loc) · 6.24 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
<?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);
if (!$objects) { return array('no objects returned'); }
$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;
}
}
?>