forked from wp-premium/gravityforms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.php
2052 lines (1776 loc) · 74.4 KB
/
notification.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();
}
/**
* Class GFNotification
* Handles notifications within Gravity Forms
*/
Class GFNotification {
/**
* Defines the fields that support notifications.
*
* @since Unknown
* @access private
*
* @var array Array of field types.
*/
private static $supported_fields = array(
'checkbox', 'radio', 'select', 'text', 'website', 'textarea', 'email', 'hidden', 'number', 'phone', 'multiselect', 'post_title',
'post_tags', 'post_custom_field', 'post_content', 'post_excerpt',
);
/**
* Gets a notification based on a Form Object and a notification ID.
*
* @since Unknown
* @access private
*
* @param array $form The Form Object.
* @param int $notification_id The notification ID.
*
* @return array The Notification Object.
*/
private static function get_notification( $form, $notification_id ) {
foreach ( $form['notifications'] as $id => $notification ) {
if ( $id == $notification_id ) {
return $notification;
}
}
return array();
}
/**
* Displays the Notification page.
*
* If the notification ID is passed, the Notification Edit page is displayed.
* Otherwise, the Notification List page is displayed.
*
* @since Unknown
* @access public
*
* @uses GFNotification::notification_edit_page()
* @uses GFNotification::notification_list_page()
*
* @return void
*/
public static function notification_page() {
$form_id = rgget( 'id' );
$notification_id = rgget( 'nid' );
if ( ! rgblank( $notification_id ) ) {
self::notification_edit_page( $form_id, $notification_id );
} else {
self::notification_list_page( $form_id );
}
}
/**
* Builds the Notification Edit page.
*
* @access public
*
* @used-by GFNotification::notification_page()
* @uses GFFormsModel::get_form_meta()
* @uses GFNotification::get_notification()
* @uses GFNotification::validate_notification
* @uses GFFormsModel::sanitize_conditional_logic()
* @uses GFFormsModel::trim_conditional_logic_values_from_element()
* @uses GFFormsModel::save_form_notifications()
* @uses GFCommon::add_message()
* @uses GFCommon::json_decode()
* @uses GFCommon::add_error_message()
* @uses GFFormSettings::page_header()
* @uses GFNotification::get_notification_ui_settings()
* @uses SCRIPT_DEBUG
* @uses GFFormsModel::get_entry_meta()
* @uses GFFormSettings::output_field_scripts()
* @uses GFFormSettings::page_footer()
*
* @param int $form_id The ID of the form that the notification belongs to
* @param int $notification_id The ID of the notification being edited
*
* @return void
*/
public static function notification_edit_page( $form_id, $notification_id ) {
if ( ! rgempty( 'gform_notification_id' ) ) {
$notification_id = rgpost( 'gform_notification_id' );
}
$form = RGFormsModel::get_form_meta( $form_id );
/**
* Filters the form to be used in the notification page
*
* @since 1.8.6
*
* @param array $form The Form Object
* @param int $notification_id The notification ID
*/
$form = gf_apply_filters( array( 'gform_form_notification_page', $form_id ), $form, $notification_id );
$notification = ! $notification_id ? array() : self::get_notification( $form, $notification_id );
// Added second condition to account for new notifications with errors as notification ID will
// Be available in $_POST but the notification has not actually been saved yet
$is_new_notification = empty( $notification_id ) || empty( $notification );
$is_valid = true;
$is_update = false;
if ( ! empty( $_POST ) ) {
check_admin_referer( 'gforms_save_notification', 'gforms_save_notification' );
// Clear out notification because it could have legacy data populated
$notification = array( 'isActive' => isset( $notification['isActive'] ) ? rgar( $notification, 'isActive' ) : true );
$notification['name'] = sanitize_text_field( rgpost( 'gform_notification_name' ) );
$notification['service'] = sanitize_text_field( rgpost( 'gform_notification_service' ) );
$notification['event'] = sanitize_text_field( rgpost( 'gform_notification_event' ) );
$notification['to'] = rgpost( 'gform_notification_to_type' ) == 'field' ? rgpost( 'gform_notification_to_field' ) : rgpost( 'gform_notification_to_email' );
$to_type = rgpost( 'gform_notification_to_type' );
if ( ! in_array( $to_type, array( 'email', 'field', 'routing', 'hidden' ) ) ) {
$to_type = 'email';
}
$notification['toType'] = $to_type;
$notification['cc'] = rgpost( 'gform_notification_cc' );
$notification['bcc'] = rgpost( 'gform_notification_bcc' );
$notification['subject'] = sanitize_text_field( rgpost( 'gform_notification_subject' ) );
$notification['message'] = rgpost( 'gform_notification_message' );
$notification['from'] = sanitize_text_field( rgpost( 'gform_notification_from' ) );
$notification['fromName'] = sanitize_text_field( rgpost( 'gform_notification_from_name' ) );
$notification['replyTo'] = rgpost( 'gform_notification_reply_to' );
$routing = ! rgempty( 'gform_routing_meta' ) ? GFCommon::json_decode( rgpost( 'gform_routing_meta' ), true ) : null;
if ( ! empty ( $routing ) ) {
$routing_logic = array( 'rules' => $routing );
$routing_logic = GFFormsModel::sanitize_conditional_logic( $routing_logic );
$notification['routing'] = $routing_logic['rules'];
}
$notification['routing'] = $routing;
$conditional_logic = ! rgempty( 'gform_conditional_logic_meta' ) ? GFCommon::json_decode( rgpost( 'gform_conditional_logic_meta' ), true ) : null;
$notification['conditionalLogic'] = GFFormsModel::sanitize_conditional_logic( $conditional_logic );
$notification['disableAutoformat'] = (bool) rgpost( 'gform_notification_disable_autoformat' );
$notification['enableAttachments'] = (bool) rgpost( 'gform_notification_attachments' );
if ( rgpost( 'save' ) ) {
$is_update = true;
if ( $is_new_notification ) {
$notification_id = uniqid();
$notification['id'] = $notification_id;
} else {
$notification['id'] = $notification_id;
}
if ( rgpost( 'gform_is_default' ) ) {
$notification['isDefault'] = true;
}
/**
* Filters the notification before it is saved
*
* @param array $notification The Notification Object.
* @param array $form The Form Object.
* @param bool $is_new_notification True if it is a new notification. False otherwise.
*
* @since 1.7
*/
$notification = gf_apply_filters( array( 'gform_pre_notification_save', $form_id ), $notification, $form, $is_new_notification );
// Validating input...
$is_valid = self::validate_notification();
/**
* Allows overriding of if the notification passes validation
*
* @param bool $is_valid True if it is valid. False otherwise.
* @param array $notification The Notification Object
* @param array $form The Form Object
*
* @since 1.9.16
*
*/
$is_valid = gf_apply_filters( array( 'gform_notification_validation', $form_id ), $is_valid, $notification, $form );
if ( $is_valid ) {
// Input valid, updating...
// Emptying notification email if it is supposed to be disabled
if ( $_POST['gform_notification_to_type'] == 'routing' ) {
$notification['to'] = '';
} else {
$notification['routing'] = null;
}
// Trim values
$notification = GFFormsModel::trim_conditional_logic_values_from_element( $notification, $form );
$form['notifications'][ $notification_id ] = $notification;
RGFormsModel::save_form_notifications( $form_id, $form['notifications'] );
}
}
}
if ( $is_update && $is_valid ) {
$url = remove_query_arg( 'nid' );
GFCommon::add_message( sprintf( esc_html__( 'Notification saved successfully. %sBack to notifications.%s', 'gravityforms' ), '<a href="' . esc_url( $url ) . '">', '</a>' ) );
/**
* Fires an action after a notification has been saved
*
* @since 1.9.16
*
* @param array $notification The Notification Object
* @param array $form The Form Object
* @param bool $is_new_notification True if this is a new notification. False otherwise.
*/
gf_do_action( array( 'gform_post_notification_save', $form_id ), $notification, $form, $is_new_notification );
} else if ( $is_update && ! $is_valid ) {
GFCommon::add_error_message( esc_html__( 'Notification could not be updated. Please enter all required information below.', 'gravityforms' ) );
}
// Moved page header loading here so the admin messages can be set upon saving and available for the header to print out.
GFFormSettings::page_header( esc_html__( 'Notifications', 'gravityforms' ) );
$notification_ui_settings = self::get_notification_ui_settings( $notification, $is_valid );
$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG || isset( $_GET['gform_debug'] ) ? '' : '.min';
?>
<link rel="stylesheet" href="<?php echo GFCommon::get_base_url() ?>/css/admin<?php echo $min; ?>.css?ver=<?php echo GFCommon::$version ?>" />
<script type="text/javascript">
var gform_has_unsaved_changes = false;
jQuery(document).ready(function () {
jQuery("#entry_form input, #entry_form textarea, #entry_form select").change(function () {
gform_has_unsaved_changes = true;
});
window.onbeforeunload = function () {
if (gform_has_unsaved_changes) {
return "You have unsaved changes.";
}
};
ToggleConditionalLogic(true, 'notification');
jQuery(document).on('input propertychange', '.gfield_routing_email', function () {
SetRoutingEmail(jQuery(this));
});
jQuery(document).on('change', '.gfield_routing_value_dropdown', function () {
SetRoutingValueDropDown(jQuery(this));
});
});
gform.addFilter("gform_merge_tags", "MaybeAddSaveLinkMergeTag");
function MaybeAddSaveLinkMergeTag(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option){
var event = document.getElementById('gform_notification_event').value;
if ( event == 'form_saved' || event == 'form_save_email_requested' ) {
mergeTags["other"].tags.push({ tag: '{save_link}', label: <?php echo json_encode( esc_html__( 'Save & Continue Link', 'gravityforms' ) ); ?> });
mergeTags["other"].tags.push({ tag: '{save_token}', label: <?php echo json_encode( esc_html__( 'Save & Continue Token', 'gravityforms' ) ); ?> });
}
return mergeTags;
}
<?php
if ( empty( $form['notifications'] ) ) {
$form['notifications'] = array();
}
$entry_meta = GFFormsModel::get_entry_meta( $form_id );
/**
* Filters the entry meta when notification conditional logic is being edited
*
* @since 1.7.6
*
* @param array $entry_meta The Entry meta
* @param array $form The Form Object
* @param int $notification_id The notification ID
*/
$entry_meta = apply_filters( 'gform_entry_meta_conditional_logic_notifications', $entry_meta, $form, $notification_id );
?>
var form = <?php echo json_encode( $form ) ?>;
var current_notification = <?php echo GFCommon::json_encode( $notification ) ?>;
var entry_meta = <?php echo GFCommon::json_encode( $entry_meta ) ?>;
function SetRoutingEmail(element) {
// Parsing ID to get routing Index
var index = element.attr('id').replace('routing_email_', '');
SetRouting(index);
}
function SetRoutingValueDropDown(element) {
// Parsing ID to get routing Index
var index = element.attr("id").replace("routing_value_", '');
SetRouting(index);
}
function CreateRouting(routings) {
var str = '';
for (var i = 0; i < routings.length; i++) {
var isSelected = routings[i].operator == 'is' ? "selected='selected'" : '';
var isNotSelected = routings[i].operator == 'isnot' ? "selected='selected'" : '';
var greaterThanSelected = routings[i].operator == '>' ? "selected='selected'" : '';
var lessThanSelected = routings[i].operator == '<' ? "selected='selected'" : '';
var containsSelected = routings[i].operator == 'contains' ? "selected='selected'" : '';
var startsWithSelected = routings[i].operator == 'starts_with' ? "selected='selected'" : '';
var endsWithSelected = routings[i].operator == 'ends_with' ? "selected='selected'" : '';
var email = routings[i]["email"] ? routings[i]["email"] : '';
str += "<div style='width:99%'>" + <?php echo json_encode( esc_html__( 'Send to', 'gravityforms' ) ); ?> + " <input type='text' id='routing_email_" + i + "' value='" + email + "' class='gfield_routing_email' />";
str += " " + <?php echo json_encode( esc_html__( 'if', 'gravityforms' ) ); ?> + " " + GetRoutingFields(i, routings[i].fieldId) + " ";
str += "<select id='routing_operator_" + i + "' onchange='SetRouting(" + i + ");' class='gform_routing_operator'>";
str += "<option value='is' " + isSelected + ">" + <?php echo json_encode( esc_html__( 'is', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='isnot' " + isNotSelected + ">" + <?php echo json_encode( esc_html__( 'is not', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='>' " + greaterThanSelected + ">" + <?php echo json_encode( esc_html__( 'greater than', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='<' " + lessThanSelected + ">" + <?php echo json_encode( esc_html__( 'less than', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='contains' " + containsSelected + ">" + <?php echo json_encode( esc_html__( 'contains', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='starts_with' " + startsWithSelected + ">" + <?php echo json_encode( esc_html__( 'starts with', 'gravityforms' ) ); ?> + "</option>";
str += "<option value='ends_with' " + endsWithSelected + ">" + <?php echo json_encode( esc_html__( 'ends with', 'gravityforms' ) ); ?> + "</option>";
str += "</select> ";
str += GetRoutingValues(i, routings[i].fieldId, routings[i].value) + " ";
str += "<a class='gf_insert_field_choice' title='add another rule' onclick=\"InsertRouting(" + (i + 1) + ");\" onkeypress=\"InsertRouting(" + (i + 1) + ");\"><i class='gficon-add'></i></a>";
if (routings.length > 1)
str += "<a class='gf_delete_field_choice' title='remove this rule' onclick=\"DeleteRouting(" + i + ");\" onkeypress=\"DeleteRouting(" + i + ");\"><i class='gficon-subtract'></i></a>";
str += "</div>";
}
jQuery("#gform_notification_to_routing_rules").html(str);
}
function GetRoutingValues(index, fieldId, selectedValue) {
var str = GetFieldValues(index, fieldId, selectedValue, 16);
return str;
}
function GetRoutingFields(index, selectedItem) {
var str = "<select id='routing_field_id_" + index + "' class='gfield_routing_select' onchange='jQuery(\"#routing_value_" + index + "\").replaceWith(GetRoutingValues(" + index + ", jQuery(this).val())); SetRouting(" + index + "); '>";
str += GetSelectableFields(selectedItem, 16);
str += "</select>";
return str;
}
//---------------------- generic ---------------
function GetSelectableFields(selectedFieldId, labelMaxCharacters) {
var str = "";
var inputType;
for (var i = 0; i < form.fields.length; i++) {
inputType = form.fields[i].inputType ? form.fields[i].inputType : form.fields[i].type;
// See if this field type can be used for conditionals.
if (IsNotificationConditionalLogicField(form.fields[i])) {
var selected = form.fields[i].id == selectedFieldId ? "selected='selected'" : "";
str += "<option value='" + form.fields[i].id + "' " + selected + ">" + form.fields[i].label + "</option>";
}
}
return str;
}
function IsNotificationConditionalLogicField(field) {
// This function is a duplicate of IsConditionalLogicField from form_editor.js
inputType = field.inputType ? field.inputType : field.type;
var supported_fields = <?php echo json_encode( self::get_routing_field_types() ); ?>;
var index = jQuery.inArray(inputType, supported_fields);
return index >= 0;
}
function GetFirstSelectableField() {
var inputType;
for (var i = 0; i < form.fields.length; i++) {
inputType = form.fields[i].inputType ? form.fields[i].inputType : form.fields[i].type;
if (IsNotificationConditionalLogicField(form.fields[i])) {
return form.fields[i].id;
}
}
return 0;
}
function TruncateMiddle(text, maxCharacters) {
if (!text)
return "";
if (text.length <= maxCharacters)
return text;
var middle = parseInt(maxCharacters / 2);
return text.substr(0, middle) + "..." + text.substr(text.length - middle, middle);
}
function GetFieldValues(index, fieldId, selectedValue, labelMaxCharacters) {
if (!fieldId)
fieldId = GetFirstSelectableField();
if (!fieldId)
return "";
var str = '';
var field = GetFieldById(fieldId);
var isAnySelected = false;
if (!field)
return "";
if (field["type"] == 'post_category' && field["displayAllCategories"]) {
var dropdown_id = 'routing_value_' + index;
var dropdown = jQuery('#' + dropdown_id + ".gfield_category_dropdown");
// Don't load category drop down if it already exists (to avoid unecessary ajax requests).
if (dropdown.length > 0) {
var options = dropdown.html();
options = options.replace("value=\"" + selectedValue + "\"", "value=\"" + selectedValue + "\" selected=\"selected\"");
str = "<select id='" + dropdown_id + "' class='gfield_routing_select gfield_category_dropdown gfield_routing_value_dropdown'>" + options + "</select>";
}
else {
// Loading categories via AJAX.
jQuery.post(ajaxurl, { action: "gf_get_notification_post_categories",
ruleIndex : index,
selectedValue : selectedValue},
function (dropdown_string) {
if (dropdown_string) {
jQuery('#gfield_ajax_placeholder_' + index).replaceWith(dropdown_string.trim());
}
}
);
// Will be replaced by real drop down during the ajax callback.
str = "<select id='gfield_ajax_placeholder_" + index + "' class='gfield_routing_select'><option>" + <?php json_encode( esc_html__( 'Loading...', 'gravityforms' ) ); ?> + "</option></select>";
}
}
else if (field.choices) {
// Create a drop down for fields that have choices (i.e. drop down, radio, checkboxes, etc...).
str = "<select class='gfield_routing_select gfield_routing_value_dropdown' id='routing_value_" + index + "'>";
if (field.placeholder) {
str += "<option value=''>" + field.placeholder + "</option>";
}
for (var i = 0; i < field.choices.length; i++) {
var choiceValue = field.choices[i].value ? field.choices[i].value : field.choices[i].text;
var isSelected = choiceValue == selectedValue;
var selected = isSelected ? "selected='selected'" : '';
if (isSelected)
isAnySelected = true;
str += "<option value='" + choiceValue.replace(/'/g, "'") + "' " + selected + ">" + field.choices[i].text + "</option>";
}
if (!isAnySelected && selectedValue) {
str += "<option value='" + selectedValue.replace(/'/g, "'") + "' selected='selected'>" + selectedValue + "</option>";
}
str += "</select>";
}
else {
selectedValue = selectedValue ? selectedValue.replace(/'/g, "'") : "";
// Create a text field for fields that don't have choices (i.e text, textarea, number, email, etc...).
str = "<input type='text' placeholder='" + <?php echo json_encode( esc_html__( 'Enter value', 'gravityforms' ) ); ?> +"' class='gfield_routing_select' id='routing_value_" + index + "' value='" + selectedValue.replace(/'/g, "'") + "' onchange='SetRouting(" + index + ");' onkeyup='SetRouting(" + index + ");'>";
}
return str;
}
//---------------------------------------------------------------------------------
function InsertRouting(index) {
var routings = current_notification.routing;
routings.splice(index, 0, new ConditionalRule());
CreateRouting(routings);
SetRouting(index);
}
function SetRouting(ruleIndex) {
if (!current_notification.routing && ruleIndex == 0)
current_notification.routing = [new ConditionalRule()];
current_notification.routing[ruleIndex]["email"] = jQuery("#routing_email_" + ruleIndex).val();
current_notification.routing[ruleIndex]["fieldId"] = jQuery("#routing_field_id_" + ruleIndex).val();
current_notification.routing[ruleIndex]["operator"] = jQuery("#routing_operator_" + ruleIndex).val();
current_notification.routing[ruleIndex]["value"] = jQuery("#routing_value_" + ruleIndex).val();
var json = jQuery.toJSON(current_notification.routing);
jQuery('#gform_routing_meta').val(json);
}
function DeleteRouting(ruleIndex) {
current_notification.routing.splice(ruleIndex, 1);
CreateRouting(current_notification.routing);
}
function SetConditionalLogic(isChecked) {
current_notification.conditionalLogic = isChecked ? new ConditionalLogic() : null;
}
function SaveJSMeta() {
jQuery('#gform_routing_meta').val(jQuery.toJSON(current_notification.routing));
jQuery('#gform_conditional_logic_meta').val(jQuery.toJSON(current_notification.conditionalLogic));
}
<?php GFFormSettings::output_field_scripts() ?>
</script>
<form method="post" id="gform_notification_form" onsubmit="gform_has_unsaved_changes = false; SaveJSMeta();">
<?php wp_nonce_field( 'gforms_save_notification', 'gforms_save_notification' ) ?>
<?php
if ( rgar( $notification, 'isDefault' ) ) {
echo '<input type="hidden" id="gform_is_default" name="gform_is_default" value="1"/>';
}
?>
<input type="hidden" id="gform_routing_meta" name="gform_routing_meta" />
<input type="hidden" id="gform_conditional_logic_meta" name="gform_conditional_logic_meta" />
<input type="hidden" id="gform_notification_id" name="gform_notification_id" value="<?php echo esc_attr( $notification_id ) ?>" />
<table class="form-table gform_nofification_edit">
<?php array_map( array( 'GFFormSettings', 'output' ), $notification_ui_settings ); ?>
</table>
<p class="submit">
<?php
$button_label = $is_new_notification ? __( 'Save Notification', 'gravityforms' ) : __( 'Update Notification', 'gravityforms' );
$notification_button = '<input class="button-primary" type="submit" value="' . esc_attr( $button_label ) . '" name="save"/>';
/**
* Filters the "Save Notification" button
*
* @since Unknown
*
* @param string $notification_button The notification button HTML
*/
echo apply_filters( 'gform_save_notification_button', $notification_button );
?>
</p>
</form>
<?php
GFFormSettings::page_footer();
}
/**
* Displays the notification list page
*
* @since Unknown
* @access public
*
* @used-by GFNotification::notification_page()
* @uses GFNotification::maybe_process_notification_list_action()
* @uses GFFormsModel::get_form_meta()
* @uses GFFormSettings::page_header()
* @uses GFNotificationTable::__construct()
* @uses GFNotificationTable::prepare_items()
* @uses GFNotificationTable::display()
* @uses GFFormSettings::page_footer()
*
* @param int $form_id The form ID to list notifications on.
*
* @return void
*/
public static function notification_list_page( $form_id ) {
// Handle form actions
self::maybe_process_notification_list_action();
$form = RGFormsModel::get_form_meta( $form_id );
GFFormSettings::page_header( esc_html__( 'Notifications', 'gravityforms' ) );
$add_new_url = add_query_arg( array( 'nid' => 0 ) );
?>
<h3><span><i class="fa fa-envelope-o"></i> <?php esc_html_e( 'Notifications', 'gravityforms' ) ?>
<a id="add-new-confirmation" class="add-new-h2" href="<?php echo esc_url( $add_new_url ) ?>"><?php esc_html_e( 'Add New', 'gravityforms' ) ?></a></span>
</h3>
<script type="text/javascript">
function ToggleActive(img, notification_id) {
var is_active = img.src.indexOf("active1.png") >= 0
if (is_active) {
img.src = img.src.replace("active1.png", "active0.png");
jQuery(img).attr('title', <?php echo json_encode( esc_html__( 'Inactive', 'gravityforms' ) ); ?>).attr('alt', <?php echo json_encode( esc_html__( 'Inactive', 'gravityforms' ) ); ?>);
}
else {
img.src = img.src.replace("active0.png", "active1.png");
jQuery(img).attr('title', <?php echo json_encode( esc_html__( 'Active', 'gravityforms' ) ); ?>).attr('alt', <?php echo json_encode( esc_html__( 'Active', 'gravityforms' ) ); ?>);
}
var mysack = new sack("<?php echo admin_url( 'admin-ajax.php' )?>");
mysack.execute = 1;
mysack.method = 'POST';
mysack.setVar("action", "rg_update_notification_active");
mysack.setVar("rg_update_notification_active", "<?php echo wp_create_nonce( 'rg_update_notification_active' ) ?>");
mysack.setVar("form_id", <?php echo intval( $form_id ) ?>);
mysack.setVar("notification_id", notification_id);
mysack.setVar("is_active", is_active ? 0 : 1);
mysack.onError = function () {
alert(<?php echo json_encode( esc_html__( 'Ajax error while updating notification', 'gravityforms' ) ) ?>)
};
mysack.runAJAX();
return true;
}
</script>
<?php
$notification_table = new GFNotificationTable( $form );
$notification_table->prepare_items();
?>
<form id="notification_list_form" method="post">
<?php $notification_table->display(); ?>
<input id="action_argument" name="action_argument" type="hidden" />
<input id="action" name="action" type="hidden" />
<?php wp_nonce_field( 'gform_notification_list_action', 'gform_notification_list_action' ) ?>
</form>
<?php
GFFormSettings::page_footer();
}
/**
* Processes a notification list action if needed.
*
* @since Unknown
* @access public
*
* @used-by GFNotification::notification_list_page()
* @uses GFNotification::delete_notification()
* @uses GFNotification::duplicate_notification()
* @uses GFCommon::add_message()
* @uses GFCommon::add_error_message()
*
* @return void
*/
public static function maybe_process_notification_list_action() {
if ( empty( $_POST ) || ! check_admin_referer( 'gform_notification_list_action', 'gform_notification_list_action' ) ) {
return;
}
$action = rgpost( 'action' );
$object_id = rgpost( 'action_argument' );
switch ( $action ) {
case 'delete':
$notification_deleted = GFNotification::delete_notification( $object_id, rgget( 'id' ) );
if ( $notification_deleted ) {
GFCommon::add_message( esc_html__( 'Notification deleted.', 'gravityforms' ) );
} else {
GFCommon::add_error_message( esc_html__( 'There was an issue deleting this notification.', 'gravityforms' ) );
}
break;
case 'duplicate':
$notification_duplicated = GFNotification::duplicate_notification( $object_id, rgget( 'id' ) );
if ( $notification_duplicated ) {
GFCommon::add_message( esc_html__( 'Notification duplicated.', 'gravityforms' ) );
} else {
GFCommon::add_error_message( esc_html__( 'There was an issue duplicating this notification.', 'gravityforms' ) );
}
break;
}
}
/**
* Builds the Notification Settings page.
*
* @since Unknown
* @access private
*
* @used-by GFNotification::notification_edit_page()
* @uses GFFormsModel::get_form_meta()
* @uses GFCommon::$errors
* @uses gform_tooltip()
* @uses GFNotification::get_notification_services()
* @uses GFNotification::get_notification_events()
* @uses GFNotification::is_valid_notification_to()
* @uses GFCommon::get_email_fields()
* @uses GFCommon::get_base_url()
*
* @param array $notification The Notification Object
* @param bool $is_valid Optional. Defines if this is a valid notification. Defaults to true.
*
* @return array $ui_settings Array of notification settings.
*/
private static function get_notification_ui_settings( $notification, $is_valid = true ) {
// These variables are used to convenient "wrap" child form settings in the appropriate HTML.
$subsetting_open = '
<td colspan="2" class="gf_sub_settings_cell">
<div class="gf_animate_sub_settings">
<table>
<tr>';
$subsetting_close = '
</tr>
</table>
</div>
</td>';
$ui_settings = array();
$form_id = rgget( 'id' );
$form = RGFormsModel::get_form_meta( $form_id );
$form = gf_apply_filters( array( 'gform_admin_pre_render', $form_id ), $form );
$is_valid = empty( GFCommon::$errors );
ob_start(); ?>
<tr valign="top" <?php echo rgar( $notification, 'isDefault' ) ? "style='display:none'" : '' ?> >
<th scope="row">
<label for="gform_notification_name">
<?php esc_html_e( 'Name', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_name' ) ?>
</label>
</th>
<td>
<input type="text" class="fieldwidth-2" name="gform_notification_name" id="gform_notification_name" value="<?php echo esc_attr( rgget( 'name', $notification ) ) ?>" />
</td>
</tr> <!-- / name -->
<?php $ui_settings['notification_name'] = ob_get_contents();
ob_clean(); ?>
<?php
$services = self::get_notification_services();
$service_style = count( $services ) == 1 ? "style='display:none'" : '';
$notification_service = ! rgempty( 'gform_notification_service' ) ? rgpost( 'gform_notification_service' ) : rgar( $notification, 'service' );
if ( empty( $notification_service ) ) {
$notification_service = key( $services );
}
ob_start(); ?>
<tr valign="top" <?php echo $service_style; ?>>
<th scope="row">
<label for="gform_notification_send_via">
<?php esc_html_e( 'Email Service', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_service' ) ?>
</label>
</th>
<td>
<?php foreach ( $services as $service_name => $service ) { ?>
<div id="gform-notification-service-<?php echo esc_attr( $service_name ); ?>" class="gform-notification-service<?php echo rgar( $service, 'disabled' ) && rgar( $service, 'disabled_message' ) ? ' gf_tooltip' : ''; ?>" <?php echo rgar( $service, 'disabled' ) && rgar( $service, 'disabled_message' ) ? 'title="' . esc_attr( $service['disabled_message'] ) . '"' : ''; ?>>
<input type="radio" id="gform_notification_service_<?php echo esc_attr( $service_name ); ?>" name="gform_notification_service" <?php checked( $service_name, $notification_service ); ?> value="<?php echo esc_attr( $service_name ); ?>" onclick="jQuery(this).parents('form').submit();" onkeypress="jQuery(this).parents('form').submit();" <?php echo rgar( $service, 'disabled' ) ? 'disabled="disabled"' : ''; ?> />
<label for="gform_notification_service_<?php echo esc_attr( $service_name ); ?>" class="inline">
<span><img src="<?php echo esc_attr( rgar( $service, 'image' ) ); ?>" /><br /><?php echo esc_html( rgar( $service, 'label' ) ); ?></span>
</label>
</div>
<?php } ?>
</td>
</tr> <!-- / send via -->
<?php $ui_settings['notification_service'] = ob_get_contents();
ob_clean(); ?>
<?php
$notification_events = self::get_notification_events( $form );
$event_style = count( $notification_events ) == 1 || rgar( $notification, 'isDefault' ) ? "style='display:none'" : '';
?>
<tr valign="top" <?php echo $event_style ?>>
<th scope="row">
<label for="gform_notification_event">
<?php esc_html_e( 'Event', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_event' ) ?>
</label>
</th>
<td>
<select name="gform_notification_event" id="gform_notification_event">
<?php
foreach ( $notification_events as $code => $label ) {
?>
<option value="<?php echo esc_attr( $code ) ?>" <?php selected( rgar( $notification, 'event' ), $code ) ?>><?php echo esc_html( $label ) ?></option>
<?php
}
?>
</select>
</td>
</tr> <!-- / event -->
<?php $ui_settings['notification_event'] = ob_get_contents();
ob_clean(); ?>
<?php
$notification_to_type = ! rgempty( 'gform_notification_to_type' ) ? rgpost( 'gform_notification_to_type' ) : rgar( $notification, 'toType' );
if ( empty( $notification_to_type ) ) {
$notification_to_type = 'email';
}
$is_invalid_email_to = ! $is_valid && ! self::is_valid_notification_to();
$send_to_class = $is_invalid_email_to ? 'gfield_error' : '';
?>
<tr valign="top" class='<?php echo esc_attr( $send_to_class ) ?>' <?php echo $notification_to_type == 'hidden' ? 'style="display:none;"': ''; ?>>
<th scope="row">
<label for="gform_notification_to_email">
<?php esc_html_e( 'Send To', 'gravityforms' ); ?><span class="gfield_required">*</span>
<?php gform_tooltip( 'notification_send_to_email' ) ?>
</label>
</th>
<td>
<input type="radio" id="gform_notification_to_type_email" name="gform_notification_to_type" <?php checked( 'email', $notification_to_type ); ?> value="email" onclick="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_email_container').show('slow');" onkeypress="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_email_container').show('slow');" />
<label for="gform_notification_to_type_email" class="inline">
<?php esc_html_e( 'Enter Email', 'gravityforms' ); ?>
</label>
<input type="radio" id="gform_notification_to_type_field" name="gform_notification_to_type" <?php checked( 'field', $notification_to_type ); ?> value="field" onclick="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_field_container').show('slow');" onkeypress="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_field_container').show('slow');" />
<label for="gform_notification_to_type_field" class="inline">
<?php esc_html_e( 'Select a Field', 'gravityforms' ); ?>
</label>
<input type="radio" id="gform_notification_to_type_routing" name="gform_notification_to_type" <?php checked( 'routing', $notification_to_type ); ?> value="routing" onclick="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_routing_container').show('slow');" onkeypress="jQuery('.notification_to_container').hide(); jQuery('#gform_notification_to_routing_container').show('slow');" />
<label for="gform_notification_to_type_routing" class="inline">
<?php esc_html_e( 'Configure Routing', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_send_to_routing' ) ?>
</label>
</td>
</tr> <!-- / to email type -->
<?php
$ui_settings['notification_to_email_type'] = ob_get_contents();
ob_clean();
if ( $notification_to_type == 'hidden' ) {
$ui_settings['notification_to_email_type'] = '<input type="hidden" name="gform_notification_to_type" value="hidden" />';
}
?>
<tr id="gform_notification_to_email_container" class="notification_to_container <?php echo esc_attr( $send_to_class ) ?>" <?php echo $notification_to_type != 'email' ? "style='display:none';" : '' ?>>
<?php echo $subsetting_open; ?>
<th scope="row"><?php esc_html_e( 'Send to Email', 'gravityforms' ) ?></th>
<td>
<?php
$to_email = rgget( 'toType', $notification ) == 'email' ? rgget( 'to', $notification ) : '';
?>
<input type="text" name="gform_notification_to_email" id="gform_notification_to_email" value="<?php echo esc_attr( $to_email ) ?>" class="fieldwidth-1" />
<?php if ( rgpost( 'gform_notification_to_type' ) == 'email' && $is_invalid_email_to ) { ?>
<span class="validation_message"><?php esc_html_e( 'Please enter a valid email address', 'gravityforms' ) ?>.</span>
<?php } ?>
</td>
<?php echo $subsetting_close; ?>
</tr> <!-- / to email -->
<?php $ui_settings['notification_to_email'] = ob_get_contents();
ob_clean(); ?>
<?php $email_fields = gf_apply_filters( array( 'gform_email_fields_notification_admin', $form['id'] ), GFCommon::get_email_fields( $form ), $form ); ?>
<tr id="gform_notification_to_field_container" class="notification_to_container <?php echo esc_attr( $send_to_class ) ?>" <?php echo $notification_to_type != 'field' ? "style='display:none';" : '' ?>>
<?php echo $subsetting_open; ?>
<th scope="row"><?php esc_html_e( 'Send to Field', 'gravityforms' ) ?></th>
<td>
<?php
if ( ! empty( $email_fields ) ) {
?>
<select name="gform_notification_to_field" id="gform_notification_to_field">
<option value=""><?php esc_html_e( 'Select an email field', 'gravityforms' ); ?></option>
<?php
$to_field = rgget( 'toType', $notification ) == 'field' ? rgget( 'to', $notification ) : '';
foreach ( $email_fields as $field ) {
?>
<option value="<?php echo esc_attr( $field->id ) ?>" <?php echo selected( $field->id, $to_field ) ?>><?php echo GFCommon::get_label( $field ) ?></option>
<?php
}
?>
</select>
<?php
} else {
?>
<div class="error_base">
<p><?php esc_html_e( 'Your form does not have an email field. Add an email field to your form and try again.', 'gravityforms' ) ?></p>
</div>
<?php
}
?>
</td>
<?php echo $subsetting_close; ?>
</tr> <!-- / to email field -->
<?php $ui_settings['notification_to_email_field'] = ob_get_contents();
ob_clean(); ?>
<tr id="gform_notification_to_routing_container" class="notification_to_container <?php echo esc_attr( $send_to_class ) ?>" <?php echo $notification_to_type != 'routing' ? "style='display:none';" : '' ?>>
<?php echo $subsetting_open; ?>
<td colspan="2">
<div id="gform_notification_to_routing_rules">
<?php
$routing_fields = self::get_routing_fields( $form, '0' );
if ( empty( $routing_fields ) ) {
?>
<div class="gold_notice">
<p><?php esc_html_e( 'To use notification routing, your form must have a field supported by conditional logic.', 'gravityforms' ); ?></p>
</div>
<?php
} else {
if ( empty( $notification['routing'] ) ) {
$notification['routing'] = array( array() );
}
$count = sizeof( $notification['routing'] );
$routing_list = ',';
for ( $i = 0; $i < $count; $i ++ ) {
$routing_list .= $i . ',';
$routing = $notification['routing'][ $i ];
$is_invalid_rule = ! $is_valid && $_POST['gform_notification_to_type'] == 'routing' && ! self::is_valid_notification_email( rgar( $routing, 'email' ) );
$class = $is_invalid_rule ? "class='grouting_rule_error'" : '';
?>
<div style='width:99%' <?php echo $class ?>>
<?php esc_html_e( 'Send to', 'gravityforms' ) ?>
<input type="text" id="routing_email_<?php echo $i ?>" value="<?php echo esc_attr( rgar( $routing, 'email' ) ); ?>" class='gfield_routing_email' />
<?php esc_html_e( 'if', 'gravityforms' ) ?>
<select id="routing_field_id_<?php echo $i ?>" class='gfield_routing_select' onchange='jQuery("#routing_value_<?php echo $i ?>").replaceWith(GetRoutingValues(<?php echo $i ?>, jQuery(this).val())); SetRouting(<?php echo $i ?>); '><?php echo self::get_routing_fields( $form, rgar( $routing, 'fieldId' ) ) ?></select>
<select id="routing_operator_<?php echo $i ?>" onchange="SetRouting(<?php echo $i ?>)" class="gform_routing_operator">
<option value="is" <?php echo rgar( $routing, 'operator' ) == 'is' ? "selected='selected'" : '' ?>><?php esc_html_e( 'is', 'gravityforms' ) ?></option>
<option value="isnot" <?php echo rgar( $routing, 'operator' ) == 'isnot' ? "selected='selected'" : '' ?>><?php esc_html_e( 'is not', 'gravityforms' ) ?></option>
<option value=">" <?php echo rgar( $routing, 'operator' ) == '>' ? "selected='selected'" : '' ?>><?php esc_html_e( 'greater than', 'gravityforms' ) ?></option>
<option value="<" <?php echo rgar( $routing, 'operator' ) == '<' ? "selected='selected'" : '' ?>><?php esc_html_e( 'less than', 'gravityforms' ) ?></option>
<option value="contains" <?php echo rgar( $routing, 'operator' ) == 'contains' ? "selected='selected'" : '' ?>><?php esc_html_e( 'contains', 'gravityforms' ) ?></option>
<option value="starts_with" <?php echo rgar( $routing, 'operator' ) == 'starts_with' ? "selected='selected'" : '' ?>><?php esc_html_e( 'starts with', 'gravityforms' ) ?></option>
<option value="ends_with" <?php echo rgar( $routing, 'operator' ) == 'ends_with' ? "selected='selected'" : '' ?>><?php esc_html_e( 'ends with', 'gravityforms' ) ?></option>
</select>
<?php echo self::get_field_values( $i, $form, rgar( $routing, 'fieldId' ), rgar( $routing, 'value' ) ) ?>
<a class='gf_insert_field_choice' title='add another rule' onclick='SetRouting(<?php echo $i ?>); InsertRouting(<?php echo $i + 1 ?>);' onkeypress='SetRouting(<?php echo $i ?>); InsertRouting(<?php echo $i + 1 ?>);'><i class='gficon-add'></i></a>
<?php if ( $count > 1 ) { ?>
<img src='<?php echo GFCommon::get_base_url() ?>/images/remove.png' id='routing_delete_<?php echo $i ?>' title='remove this email routing' alt='remove this email routing' class='delete_field_choice' style='cursor:pointer;' onclick='DeleteRouting(<?php echo $i ?>);' onkeypress='DeleteRouting(<?php echo $i ?>);' />
<?php } ?>
</div>
<?php
}
if ( $is_invalid_rule ) {
?>
<span class="validation_message"><?php esc_html_e( 'Please enter a valid email address for all highlighted routing rules above.', 'gravityforms' ) ?></span>
<?php } ?>
<input type="hidden" name="routing_count" id="routing_count" value="<?php echo $routing_list ?>" />
<?php
}
?>
</div>
</td>
<?php echo $subsetting_close; ?>
</tr> <!-- / to routing -->
<?php $ui_settings['notification_to_routing'] = ob_get_contents();
ob_clean(); ?>
<tr valign="top">
<th scope="row">
<label for="gform_notification_from_name">
<?php esc_html_e( 'From Name', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_from_name' ) ?>
</label>
</th>
<td>
<input type="text" class="fieldwidth-2 merge-tag-support mt-position-right mt-hide_all_fields" name="gform_notification_from_name" id="gform_notification_from_name" value="<?php echo esc_attr( rgget( 'fromName', $notification ) ) ?>" />
</td>
</tr> <!-- / from name -->
<?php $ui_settings['notification_from_name'] = ob_get_contents();
ob_clean(); ?>
<tr valign="top">
<th scope="row">
<label for="gform_notification_from">
<?php esc_html_e( 'From Email', 'gravityforms' ); ?>
<?php gform_tooltip( 'notification_from_email' ) ?>
</label>