-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gf-eventrebels.php
675 lines (593 loc) · 23.1 KB
/
class-gf-eventrebels.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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
<?php
GFForms::include_feed_addon_framework();
class GFEventRebels extends GFFeedAddOn
{
protected $_version = GF_EVENTREBELS_VERSION;
protected $_min_gravityforms_version = '1.9.12';
protected $_slug = 'gravityformseventrebels';
/**
* @var string $filter_prefix The prefix used for filters.
*/
protected $filter_prefix = 'gf_eventrebels_filter';
protected $_path = 'gravityformseventrebels/eventrebels.php';
protected $_full_path = __FILE__;
protected $_url = 'https://github.com/ilanco';
protected $_title = 'Gravity Forms Event Rebels Add-On';
protected $_short_title = 'Event Rebels';
protected $_enable_rg_autoupgrade = true;
protected $api = null;
private static $_instance = null;
/* Permissions */
protected $_capabilities_settings_page = 'gravityforms_eventrebels';
protected $_capabilities_form_settings = 'gravityforms_eventrebels';
protected $_capabilities_uninstall = 'gravityforms_eventrebels_uninstall';
/* Members plugin integration */
protected $_capabilities = ['gravityforms_eventrebels', 'gravityforms_eventrebels_uninstall'];
/**
* @var string $custom_field_key The custom field key (label/name); used by get_full_address().
*/
protected $custom_field_key = '';
/**
* Get instance of this class.
*
* @access public
* @static
* @return $_instance
*/
public static function get_instance()
{
if (self::$_instance == null) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Register needed plugin hooks and PayPal delayed payment support.
*
* @access public
* @return void
*/
public function init()
{
parent::init();
$this->add_delayed_payment_support([
'option_label' => esc_html__('Create Event Rebels object only when payment is received.', 'gravityformseventrebels')
]);
}
/**
* Add hook for Javascript analytics tracking.
*
* @access public
* @return void
*/
public function init_frontend()
{
parent::init_frontend();
}
/**
* Register needed styles.
*
* @access public
* @return array $styles
*/
public function styles()
{
$styles = [
[
'handle' => 'gform_eventrebels_form_settings_css',
'src' => $this->get_base_url() . '/css/form_settings.css',
'version' => $this->_version,
'enqueue' => [
['admin_page' => ['form_settings']],
]
]
];
return array_merge(parent::styles(), $styles);
}
/**
* Setup plugin settings fields.
*
* @access public
* @return array
*/
public function plugin_settings_fields()
{
return [
[
'title' => '',
'description' => $this->plugin_settings_description(),
'fields' => [
[
'name' => 'accountApiToken',
'label' => __('Account API Token', 'gravityformseventrebels'),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => [$this, 'initialize_api']
],
[
'name' => 'activityToken',
'label' => __('Activity Token', 'gravityformseventrebels'),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => [$this, 'initialize_api']
]
]
]
];
}
/**
* Prepare plugin settings description.
*
* @access public
* @return string $description
*/
public function plugin_settings_description()
{
$description = '<p>';
$description .= sprintf(
__('Event Rebels provides a complete suite of software for meeting and event planners. Use Gravity Forms to collect customer information and automatically add them to your Event Rebels account.', 'gravityformseventrebels')
);
$description .= '</p>';
if (!$this->initialize_api()) {
$description .= '<p>';
$description .= __('Gravity Forms Event Rebels Add-On requires your Account API Token and Activity Token. Contact Event Rebels for more information.', 'gravityformseventrebels');
$description .= '</p>';
}
return $description;
}
/**
* Setup fields for feed settings.
*
* @access public
* @return array
*/
public function feed_settings_fields()
{
/* Build base fields array. */
$base_fields = [
'title' => '',
'fields' => [
[
'name' => 'feedName',
'label' => __('Feed Name', 'gravityformseventrebels'),
'type' => 'text',
'required' => true,
'default_value' => $this->get_default_feed_name(),
'tooltip' => '<h6>'. __('Name', 'gravityformseventrebels') .'</h6>' . __('Enter a feed name to uniquely identify this setup.', 'gravityformseventrebels')
],
[
'name' => 'action',
'label' => __('Action', 'gravityformseventrebels'),
'type' => 'checkbox',
'required' => true,
'onclick' => "jQuery(this).parents('form').submit();",
'choices' => [
[
'name' => 'registerContact',
'label' => __('Register Contact', 'gravityformseventrebels'),
'icon' => 'fa-user',
]
]
],
[
'name' => 'fee',
'dependency' => ['field' => 'registerContact', 'values' => ('1')],
'label' => __('Fee', 'gravityformseventrebels'),
'type' => 'select',
'required' => true,
'choices' => $this->get_fee_choices()
]
]
];
/* Build contact fields array. */
$contact_fields = [
'title' => __('Contact Details', 'gravityformseventrebels'),
'dependency' => ['field' => 'registerContact', 'values' => ('1')],
'fields' => [
[
'name' => 'contactStandardFields',
'label' => __('Map Fields', 'gravityformseventrebels'),
'type' => 'field_map',
'field_map' => $this->standard_fields_for_feed_mapping(),
'tooltip' => '<h6>'. __('Map Fields', 'gravityformseventrebels') .'</h6>' . __('Select which Gravity Form fields pair with their respective Event Rebels fields.', 'gravityformseventrebels')
],
[
'name' => 'contactCustomFields',
'label' => '',
'type' => 'dynamic_field_map',
'field_map' => $this->custom_fields_for_feed_mapping(),
]
]
];
/* Build conditional logic fields array. */
$conditional_fields = [
'title' => __('Feed Conditional Logic', 'gravityformseventrebels'),
'dependency' => [$this, 'show_conditional_logic_field'],
'fields' => [
[
'name' => 'feedCondition',
'type' => 'feed_condition',
'label' => __( 'Conditional Logic', 'gravityformseventrebels' ),
'checkbox_label' => __( 'Enable', 'gravityformseventrebels' ),
'instructions' => __( 'Export to Event Rebels if', 'gravityformseventrebels' ),
'tooltip' => '<h6>' . __( 'Conditional Logic', 'gravityformseventrebels' ) . '</h6>' . __( 'When conditional logic is enabled, form submissions will only be exported to Event Rebels when the condition is met. When disabled, all form submissions will be posted.', 'gravityformseventrebels' )
]
]
];
return [$base_fields, $contact_fields, $conditional_fields];
}
/**
* Set custom dependency for conditional logic.
*
* @access public
* @return bool
*/
public function show_conditional_logic_field()
{
/* Get current feed. */
$feed = $this->get_current_feed();
/* Get posted settings. */
$posted_settings = $this->get_posted_settings();
/* Show if an action is chosen */
if (rgar($posted_settings, 'registerContact') == '1' || rgars($feed, 'meta/registerContact') == '1') {
return true;
}
return false;
}
/**
* Prepare standard fields for feed field mapping.
*
* @access public
* @return array
*/
public function standard_fields_for_feed_mapping()
{
return [
[
'name' => 'first_name',
'label' => __('First Name', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['name', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('name', 3)
],
[
'name' => 'last_name',
'label' => __('Last Name', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['name', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('name', 6)
],
[
'name' => 'email_address',
'label' => __('Email Address', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['email', 'hidden'],
'default_value' => $this->get_first_field_by_type('email')
],
[
'name' => 'phone_number',
'label' => __('Phone Number', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['phone', 'hidden'],
'default_value' => $this->get_first_field_by_type('phone')
],
[
'name' => 'address_street1',
'label' => __('Address Street 1', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 1)
],
[
'name' => 'address_street2',
'label' => __('Address Street 2', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 2)
],
[
'name' => 'address_city',
'label' => __('Address City', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 3)
],
[
'name' => 'address_state',
'label' => __('Address State', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 4)
],
[
'name' => 'address_zip',
'label' => __('Address Zip', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 5)
],
[
'name' => 'address_country',
'label' => __('Address Country', 'gravityformseventrebels'),
'required' => true,
'field_type' => ['address', 'text', 'hidden'],
'default_value' => $this->get_first_field_by_type('address', 6)
]
];
}
/**
* Prepare contact and custom fields for feed field mapping.
*
* @access public
* @return array
*/
public function custom_fields_for_feed_mapping()
{
return [
[
'label' => __('Choose a Field', 'gravityformseventrebels')
],
[
'value' => 'gf_custom',
'label' => __('Add a Custom Field', 'gravityformseventrebels')
]
];
}
/**
* Set feed creation control.
*
* @access public
* @return bool
*/
public function can_create_feed()
{
return $this->initialize_api();
}
/**
* Setup columns for feed list table.
*
* @access public
* @return array
*/
public function feed_list_columns()
{
return [
'feedName' => __('Name', 'gravityformseventrebels'),
'action' => __('Action', 'gravityformseventrebels'),
];
}
/**
* Get value for action feed list column.
*
* @access public
* @param array $feed
* @return string $action
*/
public function get_column_value_action($feed)
{
if (rgars($feed, 'meta/registerContact') == '1') {
return esc_html__('Register New Contact', 'gravityformseventrebels');
}
}
/**
* Process feed.
*
* @access public
* @param array $feed
* @param array $entry
* @param array $form
* @return void
*/
public function process_feed($feed, $entry, $form)
{
$this->log_debug(__METHOD__ . '(): Processing feed.');
/* If API instance is not initialized, exit. */
if (!$this->initialize_api()) {
$this->add_feed_error(esc_html__('Feed was not processed because API was not initialized.', 'gravityformsicontact'), $feed, $entry, $form);
return;
}
/* Register contact? */
if (rgars($feed, 'meta/registerContact') == 1) {
$contact = $this->register_contact($feed, $entry, $form);
}
}
/**
* Register contact.
*
* @access public
* @param array $feed
* @param array $entry
* @param array $form
* @return array $contact
*/
public function register_contact($feed, $entry, $form)
{
$this->log_debug(__METHOD__ . '(): Registering contact.');
/* Get fee item id. */
$fee_item_id = rgars($feed, 'meta/fee');
/* Setup mapped fields array. */
$contact_standard_fields = $this->get_field_map_fields($feed, 'contactStandardFields');
$contact_custom_fields = $this->get_dynamic_field_map_fields($feed, 'contactCustomFields');
/* Setup base fields. */
$first_name = $this->get_field_value($form, $entry, $contact_standard_fields['first_name']);
$last_name = $this->get_field_value($form, $entry, $contact_standard_fields['last_name']);
$email_address = $this->get_field_value($form, $entry, $contact_standard_fields['email_address']);
$phone_number = $this->get_field_value($form, $entry, $contact_standard_fields['phone_number']);
$address_street1 = $this->get_field_value($form, $entry, $contact_standard_fields['address_street1']);
$address_street2 = $this->get_field_value($form, $entry, $contact_standard_fields['address_street2']);
$address_city = $this->get_field_value($form, $entry, $contact_standard_fields['address_city']);
$address_state = $this->get_field_value($form, $entry, $contact_standard_fields['address_state']);
$address_zip = $this->get_field_value($form, $entry, $contact_standard_fields['address_zip']);
$address_country = $this->get_field_value($form, $entry, $contact_standard_fields['address_country']);
/* If the name is empty, exit. */
if (rgblank($first_name) || rgblank($last_name)) {
$this->add_feed_error(esc_html__('Contact could not be created as first and/or last name were not provided.', 'gravityformseventrebels'), $feed, $entry, $form);
return null;
}
/* If the email address is empty, exit. */
if (GFCommon::is_invalid_or_empty_email($email_address)) {
$this->add_feed_error(esc_html__('Contact could not be created as email address was not provided.', 'gravityformseventrebels'), $feed, $entry, $form);
return null;
}
/* Build base contact. */
$contact = [
'FeeItemID' => $fee_item_id,
'FirstName' => $first_name,
'LastName' => $last_name,
'Email' => $email_address,
'PhoneNumber' => $phone_number,
'Address1' => $address_street1,
'Address2' => $address_street2,
'City' => $address_city,
'State' => $address_state,
'ZipCode' => $address_zip,
'Country' => $address_country,
];
/* Add custom field data. */
foreach ($contact_custom_fields as $field_key => $field_id) {
/* Get the field value. */
$this->custom_field_key = $field_key;
$field_value = $this->get_field_value($form, $entry, $field_id);
/* If the field value is empty, skip this field. */
if (rgblank($field_value)) {
continue;
}
/* Custom field processing. */
$contact = apply_filters($this->filter_prefix . '_pre_contact_custom_field', $contact, $field_key, $field_value, $field_id, $form);
$contact = $this->add_contact_property($contact, $field_key, $field_value, $field_id, $form);
}
$this->log_debug(__METHOD__ . '(): Registering contact: ' . print_r($contact, true));
try {
/* Register contact. */
$contact = $this->api->register_contact($contact);
/* Save registrant ID to entry. */
gform_update_meta($entry['id'], 'eventrebels_registrant_id', $contact['RegistrantID']);
/* Save billing ID to entry. */
gform_update_meta($entry['id'], 'eventrebels_billing_id', $contact['BillingID']);
/* Log that contact was created. */
$this->log_debug(__METHOD__ . '(): Contact #' . $contact['RegistrantID'] . ' created.');
} catch (Exception $e) {
$this->add_feed_error(sprintf(esc_html__('Contact could not be created. %s', 'gravityformseventrebels'), $e->getMessage()), $feed, $entry, $form);
return null;
}
return $contact;
}
/**
* Add property to contact object.
*
* @access public
* @param array $contact
* @param string $field_key
* @param string $field_value
* @param bool $replace (default: false)
* @return array $contact
*/
public function add_contact_property($contact, $field_key, $field_value, $field_id, $form)
{
if (ctype_digit($field_key)) {
$field_key = 'UDF' . $field_key;
/* Check if field key already exists. */
if (array_key_exists($field_key, $contact)) {
return $contact;
}
if (!isset($form['fields'][$field_id])) {
$field_value = 'Yes';
}
/* Add property object to properties array. */
$contact[$field_key] = $field_value;
}
return $contact;
}
/**
* Initializes Event Rebels API if credentials are valid.
*
* @access public
* @return bool
*/
public function initialize_api()
{
if (!is_null($this->api)) {
return true;
}
/* Load the Event Rebels API library. */
if (!class_exists('Eventrebels_API')) {
require_once 'includes/class-eventrebels-api.php';
}
/* Get the plugin settings */
$settings = $this->get_plugin_settings();
/* If any of the account information fields are empty, return null. */
if (rgblank($settings['accountApiToken']) || rgblank($settings['activityToken'])) {
return null;
}
$this->log_debug(__METHOD__ . "(): Validating API info for {$settings['accountApiToken']} / {$settings['activityToken']}.");
$eventrebels = new Eventrebels_API($settings['accountApiToken'], $settings['activityToken']);
try {
/* Run API test. */
$eventrebels->get_fees();
/* Log that test passed. */
$this->log_debug(__METHOD__ . '(): API credentials are valid.');
/* Assign Event Rebels object to the class. */
$this->api = $eventrebels;
return true;
} catch (Exception $e) {
/* Log that test failed. */
$this->log_error(__METHOD__ . '(): API credentials are invalid; '. $e->getMessage());
/* Assign null to the class. */
$this->api = null;
return false;
}
}
/**
* Returns the combined value of the specified Address field.
*
* @param array $entry
* @param string $field_id
*
* @return string
*/
public function get_full_address($entry, $field_id)
{
$street_value = str_replace(' ', ' ', trim(rgar($entry, $field_id . '.1')));
$street2_value = str_replace(' ', ' ', trim(rgar($entry, $field_id . '.2')));
$city_value = str_replace(' ', ' ', trim(rgar($entry, $field_id . '.3')));
$state_value = str_replace(' ', ' ', trim(rgar($entry, $field_id . '.4')));
$zip_value = trim(rgar($entry, $field_id . '.5'));
$country_value = trim(rgar($entry, $field_id . '.6'));
$address = $street_value;
$address .= !empty($street_value) && !empty($street2_value) ? " $street2_value" : $street2_value;
if (strpos($this->custom_field_key, 'address_') === 0) {
$address_array = [
'address' => $address,
'city' => $city_value,
'state' => $state_value,
'zip' => $zip_value,
'country' => $country_value,
];
return json_encode($address_array);
} else {
$address .= !empty($address) && (!empty($city_value) || !empty($state_value)) ? ", $city_value," : $city_value;
$address .= !empty($address) && !empty($city_value) && !empty($state_value) ? " $state_value" : $state_value;
$address .= !empty($address) && !empty($zip_value) ? " $zip_value," : $zip_value;
$address .= !empty($address) && !empty($country_value) ? " $country_value" : $country_value;
return $address;
}
}
protected function get_fee_choices()
{
if (!$this->initialize_api()) {
return false;
}
$fees = $this->api->get_fees();
$result = [[
'label' => 'Choose Fee',
'value' => ''
]];
foreach ($fees as $fee) {
$result[] = [
'label' => $fee['FeeName'],
'value' => $fee['FeeItemID']
];
}
return $result;
}
}