bp-em-core.php
12.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
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
<?php
//Main loader for buddypress
/**
* Events Manager component for BuddyPress
* @author marcus
* @since 5.0
*/
class BP_EM_Component extends BP_Component {
function __construct() {
global $bp;
parent::start('events', __('Events', 'events-manager'), EM_DIR);
$this->includes();
//TODO make BP component optional
$bp->active_components[$this->id] = '1';
}
function includes( $includes = array() ) {
// Files to include
$includes = array(
'buddypress/bp-em-activity.php',
'buddypress/bp-em-templatetags.php',
'buddypress/bp-em-notifications.php',
'buddypress/screens/profile.php',
'buddypress/screens/my-events.php',
'buddypress/screens/my-locations.php',
'buddypress/screens/attending.php',
'buddypress/screens/my-bookings.php',
'buddypress/screens/my-group-events.php'
);
if( bp_is_active('groups') ){
$includes[] = 'buddypress/screens/group-events.php';
$includes[] = 'buddypress/bp-em-groups.php';
}
parent::includes( $includes );
//TODO add admin pages for extra BP specific settings
}
/**
* Sets up the global Events Manager BuddyPress Components
*/
function setup_globals( $args = array() ) {
global $bp, $wpdb;
// Define a slug constant that will be used to view this components pages
if ( !defined( 'BP_EM_SLUG' ) )
define ( 'BP_EM_SLUG', str_replace('/','-', EM_POST_TYPE_EVENT_SLUG) );
// Set up the $globals array to be passed along to parent::setup_globals()
$globals = array(
'slug' => BP_EM_SLUG,
'has_directory' => false, //already done by EM
'notification_callback' => 'bp_em_format_notifications',
'search_string' => sprintf(__( 'Search %s...', 'events-manager'),__('Events','events-manager')),
);
// Let BP_Component::setup_globals() do its work.
parent::setup_globals( $globals );
//quick link shortcut - may need to revisit this
$bp->{$this->id}->link = trailingslashit($bp->loggedin_user->domain).BP_EM_SLUG.'/';
}
public function setup_nav( $main_nav = array(), $sub_nav = array() ) {
global $blog_id;
//check multisite or normal mode for correct permission checking
if(is_multisite() && $blog_id != BP_ROOT_BLOG){
//FIXME MS mode doesn't seem to recognize cross subsite caps, using the proper functions, for now we use switch_blog.
switch_to_blog(BP_ROOT_BLOG);
$can_manage_events = current_user_can_for_blog(BP_ROOT_BLOG, 'edit_events');
$can_manage_locations = current_user_can_for_blog(BP_ROOT_BLOG, 'edit_locations');
$can_manage_bookings = current_user_can_for_blog(BP_ROOT_BLOG, 'manage_bookings');
restore_current_blog();
}else{
$can_manage_events = current_user_can('edit_events');
$can_manage_locations = current_user_can('edit_locations');
$can_manage_bookings = current_user_can('manage_bookings');
}
/* Add 'Events' to the main user profile navigation */
$event_count = EM_Events::count( array( 'scope'=>'future', 'owner'=> bp_displayed_user_id() ));
if( empty($event_count) ) $event_count = 0;
$event_count_span = $event_count > 0 ? ' <span class="count">'.esc_html($event_count).'</span>':'';
$main_nav = array(
'name' => __( 'Events', 'events-manager'). $event_count_span,
'slug' => em_bp_get_slug(),
'position' => 80,
'screen_function' => 'bp_em_events',
'default_subnav_slug' => 'profile'
);
$em_link = trailingslashit( bp_displayed_user_domain() . em_bp_get_slug() );
/* Create SubNav Items */
$sub_nav[] = array(
'name' => __( 'My Profile', 'events-manager'),
'slug' => 'profile',
'parent_slug' => em_bp_get_slug(),
'parent_url' => $em_link,
'screen_function' => 'bp_em_events',
'position' => 10
);
if( get_option('dbem_rsvp_enabled') ) { // Only if bookings enabled
$sub_nav[] = array(
'name' => __( 'Events I\'m Attending', 'events-manager'),
'slug' => 'attending',
'parent_slug' => em_bp_get_slug(),
'parent_url' => $em_link,
'screen_function' => 'bp_em_attending',
'position' => 20,
'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
);
}
if( $can_manage_events ){
$sub_nav[] = array(
'name' => __( 'My Events', 'events-manager'),
'slug' => 'my-events',
'parent_slug' => em_bp_get_slug(),
'parent_url' => $em_link,
'screen_function' => 'bp_em_my_events',
'position' => 30,
'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
);
}
if( $can_manage_locations && get_option('dbem_locations_enabled') ){
$sub_nav[] = array(
'name' => __( 'My Locations', 'events-manager'),
'slug' => 'my-locations',
'parent_slug' => em_bp_get_slug(),
'parent_url' => $em_link,
'screen_function' => 'bp_em_my_locations',
'position' => 40,
'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
);
}
if( $can_manage_bookings && get_option('dbem_rsvp_enabled') ){
$sub_nav[] = array(
'name' => __( 'My Event Bookings', 'events-manager'),
'slug' => 'my-bookings',
'parent_slug' => em_bp_get_slug(),
'parent_url' => $em_link,
'screen_function' => 'bp_em_my_bookings',
'position' => 50,
'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
);
}
if( bp_is_active('groups') ){
/* Create Profile Group Sub-Nav */
$sub_nav[] = array(
'name' => __( 'Events', 'events-manager'),
'slug' => 'group-events',
'parent_slug' => bp_get_groups_slug(),
'parent_url' =>trailingslashit( bp_displayed_user_domain() . bp_get_groups_slug() ),
'screen_function' => 'bp_em_my_group_events',
'position' => 60,
'user_has_access' => bp_is_my_profile() // Only the logged in user can access this on his/her profile
);
}
$main_nav = apply_filters('em_bp_menu_main_nav', $main_nav);
$sub_nav = apply_filters('em_bp_menu_sub_nav', $sub_nav);
parent::setup_nav( $main_nav, $sub_nav );
add_action( 'bp_init', array(&$this, 'setup_group_nav') );
}
public function setup_admin_bar( $wp_admin_nav = array() ) {
global $bp, $blog_id;
// Prevent debug notices
$wp_admin_nav = array();
// Menus for logged in user
if ( is_user_logged_in() ) {
//check multisite or normal mode for correct permission checking
if(is_multisite() && $blog_id != BP_ROOT_BLOG){
//FIXME MS mode doesn't seem to recognize cross subsite caps, using the proper functions, for now we use switch_blog.
$current_blog = $blog_id;
switch_to_blog(BP_ROOT_BLOG);
$can_manage_events = current_user_can_for_blog(BP_ROOT_BLOG, 'edit_events');
$can_manage_locations = current_user_can_for_blog(BP_ROOT_BLOG, 'edit_locations');
$can_manage_bookings = current_user_can_for_blog(BP_ROOT_BLOG, 'manage_bookings');
restore_current_blog();
}else{
$can_manage_events = current_user_can('edit_events');
$can_manage_locations = current_user_can('edit_locations');
$can_manage_bookings = current_user_can('manage_bookings');
}
$em_link = trailingslashit( bp_loggedin_user_domain() . em_bp_get_slug() );
/* Add 'Events' to the main user profile navigation */
$wp_admin_nav[] = array(
'parent' => $bp->my_account_menu_id,
'id' => 'my-em-' . $this->id,
'title' => __( 'Events', 'events-manager'),
'href' => $em_link
);
/* Create SubNav Items */
$wp_admin_nav[] = array(
'parent' => 'my-em-' . $this->id,
'id' => 'my-em-' . $this->id .'-profile',
'title' => __( 'My Profile', 'events-manager'),
'href' => $em_link.'profile/'
);
$wp_admin_nav[] = array(
'parent' => 'my-em-' . $this->id,
'id' => 'my-em-' . $this->id .'-attending',
'title' => __( 'Events I\'m Attending', 'events-manager'),
'href' => $em_link.'attending/'
);
if( $can_manage_events ){
$wp_admin_nav[] = array(
'parent' => 'my-em-' . $this->id,
'id' => 'my-em-' . $this->id .'-my-events',
'title' => __( 'My Events', 'events-manager'),
'href' => $em_link.'my-events/'
);
}
if( $can_manage_locations && get_option('dbem_locations_enabled') ){
$wp_admin_nav[] = array(
'parent' => 'my-em-' . $this->id,
'id' => 'my-em-' . $this->id .'-my-locations',
'title' => __( 'My Locations', 'events-manager'),
'href' => $em_link.'my-locations/'
);
}
if( $can_manage_bookings && get_option('dbem_rsvp_enabled') ){
$wp_admin_nav[] = array(
'parent' => 'my-em-' . $this->id,
'id' => 'my-em-' . $this->id .'-my-bookings',
'title' => __( 'My Event Bookings', 'events-manager'),
'href' => $em_link.'my-bookings/'
);
}
if( bp_is_active('groups') ){
/* Create Profile Group Sub-Nav */
$wp_admin_nav[] = array(
'parent' => 'my-account-groups',
'id' => 'my-account-groups-' . $this->id ,
'title' => __( 'Events', 'events-manager'),
'href' => trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() ) . 'group-events/'
);
}
}
parent::setup_admin_bar( $wp_admin_nav );
}
function setup_group_nav(){
global $bp;
/* Add some group subnav items */
$user_access = false;
$group_link = '';
if( bp_is_active('groups') && !empty($bp->groups->current_group) ){
$group_link = $bp->root_domain . '/' . bp_get_groups_root_slug() . '/' . $bp->groups->current_group->slug . '/';
$user_access = $bp->groups->current_group->user_has_access;
if( !empty($bp->current_component) && $bp->current_component == 'groups' ){
$count = EM_Events::count(array('group'=>$bp->groups->current_group->id));
if( empty($count) ) $count = 0;
}
bp_core_new_subnav_item( array(
'name' => __( 'Events', 'events-manager') . " <span>$count</span>",
'slug' => 'events',
'parent_url' => $group_link,
'parent_slug' => $bp->groups->current_group->slug,
'screen_function' => 'bp_em_group_events',
'position' => 50,
'user_has_access' => $user_access,
'item_css_id' => 'events'
));
}
}
}
function bp_em_load_core_component() {
global $bp;
$bp->events = new BP_EM_Component();
}
add_action( 'bp_loaded', 'bp_em_load_core_component' );
if( !is_admin() || ( defined('DOING_AJAX') && !empty($_REQUEST['is_public'])) ){
/*
* Links and URL Rewriting
*/
function em_bp_rewrite_edit_url($url, $EM_Event){
global $bp;
return $bp->events->link.'my-events/?action=edit&event_id='.$EM_Event->event_id;
}
function em_bp_rewrite_events_admin_url( $url ){
global $bp;
return $bp->events->link.'my-events/';
}
if( !get_option('dbem_edit_events_page') ){
add_filter('em_event_get_edit_url','em_bp_rewrite_edit_url',10,2);
add_filter('em_get_events_admin_url','em_bp_rewrite_edit_url',10,2);
}
function em_bp_rewrite_bookings_url($url, $EM_Event){
global $bp;
return $bp->events->link.'my-bookings/?event_id='.$EM_Event->event_id;
}
if( !get_option('dbem_edit_bookings_page') ){
add_filter('em_event_get_bookings_url','em_bp_rewrite_bookings_url',10,2);
}
function em_bp_rewrite_edit_location_url($url, $EM_Location){
global $bp;
return $bp->events->link.'my-locations/?action=edit&location_id='.$EM_Location->location_id;
}
if( !get_option('dbem_edit_locations_page') ){
add_filter('em_location_get_edit_url','em_bp_rewrite_edit_location_url',10,2);
}
}
//CSS and JS Loading
function bp_em_enqueue_scripts( ){
if( bp_is_current_component('events') || (bp_is_current_component('groups') && bp_is_current_action('group-events')) ){
add_filter('option_dbem_js_limit', '__return_false');
add_filter('option_dbem_css_limit', '__return_false');
}
}
add_action('wp_enqueue_scripts','bp_em_enqueue_scripts',1);
function bp_em_messages_js_compat() {
if(bp_is_messages_compose_screen()){
wp_deregister_script( 'events-manager' );
}
}
add_action( 'wp_print_scripts', 'bp_em_messages_js_compat', 100 );
/**
* Delete events when you delete a user.
*/
function bp_em_remove_data( $user_id ) {
$EM_Events = EM_Events::get(array('scope'=>'all','owner'=>$user_id, 'status'=>false));
EM_Events::delete($EM_Events);
}
add_action( 'wpmu_delete_user', 'bp_em_remove_data', 1 );
add_action( 'delete_user', 'bp_em_remove_data', 1 );
define('EM_BP_LOADED',true); //so we know
?>