| 1 |
<?php |
| 2 |
/** |
| 3 |
* Ninja Forms notifications |
| 4 |
* |
| 5 |
* @package Wa_Notifier |
| 6 |
*/ |
| 7 |
class Notifier_NinjaForms { |
| 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 = Ninja_Forms()->form()->get_forms(); |
| 22 |
|
| 23 |
if(is_array($forms)){ |
| 24 |
foreach($forms as $form){ |
| 25 |
$form_id = $form->get_id(); |
| 26 |
$title = $form->get_setting( 'form_title' ); |
| 27 |
$trigger_id = 'ninjaforms_' . $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' => 'ninja_forms_after_submission', |
| 37 |
'callback' => function ( $form_data ) use ( $trigger_id ) { |
| 38 |
Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $form_data ); |
| 39 |
}, |
| 40 |
) |
| 41 |
); |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
$existing_triggers['Ninja Forms'] = $triggers; |
| 46 |
return $existing_triggers; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Get merge tags for the current form |
| 51 |
*/ |
| 52 |
public static function get_merge_tags( $form_id ) { |
| 53 |
|
| 54 |
$merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags(); |
| 55 |
$ninjaforms_fields = Ninja_Forms()->form( $form_id )->get_fields(); |
| 56 |
|
| 57 |
$excluded_field_types = array('submit', 'recaptcha', 'confirm', 'spam', 'hr', 'repeater', 'save'); |
| 58 |
$form_fields = $ninjaforms_fields; |
| 59 |
|
| 60 |
if(is_array($form_fields)){ |
| 61 |
foreach($form_fields as $field){ |
| 62 |
|
| 63 |
if(in_array($field->get_setting('type'), $excluded_field_types)){ |
| 64 |
continue; |
| 65 |
} |
| 66 |
|
| 67 |
$field_name = $field->get_setting('label'); |
| 68 |
$field_id = $field->get_id(); |
| 69 |
$return_type = 'text'; |
| 70 |
|
| 71 |
$merge_tags['Ninja Forms'][] = array( |
| 72 |
'id' => 'ninjaforms_' . $form_id . '_' . $field_id, |
| 73 |
'label' => $field_name, |
| 74 |
'preview_value' => '', |
| 75 |
'return_type' => $return_type, |
| 76 |
'value' => function ( $form_data ) use ( $field_id ) { |
| 77 |
$value = isset($form_data['fields'][$field_id][ 'value' ]) ? $form_data['fields'][$field_id][ 'value' ] : ''; |
| 78 |
if(is_array($value)){ |
| 79 |
$value = implode(', ', $value); |
| 80 |
} |
| 81 |
return html_entity_decode(sanitize_text_field($value)); |
| 82 |
} |
| 83 |
); |
| 84 |
} |
| 85 |
} |
| 86 |
return $merge_tags; |
| 87 |
} |
| 88 |
|
| 89 |
/* |
| 90 |
* Get recipient fields |
| 91 |
*/ |
| 92 |
public static function get_recipient_fields( $form_id ){ |
| 93 |
$ninjaforms_fields = Ninja_Forms()->form( $form_id )->get_fields(); |
| 94 |
$form_fields = $ninjaforms_fields; |
| 95 |
$recipient_fields = array(); |
| 96 |
|
| 97 |
if(is_array($form_fields)){ |
| 98 |
foreach($form_fields as $field){ |
| 99 |
if('phone' != $field->get_setting('type')){ |
| 100 |
continue; |
| 101 |
} |
| 102 |
|
| 103 |
$field_name = $field->get_setting('label'); |
| 104 |
$field_id = $field->get_id(); |
| 105 |
|
| 106 |
$recipient_fields['Ninja Forms'][] = array( |
| 107 |
'id' => 'ninjaforms_' . $form_id . '_' . $field_id, |
| 108 |
'label' => $field_name, |
| 109 |
'value' => function ( $form_data ) use ( $field_id ) { |
| 110 |
$value = isset($form_data['fields'][$field_id][ 'value' ]) ? $form_data['fields'][$field_id][ 'value' ] : ''; |
| 111 |
return html_entity_decode(sanitize_text_field($value)); |
| 112 |
} |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
return $recipient_fields; |
| 117 |
} |
| 118 |
} |
| 119 |
|