| 1 |
<?php |
| 2 |
/** |
| 3 |
* Formidable Form notifications |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
class Notifier_Formidable { |
| 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 = FrmForm::getall(); |
| 22 |
|
| 23 |
if(is_array($forms)){ |
| 24 |
foreach($forms as $form){ |
| 25 |
$form_id = $form->id; |
| 26 |
$title = $form->name; |
| 27 |
$trigger_id = 'formidableforms_' . $form_id; |
| 28 |
|
| 29 |
$triggers[] = array( |
| 30 |
'id' => $trigger_id, |
| 31 |
'label' => 'Form "' . $title . '" is submitted', |
| 32 |
'description' => 'Trigger notification when <b>'.$title.'</b> form is submitted.', |
| 33 |
'merge_tags' => self::get_merge_tags($form_id), |
| 34 |
'recipient_fields' => self::get_recipient_fields($form_id), |
| 35 |
'action' => array( |
| 36 |
'hook' => 'frm_after_create_entry', |
| 37 |
'callback' => function ( $entry_id, $form_id ) use ( $trigger_id ) { |
| 38 |
$trigger_form_id = str_replace('formidableforms_', '', $trigger_id); |
| 39 |
if($form_id != $trigger_form_id){ |
| 40 |
return; |
| 41 |
} |
| 42 |
|
| 43 |
$sanitized_data = array(); |
| 44 |
foreach($_POST['item_meta'] as $key => $data){ |
| 45 |
if(is_array($data)){ |
| 46 |
$sanitized_data[$key] = notifier_sanitize_array($data); |
| 47 |
} |
| 48 |
else{ |
| 49 |
$sanitized_data[$key] = sanitize_text_field($data); |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $sanitized_data ); |
| 54 |
}, |
| 55 |
'args_num' => 2, |
| 56 |
) |
| 57 |
); |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
$existing_triggers['Formidable Forms'] = $triggers; |
| 62 |
return $existing_triggers; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Get merge tags for the current form |
| 67 |
*/ |
| 68 |
public static function get_merge_tags( $form_id ) { |
| 69 |
|
| 70 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags(); |
| 71 |
$formidableforms_fields = FrmField::get_all_for_form( $form_id ); |
| 72 |
|
| 73 |
$excluded_field_types = array('submit', 'captcha', 'spam', 'hr', 'repeater', 'html'); |
| 74 |
$form_fields = $formidableforms_fields; |
| 75 |
|
| 76 |
|
| 77 |
if(is_array($form_fields)){ |
| 78 |
foreach($form_fields as $field){ |
| 79 |
|
| 80 |
if(in_array($field->type, $excluded_field_types)){ |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
$field_name = $field->type; |
| 85 |
$field_id = $field->id; |
| 86 |
$return_type = 'text'; |
| 87 |
|
| 88 |
$merge_tags['Formidable Forms'][] = array( |
| 89 |
'id' => 'formidableforms_' . $form_id . '_' . $field_id, |
| 90 |
'label' => $field_name, |
| 91 |
'preview_value' => '', |
| 92 |
'return_type' => $return_type, |
| 93 |
'value' => function ( $sanitized_data ) use ( $field_id ) { |
| 94 |
$value = isset($sanitized_data[$field_id]) ? $sanitized_data[$field_id] : ''; |
| 95 |
if(is_array($value)){ |
| 96 |
$value = implode(', ', $value); |
| 97 |
} |
| 98 |
return html_entity_decode(sanitize_text_field($value)); |
| 99 |
} |
| 100 |
); |
| 101 |
} |
| 102 |
} |
| 103 |
return $merge_tags; |
| 104 |
} |
| 105 |
|
| 106 |
/* |
| 107 |
* Get recipient fields |
| 108 |
*/ |
| 109 |
public static function get_recipient_fields( $form_id ){ |
| 110 |
$formidableforms_fields = FrmField::get_all_for_form( $form_id ); |
| 111 |
$form_fields = $formidableforms_fields; |
| 112 |
$recipient_fields = array(); |
| 113 |
|
| 114 |
if(is_array($form_fields)){ |
| 115 |
foreach($form_fields as $field){ |
| 116 |
if('phone' != $field->type){ |
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
$field_name = $field->name; |
| 121 |
$field_id = $field->id; |
| 122 |
|
| 123 |
$recipient_fields['Formidable Forms'][] = array( |
| 124 |
'id' => 'formidableforms_' . $form_id . '_' . $field_id, |
| 125 |
'label' => $field_name, |
| 126 |
'value' => function ( $sanitized_data ) use ( $field_id ) { |
| 127 |
$value = isset($sanitized_data[$field_id]) ? $sanitized_data[$field_id] : ''; |
| 128 |
return html_entity_decode(sanitize_text_field($value)); |
| 129 |
} |
| 130 |
); |
| 131 |
} |
| 132 |
} |
| 133 |
return $recipient_fields; |
| 134 |
} |
| 135 |
} |
| 136 |
|