-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-gravityforms-coda-addon.php
689 lines (554 loc) · 17.4 KB
/
class-gravityforms-coda-addon.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
682
683
684
685
686
687
688
689
<?php
// don't load directly
if ( ! defined( 'ABSPATH' ) ) exit;
GFForms::include_feed_addon_framework();
class GravityFormsCodaFeedAddOn extends GFFeedAddOn
{
protected $_version = GF_CODA_VERSION;
protected $_min_gravityforms_version = '2.4.8.5';
protected $_slug = GF_CODA_SLUG;
protected $_path = GF_CODA_PATH;
protected $_full_path = __FILE__;
protected $_title = 'GravityForms Coda.io AddOn';
protected $_short_title = 'Coda.io';
private static $_instance = null;
public $api = null;
public $_async_feed_processing = true;
/**
* Defines the capabilities needed for the Coda.io Add-On
*
* @since 3.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_coda', 'gravityforms_coda_uninstall' );
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 3.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_coda';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 3.0
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_coda';
/**
* Defines the Coda list field tag name.
*
* @since 3.7
* @access protected
* @var string $merge_var_name The Coda.io doc list field tag name;
*/
protected $merge_var_name = '';
protected $merge_fields = array();
/**
* Get an instance of this class.
*
* @return GravityFormsCodaFeedAddOn
*/
public static function get_instance()
{
if ( self::$_instance == null )
{
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Autoload the required libraries.
*
* @since 4.0
* @access public
*
* @uses GFAddOn::is_gravityforms_supported()
*/
public function pre_init() {
parent::pre_init();
if ( $this->is_gravityforms_supported() ) {
// Load the Coda.io API library.
if ( ! class_exists( 'CodaPHP' ) ) {
require_once ('vendor/autoload.php');
}
}
}
/**
* Plugin starting point. Handles hooks, loading of language files and PayPal delayed payment support.
*/
public function init()
{
parent::init();
add_action( 'gform_pre_submission', array($this, 'gform_pre_submission'));
add_filter( 'gform_confirmation', array($this, 'gform_confirmation') , 10, 4 );
}
/**
* Remove unneeded settings.
*
* @since 4.0
* @access public
*/
public function uninstall()
{
parent::uninstall();
GFCache::delete( 'coda_plugin_settings' );
delete_option( 'gf_coda_settings' );
delete_option( 'gf_coda_version' );
}
/**
* Register needed styles.
*
* @since 4.0
* @access public
*
* @return array
*/
public function styles()
{
$styles = array(
array(
'handle' => $this->_slug . '_form_settings',
'src' => $this->get_base_url() . '/css/style.css',
'version' => $this->_version,
'enqueue' => array(
array( 'admin_page' => array( 'form_settings' ) ),
),
),
);
return array_merge( parent::styles(), $styles );
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the add-on settings tab.
*
* @since 3.0
* @access public
*
* @return array
*/
public function plugin_settings_fields()
{
return array(
array(
'description' => '<p>' .
sprintf(
esc_html__( 'Coda.io is a new type of powerful document. Use Gravity Forms to collect any form entry information and automatically add it to your Coda.io document table. If you don\'t have a Coda.io account, you can %1$ssign up for one here.%2$s', 'gravityforms-coda' ),
'<a href="http://www.coda.io/" target="_blank">', '</a>'
)
. '</p>',
'fields' => array(
array(
'name' => 'apiKey',
'label' => esc_html__( 'Coda.io API Key', 'gravityforms-coda' ),
'type' => 'text',
'class' => 'medium',
'feedback_callback' => array( $this, 'initialize_api' ),
),
),
),
);
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Configures the settings which should be rendered on the feed edit page.
*
* @since 3.0
* @access public
*
* @return array
*/
public function feed_settings_fields()
{
$settings = array(
array(
'title' => esc_html__( 'Coda.io Feed Settings', 'gravityforms-coda' ),
'fields' => array(
array(
'name' => 'feedName',
'label' => esc_html__( 'Name', 'gravityforms-coda' ),
'type' => 'text',
'required' => true,
'class' => 'medium',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Name', 'gravityforms-coda' ),
esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gravityforms-coda' )
),
),
array(
'name' => 'codaDoc',
'label' => esc_html__( 'Coda Docs', 'gravityforms-coda' ),
'type' => 'coda_doc',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Coda Docs', 'gravityforms-coda' ),
esc_html__( 'Select the Coda Doc you would like to add to.', 'gravityforms-coda' )
),
),
),
),
array(
'dependency' => 'codaDoc',
'fields' => array(
array(
'name' => 'codaTable',
'label' => esc_html__( 'Doc Tables', 'gravityforms-coda' ),
'type' => 'coda_doc_tables',
'required' => true,
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Coda Doc Tables', 'gravityforms-coda' ),
esc_html__( 'Select the Coda Doc Table you would like to add to.', 'gravityforms-coda' )
),
),
),
),
array(
'title' => esc_html__( 'Doc Table Columns', 'gravityforms-coda' ),
'dependency' => 'codaTable',
'fields' => array(
array(
'name' => 'mappedFields',
'label' => esc_html__( 'Map Fields', 'gravityforms-coda' ),
'type' => 'field_map',
'field_map' => $this->merge_vars_field_map(),
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Map Fields', 'gravityforms-coda' ),
esc_html__( 'Associate your Coda Doc Table columns to the appropriate Gravity Form fields by selecting the appropriate form field from the list.', 'gravityforms-coda' )
),
),
array(
'name' => 'optinCondition',
'label' => esc_html__( 'Conditional Logic', 'gravityforms-coda' ),
'type' => 'feed_condition',
'tooltip' => sprintf(
'<h6>%s</h6>%s',
esc_html__( 'Conditional Logic', 'gravityforms-coda' ),
esc_html__( 'When conditional logic is enabled, form submissions will only be exported to Coda when the conditions are met. When disabled all form submissions will be exported.', 'gravityforms-coda' )
),
),
array( 'type' => 'save' ),
)
),
);
// Get currently selected list.
$doc = $this->get_setting( 'codaDoc' );
$table = $this->get_setting( 'codaTable' );
return $settings;
}
/**
* Return an array of Coda.io document table fields which can be mapped to the Form fields/entry meta.
*
* @since 3.0
* @access public
*
* @return array
*/
public function merge_vars_field_map()
{
// Initialize field map array.
$field_map = array();
$doc = $this->get_setting( 'codaDoc' );
$table = $this->get_setting( 'codaTable' );
// If unable to initialize API, return field map.
if ( ! $this->initialize_api() ) {
return $field_map;
}
if(empty($doc) || empty($table)){
return $field_map;
}
try {
// Get lists.
$cols = $this->api->listColumns($doc, $table);
} catch ( Exception $e ) {
// Log that contact lists could not be obtained.
$this->log_error( __METHOD__ . '(): Could not retrieve Doc; ' . $e->getMessage() );
// Display error message.
printf( esc_html__( 'Could not load Doc. %sError: %s', 'gravityforms-coda' ), '<br/>', $e->getMessage() );
return;
}
// If no lists were found, display error message.
if ( 0 === count($cols['items']) ) {
// Log that no lists were found.
$this->log_error( __METHOD__ . '(): Could not load Coda.io Doc Table Columns; no columns found.' );
// Display error message.
printf( esc_html__( 'Could not load Coda.io Doc Table Columns. %sError: %s', 'gravityforms-coda' ), '<br/>', esc_html__( 'No Doc Table Columns found.', 'gravityforms-coda' ) );
return;
}
$this->log_debug( __METHOD__ . '(): Number of Docs: ' . count( $cols['items'] ) );
// $options = array();
foreach ( $cols['items'] as $col ) {
$is_cal = isset($col['calculated']) ? $col['calculated'] : false;
$is_displayed = isset($col['display']) ? $col['display'] : true;
$is_creator = $col['name']=='Creator' ? true : false;
$field_type = null;
if( 'email'===strtolower($col['name']) ){
$field_type = array( 'email', 'hidden' );
}
if(!$is_cal && !$is_creator){
$field_map[ $col['id'] ] = array(
'name' => $col['id'],
'label' => $col['name'],
'required' => false,
'field_type' => $field_type,
);
}
}
return $field_map;
}
/**
* Define the markup for the coda_doc type field.
*
* @since 3.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_coda_doc( $field, $echo = true )
{
// Initialize HTML string.
$html = '';
// If API is not initialized, return.
if ( ! $this->initialize_api() ) {
return $html;
}
try {
// Get lists.
$docs = $this->api->listDocs( );
} catch ( Exception $e ) {
// Log that contact lists could not be obtained.
$this->log_error( __METHOD__ . '(): Could not retrieve Doc; ' . $e->getMessage() );
// Display error message.
printf( esc_html__( 'Could not load Doc. %sError: %s', 'gravityforms-coda' ), '<br/>', $e->getMessage() );
return;
}
// If no lists were found, display error message.
if ( 0 === count($docs['items']) ) {
// Log that no lists were found.
$this->log_error( __METHOD__ . '(): Could not load Coda.io Docs; no lists found.' );
// Display error message.
printf( esc_html__( 'Could not load Coda.io Docs. %sError: %s', 'gravityforms-coda' ), '<br/>', esc_html__( 'No Docs found.', 'gravityforms-coda' ) );
return;
}
// Log number of lists retrieved.
$this->log_debug( __METHOD__ . '(): Number of Docs: ' . count( $docs['items'] ) );
// Initialize select options.
$options = array(
array(
'label' => esc_html__( 'Select a Document', 'gravityforms-coda' ),
'value' => '',
),
);
// Loop through Coda Docs.
foreach ( $docs['items'] as $doc ) {
// Add list to select options.
$options[] = array(
'label' => esc_html( $doc['name'] ),
'value' => esc_attr( $doc['id'] ),
);
}
// Add select field properties.
$field['type'] = 'select';
$field['choices'] = $options;
$field['onchange'] = 'jQuery(this).parents("form").submit();';
// Generate select field.
$html = $this->settings_select( $field, false );
if ( $echo ) {
echo $html;
}
return $html;
}
public function settings_coda_doc_tables( $field, $echo = true )
{
// Initialize HTML string.
$html = '';
$doc_id = $this->get_setting( 'codaDoc' );
// If API is not initialized, return.
if ( ! $this->initialize_api() ) {
return $html;
}
if(empty($doc_id)){
return $html;
}
try {
// Get lists.
$tables = $this->api->listTables($doc_id);
} catch ( Exception $e ) {
// Log that contact lists could not be obtained.
$this->log_error( __METHOD__ . '(): Could not retrieve Coda Doc Tables; ' . $e->getMessage() );
// Display error message.
printf( esc_html__( 'Could not load Coda Doc Tables. %sError: %s', 'gravityforms-coda' ), '<br/>', $e->getMessage() );
return;
}
// If no tables were found, display error message.
if ( 0 === count($tables['items']) ) {
// Log that no lists were found.
$this->log_error( __METHOD__ . '(): Could not load Coda.io Doc Tables; no tables found.' );
// Display error message.
printf( esc_html__( 'Could not load Coda.io Doc Tables. %sError: %s', 'gravityforms-coda' ), '<br/>', esc_html__( 'No Doc Tables found.', 'gravityforms-coda' ) );
return;
}
// Log number of lists retrieved.
$this->log_debug( __METHOD__ . '(): Number of Doc Tables: ' . count( $tables['items'] ) );
// Initialize select options.
$options = array(
array(
'label' => esc_html__( 'Select a Table', 'gravityforms-coda' ),
'value' => '',
),
);
// Loop through Coda Docs.
foreach ( $tables['items'] as $table ) {
// Add list to select options.
$options[] = array(
'label' => esc_html( $table['name'] ),
'value' => esc_attr( $table['id'] ),
);
}
// Add select field properties.
$field['type'] = 'select';
$field['choices'] = $options;
$field['onchange'] = 'jQuery(this).parents("form").submit();';
// Generate select field.
$html = $this->settings_select( $field, false );
if ( $echo ) {
echo $html;
}
return $html;
}
// # FEED PROCESSING -----------------------------------------------------------------------------------------------
/**
* Process the feed e.g. subscribe the user to a list.
*
* @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 )
{
$doc = $feed['meta']['codaDoc'];
$table = $feed['meta']['codaTable'];
// Log that we are processing feed.
$this->log_debug( __METHOD__ . '(): Processing feed.' );
// If unable to initialize API, log error and return.
if ( ! $this->initialize_api() ) {
$this->add_feed_error( esc_html__( 'Unable to process feed because API could not be initialized.', 'gravityforms-coda' ), $feed, $entry, $form );
return $entry;
}
// Get field map values.
$field_map = $this->get_field_map_fields( $feed, 'mappedFields' );
// Loop through field map.
$row = array();
foreach ( $field_map as $name => $field_id ) {
// Get field object.
$field = GFFormsModel::get_field( $form, $field_id );
// Get field value.
$field_value = $this->get_field_value( $form, $entry, $field_id );
$row[$name] = $field_value;
}
try {
$result = $this->api->insertRows($doc, $table, $row); // Insert/updates a row in a table
} catch ( Exception $e ) {
// Log that contact lists could not be obtained.
$this->log_error( __METHOD__ . '(): Could not insert entry in to Coda Doc Table; ' . $e->getMessage() );
// Display error message.
printf( esc_html__( 'Could not insert entry in to Coda Doc Table' ), '<br/>', $e->getMessage() );
return;
}
}
/***********************************************/
/************ Gravity Forms Hooks **************/
/***********************************************/
public function gform_pre_submission($form)
{
return $form;
}
public function gform_confirmation( $confirmation, $form, $entry, $ajax )
{
return $confirmation;
}
// # ADMIN FUNCTIONS -----------------------------------------------------------------------------------------------
/**
* Creates a custom page for this add-on.
*/
// public function plugin_page()
// {
// echo 'This page appears in the Forms menu';
// }
public function initialize_api( $api_key = null )
{
// If API is already initialized, return true.
if ( ! is_null( $this->api ) ) {
return true;
}
// Get the API key.
if ( rgblank( $api_key ) ) {
$api_key = $this->get_plugin_setting( 'apiKey' );
}
// If the API key is blank, do not run a validation check.
if ( rgblank( $api_key ) ) {
return null;
}
$coda = new CodaPHP\CodaPHP($api_key);
try {
// Retrieve account information.
$coda->listDocs();
// Assign API library to class.
$this->api = $coda;
// Log that authentication test passed.
$this->log_debug( __METHOD__ . '(): Coda.io successfully authenticated.' );
return true;
} catch ( Exception $e ) {
// Log that authentication test failed.
$this->log_error( __METHOD__ . '(): Unable to authenticate with Coda; '. $e->getMessage() );
return false;
}
}
/**
* Configures which columns should be displayed on the feed list page.
*
* @return array
*/
public function feed_list_columns()
{
return array(
'feedName' => esc_html__( 'Name', 'gf-coda-feed-addon' ),
//'mytextbox' => esc_html__( 'My Textbox', 'gf-coda-feed-addon' ),
);
}
/**
* Format the value to be displayed in the mytextbox column.
*
* @param array $feed The feed being included in the feed list.
*
* @return string
*/
public function get_column_value_mytextbox( $feed )
{
return '<b>' . rgars( $feed, 'meta/mytextbox' ) . '</b>';
}
/**
* Prevent feeds being listed or created if an api key isn't valid.
*
* @return bool
*/
public function can_create_feed()
{
// Get the plugin settings.
$settings = $this->get_plugin_settings();
// Access a specific setting e.g. an api key
$key = rgar( $settings, 'apiKey' );
return true;
}
}