-
Notifications
You must be signed in to change notification settings - Fork 0
/
class-gragrid.php
681 lines (601 loc) · 17.9 KB
/
class-gragrid.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
676
677
678
679
680
681
<?php
/**
* The SendGrid Add-on
*
* phpcs:disable PSR2.Classes.PropertyDeclaration.Underscore
*
* @since 1.0.0
*
* @package Gragrid
* @author Vladimir Contreras
*/
GFForms::include_feed_addon_framework();
require_once 'includes/concerns/class-gragrid-converts-case.php';
/**
* The SendGrid Add-on Class
*
* @since 1.0.0
* @author Vladimir Contreras
*/
class Gragrid extends GFFeedAddOn {
use Gragrid_Converts_Case;
/**
* Contains an instance of this class, if available.
*
* @since 1.0.0
*
* @access private
* @var Gragrid $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the SendGrid Add-On.
*
* @since 1.0.0
*
* @access protected
* @var string $_version Contains the version, defined from gragrid.php
*/
protected $_version = GRAGRID_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0.0
*
* @access protected
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.12';
/**
* Defines the plugin slug.
*
* @since 1.0.0
*
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gragrid';
/**
* Defines the main plugin file.
*
* @since 1.0.0
*
* @access protected
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gragrid/gragrid.php';
/**
* Defines the full path to this class file.
*
* @since 1.0.0
*
* @access protected
* @var string $_full_path The full path.
*/
protected $_full_path = __FILE__;
/**
* Defines the URL where this Add-On can be found.
*
* @since 1.0.0
*
* @access protected
* @var string The URL of the Add-On.
*/
protected $_url = 'https://github.com/vlasscontreras/gragrid';
/**
* Defines the title of this Add-On.
*
* @since 1.0.0
*
* @access protected
* @var string $_title The title of the Add-On.
*/
protected $_title = null;
/**
* Defines the short title of the Add-On.
*
* @since 1-0-0
*
* @access protected
* @var string $_short_title The short title.
*/
protected $_short_title = 'SendGrid';
/**
* Contains an instance of the SendGrid API library.
*
* @since 1.0.0
*
* @access protected
* @var Gragrid_API|null $api Contains an instance of the SendGrid API library.
*/
protected $api = null;
/**
* Add-on constructor
*
* @since 1.0.0
*/
public function __construct() {
parent::__construct();
$this->_title = esc_html__( 'Gravity Forms: SendGrid Add-on', 'gragrid' );
}
/**
* Get an instance of this class.
*
* @since 1.0.0
*
* @access public
* @return Gragrid
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new self();
}
return self::$_instance;
}
/**
* Autoload the required libraries.
*
* @since 1.0.0
*
* @access public
* @uses GFAddOn::is_gravityforms_supported()
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() && ! class_exists( 'Gragrid_API' ) ) {
require_once 'includes/class-gragrid-api.php';
}
}
// # PLUGIN SETTINGS --------------------------------------------
/**
* Configures the settings which should be rendered on the add-on
* settings tab.
*
* @since 1.0.0
*
* @access public
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'description' => sprintf(
'<p>%s</p>',
sprintf(
// Translators: 1 open anchor tag, 2 close anchor tag, 3 open anchor tag, 4 close anchor tag.
esc_html__( 'SendGrid makes it easy to reliably send email notifications. If you don\'t have a SendGrid account, you can %1$ssign up for one here%2$s. Once you have signed up, you can %3$sfind your API keys here%4$s.', 'gragrid' ),
'<a href="https://sendgrid.com" target="_blank" rel="noopener noreferrer">',
'</a>',
'<a href="https://app.sendgrid.com/settings/api_keys" target="_blank" rel="noopener noreferrer">',
'</a>'
)
),
'fields' => array(
array(
'name' => 'api_key',
'label' => esc_html__( 'SendGrid API Key', 'gragrid' ),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => array( $this, 'init_api' ),
),
),
),
);
}
// # FEED SETTINGS ----------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @since 1.0.0
* @since 1.1.0 Added conditional feed setting.
* @since 2.1.0 Added custom field mapping.
*
* @access public
* @return array
*/
public function feed_settings_fields() {
$custom_fields = $this->sengrid_custom_fields_map();
$custom_field_map = null;
if ( ! count( $custom_fields ) > 0 ) {
$this->log_error( __METHOD__ . ': API retured empty set of custom fields.' );
} else {
$custom_field_map = array(
'name' => 'mappedCustomFields',
'label' => esc_html__( 'Custom Fields', 'gragrid' ),
'type' => 'field_map',
'field_map' => $custom_fields,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Custom Fields', 'gragrid' ),
esc_html__( 'Associate your custom SendGrid fields to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'gragrid' )
),
);
}
$fields = array(
array(
'name' => 'feedName',
'label' => esc_html__( 'Name', 'gragrid' ),
'type' => 'text',
'required' => true,
'class' => 'medium',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gragrid' ),
esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gragrid' )
),
),
array(
'name' => 'sendgrid_list',
'label' => esc_html__( 'SendGrid Contact List', 'gragrid' ),
'type' => 'sendgrid_list',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'SendGrid Contact List', 'gragrid' ),
esc_html__( 'Select the contact list you would like to add emails s to.', 'gragrid' )
),
),
array(
'name' => 'mappedFields',
'label' => esc_html__( 'Map Fields', 'gragrid' ),
'type' => 'field_map',
'field_map' => $this->sengrid_field_map(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Map Fields', 'gragrid' ),
esc_html__( 'Associate the SendGrid fields to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'gragrid' )
),
),
$custom_field_map,
array(
'type' => 'feed_condition',
'name' => 'enabled',
'label' => esc_html__( 'Conditional logic', 'gragrid' ),
'checkbox_label' => esc_html__( 'Enable', 'gragrid' ),
'instructions' => esc_html__( 'Send this lead to SendGrid if', 'gragrid' ),
),
array( 'type' => 'save' ),
);
$settings = array(
array(
'title' => esc_html__( 'SendGrid Feed Settings', 'gragrid' ),
'fields' => array_filter( $fields ),
),
);
return $settings;
}
/**
* Define the markup for the sendgrid_list type field.
*
* @since 1.0.0
*
* @access public
* @param array $field The field properties.
* @param bool $echo Should the setting markup be echoed. Defaults to true.
* @return string
*/
public function settings_sendgrid_list( $field, $echo = true ) {
if ( ! $this->init_api() ) {
return;
}
$lists = $this->api->get_lists();
if ( is_wp_error( $lists ) ) {
$this->log_error( __METHOD__ . ': Could not retrieve the contact lists ' . $lists->get_error_message() );
return sprintf(
'<div class="notice notice-error inline" style="display: block !important;"><p>%s</p></div>',
esc_html__( 'Could not load the contact lists. Make sure you have a valid API key.', 'gragrid' )
);
}
if ( ! count( $lists['result'] ) > 0 ) {
$this->log_error( __METHOD__ . ': API retured empty set of lists.' );
printf( esc_html__( 'You don\'t have contact lists in your account. Please create one first and try again.', 'gragrid' ) );
return;
}
// Initialize select options.
$options = array(
array(
'label' => esc_html__( 'Select a SendGrid list', 'gragrid' ),
'value' => '',
),
);
foreach ( $lists['result'] as $list ) {
$options[] = array(
'label' => esc_html( $list['name'] . ' (' . $list['contact_count'] . ')' ),
'value' => esc_attr( $list['id'] ),
);
}
// Add select field properties.
$field['type'] = 'select';
$field['choices'] = $options;
// Generate select field.
$html = $this->settings_select( $field, false );
if ( $echo ) {
echo $html; // phpcs:ignore: XSS ok.
}
return $html;
}
/**
* Return an array of SendGrid list/audience fields which can be mapped to the Form fields/entry meta.
*
* @since 1.0.0
* @since 2.0.0 Added address fields.
* @since 2.1.0 Added more default SendGrid fields.
*
* @access public
* @return array
*/
public function sengrid_field_map() {
return array(
'email' => array(
'name' => 'email',
'label' => esc_html__( 'Email Address', 'gragrid' ),
'required' => true,
'field_type' => array( 'email', 'hidden' ),
),
'first_name' => array(
'name' => 'first_name',
'label' => esc_html__( 'First Name', 'gragrid' ),
'required' => false,
'field_type' => array( 'name', 'text', 'hidden' ),
),
'last_name' => array(
'name' => 'last_name',
'label' => esc_html__( 'Last Name', 'gragrid' ),
'required' => false,
'field_type' => array( 'name', 'text', 'hidden' ),
),
'phone_number' => array(
'name' => 'phone_number',
'label' => esc_html__( 'Phone Number', 'gragrid' ),
'required' => false,
'field_type' => array( 'phone', 'text', 'hidden' ),
),
'address_line_1' => array(
'name' => 'address_line_1',
'label' => esc_html__( 'Address Line 1', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'address_line_2' => array(
'name' => 'address_line_2',
'label' => esc_html__( 'Address Line 2', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'city' => array(
'name' => 'city',
'label' => esc_html__( 'City', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'state_province_region' => array(
'name' => 'state_province_region',
'label' => esc_html__( 'State/Province/Region', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'postal_code' => array(
'name' => 'postal_code',
'label' => esc_html__( 'Postal Code', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'country' => array(
'name' => 'country',
'label' => esc_html__( 'Country', 'gragrid' ),
'required' => false,
'field_type' => array( 'address', 'text', 'hidden' ),
),
'whatsapp' => array(
'name' => 'whatsapp',
'label' => esc_html__( 'WhatsApp', 'gragrid' ),
'required' => false,
'field_type' => array( 'phone', 'text', 'hidden' ),
),
'line' => array(
'name' => 'line',
'label' => esc_html__( 'Line', 'gragrid' ),
'required' => false,
'field_type' => array( 'phone', 'text', 'hidden' ),
),
'facebook' => array(
'name' => 'facebook',
'label' => esc_html__( 'Facebook', 'gragrid' ),
'required' => false,
'field_type' => array( 'website', 'text', 'hidden' ),
),
'unique_name' => array(
'name' => 'unique_name',
'label' => esc_html__( 'Unique Name', 'gragrid' ),
'required' => false,
'field_type' => array( 'name', 'text', 'hidden' ),
),
);
}
/**
* Map custom SendGrid fields
*
* @since 2.1.0
*
* @return array
*/
public function sengrid_custom_fields_map() {
if ( ! $this->init_api() ) {
return array();
}
$fields = array();
$custom_fields = (array) rgar( $this->api->get_custom_fields(), 'custom_fields' );
$custom_fields = array_filter( $custom_fields );
foreach ( $custom_fields as $custom_field ) {
$fields[ $custom_field['id'] ] = array(
'name' => $custom_field['id'],
'label' => $this->snake_to_title( $custom_field['name'] ),
'required' => false,
);
}
return $fields;
}
/**
* Prevent feeds being listed or created if the API key isn't valid.
*
* @since 1.0.0
*
* @access public
* @return bool
*/
public function can_create_feed() {
return $this->init_api();
}
/**
* Allow the feed to be duplicated.
*
* @since 1.0.0
*
* @param array|int $id The ID of the feed to be duplicated or the feed object when duplicating a form.
* @return bool
*/
public function can_duplicate_feed( $id ) {
return true;
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @since 1.0.0
*
* @access public
* @return array
*/
public function feed_list_columns() {
return array(
'feedName' => esc_html__( 'Name', 'gragrid' ),
'sendgrid_list' => esc_html__( 'SendGrid List', 'gragrid' ),
);
}
/**
* Get the name of the SendGrid list for the feed table view.
*
* @since 1.0.0
*
* @param array $feed The feed being included in the feed list.
* @return string
*/
public function get_column_value_sendgrid_list( $feed ) {
if ( ! $this->init_api() ) {
return rgars( $feed, 'meta/sendgrid_list' );
}
$list = $this->api->get_list( rgars( $feed, 'meta/sendgrid_list' ) );
if ( is_wp_error( $list ) ) {
$this->log_error( __METHOD__ . ': Could not retrieve the contact list: ' . $list->get_error_message() );
return rgars( $feed, 'meta/sendgrid_list' );
}
return $list['name'];
}
// # FEED PROCESSING -----------------------------------------------------------------------------------------------
/**
* Process the feed e.g. subscribe the user to a list.
*
* @since 1.0.0
* @since 2.1.0 Added custom fields to the request data.
*
* @param array $feed The feed object to be processed.
* @param array $entry The entry object currently being processed.
* @param array $form The form object currently being processed.
* @return bool|void
*/
public function process_feed( $feed, $entry, $form ) {
if ( ! $this->init_api() ) {
$this->add_feed_error( esc_html__( 'Unable to process feed because API could not be initialized.', 'gragrid' ), $feed, $entry, $form );
return $entry;
}
$contact = array();
// Map reserved/standard/default fields.
$fields = $this->get_field_map_fields( $feed, 'mappedFields' );
foreach ( $fields as $name => $field_id ) {
$contact[ $name ] = $this->get_field_value( $form, $entry, $field_id );
}
// Map custom fields.
$custom_fields = $this->get_field_map_fields( $feed, 'mappedCustomFields' );
foreach ( $custom_fields as $name => $field_id ) {
$contact['custom_fields'][ $name ] = $this->get_field_value( $form, $entry, $field_id );
}
$contact_params = array(
'list_ids' => array( rgars( $feed, 'meta/sendgrid_list' ) ),
'contacts' => array( $contact ),
);
/**
* Contact parameters
*
* @since 2.1.0
*
* @param array $contact_params Contact parameters.
* @param array $entry The entry object currently being processed.
* @param array $form The form object currently being processed.
*/
$contact_params = apply_filters( 'gragrid_contact_params', $contact_params, $entry, $form );
try {
// Save the contacts.
$response = $this->api->add_contacts( $contact_params );
if ( is_wp_error( $response ) ) {
// Translators: %s error message.
$this->add_feed_error( sprintf( esc_html__( 'Unable to add the contact: %s', 'gragrid' ), $response->get_error_message() ), $feed, $entry, $form );
return $entry;
}
$this->add_note(
$entry['id'],
sprintf(
// Translators: %s SendGrid list ID.
esc_html__( 'Gragrid successfully passed the lead details to the SendGrid list #%s.', 'gragrid' ),
rgars( $feed, 'meta/sendgrid_list' )
),
'success'
);
return $entry;
} catch ( Exception $e ) {
// Translators: %s error message.
$this->add_feed_error( sprintf( esc_html__( 'Unable to add recipient to list: %s', 'gragrid' ), $e->getMessage() ), $feed, $entry, $form );
return $entry;
}
}
// # HELPERS ----------------------------------------------------
/**
* Initializes SendGrid API if credentials are valid.
*
* @since 1.0.0
*
* @uses GFAddOn::get_plugin_setting()
* @uses GFAddOn::log_debug()
* @uses GFAddOn::log_error()
* @uses Gragrid_API::valid_key()
*
* @access public
* @param string $api_key SendGrid API key.
* @return bool|null
*/
public function init_api( $api_key = null ) {
// If the API is already initialized, return true.
if ( ! is_null( $this->api ) ) {
return true;
}
if ( rgblank( $api_key ) ) {
$api_key = $this->get_plugin_setting( 'api_key' );
}
// If the API key is blank, do not run a validation check.
if ( rgblank( $api_key ) ) {
return null;
}
$this->log_debug( __METHOD__ . '(): Validating API key.' );
try {
$this->api = new Gragrid_API( $api_key );
if ( $this->api->valid_key() ) {
$this->log_debug( __METHOD__ . '(): SendGrid successfully authenticated.' );
return true;
} else {
$this->log_debug( __METHOD__ . '(): Unable to authenticate with SendGrid.' );
return false;
}
} catch ( Exception $e ) {
$this->log_error( __METHOD__ . '(): Unable to authenticate with SendGrid; ' . $e->getMessage() );
return false;
}
}
}