-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbp-event-organiser-eo.php
631 lines (416 loc) · 13.3 KB
/
bp-event-organiser-eo.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
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
<?php /*
--------------------------------------------------------------------------------
BuddyPress_Event_Organiser_EO Class
--------------------------------------------------------------------------------
*/
class BuddyPress_Event_Organiser_EO {
/**
* properties
*/
// parent object
public $plugin;
// group IDs
public $group_ids;
/**
* @description: initialises this object
* @return object
*/
function __construct() {
// register hooks
$this->register_hooks();
// --<
return $this;
}
/**
* @description: set references to other objects
* @return nothing
*/
public function set_references( $parent ) {
// store
$this->plugin = $parent;
}
/**
* @description: register hooks on BuddyPress loaded
* @return nothing
*/
public function register_hooks() {
// check for Event Organiser
if ( !$this->is_active() ) return;
// add our event meta box
add_action( 'add_meta_boxes', array( $this, 'event_meta_box' ) );
// intercept save event
add_action( 'eventorganiser_save_event', array( $this, 'intercept_save_event' ), 10, 1 );
// intercept before break occurrence
add_action( 'eventorganiser_pre_break_occurrence', array( $this, 'pre_break_occurrence' ), 10, 2 );
// intercept after break occurrence, because a new post is created
//add_action( 'eventorganiser_occurrence_broken', array( $this, 'occurrence_broken' ), 10, 3 );
// intercept calendar display
add_filter( 'eventorganiser_fullcalendar_event', array( $this, 'intercept_calendar' ), 10, 3 );
// intercept post content - try to catch calendar shortcodes
add_filter( 'the_content', array( $this, 'intercept_content' ), 10, 1 );
}
/**
* @description: utility to check if Event Organiser is present and active
* @return bool
*/
public function is_active() {
// only check once
static $eo_active = false;
if ( $eo_active ) { return true; }
// access Event Organiser option
$installed_version = get_option( 'eventorganiser_version' );
// this plugin will not work without EO
if ( $installed_version === false ) {
wp_die( '<p>Event Organiser plugin is required</p>' );
}
// we need version 2 at least
if ( $installed_version < '2' ) {
wp_die( '<p>Event Organiser version 2 or higher is required</p>' );
}
// set flag
$eo_active = true;
// --<
return $eo_active;
}
/**
* @description: utility to check if BP Group Hierarchy is present and active
* @return bool
*/
public function is_group_hierarchy_active() {
// only check once
static $bpgh_active = false;
if ( $bpgh_active ) { return true; }
// do we have the BP Group Hierarchy plugin constant and tree method?
if (
defined( 'BP_GROUP_HIERARCHY_IS_INSTALLED' ) AND
method_exists( 'BP_Groups_Hierarchy', 'get_tree' )
) {
// set flag
$bpgh_active = true;
}
// --<
return $bpgh_active;
}
//##########################################################################
/**
* @description: intercept save event
* @param int $post_id the numeric ID of the WP post
* @return nothing
*/
public function intercept_save_event( $post_id ) {
// check that we trust the source of the data (EO does this for us)
//check_admin_referer( 'bp_event_organiser_meta_save', 'bp_event_organiser_nonce_field' );
// get post data
$post = get_post( $post_id );
// save BP groups for this EO event
$this->update_event_groups( $post_id );
}
/**
* @description: intercept before break occurrence
* @param int $post_id the numeric ID of the WP post
* @param int $occurrence_id the numeric ID of the occurrence
* @return nothing
*/
public function pre_break_occurrence( $post_id, $occurrence_id ) {
/*
// eg ( [post_id] => 31 [occurrence_id] => 2 )
print_r( array(
'method' => 'bpeo->pre_break_occurrence',
'post_id' => $post_id,
'occurrence_id' => $occurrence_id,
) ); die();
*/
// init or die
if ( ! $this->is_active() ) return;
// unhook eventorganiser_save_event, because EO copies across post-meta
remove_action( 'eventorganiser_save_event', array( $this, 'intercept_save_event' ), 10 );
}
/**
* @description: intercept after break occurrence
* @param int $post_id the numeric ID of the WP post
* @param int $occurrence_id the numeric ID of the occurrence
* @param int $new_event_id the numeric ID of the new WP post
* @return nothing
*/
public function occurrence_broken( $post_id, $occurrence_id, $new_event_id ) {
/*
print_r( array(
'method' => 'bpeo->occurrence_broken',
'post_id' => $post_id,
'occurrence_id' => $occurrence_id,
'new_event_id' => $new_event_id,
) ); die();
*/
// --<
return;
// get existing event groups
$existing = $this->get_event_groups( $post_id );
// transfer to new event
$this->set_event_groups( $new_event_id, $existing );
}
/**
* @description: register event meta box
* @return nothing
*/
public function event_meta_box() {
// create it
add_meta_box(
'bp_event_organiser_metabox',
'BuddyPress Groups',
array( $this, 'event_meta_box_render' ),
'event',
'side', //'normal',
'core' //'high'
);
}
/**
* @description: define venue meta box
* @return nothing
*/
public function event_meta_box_render( $event ) {
// add nonce
wp_nonce_field( 'bp_event_organiser_meta_save', 'bp_event_organiser_nonce_field' );
//print_r( $event ); die();
// is group hierarchy present?
if( $this->is_group_hierarchy_active() ) {
// get tree
$groups_list = array(
'groups' => BP_Groups_Hierarchy::get_tree()
);
// add total
$groups_list['total'] = count( $groups_list['groups'] );
} else {
// init params
$params = array(
'type' => 'alphabetical',
'per_page' => 1000, // avoid pagination cutoff
//'show_hidden' => true, // removed this since events always show up
);
// if not super admin (or admin in single site install)
if ( ! is_super_admin() ) {
// restrict groups to those the user is a member of
$params['user_id'] = bp_loggedin_user_id();
}
// get flat list
$groups_list = BP_Groups_Group::get( $params );
}
// kick out if we don't have any
if ( $groups_list['total'] === 0 ) {
// show message
echo '<p class="bp_event_organiser_desc">' . __( 'No groups were found.', 'bp-group-organizer' ) . '</p>';
// kick
return;
}
//print_r( $groups_list ); die();
// get array of checked IDs for this event
$this->group_ids = $this->get_event_groups( $event->ID );
// define walker
$walker = new Walker_BPEO_Group;
// open html
$result = '';
// open scroller
$result .= '<div class="bp_event_organiser_scroller" style="height: 200px; overflow-y: scroll; border: 1px solid #ccc; background: #fff; padding: 0 6px;">'."\n";
// open list
$result .= '<ul class="bp_event_organiser_groups_list">'."\n";
// call walker
$result .= bp_event_organiser_walk_group_tree(
$groups_list['groups'],
0,
(object) array( 'walker' => $walker )
);
// close list
$result .= '</ul>'."\n\n\n";
// close scroller
$result .= '</div>'."\n\n\n";
// show meta box
echo '
<p class="bp_event_organiser_desc">Choose the groups this event should be assigned to.</p>
'.$result;
}
//##########################################################################
/**
* @description: update event groups array
* @param int $event_id the numeric ID of the event
* @return nothing
*/
public function update_event_groups( $event_id ) {
// init as off
$value = array();
// kick out if not set
if ( isset( $_POST['bp_group_organizer_groups'] ) ) {
// retrieve meta value
$value = is_array( $_POST['bp_group_organizer_groups'] ) ? $_POST['bp_group_organizer_groups'] : array();
}
// save
$this->set_event_groups( $event_id, $value );
}
/**
* @description: update event groups array
* @param int $event_id the numeric ID of the event
* @return nothing
*/
public function set_event_groups( $event_id, $value = array() ) {
// convert to string to be safe
$string = implode( ',', $value );
// trace
//print_r( $string ); die();
//print_r( $value ); die();
// update event meta
update_post_meta( $event_id, '_bpeo_event_groups', $string );
}
/**
* @description: get all event groups
* @param int $post_id the numeric ID of the WP post
* @return bool $event_groups_array the event groups event
*/
public function get_event_groups( $post_id ) {
// get the meta value
$event_groups = get_post_meta( $post_id, '_bpeo_event_groups', true );
// if it's not yet set it will be an empty string, so cast as array
if ( $event_groups === '' ) return array();
// convert to array
$event_groups_array = explode( ',', $event_groups );
// --<
return $event_groups_array;
}
/**
* @description: delete event groups
* @param int $post_id the numeric ID of the WP post
* @return nothing
*/
public function clear_event_groups( $post_id ) {
// delete the meta value
delete_post_meta( $post_id, '_bpeo_event_groups' );
}
//##########################################################################
/**
* @description: getter method for accessing group IDs for metabox list walker
* @return array $group_ids array of group IDs
*/
public function get_group_ids() {
// do we have the property?
if ( isset( $this->group_ids ) AND is_array( $this->group_ids ) ) {
// yup, send it back
return $this->group_ids;
}
// return an empty array by default
return array();
}
//##########################################################################
/**
* @description: intercept display of group calendar
* @param object $post the WP post object
* @param int $post_id the numeric ID of the WP post
* @param int $occurrence_id the numeric ID of the EO occurrence
* @return nothing
*/
public function intercept_calendar( $post, $post_id, $occurrence_id ) {
/*
bp_get_current_group_id() reports incorrect IDs for BP Group Hierarchy
sub groups - it only reports the top level group's ID presumably because
BuddyPress is parsing the URL to get the group ID. As a result, we need to
parse $_GET, which has our injected value.
*/
// init
$group_id = 0;
// get group ID from $_GET
if ( isset( $_GET['bp_group_id'] ) AND is_numeric( $_GET['bp_group_id'] ) ) {
// set variable
$group_id = absint( $_GET['bp_group_id'] );
}
/*
throw new Exception( print_r( array(
'group_id' => $group_id,
'GET' => $_GET,
), true ) );
*/
// pass if not in group
if ( 0 == $group_id ) return $post;
/*
global $bp, $_POST, $_GET, $_SERVER;
throw new Exception( print_r( array(
//'post' => $post,
//'bp' => $bp->groups,
//'referrer' => $_SERVER['HTTP_REFERER'],
//'query' => $_SERVER['REQUEST_URI'],
//'GET' => $_GET,
), true ) );
*/
// get groups for this post
$groups = $this->get_calendar_groups( $post_id );
/*
throw new Exception( print_r( array(
'group_id' => $group_id,
'groups' => $groups,
'post' => $post,
), true ) );
*/
// do we show this post?
if ( in_array( $group_id, $groups ) ) {
// --<
return $post;
}
// --<
return null;
}
/**
* @description: get all event groups
* @param int $post_id the numeric ID of the WP post
* @return bool $event_groups_array the event groups event
*/
public function get_calendar_groups( $post_id ) {
// get the meta value
$event_groups = get_post_meta( $post_id, '_bpeo_event_groups', true );
// if it's not yet set it will be an empty string, so cast as array
if ( $event_groups === '' ) return array();
// convert to array
$event_groups_array = explode( ',', $event_groups );
// --<
return $event_groups_array;
}
//##########################################################################
/**
* @description: intercept content and clear calendar cache if shortcode is present
* @return string $content the post content
*/
public function intercept_content( $content ) {
// do we have the shortcode?
if ( has_shortcode( $content, 'eo_fullcalendar' ) ) {
// yup, bust cache
delete_transient( 'eo_full_calendar_public' );
}
// pass content on
return $content;
}
//##########################################################################
/**
* @description: debugging
* @param array $msg
* @return string
*/
private function _debug( $msg ) {
// add to internal array
$this->messages[] = $msg;
// do we want output?
if ( BUDDYPRESS_EVENT_ORGANISER_DEBUG ) print_r( $msg );
}
} // class ends
/**
* @description: get list of groups for an event
* @return array $groups comma-delimited
*/
function bp_event_organiser_get_groups( $post_id ) {
// access plugin global
global $buddypress_event_organiser;
// --<
return $buddypress_event_organiser->eo->get_event_groups( $post_id );
}
/**
* @description: get list of group IDs for an event's metabox
*/
function bp_event_organiser_get_group_ids() {
// access plugin global
global $buddypress_event_organiser;
// --<
return $buddypress_event_organiser->eo->get_group_ids();
}