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