-
Notifications
You must be signed in to change notification settings - Fork 22
/
notification.php
1656 lines (1424 loc) · 50 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
use Gravity_Forms\Gravity_Forms\Settings\Settings;
class_exists( 'GFForms' ) || die();
/**
* Class GFNotification
* Handles notifications within Gravity Forms
*/
Class GFNotification {
use Redirects_On_Save;
/**
* 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',
);
/**
* Stores the current instance of the Settings renderer.
*
* @since 2.5
*
* @var false|Settings
*/
private static $_settings_renderer = false;
/**
* 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 ) {
GFFormSettings::page_header( esc_html__( 'Notifications', 'gravityforms' ) );
if ( ! self::get_settings_renderer() ) {
self::initialize_settings_renderer();
}
// Render settings.
self::get_settings_renderer()->render();
GFFormSettings::page_footer();
}
/**
* Get Notification settings fields.
*
* @since 2.5
*
* @param array $notification Notification being edited.
* @param array $form The Form object.
*
* @return array
*/
private static function settings_fields( $notification, $form ) {
// Get notification events.
$events = self::get_notification_events( $form );
// Prepare notification events as choices.
$events_choices = array();
foreach ( $events as $name => $label ) {
$events_choices[] = array(
'label' => $label,
'value' => $name,
);
}
// Get notification services.
$services = self::get_notification_services();
// Prepare notification services as choices.
$services_choices = array();
foreach ( $services as $name => $service_meta ) {
$services_choices[] = array(
'label' => rgar( $service_meta, 'label' ),
'value' => $name,
'icon' => rgar( $service_meta, 'image' ),
'onclick' => "jQuery(this).parents('form').submit();",
);
}
/**
* Disable the From Email warning.
*
* @since 2.4.13
*
* @param bool $disable_from_warning Should the From Email warning be disabled?
*/
$disable_from_warning = gf_apply_filters( array( 'gform_notification_disable_from_warning', $form['id'], rgar( $notification, 'id' ) ), false );
$from_email_warning = '';
// Prepare From Email warning.
if ( ! $disable_from_warning && self::get_settings_renderer()->get_value( 'service' ) === 'wordpress' ) {
// Get From Email address.
$from_address = self::get_settings_renderer()->get_value( 'from' );
// Determine if From Email is invalid
$is_invalid_from_email = ! empty( $from_address ) && ! self::is_valid_notification_email( $from_address );
// Display warning message if not using an email address containing the site domain or {admin_email}.
if ( ! $is_invalid_from_email && ! self::is_site_domain_in_from( $from_address ) ) {
$from_email_warning = sprintf(
'<div class="alert warning" role="alert" style="">%s</div>',
sprintf(
esc_html__( 'Warning! Using a third-party email in the From Email field may prevent your notification from being delivered. It is best to use an email with the same domain as your website. %sMore details in our documentation.%s', 'gravityforms' ),
'<a href="https://docs.gravityforms.com/troubleshooting-notifications/#use-a-valid-from-address" target="_blank" >',
'</a>'
)
);
}
}
// Prepare To Type field.
if ( 'hidden' === rgar( $notification, 'toType' ) ) {
$to_type = array(
'name' => 'toType',
'type' => 'hidden',
'default_value' => 'hidden',
);
} else {
$to_type = array(
'name' => 'toType',
'label' => esc_html__( 'Send To', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_send_to_email', null, true ),
'type' => 'radio',
'horizontal' => true,
'choices' => array(
array(
'label' => esc_html__( 'Enter Email', 'gravityforms' ),
'value' => 'email',
),
array(
'label' => esc_html__( 'Select a Field', 'gravityforms' ),
'value' => 'field',
),
array(
'label' => esc_html__( 'Configure Routing', 'gravityforms' ),
'value' => 'routing',
'tooltip' => gform_tooltip( 'notification_send_to_routing', null, true ),
),
),
'default_value' => 'email',
);
}
$fields = array(
array(
'title' => esc_html__( 'Notifications', 'gravityforms' ),
'fields' => array(
array(
'name' => 'name',
'label' => esc_html__( 'Name', 'gravityforms' ),
'type' => 'text',
'required' => true,
),
array(
'name' => 'service',
'label' => esc_html__( 'Email Service', 'gravityforms' ),
'type' => 'radio',
'choices' => $services_choices,
'default_value' => $services_choices[0]['value'],
'hidden' => count( $services_choices ) === 1,
),
array(
'name' => 'event',
'label' => esc_html__( 'Event', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_event', null, true ),
'type' => 'select',
'choices' => $events_choices,
'hidden' => count( $events_choices ) === 1,
),
$to_type,
array(
'name' => 'toEmail',
'label' => esc_html__( 'Send To Email', 'gravityforms' ),
'type' => 'text',
'required' => true,
'default_value' => '{admin_email}',
'dependency' => array(
'live' => true,
'fields' => array(
array(
'field' => 'toType',
'values' => array( 'email' ),
),
),
),
'validation_callback' => function( $field, $value ) {
// Determine if valid.
$is_valid = GFNotification::is_valid_notification_email( $value );
// Get filter parameters.
$to_type = GFNotification::get_settings_renderer()->get_value( 'toType' );
$to_field = GFNotification::get_settings_renderer()->get_value( 'toField' );
/**
* Allows overriding of the notification destination validation
*
* @since Unknown
*
* @param bool $is_valid True if valid. False, otherwise.
* @param string $gform_notification_to_type The type of destination.
* @param string $gform_notification_to_email The destination email address, if available.
* @param string $gform_notification_to_field The field that is being used for the notification, if available.
*/
$is_valid = apply_filters( 'gform_is_valid_notification_to', $is_valid, $to_type, $value, $to_field );
if ( ! $is_valid ) {
$field->set_error( __( 'Please enter a valid email address.', 'gravityforms' ) );
}
},
),
array(
'name' => 'toField',
'label' => esc_html__( 'Send To Field', 'gravityforms' ),
'type' => 'field_select',
'required' => true,
'args' => array( 'input_types' => array( 'email' ) ),
'no_choices' => esc_html__( 'Your form does not have an email field. Add an email field to your form and try again.', 'gravityforms' ),
'fields_callback' => array( self::class, 'append_filtered_notification_email_fields' ),
'dependency' => array(
'live' => true,
'fields' => array(
array(
'field' => 'toType',
'values' => array( 'field' ),
),
),
),
'validation_callback' => function ( $field, $value ) {
// Get filter parameters.
$to_type = GFNotification::get_settings_renderer()->get_value( 'toType' );
$to_email = GFNotification::get_settings_renderer()->get_value( 'toEmail' );
/**
* Allows overriding of the notification destination validation
*
* @since Unknown
*
* @param bool $is_valid True if valid. False, otherwise.
* @param string $gform_notification_to_type The type of destination.
* @param string $gform_notification_to_email The destination email address, if available.
* @param string $gform_notification_to_field The field that is being used for the notification, if available.
*/
$is_valid = apply_filters( 'gform_is_valid_notification_to', ! empty( $value ), $to_type, $to_email, $value );
if ( ! $is_valid ) {
$field->set_error( __( 'Please select an Email Address field.', 'gravityforms' ) );
}
},
),
array(
'name' => 'routing',
'type' => 'notification_routing',
'dependency' => array(
'live' => true,
'fields' => array(
array(
'field' => 'toType',
'values' => array( 'routing' ),
),
),
),
),
array(
'name' => 'fromName',
'label' => esc_html__( 'From Name', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_from_name', null, true ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
),
array(
'name' => 'from',
'label' => esc_html__( 'From Email', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_from_email', null, true ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
'default_value' => '{admin_email}',
'after_input' => $from_email_warning,
'validation_callback' => function( $field, $value ) {
if ( ! empty( $value ) && ! GFNotification::is_valid_notification_email( $value ) ) {
$field->set_error( __( 'Please enter a valid email address or merge tag in the From Email field.', 'gravityforms' ) );
}
},
),
array(
'name' => 'replyTo',
'label' => esc_html__( 'Reply To', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_reply_to', null, true ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
'validation_callback' => function( $field, $value ) {
if ( ! empty( $value ) && ! GFNotification::is_valid_notification_email( $value ) ) {
$field->set_error( __( 'Please enter a valid email address or merge tag in the Reply To field.', 'gravityforms' ) );
}
},
),
array(
'name' => 'cc',
'label' => esc_html__( 'CC', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_cc', null, true ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
'dependency' => function() use ( $form, $notification ) {
/**
* Enable the CC Notification field.
*
* @since 2.3
*
* @param bool $enable_cc Should the CC field be enabled?
* @param array $notification The current notification object.
* @param array $from The current form object.
*/
return gf_apply_filters( array( 'gform_notification_enable_cc', $form['id'], rgar( $notification, 'id' ) ), false, $notification, $form );
},
'validation_callback' => function( $field, $value ) {
if ( ! empty( $value ) && ! GFNotification::is_valid_notification_email( $value ) ) {
$field->set_error( __( 'Please enter a valid email address or merge tag in the CC field.', 'gravityforms' ) );
}
},
),
array(
'name' => 'bcc',
'label' => esc_html__( 'BCC', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_bcc', null, true ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
'validation_callback' => function( $field, $value ) {
if ( ! empty( $value ) && ! GFNotification::is_valid_notification_email( $value ) ) {
$field->set_error( __( 'Please enter a valid email address or merge tag in the BCC field.', 'gravityforms' ) );
}
},
),
array(
'name' => 'subject',
'label' => esc_html__( 'Subject', 'gravityforms' ),
'type' => 'text',
'class' => 'merge-tag-support mt-position-right mt-hide_all_fields',
'required' => true,
),
array(
'name' => 'message',
'label' => esc_html__( 'Message', 'gravityforms' ),
'type' => 'textarea',
'use_editor' => true,
'required' => true,
),
array(
'name' => 'disableAutoformat',
'label' => esc_html__( 'Auto-formatting', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_autoformat', null, true ),
'type' => 'checkbox',
'choices' => array(
array(
'name' => 'disableAutoformat',
'label' => esc_html__( 'Disable auto-formatting', 'gravityforms' ),
),
),
),
array(
'name' => 'enableAttachments',
'label' => esc_html__( 'Attachments', 'gravityforms' ),
'tooltip' => gform_tooltip( 'notification_attachments', null, true ),
'type' => 'checkbox',
'choices' => array(
array(
'name' => 'enableAttachments',
'label' => esc_html__( 'Attach uploaded fields to notification', 'gravityforms' ),
),
),
'dependency' => function() use ( $form ) {
$upload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) );
return ! empty( $upload_fields );
}
),
array(
'name' => 'conditionalLogic',
'label' => esc_html__( 'Conditional Logic', 'gravityforms ' ),
'type' => 'conditional_logic',
'object_type' => 'notification',
'checkbox' => array(
'label' => esc_html__( 'Enable conditional logic', 'gravityforms' ),
'hidden' => false,
),
),
array(
'type' => 'save',
'value' => esc_html__( 'Update Notification', 'gravityforms' ),
),
),
),
);
// Append registered legacy settings to the fields array.
$fields = self::append_legacy_settings_fields( $fields, $notification, $form );
/**
* Filters the Notification settings fields before they are displayed.
*
* @since 2.5
*
* @param array $fields Form settings fields.
* @param array $form Form Object.
*/
$fields = gf_apply_filters( array( 'gform_notification_settings_fields', $form['id'] ), $fields, $notification, $form );
return $fields;
}
/**
* Pass the field choices for the select field through the gform_email_fields_notification_admin filter to allow
* third-parties to add or remove arbitrary fields.
*
* @since 2.5.7
*
* @param array $fields The form fields to be used as choices.
* @param array $form The form belonging to the notification being configured.
*
* @return array
*/
public static function append_filtered_notification_email_fields( $fields, $form ) {
return gf_apply_filters( array( 'gform_email_fields_notification_admin', $form['id'] ), $fields, $form );
}
/**
* Appends any legacy settings fields to the fields array, if they exist.
*
* @since 2.5.6
*
* @param array $fields Array of settings fields.
* @param array $notification The notification being edited.
* @param array $form The form being edited.
*
* @return array
*/
private static function append_legacy_settings_fields( $fields, $notification, $form ) {
/**
* Add new or modify existing notification settings that display on the Notification Edit screen.
*
* @deprecated
* @since 1.7
* @remove-in 3.0
* @param array $ui_settings An array of settings for the notification UI.
* @param array $notification The current notification object being edited.
* @param array $form The current form object to which the notification being edited belongs.
* @param null $is_valid Whether or not the current notification has passed validation. (Deprecated.)
*/
$legacy_settings = apply_filters( 'gform_notification_ui_settings', array(), $notification, $form, null );
if ( has_filter( 'gform_notification_ui_settings' ) ) {
trigger_error( 'gform_notification_ui_settings is deprecated and will be removed in version 3.0.', E_USER_DEPRECATED );
}
if ( empty( $legacy_settings ) ) {
return $fields;
}
// Add the Legacy Settings section.
$fields[] = array(
'title' => esc_html__( 'Legacy Settings', 'gravityforms' ),
'class' => 'gform-settings-panel--full',
'fields' => array(
array(
'name' => 'legacy',
'type' => 'html',
'html' => function() use ( $legacy_settings ) {
$html = '<table class="gforms_form_settings" cellspacing="0" cellpadding="0" width="100%">';
foreach ( $legacy_settings as $title => $legacy_fields ) {
$html .= sprintf(
'<tr><td colspan="2"><h4 class="gf_settings_subgroup_title">%s</h4></td>',
esc_html( $title )
);
switch ( $legacy_fields ) {
case is_string( $legacy_fields ):
$html .= $legacy_fields;
break;
case is_array( $legacy_fields ):
foreach ( $legacy_fields as $field ) {
$html .= $field;
}
break;
}
}
return $html . '</table>';
},
),
),
);
return $fields;
}
// # SETTINGS RENDERER ---------------------------------------------------------------------------------------------
/**
* Initialize Plugin Settings fields renderer.
*
* @since 2.5
*/
public static function initialize_settings_renderer() {
if ( ! class_exists( 'GFFormSettings' ) ) {
require_once( GFCommon::get_base_path() . '/form_settings.php' );
}
$form_id = rgget( 'id' );
$notification_id = rgget( 'nid' );
if ( ! rgempty( 'gform_notification_id' ) ) {
$notification_id = rgpost( 'gform_notification_id' );
}
$form = GFFormsModel::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 );
// Prepare initial values.
$initial_notification = $notification;
$initial_notification['toEmail'] = rgar( $notification, 'toType' ) === 'email' ? rgar( $notification, 'to' ) : '';
$initial_notification['toField'] = rgar( $notification, 'toType' ) === 'field' ? rgar( $notification, 'to' ) : '';
$initial_notification['notification_conditional_logic'] = is_array( rgar( $notification, 'conditionalLogic' ) );
$initial_notification['notification_conditional_logic_object'] = rgar( $notification, 'conditionalLogic' );
// Initialize new settings renderer.
$renderer = new Settings(
array(
'initial_values' => $initial_notification,
'save_callback' => function( $values ) use ( &$notification, &$form, &$notification_id ) {
// Determine if new notification.
$is_new_notification = empty( $notification ) || empty( $notification_id );
// Set notification ID.
if ( $is_new_notification ) {
$notification_id = $notification['id'] = uniqid();
}
// Removing legacy (pre-1.7) admin/user notification property.
unset( $notification['type'] );
// Save values to the confirmation object in advance so non-custom values will be rewritten when we apply values below.
$notification = GFFormSettings::save_changed_form_settings_fields( $notification, $values );
$notification['name'] = rgar( $values, 'name' );
$notification['service'] = rgar( $values, 'service' );
$notification['event'] = rgar( $values, 'event' );
$notification['toType'] = rgar( $values, 'toType' );
$notification['to'] = rgar( $values, 'toType' ) === 'email' ? rgar( $values, 'toEmail' ) : ( rgar( $values, 'toType' ) === 'field' ? rgar( $values, 'toField' ) : '' );
$notification['from'] = rgar( $values, 'from' );
$notification['fromName'] = rgar( $values, 'fromName' );
$notification['replyTo'] = rgar( $values, 'replyTo' );
$notification['cc'] = rgar( $values, 'cc' );
$notification['bcc'] = rgar( $values, 'bcc' );
$notification['subject'] = rgar( $values, 'subject' );
$notification['message'] = rgar( $values, 'message' );
$notification['disableAutoformat'] = (bool) rgar( $values, 'disableAutoformat' );
$notification['enableAttachments'] = (bool) rgar( $values, 'enableAttachments' );
// Set the conditional logic object, and clear it if conditional logic is disabled
$conditionalLogicObject = rgar( $values, 'notification_conditional_logic_object' );
$notification['conditionalLogic'] = rgar( $values, 'notification_conditional_logic' ) && is_array( $conditionalLogicObject ) ? GFFormsModel::sanitize_conditional_logic( $conditionalLogicObject ) : null;
if ( isset( $values['routing'] ) && ! empty( $values['routing'] ) ) {
$routing_logic = array( 'rules' => $values['routing'] );
$routing_logic = GFFormsModel::sanitize_conditional_logic( $routing_logic );
$notification['routing'] = $routing_logic['rules'];
}
$notification = GFCommon::fix_notification_routing( $notification );
/**
* Filters the notification before it is saved
*
* @since 1.7
*
* @param array $form The Form Object.
* @param bool $is_new_notification True if it is a new notification. False otherwise.
*
* @param array $notification The Notification Object.
*/
$notification = gf_apply_filters( array(
'gform_pre_notification_save',
$form['id'],
), $notification, $form, $is_new_notification );
// Save notification.
$notification = GFFormsModel::trim_conditional_logic_values_from_element( $notification, $form );
$form['notifications'][ $notification_id ] = $notification;
RGFormsModel::save_form_notifications( $form['id'], $form['notifications'] );
self::$_saved_item_id = $notification_id;
},
'before_fields' => function() use ( &$form, $form_id, &$notification, $notification_id ) {
?>
<script type="text/javascript">
gform.addFilter( 'gform_merge_tags', 'MaybeAddSaveLinkMergeTag' );
function MaybeAddSaveLinkMergeTag( mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option ) {
var event = document.getElementById( '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 ) ?>;
jQuery( function() {
ToggleConditionalLogic( true, 'notification' );
} );
<?php GFFormSettings::output_field_scripts() ?>
</script>
<?php
},
'after_fields' => function() use ( &$notification_id ) {
printf( '<input type="hidden" id="gform_notification_id" name="gform_notification_id" value="%s" />', esc_attr( $notification_id ) );
}
)
);
// Save renderer to class.
self::set_settings_renderer( $renderer );
// Define settings fields.
self::get_settings_renderer()->set_fields( self::settings_fields( $notification, $form ) );
if ( self::is_save_redirect( 'nid' ) ) {
self::get_settings_renderer()->set_save_message_after_redirect();
}
// Process save callback.
if ( self::get_settings_renderer()->is_save_postback() ) {
self::get_settings_renderer()->process_postback();
self::redirect_after_valid_save( 'nid' );
}
}
/**
* Gets the current instance of Settings handling settings rendering.
*
* @since 2.5
*
* @return false|Settings
*/
public static function get_settings_renderer() {
return self::$_settings_renderer;
}
/**
* Sets the current instance of Settings handling settings rendering.
*
* @since 2.5
*
* @param Settings $renderer Settings renderer.
*
* @return bool|WP_Error
*/
private static function set_settings_renderer( $renderer ) {
// Ensure renderer is an instance of Settings
if ( ! is_a( $renderer, 'Gravity_Forms\Gravity_Forms\Settings\Settings' ) ) {
return new WP_Error( 'Renderer must be an instance of Gravity_Forms\Gravity_Forms\Settings\Settings.' );
}
self::$_settings_renderer = $renderer;
return true;
}
// # NOTIFICATION LIST ---------------------------------------------------------------------------------------------
/**
* 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 );
$notification_table = new GFNotificationTable( $form );
$notification_table->prepare_items();
GFFormSettings::page_header();
?>
<div class="gform-settings-panel">
<header class="gform-settings-panel__header">
<h4 class="gform-settings-panel__title"><?php esc_html_e( 'Notifications', 'gravityforms' ); ?></h4>
</header>
<div class="gform-settings-panel__content">
<form id="notification_list_form" method="post">
<?php
$notification_table->display();
wp_nonce_field( 'gform_notification_list_action', 'gform_notification_list_action' );
?>
<input id="action_argument" name="action_argument" type="hidden" />
<input id="action" name="action" type="hidden" />
</form>
</div>
</div>
<script type="text/javascript">
function ToggleActive( btn, notification_id ) {
var is_active = jQuery( btn ).hasClass( 'gform-status--active' );
jQuery.ajax(
{
url: '<?php echo admin_url( 'admin-ajax.php' ); ?>',
method: 'POST',
dataType: 'json',
data: {
action: 'rg_update_notification_active',
rg_update_notification_active: '<?php echo wp_create_nonce( 'rg_update_notification_active' ); ?>',
form_id: '<?php echo intval( $form_id ); ?>',
notification_id: notification_id,
is_active: is_active ? 0 : 1,
},
success: function() {
if ( is_active ) {
setToggleInactive();
} else {
setToggleActive();
}
},
error: function() {
if ( ! is_active ) {
setToggleInactive();
} else {
setToggleActive();
}
alert( '<?php echo esc_js( __( 'Ajax error while updating form', 'gravityforms' ) ); ?>' );
}
}
);
function setToggleInactive() {
jQuery( btn ).removeClass( 'gform-status--active' ).addClass( 'gform-status--inactive' ).find( '.gform-status-indicator-status' ).html( <?php echo wp_json_encode( esc_attr__( 'Inactive', 'gravityforms' ) ); ?> );
}
function setToggleActive() {
jQuery( btn ).removeClass( 'gform-status--inactive' ).addClass( 'gform-status--active' ).find( '.gform-status-indicator-status' ).html( <?php echo wp_json_encode( esc_attr__( 'Active', 'gravityforms' ) ); ?> );
}
}
</script>
<?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;
}
}
/**
* Get list of notification services.
*
* @since Unknown
* @access public
*
* @return array The notification services available.
*/
public static function get_notification_services() {
$services = array(
'wordpress' => array(
'label' => esc_html__( 'WordPress', 'gravityforms' ),
'image' => admin_url( 'images/wordpress-logo.svg' )
)
);
/**
* Filters the list of notification services.
*
* @since 1.9.16
*
* @param array $services The services available.
*/
return gf_apply_filters( array( 'gform_notification_services' ), $services );
}
/**
* Get the notification events for the current form.
*
* @since Unknown