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-wsform.php

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

202 lines 5.2 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 class Notifier_WSForm {
7
8 /**
9 * Init
10 */
11 public static function init() {
12 add_filter( 'notifier_notification_triggers', array( __CLASS__, 'add_triggers'), 10 );
13 }
14
15 /**
16 * Add notification triggers
17 */
18 public static function add_triggers( $existing_triggers ) {
19
20 if ( ! class_exists( 'WS_Form_Form' ) ) {
21 return $existing_triggers;
22 }
23
24 $ws_form = new WS_Form_Form();
25 $forms = $ws_form->db_read_all( '', "NOT status='trash'", 'label ASC', '', '', false, true );
26
27 if ( empty( $forms ) || ! is_array( $forms ) ) {
28 return $existing_triggers;
29 }
30
31 $triggers = array();
32 foreach ( $forms as $form ) {
33 $form_id = $form['id'];
34 $trigger_id = 'wsform_' . $form_id;
35
36 $triggers[] = array(
37 'id' => $trigger_id,
38 'label' => 'Form "' . $form['label'] . '" submitted',
39 'description' => 'Trigger notification when <b>' . $form['label'] . '</b> form is submitted.',
40 'merge_tags' => self::get_merge_tags( $form ),
41 'recipient_fields' => self::get_recipient_fields( $form ),
42 'action' => array(
43 'hook' => 'wsf_submit_post_complete',
44 'args_num' => 1,
45 'callback' => function ( $submit ) use ( $trigger_id, $form_id ) {
46 if ( ! is_object( $submit ) || empty( $submit->form_id ) ) {
47 return;
48 }
49
50 if ( (int) $submit->form_id !== (int) $form_id ) {
51 return;
52 }
53
54 $sanitized_data = array();
55
56 if ( ! empty( $submit->meta ) && is_array( $submit->meta ) ) {
57 foreach ( $submit->meta as $meta_key => $meta_data ) {
58 if ( strpos( $meta_key, 'field_' ) === 0 ) {
59 $value = isset( $meta_data['value'] ) ? $meta_data['value'] : '';
60 if ( is_array( $value ) ) {
61 $sanitized_data[ $meta_key ] = notifier_sanitize_array( $value );
62 } else {
63 $sanitized_data[ $meta_key ] = sanitize_text_field( $value );
64 }
65 }
66 }
67 }
68
69 Notifier_Notification_Triggers::send_trigger_request( $trigger_id, $sanitized_data );
70 }
71 )
72 );
73 }
74
75 $existing_triggers['WS Form'] = $triggers;
76 return $existing_triggers;
77 }
78
79 /**
80 * Get form object with groups for field extraction.
81 *
82 * @param array $form Form row from db_read_all.
83 * @return object|null Form object with groups or null.
84 */
85 private static function get_form_object( $form ) {
86
87 if ( empty( $form['id'] ) || ! class_exists( 'WS_Form_Form' ) ) {
88 return null;
89 }
90
91 $ws_form = new WS_Form_Form();
92 $ws_form->id = (int) $form['id'];
93
94 try {
95 $form_object = $ws_form->db_read( true, true, false, false, true );
96 return is_object( $form_object ) && ! empty( $form_object->groups ) ? $form_object : null;
97 } catch ( Exception $e ) {
98 return null;
99 }
100 }
101
102 /**
103 * Get merge tags
104 *
105 * @param array $form Form row from db_read_all.
106 */
107 public static function get_merge_tags( $form ) {
108
109 $merge_tags = Notifier_Notification_Merge_Tags::get_merge_tags();
110 $form_object = self::get_form_object( $form );
111
112 if ( ! $form_object || ! method_exists( 'WS_Form_Common', 'get_fields_from_form' ) ) {
113 return $merge_tags;
114 }
115
116 $fields = WS_Form_Common::get_fields_from_form( $form_object, true );
117
118 if ( empty( $fields ) ) {
119 return $merge_tags;
120 }
121
122 $form_id = (int) $form['id'];
123 $excluded_types = array( 'html', 'section', 'captcha', 'page', 'header', 'spacer', 'submit' );
124
125 foreach ( $fields as $field ) {
126
127 if ( empty( $field->id ) || in_array( $field->type, $excluded_types, true ) ) {
128 continue;
129 }
130
131 $field_name = 'field_' . $field->id;
132 $label = isset( $field->label ) ? $field->label : $field_name;
133
134 $merge_tags['WS Form'][] = array(
135 'id' => 'wsform_' . $form_id . '_' . $field_name,
136 'label' => $label,
137 'preview_value'=> '',
138 'return_type' => 'text',
139 'value' => function ( $sanitized_data ) use ( $field_name ) {
140
141 $value = isset( $sanitized_data[ $field_name ] ) ? $sanitized_data[ $field_name ] : '';
142
143 if ( is_array( $value ) ) {
144 $value = implode( ', ', $value );
145 }
146
147 return html_entity_decode( sanitize_text_field( $value ) );
148 }
149 );
150 }
151
152 return $merge_tags;
153 }
154
155 /**
156 * Get recipient fields
157 *
158 * @param array $form Form row from db_read_all.
159 */
160 public static function get_recipient_fields( $form ) {
161
162 $recipient_fields = array();
163 $form_object = self::get_form_object( $form );
164
165 if ( ! $form_object || ! method_exists( 'WS_Form_Common', 'get_fields_from_form' ) ) {
166 return $recipient_fields;
167 }
168
169 $fields = WS_Form_Common::get_fields_from_form( $form_object, true );
170
171 if ( empty( $fields ) ) {
172 return $recipient_fields;
173 }
174
175 $form_id = (int) $form['id'];
176
177 foreach ( $fields as $field ) {
178
179 if ( empty( $field->id ) || $field->type !== 'tel' ) {
180 continue;
181 }
182
183 $field_name = 'field_' . $field->id;
184 $label = isset( $field->label ) ? $field->label : $field_name;
185
186 $recipient_fields['WS Form'][] = array(
187 'id' => 'wsform_' . $form_id . '_' . $field_name,
188 'label' => $label,
189 'value' => function ( $sanitized_data ) use ( $field_name ) {
190
191 $value = isset( $sanitized_data[ $field_name ] ) ? $sanitized_data[ $field_name ] : '';
192
193 return html_entity_decode( sanitize_text_field( $value ) );
194 }
195 );
196 }
197
198 return $recipient_fields;
199 }
200
201 }
202