-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcivicrm-eo-attendance.php
564 lines (453 loc) · 12.9 KB
/
civicrm-eo-attendance.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
<?php
/**
* CiviCRM Event Organiser Attendance
*
* Plugin Name: CiviCRM Event Organiser Attendance
* Description: Attendance functionality for CiviCRM Event Organiser plugin.
* Plugin URI: https://github.com/christianwach/civicrm-eo-attendance
* Version: 0.7.0a
* Author: Christian Wach
* Author URI: https://haystack.co.uk
* Text Domain: civicrm-eo-attendance
* Domain Path: /languages
*
* @package CiviCRM_Event_Organiser_Attendance
*/
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit;
// Set our version here.
define( 'CIVICRM_EO_ATTENDANCE_VERSION', '0.7.0a' );
// Store reference to this file.
if ( ! defined( 'CIVICRM_EO_ATTENDANCE_FILE' ) ) {
define( 'CIVICRM_EO_ATTENDANCE_FILE', __FILE__ );
}
// Store URL to this plugin's directory.
if ( ! defined( 'CIVICRM_EO_ATTENDANCE_URL' ) ) {
define( 'CIVICRM_EO_ATTENDANCE_URL', plugin_dir_url( CIVICRM_EO_ATTENDANCE_FILE ) );
}
// Store PATH to this plugin's directory.
if ( ! defined( 'CIVICRM_EO_ATTENDANCE_PATH' ) ) {
define( 'CIVICRM_EO_ATTENDANCE_PATH', plugin_dir_path( CIVICRM_EO_ATTENDANCE_FILE ) );
}
// Set our debugging status here.
if ( ! defined( 'CIVICRM_EO_ATTENDANCE_DEBUG' ) ) {
define( 'CIVICRM_EO_ATTENDANCE_DEBUG', true );
}
// Set our localhost status here.
if ( ! defined( 'CIVICRM_EO_ATTENDANCE_LOCALHOST' ) ) {
define( 'CIVICRM_EO_ATTENDANCE_LOCALHOST', true );
}
/**
* CiviCRM Event Organiser Attendance Class.
*
* A class that encapsulates this plugin's functionality.
*
* @since 0.1
*/
class CiviCRM_Event_Organiser_Attendance {
/**
* CiviCRM Event Organiser plugin reference.
*
* @since 0.1
* @access public
* @var CiviCRM_Event_Organiser
*/
public $civicrm_eo;
/**
* Participant Listing object.
*
* @since 0.1
* @access public
* @var CiviCRM_EO_Attendance_Participant_Listing
*/
public $participant_listing;
/**
* Event Leader object.
*
* @since 0.3
* @access public
* @var CiviCRM_EO_Attendance_Event_Leader
*/
public $event_leader;
/**
* Event Custom Data object.
*
* @since 0.2.2
* @access public
* @var CiviCRM_EO_Attendance_Custom_Data_Event
*/
public $custom_data_event;
/**
* Participant Custom Data object.
*
* @since 0.2.2
* @access public
* @var CiviCRM_EO_Attendance_Custom_Data_Participant
*/
public $custom_data_participant;
/**
* Event Sharing object.
*
* @since 0.2.2
* @access public
* @var CiviCRM_EO_Attendance_Event_Sharing
*/
public $event_sharing;
/**
* Event Paid object.
*
* @since 0.3.1
* @access public
* @var CiviCRM_EO_Attendance_Event_Paid
*/
public $event_paid;
/**
* Rendez Vous Manager object.
*
* @since 0.4.7
* @access public
* @var CiviCRM_EO_Attendance_Rendez_Vous
*/
public $rendez_vous;
/**
* Initialises this object.
*
* @since 0.1
*/
public function __construct() {
// Initialise.
$this->initialise();
// Translation.
add_action( 'plugins_loaded', [ $this, 'enable_translation' ] );
// External references.
add_action( 'plugins_loaded', [ $this, 'setup_objects' ], 20 );
}
/**
* Do stuff on plugin activation.
*
* @since 0.3
*/
public function activate() {
// Set up objects.
$this->setup_objects();
// Pass to classes that need activation.
$this->custom_data_participant->activate();
$this->custom_data_event->activate();
$this->rendez_vous->activate();
}
/**
* Do stuff on plugin deactivation.
*
* @since 0.3
*/
public function deactivate() {
// Pass to classes that need deactivation.
$this->rendez_vous->deactivate();
}
/**
* Do stuff on plugin init.
*
* @since 0.1
*/
public function initialise() {
// Include files.
$this->include_files();
// Add actions and filters.
$this->register_hooks();
}
/**
* Include files.
*
* @since 0.1
*/
public function include_files() {
// Load our class files.
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-participant-listing.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-event-leader.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-event-paid.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-custom-data-participant.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-custom-data-event.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-event-sharing.php';
require CIVICRM_EO_ATTENDANCE_PATH . 'includes/class-rendez-vous.php';
}
/**
* Register hooks.
*
* @since 0.1
*/
public function register_hooks() {
/*
// Test for basepage CiviEvent info requests so we can redirect to the
// relevant Event Organiser Event.
add_action( 'civicrm_initialized', array( $this, 'maybe_redirect_to_eo' ) );
*/
// Front end hooks.
if ( ! is_admin() ) {
// Change URLs on front end.
add_action( 'civicrm_alterContent', [ $this, 'content_parse' ], 10, 4 );
// Register any public styles.
add_action( 'wp_enqueue_scripts', [ $this, 'enqueue_styles' ], 20 );
}
}
/**
* Set up this plugin's objects.
*
* @since 0.3
*/
public function setup_objects() {
// Bail if CiviCRM Event Organiser plugin is not persent.
if ( ! function_exists( 'civicrm_eo' ) ) {
return;
}
// Init flag.
static $done;
// Only do this once.
if ( isset( $done ) && true === $done ) {
return;
}
// Store reference to CiviCRM Event Organiser.
$this->civicrm_eo = civicrm_eo();
// Init objects.
$this->participant_listing = new CiviCRM_EO_Attendance_Participant_Listing();
$this->event_leader = new CiviCRM_EO_Attendance_Event_Leader();
$this->event_paid = new CiviCRM_EO_Attendance_Event_Paid();
$this->custom_data_participant = new CiviCRM_EO_Attendance_Custom_Data_Participant();
$this->custom_data_event = new CiviCRM_EO_Attendance_Custom_Data_Event();
$this->event_sharing = new CiviCRM_EO_Attendance_Event_Sharing();
$this->rendez_vous = new CiviCRM_EO_Attendance_Rendez_Vous();
// Store references.
$this->participant_listing->set_references( $this );
$this->event_leader->set_references( $this );
$this->event_paid->set_references( $this );
$this->custom_data_participant->set_references( $this );
$this->custom_data_event->set_references( $this );
$this->event_sharing->set_references( $this );
$this->rendez_vous->set_references( $this );
// We're done.
$done = true;
}
/**
* Load translation files.
*
* A good reference on how to implement translation in WordPress:
* http://ottopress.com/2012/internationalization-youre-probably-doing-it-wrong/
*
* @since 0.1
*/
public function enable_translation() {
// Load translations.
// phpcs:ignore WordPress.WP.DeprecatedParameters.Load_plugin_textdomainParam2Found
load_plugin_textdomain(
'civicrm-eo-attendance', // Unique name.
false, // Deprecated argument.
dirname( plugin_basename( __FILE__ ) ) . '/languages/' // Relative path.
);
}
/**
* Add our front-end stylesheets.
*
* @since 0.4.5
*/
public function enqueue_styles() {
// Add front end CSS.
wp_enqueue_style(
'civicrm-eo-attendance',
CIVICRM_EO_ATTENDANCE_URL . 'assets/css/civicrm-eo-attendance.css',
null,
CIVICRM_EO_ATTENDANCE_VERSION,
'all' // Media.
);
}
/**
* Alter generated page content for a CiviEvent.
*
* @since 0.3.4
*
* @param str $content Previously generated content.
* @param str $context Context of content - page or form.
* @param str $tpl_name The file name of the template.
* @param object $object A reference to the page or form object.
*/
public function content_parse( $content, $context, $tpl_name, $object ) {
// Bail if not one of the forms we want.
if (
'CRM/Event/Form/Registration/ThankYou.tpl' !== $tpl_name &&
'CRM/Event/Page/EventInfo.tpl' !== $tpl_name
) {
return;
}
// CiviEvent ID needs to be retrieved differently per template.
if ( 'CRM/Event/Form/Registration/ThankYou.tpl' === $tpl_name ) {
// Do we have a CiviEvent ID?
if ( ! isset( $object->_values['event']['id'] ) ) {
return;
}
// Get CiviEvent ID.
$civi_event_id = absint( $object->_values['event']['id'] );
}
// CiviEvent ID needs to be retrieved differently per template.
if ( 'CRM/Event/Page/EventInfo.tpl' === $tpl_name ) {
// Do we have a CiviEvent ID?
if ( ! isset( $object->_id ) ) {
return;
}
// Get CiviEvent ID.
$civi_event_id = absint( $object->_id );
}
// Init links.
$links = [];
// Use CiviCRM to construct link.
$link = CRM_Utils_System::url(
'civicrm/event/info',
'reset=1&id=' . $civi_event_id,
true,
null,
false,
true
);
// Add this and its processed variant.
$links[] = $link;
$links[] = htmlentities( $link );
// Use CiviCRM to construct alternative link.
$link = CRM_Utils_System::url(
'civicrm/event/info',
'id=' . $civi_event_id . '&reset=1',
true,
null,
false,
true
);
// Add this and its processed variant.
$links[] = $link;
$links[] = htmlentities( $link );
// Get EO Event ID.
$event_id = $this->civicrm_eo->mapping->get_eo_event_id_by_civi_event_id( $civi_event_id );
// Bail if we didn't get one.
if ( false === $event_id ) {
return;
}
// Get target permalink.
$event_url = get_permalink( $event_id );
// Replace into content.
$content = str_replace( $links, $event_url, $content );
}
/**
* Redirect to an Event Organiser Event when viewing a CiviEvent info page.
*
* @since 0.3.4
*/
public function maybe_redirect_to_eo() {
// Only fire once.
remove_action( 'civicrm_initialized', [ $this, 'maybe_redirect_to_eo' ] );
// Bail if no CiviCRM.
if ( ! function_exists( 'civi_wp' ) ) {
return;
}
// Bail if not on CiviCRM's basepage.
if ( 'basepage' !== civi_wp()->civicrm_context_get() ) {
return;
}
// Get CiviCRM's arguments.
$args = civi_wp()->get_request_args();
// Bail if we don't have any.
if ( is_null( $args['argString'] ) ) {
return;
}
// Init path.
$path = [];
// Check for Event.
if ( isset( $args['args'][0] ) && 'civicrm' === $args['args'][0] ) {
$path[] = $args['args'][0];
}
// Check for Event.
if ( isset( $args['args'][1] ) && 'event' === $args['args'][1] ) {
$path[] = $args['args'][1];
}
// Check for info page.
if ( isset( $args['args'][2] ) && 'info' === $args['args'][2] ) {
$path[] = $args['args'][2];
}
// Bail if not on a CiviEvent info page.
if ( 'civicrm/event/info' !== implode( '/', $path ) ) {
return;
}
// Bail if this is itself a redirect to an info page.
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
if ( isset( $_GET['noFullMsg'] ) && 'true' === sanitize_text_field( wp_unslash( $_GET['noFullMsg'] ) ) ) {
return;
}
// Bail if no ID.
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
if ( ! isset( $_GET['id'] ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
if ( ! is_numeric( sanitize_text_field( wp_unslash( $_GET['id'] ) ) ) ) {
return;
}
// Get the ID of the CiviEvent.
// phpcs:ignore WordPress.Security.NonceVerification.Missing, WordPress.Security.NonceVerification.Recommended
$civi_event_id = (int) sanitize_text_field( wp_unslash( $_GET['id'] ) );
// Get EO Event ID.
$event_id = $this->civicrm_eo->mapping->get_eo_event_id_by_civi_event_id( $civi_event_id );
// Bail if we didn't get one.
if ( false === $event_id ) {
return;
}
// Get target permalink.
$event_url = get_permalink( $event_id );
// Redirect now.
wp_safe_redirect( $event_url );
exit();
}
/**
* Write to the error log.
*
* @since 0.6.1
*
* @param array $data The data to write to the log file.
*/
public function log_error( $data = [] ) {
// Skip if not debugging.
if ( false === CIVICRM_EO_ATTENDANCE_DEBUG ) {
return;
}
// Skip if empty.
if ( empty( $data ) ) {
return;
}
// Format data.
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_print_r
$error = print_r( $data, true );
// Write to log file.
if ( true === CIVICRM_EO_ATTENDANCE_LOCALHOST ) {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $error );
} else {
// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
error_log( $error, 3, WP_CONTENT_DIR . '/debug.log' );
}
}
}
/**
* Utility to get a reference to this plugin.
*
* @since 0.1
*
* @return object $plugin The plugin reference.
*/
function civicrm_eo_attendance() {
static $plugin;
// Instantiate if not yet done.
if ( ! isset( $plugin ) ) {
$plugin = new CiviCRM_Event_Organiser_Attendance();
}
// --<
return $plugin;
}
// Declare as global.
civicrm_eo_attendance();
// Activation.
register_activation_hook( __FILE__, [ civicrm_eo_attendance(), 'activate' ] );
// Deactivation.
register_deactivation_hook( __FILE__, [ civicrm_eo_attendance(), 'deactivate' ] );