| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { |
| 3 |
exit; |
| 4 |
} |
| 5 |
|
| 6 |
/** |
| 7 |
* FluentForms notifications |
| 8 |
* |
| 9 |
* @package Wa_Notifier |
| 10 |
*/ |
| 11 |
class Notifier_FluentForms { |
| 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 |
|
| 26 |
$formApi = fluentFormApi('forms'); |
| 27 |
|
| 28 |
$atts = [ |
| 29 |
'status' => 'published', |
| 30 |
'sort_column' => 'id', |
| 31 |
'sort_by' => 'DESC', |
| 32 |
'page' => 1, |
| 33 |
]; |
| 34 |
|
| 35 |
$forms = $formApi->forms($atts, $withFields = false); |
| 36 |
|
| 37 |
if(is_array($forms['data'])){ |
| 38 |
foreach($forms['data'] as $form){ |
| 39 |
$trigger_id = 'fluentforms_' . $form->id; |
| 40 |
$form_id = $form->id; |
| 41 |
$title = $form->title; |
| 42 |
$triggers[] = array( |
| 43 |
'id' => $trigger_id, |
| 44 |
'label' => 'Form "' . $title . '" is submitted', |
| 45 |
'description' => 'Trigger notification when <b>'.$title.'</b> form is submitted.', |
| 46 |
'merge_tags' => self::get_merge_tags($form->id), |
| 47 |
'recipient_fields' => self::get_recipient_fields($form->id), |
| 48 |
'action' => array( |
| 49 |
'hook' => 'fluentform_submission_inserted', |
| 50 |
'callback' => function ( $entryId, $formData, $form ) use ( $trigger_id, $form_id ) { |
| 51 |
if($form->id != $form_id){ |
| 52 |
return; |
| 53 |
} |
| 54 |
Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $formData ); |
| 55 |
}, |
| 56 |
'args_num' => 3, |
| 57 |
) |
| 58 |
); |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
$existing_triggers['Fluent Forms'] = $triggers; |
| 63 |
return $existing_triggers; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Get merge tags for the current form |
| 68 |
*/ |
| 69 |
public static function get_merge_tags( $form_id ) { |
| 70 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags(); |
| 71 |
|
| 72 |
$fields_data = array(); |
| 73 |
$formApi = fluentFormApi('forms')->form($form_id); |
| 74 |
$form_fields = $formApi->fields(); |
| 75 |
$excluded_field_types = array('custom_submit_button', 'section_break', 'custom_html','tabular_grid','repeater_field','terms_and_condition','action_hook','form_step','chained_select','save_progress_button', 'cpt_selection','shortcode'); |
| 76 |
|
| 77 |
if(is_array($form_fields)){ |
| 78 |
foreach($form_fields['fields'] as $key => $value){ |
| 79 |
if(in_array($value['element'], $excluded_field_types)){ |
| 80 |
continue; |
| 81 |
} |
| 82 |
|
| 83 |
if('input_name' === $value['element']){ |
| 84 |
foreach($value['fields'] as $sub_fkey => $sub_fvalue){ |
| 85 |
$field_type = $sub_fvalue['attributes']['type']; |
| 86 |
$field_lbl = $sub_fvalue['settings']['label']; |
| 87 |
$field_name = $sub_fvalue['attributes']['name']; |
| 88 |
|
| 89 |
$fields_data[$value['attributes']['name']]['type'] = $field_type; |
| 90 |
$fields_data[$value['attributes']['name']]['label'] = $value['settings']['admin_field_label']; |
| 91 |
$fields_data[$value['attributes']['name']][$sub_fkey] = [ |
| 92 |
'label' => $field_lbl |
| 93 |
]; |
| 94 |
} |
| 95 |
}else if('container' === $value['element']){ |
| 96 |
if(!empty($value['columns']) && is_array($value['columns'])){ |
| 97 |
foreach($value['columns'] as $col_key => $col_value){ |
| 98 |
foreach($col_value['fields'] as $ckey => $cval){ |
| 99 |
|
| 100 |
if(in_array($cval['element'], $excluded_field_types)){ |
| 101 |
continue; |
| 102 |
} |
| 103 |
|
| 104 |
$field_type = isset($cval['attributes']['type']) ? $cval['attributes']['type']: ''; |
| 105 |
$field_lbl = !empty($cval['settings']['label'])?$cval['settings']['label']:'Field_'.$cval['attributes']['name']; |
| 106 |
$field_name = $cval['attributes']['name']; |
| 107 |
if('input_image' == $cval['element']){ |
| 108 |
$field_type = 'image'; |
| 109 |
} |
| 110 |
|
| 111 |
if($field_name == 'names') { |
| 112 |
foreach($cval['fields'] as $field_key => $field_val){ |
| 113 |
$field_type = $field_val['attributes']['type']; |
| 114 |
$field_name = $field_val['attributes']['name']; |
| 115 |
$field_lbl = $field_val['settings']['label']; |
| 116 |
|
| 117 |
$fields_data[$cval['attributes']['name']]['type'] = $field_type; |
| 118 |
$fields_data[$cval['attributes']['name']]['label'] = $cval['settings']['admin_field_label']; |
| 119 |
$fields_data[$cval['attributes']['name']][$field_key] = [ |
| 120 |
'label' => $field_lbl |
| 121 |
]; |
| 122 |
} |
| 123 |
}else{ |
| 124 |
$fields_data[$field_name] = [ |
| 125 |
'label' => $field_lbl, |
| 126 |
'type' => $field_type, |
| 127 |
]; |
| 128 |
} |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
}else if('input_image' === $value['element']) { |
| 133 |
$field_type = 'image'; |
| 134 |
$field_name = isset($value['attributes']['name']) ? $value['attributes']['name'] : ''; |
| 135 |
|
| 136 |
if(!empty($value['settings']['label'])){ |
| 137 |
$field_lbl = $value['settings']['label']; |
| 138 |
}else if(!empty($value['settings']['admin_field_label'])){ |
| 139 |
$field_lbl = $value['settings']['admin_field_label']; |
| 140 |
}else{ |
| 141 |
$field_lbl = 'Field_'.$value['attributes']['name']; |
| 142 |
} |
| 143 |
|
| 144 |
$fields_data[$field_name] = [ |
| 145 |
'label' => $field_lbl, |
| 146 |
'type' => $field_type, |
| 147 |
]; |
| 148 |
}else { |
| 149 |
$field_type = !empty($value['attributes']['type']) ? $value['attributes']['type'] : 'text'; |
| 150 |
$field_name = isset($value['attributes']['name']) ? $value['attributes']['name'] : ''; |
| 151 |
|
| 152 |
if(!empty($value['settings']['label'])){ |
| 153 |
$field_lbl = $value['settings']['label']; |
| 154 |
}else if(!empty($value['settings']['admin_field_label'])){ |
| 155 |
$field_lbl = $value['settings']['admin_field_label']; |
| 156 |
}else{ |
| 157 |
$field_lbl = 'lbl_'.$field_name; |
| 158 |
} |
| 159 |
|
| 160 |
$fields_data[$field_name] = [ |
| 161 |
'label' => $field_lbl, |
| 162 |
'type' => $field_type, |
| 163 |
]; |
| 164 |
} |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
if(is_array($fields_data) && !empty($fields_data)){ |
| 169 |
foreach($fields_data as $field_key => $field_value){ |
| 170 |
|
| 171 |
$field_name = isset($field_value['name']) ? $field_value['name'] : ''; |
| 172 |
$field_label = $field_value['label']; |
| 173 |
$field_type = $field_value['type']; |
| 174 |
if($field_type == 'image'){ |
| 175 |
$return_type = 'image'; |
| 176 |
}else{ |
| 177 |
$return_type = 'text'; |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
$merge_tags['Fluent Forms'][] = array( |
| 182 |
'id' => 'fluentforms_' . $form_id . '_' . $field_key, |
| 183 |
'label' => ucfirst(str_replace('_',' ',$field_label)), |
| 184 |
'preview_value' => '', |
| 185 |
'return_type' => $return_type, |
| 186 |
'value' => function ( $formData ) use ( $field_key ) { |
| 187 |
$value = isset($formData[$field_key]) ? $formData[$field_key] : ''; |
| 188 |
if(is_array($value)){ |
| 189 |
$value = implode(', ', $value); |
| 190 |
} |
| 191 |
return html_entity_decode(sanitize_text_field($value)); |
| 192 |
} |
| 193 |
); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return $merge_tags; |
| 198 |
} |
| 199 |
|
| 200 |
/* |
| 201 |
* Get recipient fields |
| 202 |
*/ |
| 203 |
public static function get_recipient_fields( $form_id ){ |
| 204 |
$recipient_fields = array(); |
| 205 |
$fields_data = array(); |
| 206 |
$formApi = fluentFormApi('forms')->form($form_id); |
| 207 |
$form_fields = $formApi->fields(); |
| 208 |
$excluded_field_types = array('custom_submit_button', 'section_break', 'custom_html','tabular_grid','repeater_field','terms_and_condition','action_hook','form_step','chained_select','save_progress_button', 'cpt_selection', 'shortcode'); |
| 209 |
|
| 210 |
if(is_array($form_fields)){ |
| 211 |
foreach($form_fields['fields'] as $key => $value){ |
| 212 |
if(in_array($value['element'], $excluded_field_types)){ |
| 213 |
continue; |
| 214 |
} |
| 215 |
|
| 216 |
if('container' === $value['element']){ |
| 217 |
if(!empty($value['columns']) && is_array($value['columns'])){ |
| 218 |
foreach($value['columns'] as $col_key => $col_value){ |
| 219 |
foreach($col_value['fields'] as $ckey => $cval){ |
| 220 |
|
| 221 |
if(in_array($cval['element'], $excluded_field_types)){ |
| 222 |
continue; |
| 223 |
} |
| 224 |
|
| 225 |
$field_type = isset($cval['attributes']['type']) ? $cval['attributes']['type'] : ''; |
| 226 |
if('tel' == $field_type){ |
| 227 |
$field_lbl = !empty($cval['settings']['label'])?$cval['settings']['label']:'Field_'.$cval['attributes']['name']; |
| 228 |
$field_name = $cval['attributes']['name']; |
| 229 |
|
| 230 |
$fields_data[$field_name] = [ |
| 231 |
'label' => $field_lbl, |
| 232 |
'type' => $field_type, |
| 233 |
]; |
| 234 |
} |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
}else { |
| 239 |
$field_type = isset($value['attributes']['type']) ? $value['attributes']['type'] : ''; |
| 240 |
if('tel' == $field_type){ |
| 241 |
$field_name = $value['attributes']['name']; |
| 242 |
|
| 243 |
if(!empty($value['settings']['label'])){ |
| 244 |
$field_lbl = $value['settings']['label']; |
| 245 |
}else if(!empty($value['settings']['admin_field_label'])){ |
| 246 |
$field_lbl = $value['settings']['admin_field_label']; |
| 247 |
}else{ |
| 248 |
$field_lbl = 'lbl_'.$value['attributes']['name']; |
| 249 |
} |
| 250 |
|
| 251 |
$fields_data[$field_name] = [ |
| 252 |
'label' => $field_lbl, |
| 253 |
'type' => $field_type, |
| 254 |
]; |
| 255 |
} |
| 256 |
} |
| 257 |
} |
| 258 |
} |
| 259 |
|
| 260 |
if(is_array($fields_data) && !empty($fields_data)){ |
| 261 |
foreach($fields_data as $field_key => $field_value){ |
| 262 |
if('tel' != $field_value['type']){ |
| 263 |
continue; |
| 264 |
} |
| 265 |
|
| 266 |
$field_name = isset($field_value['name']) ? $field_value['name'] : ''; |
| 267 |
$field_label = $field_value['label']; |
| 268 |
|
| 269 |
$recipient_fields['Fluent Forms'][] = array( |
| 270 |
'id' => 'fluentforms_' . $form_id . '_' . $field_key, |
| 271 |
'label' => ucfirst(str_replace('_',' ',$field_label)), |
| 272 |
'value' => function ( $formData ) use ( $field_key ) { |
| 273 |
$value = isset($formData[$field_key]) ? $formData[$field_key] : ''; |
| 274 |
return html_entity_decode(sanitize_text_field($value)); |
| 275 |
} |
| 276 |
); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
return $recipient_fields; |
| 281 |
} |
| 282 |
} |
| 283 |
|