PluginProbe
WANotifier for Forms and Actions / trunk
WANotifier for Forms and Actions vtrunk
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-ninjaforms.php

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

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