PluginProbe
WANotifier for Forms and Actions / 2.7.1
WANotifier for Forms and Actions v2.7.1
3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 1.0.3 All 66 releases
notifier / includes / classes / integrations / class-notifier-wpforms.php

class-notifier-wpforms.php in WANotifier for Forms and Actions 2.7.1, at includes/classes/integrations/class-notifier-wpforms.php

121 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WPForms notifications
4 *
5 * @package Wa_Notifier
6 */
7 class Notifier_WPForms {
8
9 /**
10 * Init.
11 */
12 public static function init() {
13 add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_triggers'), 10 );
14 }
15
16 /**
17 * Add notification triggers
18 */
19 public static function add_triggers($existing_triggers) {
20 $triggers = array();
21 $form_ids = get_posts(array (
22 'post_type' => 'wpforms',
23 'post_status' => 'publish',
24 'numberposts' => -1,
25 'fields' => 'ids'
26 ));
27
28 foreach($form_ids as $form_id){
29 $trigger_id = 'wpf_' . $form_id;
30 $title = get_the_title( $form_id );
31 $triggers[] = array(
32 'id' => $trigger_id,
33 'label' => 'Form "' . $title . '" is submitted',
34 'description' => 'Trigger notification when <b>'.$title.'</b> form is submitted.',
35 'merge_tags' => self::get_merge_tags($form_id),
36 'recipient_fields' => self::get_recipient_fields($form_id),
37 'action' => array(
38 'hook' => 'wpforms_process_complete_'.$form_id,
39 'callback' => function ( $fields, $entry ) use ( $trigger_id ) {
40 Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $fields );
41 },
42 'args_num' => 2,
43 )
44 );
45 }
46 $existing_triggers['WPForms'] = $triggers;
47 return $existing_triggers;
48 }
49
50 /**
51 * Get merge tags for the current form
52 */
53 public static function get_merge_tags( $form_id ) {
54
55 $merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags();
56 $wpforms = wpforms()->get( 'form' )->get( $form_id, [ 'content_only' => true ] );
57
58 $excluded_field_types = array('html', 'captcha_recaptcha', 'pagebreak', 'divider');
59 $form_fields = $wpforms['fields'];
60
61 if(is_array($form_fields)){
62 foreach($form_fields as $field){
63
64 if(in_array($field['type'], $excluded_field_types)){
65 continue;
66 }
67
68 $field_name = $field['label'];
69 $field_id = $field['id'];
70 $return_type = 'text';
71
72 $merge_tags['WPForms'][] = array(
73 'id' => 'wpf_' . $form_id . '_' . $field_id,
74 'label' => $field_name,
75 'preview_value' => '',
76 'return_type' => $return_type,
77 'value' => function ( $fields ) use ( $field_id ) {
78 $value = isset($fields[$field_id][ 'value' ]) ? $fields[$field_id][ 'value' ] : '';
79 if(is_array($value)){
80 $value = implode(', ', $value);
81 }
82 return html_entity_decode(sanitize_text_field($value));
83 }
84 );
85 }
86 }
87 return $merge_tags;
88 }
89
90 /*
91 * Get recipient fields
92 */
93 public static function get_recipient_fields($form_id){
94 $wpforms = wpforms()->get( 'form' )->get( $form_id, [ 'content_only' => true ] );
95 $form_fields = $wpforms['fields'];
96
97 $recipient_fields = array();
98
99 if(is_array($form_fields)){
100 foreach($form_fields as $field){
101 if('phone' != $field['type']){
102 continue;
103 }
104
105 $field_name = $field['label'];
106 $field_id = $field['id'];
107
108 $recipient_fields['WPForms'][] = array(
109 'id' => 'wpf_' . $form_id . '_' . $field_id,
110 'label' => $field_name,
111 'value' => function ( $fields ) use ( $field_id ) {
112 $value = isset($fields[$field_id][ 'value' ]) ? $fields[$field_id][ 'value' ] : '';
113 return html_entity_decode(sanitize_text_field($value));
114 }
115 );
116 }
117 }
118 return $recipient_fields;
119 }
120 }
121