-
Notifications
You must be signed in to change notification settings - Fork 7
/
em-posts.php
347 lines (342 loc) · 14.5 KB
/
em-posts.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
<?php
//Custom Post Type and Taxonomy Names, overridable in wp-config.php, not adviseable but the point is flexibility if you know what you're doing ;)
if( !defined('EM_POST_TYPE_EVENT') ) define('EM_POST_TYPE_EVENT','event');
if( !defined('EM_POST_TYPE_LOCATION') ) define('EM_POST_TYPE_LOCATION','location');
if( !defined('EM_TAXONOMY_CATEGORY') ) define('EM_TAXONOMY_CATEGORY','event-categories');
if( !defined('EM_TAXONOMY_TAG') ) define('EM_TAXONOMY_TAG','event-tags');
//Slugs
define('EM_POST_TYPE_EVENT_SLUG',get_option('dbem_cp_events_slug', 'events'));
define('EM_POST_TYPE_LOCATION_SLUG',get_option('dbem_cp_locations_slug', 'locations'));
define('EM_TAXONOMY_CATEGORY_SLUG', get_site_option('dbem_taxonomy_category_slug', 'events/categories'));
define('EM_TAXONOMY_TAG_SLUG', get_option('dbem_taxonomy_tag_slug', 'events/tags'));
if( !get_option('disable_post_thumbnails') && function_exists('add_theme_support') ){
add_theme_support('post-thumbnails'); //need to add this for themes that don't have it.
}
add_action('init','wp_events_plugin_init',1);
function wp_events_plugin_init(){
define('EM_ADMIN_URL',admin_url().'edit.php?post_type='.EM_POST_TYPE_EVENT); //we assume the admin url is absolute with at least one querystring
if( get_option('dbem_tags_enabled', true) ){
register_taxonomy(EM_TAXONOMY_TAG,array(EM_POST_TYPE_EVENT,'event-recurring'),array(
'hierarchical' => false,
'public' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => EM_TAXONOMY_TAG_SLUG,'with_front'=>false),
//'update_count_callback' => '',
//'show_tagcloud' => true,
//'show_in_nav_menus' => true,
'label' => __('Event Tags'),
'singular_label' => __('Event Tag'),
'labels' => array(
'name'=>__('Event Tags','dbem'),
'singular_name'=>__('Event Tag','dbem'),
'search_items'=>__('Search Event Tags','dbem'),
'popular_items'=>__('Popular Event Tags','dbem'),
'all_items'=>__('All Event Tags','dbem'),
'parent_items'=>__('Parent Event Tags','dbem'),
'parent_item_colon'=>__('Parent Event Tag:','dbem'),
'edit_item'=>__('Edit Event Tag','dbem'),
'update_item'=>__('Update Event Tag','dbem'),
'add_new_item'=>__('Add New Event Tag','dbem'),
'new_item_name'=>__('New Event Tag Name','dbem'),
'seperate_items_with_commas'=>__('Seperate event tags with commas','dbem'),
'add_or_remove_items'=>__('Add or remove events','dbem'),
'choose_from_the_most_used'=>__('Choose from most used event tags','dbem'),
),
'capabilities' => array(
'manage_terms' => 'edit_event_categories',
'edit_terms' => 'edit_event_categories',
'delete_terms' => 'delete_event_categories',
'assign_terms' => 'edit_events',
)
));
}
if( get_option('dbem_categories_enabled', true) ){
$supported_array = (EM_MS_GLOBAL && !is_main_site()) ? array():array(EM_POST_TYPE_EVENT,'event-recurring');
register_taxonomy(EM_TAXONOMY_CATEGORY,$supported_array,array(
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'query_var' => true,
'rewrite' => array('slug' => EM_TAXONOMY_CATEGORY_SLUG, 'hierarchical' => true,'with_front'=>false),
//'update_count_callback' => '',
//'show_tagcloud' => true,
//'show_in_nav_menus' => true,
'label' => __('Event Categories','dbem'),
'singular_label' => __('Event Category','dbem'),
'labels' => array(
'name'=>__('Event Categories','dbem'),
'singular_name'=>__('Event Category','dbem'),
'search_items'=>__('Search Event Categories','dbem'),
'popular_items'=>__('Popular Event Categories','dbem'),
'all_items'=>__('All Event Categories','dbem'),
'parent_items'=>__('Parent Event Categories','dbem'),
'parent_item_colon'=>__('Parent Event Category:','dbem'),
'edit_item'=>__('Edit Event Category','dbem'),
'update_item'=>__('Update Event Category','dbem'),
'add_new_item'=>__('Add New Event Category','dbem'),
'new_item_name'=>__('New Event Category Name','dbem'),
'seperate_items_with_commas'=>__('Seperate event categories with commas','dbem'),
'add_or_remove_items'=>__('Add or remove events','dbem'),
'choose_from_the_most_used'=>__('Choose from most used event categories','dbem'),
),
'capabilities' => array(
'manage_terms' => 'edit_event_categories',
'edit_terms' => 'edit_event_categories',
'delete_terms' => 'delete_event_categories',
'assign_terms' => 'edit_events',
)
));
}
register_post_type(EM_POST_TYPE_EVENT, array(
'public' => true,
'hierarchical' => false,
'show_ui' => true,
'show_in_menu' => true,
'show_in_nav_menus'=>true,
'can_export' => true,
'exclude_from_search' => false,
'publicly_queryable' => true,
'rewrite' => array('slug' => EM_POST_TYPE_EVENT_SLUG,'with_front'=>false),
'has_archive' => get_option('dbem_cp_events_has_archive', false) == true && get_option('dbem_events_page') == 0,
'supports' => apply_filters('em_cp_event_supports', array('custom-fields','title','editor','excerpt','comments','thumbnail','author')),
'capability_type' => 'event',
'capabilities' => array(
'publish_posts' => 'publish_events',
'edit_posts' => 'edit_events',
'edit_others_posts' => 'edit_others_events',
'delete_posts' => 'delete_events',
'delete_others_posts' => 'delete_others_events',
'read_private_posts' => 'read_private_events',
'edit_post' => 'edit_event',
'delete_post' => 'delete_event',
'read_post' => 'read_event',
),
'label' => __('Events','dbem'),
'description' => __('Display events on your blog.','dbem'),
'labels' => array (
'name' => __('Events','dbem'),
'singular_name' => __('Event','dbem'),
'menu_name' => __('Events','dbem'),
'add_new' => __('Add Event','dbem'),
'add_new_item' => __('Add New Event','dbem'),
'edit' => __('Edit','dbem'),
'edit_item' => __('Edit Event','dbem'),
'new_item' => __('New Event','dbem'),
'view' => __('View','dbem'),
'view_item' => __('View Event','dbem'),
'search_items' => __('Search Events','dbem'),
'not_found' => __('No Events Found','dbem'),
'not_found_in_trash' => __('No Events Found in Trash','dbem'),
'parent' => __('Parent Event','dbem'),
),
'menu_icon' => plugins_url('events-manager/includes/images/calendar-16.png')
));
if ( get_option('dbem_recurrence_enabled') ){
register_post_type('event-recurring', array(
'public' => apply_filters('em_cp_event_recurring_public', false),
'show_ui' => true,
'show_in_menu' => 'edit.php?post_type='.EM_POST_TYPE_EVENT,
'show_in_nav_menus'=>false,
'publicly_queryable' => apply_filters('em_cp_event_recurring_publicly_queryable', false),
'exclude_from_search' => true,
'has_archive' => false,
'can_export' => true,
'hierarchical' => false,
'supports' => apply_filters('em_cp_event_supports', array('custom-fields','title','editor','excerpt','comments','thumbnail','author')),
'capability_type' => 'recurring_events',
'rewrite' => array('slug' => 'events-recurring','with_front'=>false),
'capabilities' => array(
'publish_posts' => 'publish_recurring_events',
'edit_posts' => 'edit_recurring_events',
'edit_others_posts' => 'edit_others_recurring_events',
'delete_posts' => 'delete_recurring_events',
'delete_others_posts' => 'delete_others_recurring_events',
'read_private_posts' => 'read_private_recurring_events',
'edit_post' => 'edit_recurring_event',
'delete_post' => 'delete_recurring_event',
'read_post' => 'read_recurring_event',
),
'label' => __('Recurring Events','dbem'),
'description' => __('Recurring Events Template','dbem'),
'labels' => array (
'name' => __('Recurring Events','dbem'),
'singular_name' => __('Recurring Event','dbem'),
'menu_name' => __('Recurring Events','dbem'),
'add_new' => __('Add Recurring Event','dbem'),
'add_new_item' => __('Add New Recurring Event','dbem'),
'edit' => __('Edit','dbem'),
'edit_item' => __('Edit Recurring Event','dbem'),
'new_item' => __('New Recurring Event','dbem'),
'view' => __('View','dbem'),
'view_item' => __('Add Recurring Event','dbem'),
'search_items' => __('Search Recurring Events','dbem'),
'not_found' => __('No Recurring Events Found','dbem'),
'not_found_in_trash' => __('No Recurring Events Found in Trash','dbem'),
'parent' => __('Parent Recurring Event','dbem'),
)
));
}
if( get_option('dbem_locations_enabled', true) ){
register_post_type(EM_POST_TYPE_LOCATION, array(
'public' => true,
'hierarchical' => false,
'show_ui' => !(EM_MS_GLOBAL && !is_main_site() && get_site_option('dbem_ms_mainblog_locations')),
'show_in_menu' => 'edit.php?post_type='.EM_POST_TYPE_EVENT,
'show_in_nav_menus'=>true,
'can_export' => true,
'exclude_from_search' => get_option('dbem_cp_locations_search_results') == false,
'publicly_queryable' => true,
'rewrite' => array('slug' => EM_POST_TYPE_LOCATION_SLUG, 'with_front'=>false),
'query_var' => true,
'has_archive' => get_option('dbem_cp_locations_has_archive', false) == true,
'supports' => apply_filters('em_cp_location_supports', array('title','editor','excerpt','custom-fields','comments','thumbnail','author')),
'capability_type' => 'location',
'capabilities' => array(
'publish_posts' => 'publish_locations',
'delete_others_posts' => 'delete_others_locations',
'delete_posts' => 'delete_locations',
'delete_post' => 'delete_location',
'edit_others_posts' => 'edit_others_locations',
'edit_posts' => 'edit_locations',
'edit_post' => 'edit_location',
'read_private_posts' => 'read_private_locations',
'read_post' => 'read_location',
),
'label' => __('Locations','dbem'),
'description' => __('Display locations on your blog.','dbem'),
'labels' => array (
'name' => __('Locations','dbem'),
'singular_name' => __('Location','dbem'),
'menu_name' => __('Locations','dbem'),
'add_new' => __('Add Location','dbem'),
'add_new_item' => __('Add New Location','dbem'),
'edit' => __('Edit','dbem'),
'edit_item' => __('Edit Location','dbem'),
'new_item' => __('New Location','dbem'),
'view' => __('View','dbem'),
'view_item' => __('View Location','dbem'),
'search_items' => __('Search Locations','dbem'),
'not_found' => __('No Locations Found','dbem'),
'not_found_in_trash' => __('No Locations Found in Trash','dbem'),
'parent' => __('Parent Location','dbem'),
)
));
}
}
//Post Customization
function supported_event_custom_fields($supported){
$remove = array();
if( !get_option('dbem_cp_events_custom_fields') ) $remove[] = 'custom-fields';
if( !get_option('dbem_cp_events_comments') ) $remove[] = 'comments';
return supported_custom_fields($supported, $remove);
}
add_filter('em_cp_event_supports', 'supported_event_custom_fields',10,1);
function supported_location_custom_fields($supported){
$remove = array();
if( !get_option('dbem_cp_locations_custom_fields') ) $remove[] = 'custom-fields';
if( !get_option('dbem_cp_locations_comments') ) $remove[] = 'comments';
return supported_custom_fields($supported, $remove);
}
add_filter('em_cp_location_supports', 'supported_location_custom_fields',10,1);
function supported_custom_fields($supported, $remove = array()){
foreach($supported as $key => $support_field){
if( in_array($support_field, $remove) ){
unset($supported[$key]);
}
}
return $supported;
}
function em_map_meta_cap( $caps, $cap, $user_id, $args ) {
/* Handle event reads */
if ( 'edit_event' == $cap || 'delete_event' == $cap || 'read_event' == $cap ) {
$EM_Event = em_get_event($args[0],'post_id');
$post_type = get_post_type_object( $EM_Event->post_type );
/* Set an empty array for the caps. */
$caps = array();
//Filter according to event caps
switch( $cap ){
case 'read_event':
if ( 'private' != $EM_Event->post_status )
$caps[] = 'read';
elseif ( $user_id == $EM_Event->event_owner )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
break;
case 'edit_event':
if ( $user_id == $EM_Event->event_owner )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
break;
case 'delete_event':
if ( $user_id == $EM_Event->event_owner )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
break;
}
}
if ( 'edit_recurring_event' == $cap || 'delete_recurring_event' == $cap || 'read_recurring_event' == $cap ) {
$EM_Event = em_get_event($args[0],'post_id');
$post_type = get_post_type_object( $EM_Event->post_type );
/* Set an empty array for the caps. */
$caps = array();
//Filter according to recurring_event caps
switch( $cap ){
case 'read_recurring_event':
if ( 'private' != $EM_Event->post_status )
$caps[] = 'read';
elseif ( $user_id == $EM_Event->event_owner )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
break;
case 'edit_recurring_event':
if ( $user_id == $EM_Event->event_owner )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
break;
case 'delete_recurring_event':
if ( $user_id == $EM_Event->event_owner )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
break;
}
}
if ( 'edit_location' == $cap || 'delete_location' == $cap || 'read_location' == $cap ) {
$EM_Location = em_get_location($args[0],'post_id');
$post_type = get_post_type_object( $EM_Location->post_type );
/* Set an empty array for the caps. */
$caps = array();
//Filter according to location caps
switch( $cap ){
case 'read_location':
if ( 'private' != $EM_Location->post_status )
$caps[] = 'read';
elseif ( $user_id == $EM_Location->location_owner )
$caps[] = 'read';
else
$caps[] = $post_type->cap->read_private_posts;
break;
case 'edit_location':
if ( $user_id == $EM_Location->location_owner )
$caps[] = $post_type->cap->edit_posts;
else
$caps[] = $post_type->cap->edit_others_posts;
break;
case 'delete_location':
if ( $user_id == $EM_Location->location_owner )
$caps[] = $post_type->cap->delete_posts;
else
$caps[] = $post_type->cap->delete_others_posts;
break;
}
}
/* Return the capabilities required by the user. */
return $caps;
}
add_filter( 'map_meta_cap', 'em_map_meta_cap', 10, 4 );