-
Notifications
You must be signed in to change notification settings - Fork 9
/
class-gf-slack.php
1780 lines (1413 loc) · 46 KB
/
class-gf-slack.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
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
if ( ! class_exists( 'GFForms') ) {
die();
}
GFForms::include_feed_addon_framework();
/**
* Gravity Forms Slack Add-On.
*
* @since 1.0
* @package GravityForms
* @author Rocketgenius
* @copyright Copyright (c) 2016, Rocketgenius
*/
class GFSlack extends GFFeedAddOn {
/**
* Contains an instance of this class, if available.
*
* @since 1.0
* @access private
* @var object $_instance If available, contains an instance of this class.
*/
private static $_instance = null;
/**
* Defines the version of the Slack Add-On.
*
* @since 1.0
* @access protected
* @var string $_version Contains the version, defined from slack.php
*/
protected $_version = GF_SLACK_VERSION;
/**
* Defines the minimum Gravity Forms version required.
*
* @since 1.0
* @access protected
* @var string $_min_gravityforms_version The minimum version required.
*/
protected $_min_gravityforms_version = '1.9.14.26';
/**
* Defines the plugin slug.
*
* @since 1.0
* @access protected
* @var string $_slug The slug used for this plugin.
*/
protected $_slug = 'gravityformsslack';
/**
* Defines the main plugin file.
*
* @since 1.0
* @access protected
* @var string $_path The path to the main plugin file, relative to the plugins folder.
*/
protected $_path = 'gravityformsslack/slack.php';
/**
* Defines the full path to this class file.
*
* @since 1.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
* @access protected
* @var string The URL of the Add-On.
*/
protected $_url = 'http://www.gravityforms.com';
/**
* Defines the title of this Add-On.
*
* @since 1.0
* @access protected
* @var string $_title The title of the Add-On.
*/
protected $_title = 'Gravity Forms Slack Add-On';
/**
* Defines the short title of the Add-On.
*
* @since 1.0
* @access protected
* @var string $_short_title The short title.
*/
protected $_short_title = 'Slack';
/**
* Defines if Add-On should use Gravity Forms servers for update data.
*
* @since 1.0
* @access protected
* @var bool
*/
protected $_enable_rg_autoupgrade = true;
/**
* Defines the capability needed to access the Add-On settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_settings_page The capability needed to access the Add-On settings page.
*/
protected $_capabilities_settings_page = 'gravityforms_slack';
/**
* Defines the capability needed to access the Add-On form settings page.
*
* @since 1.0
* @access protected
* @var string $_capabilities_form_settings The capability needed to access the Add-On form settings page.
*/
protected $_capabilities_form_settings = 'gravityforms_slack';
/**
* Defines the capability needed to uninstall the Add-On.
*
* @since 1.0
* @access protected
* @var string $_capabilities_uninstall The capability needed to uninstall the Add-On.
*/
protected $_capabilities_uninstall = 'gravityforms_slack_uninstall';
/**
* Defines the capabilities needed for the Post Creation Add-On
*
* @since 1.0
* @access protected
* @var array $_capabilities The capabilities needed for the Add-On
*/
protected $_capabilities = array( 'gravityforms_slack', 'gravityforms_slack_uninstall' );
/**
* Contains an instance of the Slack API library, if available.
*
* @since 1.0
* @access protected
* @var object $api If available, contains an instance of the Slack API library.
*/
protected $api = null;
/**
* Get instance of this class.
*
* @access public
* @static
*
* @return GFSlack
*/
public static function get_instance() {
if ( null === self::$_instance ) {
self::$_instance = new self;
}
return self::$_instance;
}
/**
* Plugin starting point. Adds PayPal delayed payment support.
*
* @since 1.3
* @access public
*/
public function init() {
parent::init();
$this->add_delayed_payment_support(
array(
'option_label' => esc_html__( 'Send message to Slack only when payment is received.', 'gravityformsslack' ),
)
);
}
/**
* Add AJAX callbacks.
*
* @since 1.7
* @access public
*/
public function init_ajax() {
parent::init_ajax();
// Add AJAX callback for de-authorizing with Dropbox.
add_action( 'wp_ajax_gfslack_deauthorize', array( $this, 'ajax_deauthorize' ) );
}
/**
* Enqueue admin scripts.
*
* @since 1.7
* @access public
*
* @return array
*/
public function scripts() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$scripts = array(
array(
'handle' => 'gform_slack_pluginsettings',
'deps' => array( 'jquery' ),
'src' => $this->get_base_url() . "/js/plugin_settings{$min}.js",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings' ),
'tab' => $this->_slug,
),
),
'strings' => array(
'disconnect' => wp_strip_all_tags( __( 'Are you sure you want to disconnect from Slack?', 'gravityformsslack' ) ),
'nonce_deauthorize' => wp_create_nonce( 'gfslack_deauthorize' ),
'pluginSettingsURL' => admin_url( 'admin.php?page=gf_settings&subview=' . $this->get_slug() ),
),
),
);
return array_merge( parent::scripts(), $scripts );
}
/**
* Enqueue needed stylesheets.
*
* @since 1.7
* @access public
*
* @return array
*/
public function styles() {
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
$styles = array(
array(
'handle' => 'gform_slack_pluginsettings',
'src' => $this->get_base_url() . "/css/plugin_settings{$min}.css",
'version' => $this->_version,
'enqueue' => array(
array(
'admin_page' => array( 'plugin_settings' ),
'tab' => $this->_slug,
),
),
),
);
return array_merge( parent::styles(), $styles );
}
/**
* Return the plugin's icon for the plugin/form settings menu.
*
* @since 1.10
*
* @return string
*/
public function get_menu_icon() {
return file_get_contents( $this->get_base_path() . '/images/menu-icon.svg' );
}
// # PLUGIN SETTINGS -----------------------------------------------------------------------------------------------
/**
* Maybe save access token.
*
* @since 1.7
* @access public
*/
public function plugin_settings_page() {
// If access token is provided, save it.
if ( rgpost( 'access_token' ) ) {
// If state does not match, do not save.
if ( ! wp_verify_nonce( rgpost( 'state' ), $this->get_authentication_state_action() ) ) {
// Add error message.
GFCommon::add_error_message( esc_html__( 'Unable to authenticate with Slack due to mismatched state.', 'gravityformsslack' ) );
return parent::plugin_settings_page();
}
// Get current plugin settings.
$settings = $this->get_plugin_settings();
// Add access token to plugin settings.
$settings['auth_token'] = rgpost( 'access_token' );
// Get team name.
$settings['team_name'] = $this->get_team_name( $settings['auth_token'] );
// Save plugin settings.
$this->update_plugin_settings( $settings );
}
// If error is provided, display message.
if ( rgpost( 'auth_error' ) || rgpost( 'error' ) ) {
// Add error message.
GFCommon::add_error_message( esc_html__( 'Unable to authenticate with Slack.', 'gravityformsslack' ) );
}
return parent::plugin_settings_page();
}
/**
* Setup plugin settings fields.
*
* @since 1.0
* @access public
*
* @return array
*/
public function plugin_settings_fields() {
return array(
array(
'description' => sprintf(
'<p>%s</p>',
sprintf(
esc_html__( 'Slack provides simple group chat for your team. Use Gravity Forms to alert your Slack channels of a new form submission. If you don\'t have a Slack account, you can %1$s sign up for one here.%2$s', 'gravityformsslack' ),
'<a href="https://www.slack.com/" target="_blank">', '</a>'
)
),
'fields' => array(
array(
'name' => 'auth_token',
'type' => 'auth_token',
'feedback_callback' => array( $this, 'initialize_api' ),
),
array(
'name' => 'team_name',
'type' => 'hidden',
'readonly' => true,
'save_callback' => array( $this, 'update_team_name' ),
),
),
),
);
}
/**
* Update team name when saving legacy token.
*
* @since 1.8
* @access public
*
* @param array $field Field settings.
* @param string $field_value Field value.
*
* @uses GFAddOn::get_posted_settings()
* @uses GFSlack::get_team_name()
*
* @return null|string
*/
public function update_team_name( $field, $field_value ) {
// Get posted settings.
$settings = $this->get_posted_settings();
// If auth token was not provided, return.
if ( ! rgar( $settings, 'auth_token' ) ) {
return $field_value;
}
// Get team name.
$team_name = $this->get_team_name( $settings['auth_token'] );
// Update posted setting.
$_gaddon_posted_settings[ $field['name'] ] = $team_name;
return $team_name;
}
/**
* Create Generate Auth Token settings field.
*
* @since 1.7
* @access public
*
* @param array $field Field settings.
* @param bool $echo Display field. Defaults to true.
*
* @uses GFAddOn::settings_text()
* @uses GFSlack::initialize_api()
*
* @return string
*/
public function settings_auth_token( $field, $echo = true ) {
// Initialize return HTML.
$html = '';
// If Slack is authenticated, display de-authorize button.
if ( $this->initialize_api() ) {
// Get account information.
$account = $this->api->auth_test();
$html .= '<p>' . esc_html__( 'Signed into Slack team: ', 'gravityformsslack' );
$html .= sprintf( '%s (%s)', esc_html( $account['team'] ), esc_html( $account['user'] ) );
$html .= '</p>';
$html .= sprintf(
' <a href="#" class="button primary" id="gform_slack_deauth_button">%1$s</a>',
esc_html__( 'Disconnect Slack', 'gravityformsslack' )
);
} else {
// Prepare authorization URL.
$license_key = GFCommon::get_key();
$settings_url = urlencode( admin_url( 'admin.php?page=gf_settings&subview=' . $this->_slug ) );
$auth_url = add_query_arg( array(
'redirect_to' => $settings_url,
'license' => $license_key,
'state' => wp_create_nonce( $this->get_authentication_state_action() ),
), $this->get_gravity_api_url( '/auth/slack' ) );
$html .= sprintf(
'<span id="gform_slack_auth_container" style="%3$s"><a href="%2$s" class="button primary" id="gform_slack_auth_button">%1$s</a></span>',
esc_html__( 'Connect to Slack', 'gravityformsslack' ),
$auth_url,
$this->get_setting( 'auth_token' ) ? 'display:none;' : 'display:block'
);
}
if ( $echo ) {
echo $html;
}
return $html;
}
/**
* Deauthorize with Slack.
*
* @since 1.7
* @access public
*
* @uses GFAddOn::get_plugin_settings()
* @uses GFAddOn::log_debug()
* @uses GFAddOn::log_error()
* @uses GFAddOn::update_plugin_settings()
* @uses GFSlack::initialize_api()
* @uses GF_Slack_API::auth_revoke()
*/
public function ajax_deauthorize() {
// Verify nonce.
if ( wp_verify_nonce( rgget( 'nonce' ), 'gfslack_deauthorize' ) === false ) {
wp_send_json_error( array( 'message' => esc_html__( 'Access denied.', 'gravityformsslack' ) ) );
}
// If user is not authorized, exit.
if ( ! GFCommon::current_user_can_any( $this->_capabilities_settings_page ) ) {
wp_send_json_error( array( 'message' => esc_html__( 'Access denied.', 'gravityformsslack' ) ) );
}
// Initialize Slack API.
$this->initialize_api();
try {
// Revoke access token.
$revoke = $this->api->auth_revoke();
// Send JSON based on revoke response.
if ( rgar( $revoke, 'ok' ) ) {
// Log that we revoked the access token.
$this->log_debug( __METHOD__ . '(): Access token revoked.' );
// Save settings.
$this->update_plugin_settings( array() );
// Return success response.
wp_send_json_success();
} else {
// Log that we could not revoke the access token.
$this->log_error( __METHOD__ . '(): Unable to revoke access token; ' . rgar( $revoke, 'error' ) );
// Return error response.
wp_send_json_error( array( 'message' => esc_html__( 'Unable to de-authorize with Slack.', 'gravityformsslack' ) ) );
}
} catch ( Exception $e ) {
// Log that we could not revoke the access token.
$this->log_error( __METHOD__ . '(): Unable to revoke access token; ' . $e->getMessage() );
// Return error response.
wp_send_json_error( array( 'message' => $e->getMessage() ) );
}
}
// # FEED SETTINGS -------------------------------------------------------------------------------------------------
/**
* Setup fields for feed settings.
*
* @since 1.0
* @access public
*
* @uses GFFeedAddOn::get_default_feed_name()
* @uses GFSlack::channels_for_feed_setting()
* @uses GFSlack::fileupload_fields_for_feed_setting()
* @uses GFSlack::groups_for_feed_setting()
* @uses GFSlack::send_to_for_feed_setting()
* @uses GFSlack::tooltip_for_feed_setting()
* @uses GFSlack::users_for_feed_setting()
*
* @return array $settings
*/
public function feed_settings_fields() {
$settings = array(
array(
'fields' => array(
array(
'name' => 'feed_name',
'label' => esc_html__( 'Name', 'gravityformsslack' ),
'type' => 'text',
'class' => 'medium',
'required' => true,
'tooltip' => $this->tooltip_for_feed_setting( 'feed_name' ),
'default_value' => $this->get_default_feed_name(),
),
array(
'name' => 'action',
'label' => esc_html__( 'Action', 'gravityformsslack' ),
'type' => 'radio',
'required' => true,
'onchange' => "jQuery(this).parents('form').submit();",
'choices' => $this->action_for_feed_setting(),
),
array(
'name' => 'email',
'label' => esc_html__( 'Email Address', 'gravityformsslack' ),
'type' => 'field_select',
'required' => true,
'tooltip' => $this->tooltip_for_feed_setting( 'email' ),
'dependency' => array( 'field' => 'action', 'values' => array( 'invite' ) ),
'args' => array( 'input_types' => array( 'email' ) ),
),
array(
'name' => 'first_name',
'label' => esc_html__( 'First Name', 'gravityformsslack' ),
'type' => 'field_select',
'dependency' => array( 'field' => 'action', 'values' => array( 'invite' ) ),
),
array(
'name' => 'last_name',
'label' => esc_html__( 'Last Name', 'gravityformsslack' ),
'type' => 'field_select',
'dependency' => array( 'field' => 'action', 'values' => array( 'invite' ) ),
),
array(
'name' => 'channels[]',
'label' => esc_html__( 'Slack Channels', 'gravityformsslack' ),
'type' => 'select',
'class' => 'medium',
'choices' => $this->channels_for_feed_setting( false ),
'multiple' => true,
'dependency' => array( 'field' => 'action', 'values' => array( 'invite' ) ),
),
array(
'name' => 'send_to',
'label' => esc_html__( 'Send To', 'gravityformsslack' ),
'type' => 'radio',
'required' => true,
'onchange' => "jQuery(this).parents('form').submit();",
'choices' => $this->send_to_for_feed_setting(),
'tooltip' => $this->tooltip_for_feed_setting( 'send_to' ),
'dependency' => array( 'field' => 'action', 'values' => array( 'message' ) ),
),
array(
'name' => 'channel',
'label' => esc_html__( 'Slack Channel', 'gravityformsslack' ),
'type' => 'select',
'required' => true,
'choices' => $this->channels_for_feed_setting(),
'tooltip' => $this->tooltip_for_feed_setting( 'channel' ),
'dependency' => array( 'field' => 'send_to', 'values' => array( 'channel' ) ),
),
array(
'name' => 'group',
'label' => esc_html__( 'Slack Private Group', 'gravityformsslack' ),
'type' => 'select',
'required' => true,
'choices' => $this->groups_for_feed_setting(),
'tooltip' => $this->tooltip_for_feed_setting( 'group' ),
'dependency' => array( 'field' => 'send_to', 'values' => array( 'group' ) ),
),
array(
'name' => 'user',
'label' => esc_html__( 'Slack User', 'gravityformsslack' ),
'type' => 'select',
'required' => true,
'choices' => $this->users_for_feed_setting(),
'tooltip' => $this->tooltip_for_feed_setting( 'user' ),
'dependency' => array( 'field' => 'send_to', 'values' => array( 'user' ) ),
),
array(
'name' => 'message',
'label' => esc_html__( 'Message', 'gravityformsslack' ),
'type' => 'textarea',
'required' => true,
'class' => 'medium merge-tag-support mt-position-right mt-hide_all_fields',
'tooltip' => $this->tooltip_for_feed_setting( 'message' ),
'value' => 'Entry #{entry_id} ({entry_url}) has been added.',
'dependency' => array( 'field' => 'send_to', 'values' => array( '_notempty_' ) ),
),
),
),
);
$fileupload_fields_for_feed = $this->fileupload_fields_for_feed_setting();
if ( ! empty( $fileupload_fields_for_feed ) ) {
$settings[0]['fields'][] = array(
'name' => 'attachments',
'type' => 'checkbox',
'label' => __( 'Image Attachments', 'gravityformsslack' ),
'choices' => $fileupload_fields_for_feed,
'tooltip' => $this->tooltip_for_feed_setting( 'attachments' ),
'dependency' => array( 'field' => 'send_to', 'values' => array( '_notempty_' ) ),
);
}
$settings[0]['fields'][] = array(
'name' => 'feed_condition',
'label' => esc_html__( 'Conditional Logic', 'gravityformsslack' ),
'type' => 'feed_condition',
'checkbox_label' => esc_html__( 'Enable', 'gravityformsslack' ),
'instructions' => esc_html__( 'Post to Slack if', 'gravityformsslack' ),
'tooltip' => $this->tooltip_for_feed_setting( 'feed_condition' ),
'dependency' => array( 'field' => 'action', 'values' => array( '_notempty_' ) ),
);
return $settings;
}
/**
* Get feed tooltip.
*
* @since 1.0
* @access public
*
* @param string $field Field name.
*
* @return string
*/
public function tooltip_for_feed_setting( $field ) {
// Setup tooltip array.
$tooltips = array();
// Feed Name.
$tooltips['feed_name'] = '<h6>' . esc_html__( 'Name', 'gravityformsslack' ) . '</h6>';
$tooltips['feed_name'] .= esc_html__( 'Enter a feed name to uniquely identify this setup.', 'gravityformsslack' );
// Send To.
$tooltips['send_to'] = '<h6>' . esc_html__( 'Send To', 'gravityformsslack' ) . '</h6>';
$tooltips['send_to'] .= esc_html__( 'Select what type of channel Slack will send the message to: a public channel, a private group or an IM channel.', 'gravityformsslack' );
// Email Address.
$tooltips['email'] = '<h6>' . esc_html__( 'Email Address', 'gravityformsslack' ) . '</h6>';
$tooltips['email'] .= esc_html__( 'Select what email field will be used to send the Slack invite.', 'gravityformsslack' );
// Channel.
$tooltips['channel'] = '<h6>' . esc_html__( 'Slack Channel', 'gravityformsslack' ) . '</h6>';
$tooltips['channel'] .= esc_html__( 'Select which Slack channel this feed will post a message to.', 'gravityformsslack' );
// Private Group.
$tooltips['group'] = '<h6>' . esc_html__( 'Slack Private Group', 'gravityformsslack' ) . '</h6>';
$tooltips['group'] .= esc_html__( 'Select which Slack private group this feed will post a message to.', 'gravityformsslack' );
// User.
$tooltips['user'] = '<h6>' . esc_html__( 'Slack User', 'gravityformsslack' ) . '</h6>';
$tooltips['user'] .= esc_html__( 'Select which Slack user this feed will post a message to.', 'gravityformsslack' );
// Message.
$tooltips['message'] = '<h6>' . __( 'Message', 'gravityformsslack' ) . '</h6>';
$tooltips['message'] .= esc_html__( 'Enter the message that will be posted to the room.', 'gravityformsslack' ) . '<br /><br />';
$tooltips['message'] .= '<strong>' . __( 'Available formatting:', 'gravityformsslack' ) . '</strong><br />';
$tooltips['message'] .= '<strong>*asterisks*</strong> to create bold text<br />';
$tooltips['message'] .= '<em>_underscores_</em> to italicize text<br /><br />';
$tooltips['message'] .= '<strong>></strong> to indent a single line<br />';
$tooltips['message'] .= '<strong>>>></strong> to indent multiple paragraphs<br /><br />';
$tooltips['message'] .= '<strong>`single backticks`</strong> display as inline fixed-width text<br />';
$tooltips['message'] .= '<strong>```triple backticks```</strong> create a block of pre-formatted, fixed-width text';
// Image Attachment.
$tooltips['attachments'] = '<h6>' . esc_html__( 'Image Attachments', 'gravityformsslack' ) . '</h6>';
$tooltips['attachments'] .= esc_html__( 'Select which file upload fields will be attached to the Slack message. Only image files will be attached.', 'gravityformsslack' );
// Feed Condition.
$tooltips['feed_condition'] = '<h6>' . esc_html__( 'Conditional Logic', 'gravityformsslack' ) . '</h6>';
$tooltips['feed_condition'] .= esc_html__( 'When conditional logic is enabled, form submissions will only be posted to Slack when the condition is met. When disabled, all form submissions will be posted.', 'gravityformsslack' );
// Return desired tooltip.
return $tooltips[ $field ];
}
/**
* Get Slack action types for feed settings field.
*
* @since 1.4
* @access public
*
* @uses GFSlack::can_invite_to_team()
*
* @return array
*/
public function action_for_feed_setting() {
// Initialize actions.
$actions = array(
array(
'label' => esc_html__( 'Send Message', 'gravityformsslack' ),
'value' => 'message',
'icon' => 'fa-comment',
),
);
// If API user is an admin, add invite action.
if ( $this->can_invite_to_team() ) {
$actions[] = array(
'label' => esc_html__( 'Invite to Team', 'gravityformsslack' ),
'value' => 'invite',
'icon' => 'fa-users',
);
}
return $actions;
}
/**
* Get Slack channel types for feed settings field.
*
* @since 1.0
* @access public
*
* @return array
*/
public function send_to_for_feed_setting() {
return array(
array(
'label' => esc_html__( 'Public Channel', 'gravityformsslack' ),
'value' => 'channel',
'icon' => 'fa-hashtag',
),
array(
'label' => esc_html__( 'Private Group', 'gravityformsslack' ),
'value' => 'group',
'icon' => 'fa-users',
),
array(
'label' => esc_html__( 'Direct Message', 'gravityformsslack' ),
'value' => 'user',
'icon' => 'fa-user-secret',
),
);
}
/**
* Get Slack channels for feed settings field.
*
* @since 1.0
* @access public
*
* @param bool $include_initital Include initital "Select a Channel" choice.
*
* @uses GFSlack::initialize_api()
* @uses GF_Slack_API::get_channels()
*
* @return array
*/
public function channels_for_feed_setting( $include_initial = true ) {
// Initialize choices array.
$choices = array();
// Add initial choice.
if ( $include_initial ) {
$choices[] = array(
'label' => esc_html__( 'Select a Channel', 'gravityformsslack' ),
'value' => '',
);
}
// If Slack API instance is not initialized, return choices.
if ( ! $this->initialize_api() ) {
return $choices;
}
// Get the channels.
$channels = $this->api->get_channels();
// Add lists to the choices array.
if ( rgar( $channels, 'channels' ) && ! empty( $channels['channels'] ) ) {
foreach ( $channels['channels'] as $channel ) {
$choices[] = array(
'label' => $channel['name'],
'value' => $channel['id'],
);
}
}
return $choices;
}
/**
* Get Slack groups for feed settings field.
*
* @since 1.0
* @access public
*
* @uses GFSlack::initialize_api()
* @uses GF_Slack_API::get_groups()
*
* @return array
*/
public function groups_for_feed_setting() {
// Setup choices array.
$choices = array(
array(
'label' => esc_html__( 'Select a Group', 'gravityformsslack' ),
'value' => '',
),
);
// If Slack API instance is not initialized, return choices.
if ( ! $this->initialize_api() ) {
return $choices;
}
// Get the channels.
$groups = $this->api->get_groups();
// Add lists to the choices array.
if ( rgar( $groups, 'groups' ) && ! empty( $groups['groups'] ) ) {
foreach ( $groups['groups'] as $group ) {
$choices[] = array(
'label' => $group['name'],
'value' => $group['id'],
);
}
}
return $choices;
}
/**
* Get Slack users for feed settings field.
*
* @since 1.0
* @access public
*
* @uses GFSlack::initialize_api()
* @uses GF_Slack_API::get_users()
*
* @return array
*/
public function users_for_feed_setting() {
// Setup choices array.
$choices = array(
array(
'label' => esc_html__( 'Select a User', 'gravityformsslack' ),
'value' => '',
),
);
// If Slack API instance is not initialized, return choices.
if ( ! $this->initialize_api() ) {
return $choices;
}
// Setup choices array.
$choices = array();
// Get the channels.
$users = $this->api->get_users();
// Add lists to the choices array.
if ( rgar( $users, 'members' ) && ! empty( $users['members'] ) ) {
foreach ( $users['members'] as $user ) {
$choices[] = array(
'label' => $user['name'],
'value' => $user['id'],
);
}
}
return $choices;
}
/**
* Get file upload fields for feed settings field.
*
* @since 1.0
* @access public
*
* @uses GFAPI::get_form()
* @uses GFCommon::get_fields_by_type()
*
* @return array
*/
public function fileupload_fields_for_feed_setting() {
// Setup choices array.
$choices = array();
// Get the form file fields.
$form = GFAPI::get_form( rgget( 'id' ) );
$file_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
if ( ! empty( $file_fields ) ) {
foreach ( $file_fields as $field ) {
$choices[] = array(
'name' => 'attachments[' . $field->id . ']',
'label' => $field->label,
'default_value' => 0,
);
}
}
return $choices;