PluginProbe
WANotifier for Forms and Actions / 3.0.3
WANotifier for Forms and Actions v3.0.3
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-gravityforms.php

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

182 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Gravity Forms notifications
8 *
9 * @package Wa_Notifier
10 */
11 class Notifier_GravityForms {
12
13 /**
14 * Init.
15 */
16 public static function init() {
17 add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_triggers'), 10 );
18 }
19
20 /**
21 * Add notification triggers
22 */
23 public static function add_triggers($existing_triggers) {
24 $triggers = array();
25 $forms = GFAPI::get_forms(true);
26 foreach($forms as $form){
27 $trigger_id = 'gravityforms_' . $form['id'];
28 $triggers[] = array(
29 'id' => $trigger_id,
30 'label' => 'Form "' . $form['title'] . '" is submitted',
31 'description' => 'Trigger notification when <b>'.$form['title'].'</b> form is submitted.',
32 'merge_tags' => self::get_merge_tags($form),
33 'recipient_fields' => self::get_recipient_fields($form),
34 'action' => array(
35 'hook' => 'gform_after_submission_' . $form['id'],
36 'callback' => function ( $entry ) use ( $trigger_id ) {
37 Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $entry );
38 }
39 )
40 );
41 }
42 $existing_triggers['Gravity Forms'] = $triggers;
43 return $existing_triggers;
44 }
45
46 /**
47 * Get merge tags for the current form
48 */
49 public static function get_merge_tags( $form ) {
50 $merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags();
51 $gf_fields = array(
52 'ip' => array(
53 'label' => 'User IP',
54 'preview' => '0.0.0.0'
55 ),
56 'source_url' => array(
57 'label' => 'Source URL',
58 'preview' => site_url()
59 ),
60 'date_created' => array(
61 'label' => 'Date created'
62 ),
63 'date_updated' => array(
64 'label' => 'Date updated'
65 ),
66 'user_agent' => array(
67 'label' => 'User agent',
68 'preview' => 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36'
69 )
70 );
71
72 foreach ($gf_fields as $field_id => $field_data) {
73 $merge_tags['Gravity Forms'][] = array(
74 'id' => 'gravityforms_' . $field_id,
75 'label' => $field_data['label'],
76 'preview_value' => isset($field_data['preview']) ? $field_data['preview'] : '',
77 'return_type' => isset($field_data['return_type']) ? $field_data['return_type'] : 'text',
78 'value' => function ( $entry ) use ($field_id) {
79 $value = isset($entry[$field_id]) ? $entry[$field_id] : '';
80 return html_entity_decode(sanitize_text_field($value));
81 }
82 );
83 }
84
85 $excluded_field_types = array('html', 'section', 'captcha', 'page');
86 foreach($form['fields'] as $field){
87 if(in_array($field->type, $excluded_field_types)){
88 continue;
89 }
90
91 if('post_image' == $field->type){
92 $return_type = 'image';
93 }
94 else{
95 $return_type = 'text';
96 }
97
98 if(empty($field->inputs)) {
99 $field_id = $field->id;
100 $field_type = $field->type;
101 $merge_tags['Gravity Forms'][] = array(
102 'id' => 'gravityforms_' . $form['id'] . '_' . $field_id,
103 'label' => $field->label,
104 'preview_value' => '',
105 'return_type' => $return_type,
106 'value' => function ( $entry ) use ( $field_id, $field_type ) {
107 if('list' == $field_type){
108 $values = maybe_unserialize( $entry[$field_id] );
109 $value = implode(', ', $values);
110 }
111 else{
112 $value = isset($entry[$field_id]) ? $entry[$field_id] : '';
113 }
114 return html_entity_decode(sanitize_text_field($value));
115 }
116 );
117 }
118 else if('checkbox' == $field->type){
119 $field_id = $field->id;
120 $merge_tags['Gravity Forms'][] = array(
121 'id' => 'gravityforms_' . $form['id'] . '_' . $field_id,
122 'label' => $field->label,
123 'preview_value' => '',
124 'return_type' => $return_type,
125 'value' => function ( $entry ) use ( $field_id ) {
126 $values = array();
127 foreach($entry as $key => $value){
128 if ( strpos($key, $field_id . '.') !== false && '' != trim($value)){
129 $values[] = $value;
130 }
131 }
132 $final_value = implode(', ', $values);
133 return html_entity_decode(sanitize_text_field($final_value));
134 }
135 );
136 }
137 else {
138 foreach($field->inputs as $input){
139 $field_id = $input['id'];
140 if(isset($input['isHidden']) && '1' == $input['isHidden']){
141 continue;
142 }
143 $merge_tags['Gravity Forms'][] = array(
144 'id' => 'gravityforms_' . $form['id'] . '_' . $field_id,
145 'label' => $field->label . ' (' . $input['label'] . ')',
146 'preview_value' => '',
147 'return_type' => $return_type,
148 'value' => function ( $entry ) use ( $field_id ) {
149 $value = isset($entry[$field_id]) ? $entry[$field_id] : '';
150 return html_entity_decode(sanitize_text_field($value));
151 }
152 );
153 }
154 }
155 }
156 return $merge_tags;
157 }
158
159 /*
160 * Get recipient fields
161 */
162 public static function get_recipient_fields($form){
163 $recipient_fields = array();
164 foreach($form['fields'] as $field){
165 if('phone' != $field->type){
166 continue;
167 }
168 $field_id = $field->id;
169 $recipient_fields['Gravity Forms'][] = array(
170 'id' => 'gravityforms_' . $form['id'] . '_' . $field_id,
171 'label' => $field->label,
172 'value' => function ( $entry ) use ( $field_id ) {
173 $value = isset($entry[$field_id]) ? $entry[$field_id] : '';
174 return html_entity_decode(sanitize_text_field($value));
175 }
176 );
177 }
178 return $recipient_fields;
179 }
180
181 }
182