PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.6.1
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.6.1
1.6.7 1.6.8 1.6.9 1.6.12 1.6.13 1.6.14 1.6.15 1.6.16 1.6.17 1.6.18 1.6.19 1.6.2 1.6.20 1.6.21 1.6.22 1.6.23 1.6.24 1.6.25 1.6.26 1.6.27 1.6.28 1.6.3 1.6.4 1.6.5 1.6.6 All 74 releases
weforms / includes / importer / class-importer-ninja-forms.php

class-importer-ninja-forms.php in weForms – Easy Drag & Drop Contact Form Builder For WordPress 1.6.1, at includes/importer/class-importer-ninja-forms.php

451 lines 14.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Ninja Form
5 *
6 * Import Ninja form forms
7 */
8 class WeForms_Importer_Ninja_Forms extends WeForms_Importer_Abstract {
9
10 /**
11 * Will store submit button label
12 *
13 * @var string
14 */
15 private $submit_text;
16
17 public function __construct() {
18 $this->id = 'nf';
19 $this->title = 'Ninja Forms';
20 $this->shortcode = 'ninja_form';
21
22 parent::__construct();
23 }
24
25 /**
26 * See if the plugin exists
27 *
28 * @return bool
29 */
30 public function plugin_exists() {
31 return class_exists( 'Ninja_Forms' );
32 }
33
34 /**
35 * Show notice if Ninja From found
36 *
37 * @return void
38 */
39 public function ninja_form_field( $form ) {
40 $data = [];
41
42 foreach ( Ninja_Forms()->form( $form->get_id() )->get_fields() as $field ) {
43 $data[$field->get_settings( 'order' )] = [
44 'type' => $field->get_setting( 'type' ),
45 'key' => $field->get_setting( 'key' ),
46 'label' => $field->get_setting( 'label' ),
47 'default' => $field->get_setting( 'default' ),
48 'required' => $field->get_setting( 'required' ) ? $field->get_setting( 'required' ) : 0,
49 ];
50
51 if ( in_array( $field->get_setting( 'type' ), ['listselect', 'listradio', 'listcheckbox', 'listmultiselect'] ) ) {
52 foreach ( $field->get_setting( 'options' ) as $option ) {
53 $data[$field->get_settings( 'order' )]['options'][] = [
54 'label' => $option['label'],
55 'value' => $option['value'],
56 ];
57 }
58 }
59 }
60
61 return $data;
62 }
63
64 /**
65 * Get all the forms
66 *
67 * @return array
68 */
69 public function get_forms() {
70 $items = Ninja_Forms()->form()->get_forms();
71
72 return $items;
73 }
74
75 /**
76 * Get form name
77 *
78 * @param object $form
79 *
80 * @return string
81 */
82 public function get_form_name( $form ) {
83 return $form->get_setting( 'title' );
84 }
85
86 /**
87 * Get the form id
88 *
89 * @param mixed $form
90 *
91 * @return int
92 */
93 protected function get_form_id( $form ) {
94 return $form->get_id();
95 }
96
97 /**
98 * Get the form fields
99 *
100 * @param object $form
101 *
102 * @return array
103 */
104 public function get_form_fields( $form ) {
105 $ninja_form_fields = $this->ninja_form_field( $form );
106
107 // echo "<pre>";
108 // print_r($ninja_form_fields);
109 // echo "</pre>";
110
111 // die;
112
113 $form_fields = [];
114
115 foreach ( $ninja_form_fields as $menu_order => $field ) {
116 $field_content = [];
117
118 switch ( $field['type'] ) {
119 case 'textbox':
120 case 'text':
121 case 'email':
122 case 'textarea':
123 case 'date':
124 case 'url':
125 case 'firstname':
126 case 'lastname':
127 case 'zip':
128 case 'product':
129 case 'city':
130 case 'quiz':
131 case 'address':
132 case 'spam':
133
134 if ( $field['type'] == 'firstname' || $field['type'] == 'lastname' || $field['type'] == 'zip' || $field['type'] == 'product' || $field['type'] == 'city' || $field['type'] == 'quiz' || $field['type'] == 'address' || $field['type'] == 'spam' || $field['type'] == 'textbox' ) {
135 $field['type'] = 'text';
136 }
137
138 $form_fields[] = $this->get_form_field( $field['type'], [
139 'required' => $field['required'] ? 'yes' : 'no',
140 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
141 'name' => $field['key'],
142 ] );
143 break;
144
145 case 'checkbox':
146 case 'listcheckbox':
147 if ( $field['type'] == 'listcheckbox' ) {
148 $field['type'] = 'checkbox';
149 }
150
151 $form_fields[] = $this->get_form_field( $field['type'], [
152 'required' => $field['required'] ? 'yes' : 'no',
153 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
154 'name' => $field['key'],
155 'options' => $this->get_options( $field['options'] ) ? $this->get_options( $field['options'] ) : [ 'label' => $field['label'], 'value' => $field['key'] ],
156 ] );
157
158 break;
159
160 case 'select':
161 case 'radio':
162 case 'listmultiselect':
163 case 'listradio':
164 case 'listselect':
165 case 'listcountry':
166 case 'starrating':
167 if ( $field['type'] == 'listcountry' || $field['type'] == 'starrating' ) {
168 $field['type'] = 'select';
169 } elseif ( $field['type'] == 'listmultiselect' || $field['type'] == 'listselect' ) {
170 $field['type'] = 'multiselect';
171 } elseif ( $field['type'] == 'listradio' ) {
172 $field['type'] = 'radio';
173 }
174
175 $form_fields[] = $this->get_form_field( $field['type'], [
176 'required' => $field['required'] ? 'yes' : 'no',
177 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
178 'name' => $field['key'],
179 'options' => $this->get_options( $field['options'] ),
180 ] );
181 break;
182
183 case 'range':
184 case 'number':
185 case 'phone':
186 case 'quantity':
187 case 'total':
188 case 'shipping':
189 if ( $field['type'] == 'phone' || $field['type'] == 'quantity' || $field['type'] == 'total' || $field['type'] == 'shipping' ) {
190 $field['type'] = 'number';
191 }
192
193 $form_fields[] = $this->get_form_field( $field['type'], [
194 'required' => $field['required'] ? 'yes' : 'no',
195 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
196 'name' => $field['key'],
197 ] );
198
199 break;
200
201 case 'hr':
202 case 'hidden':
203 case 'html':
204
205 if ( $field['type'] == 'hr' ) {
206 $field['type'] = 'section_break';
207 }
208
209 $form_fields[] = $this->get_form_field( $field['type'], [
210 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
211 'name' => $field['key'],
212 'default' => $field['default'],
213 ] );
214
215 break;
216
217 case 'acceptance':
218
219 $form_fields[] = $this->get_form_field( 'toc', [
220 'required' => $field['required'] ? 'yes' : 'no',
221 'description' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
222 'name' => $field['key'],
223 ] );
224 break;
225
226 case 'recaptcha':
227
228 $form_fields[] = $this->get_form_field( $field['type'], [
229 'required' => $field['required'] ? 'yes' : 'no',
230 'label' => $this->find_label( $field['label'], $field['type'], $field['key'] ),
231 'name' => $field['key'],
232 ] );
233 break;
234
235 case 'submit':
236
237 $this->submit_text = $field['label'];
238
239 break;
240 }
241 }
242
243 return $form_fields;
244 }
245
246 /**
247 * Get form settings
248 *
249 * @param object $form
250 *
251 * @return array
252 */
253 public function get_form_settings( $form ) {
254 $all_settings = get_option( 'nf_form_' . $form->get_id(), true );
255
256 foreach ( $all_settings['actions'] as $actions ) {
257 if ( 'successmessage' == $actions['settings']['type'] ) {
258 $message = $actions['settings']['message'];
259 }
260 }
261 $message = str_replace( ' {field:name}', '', $message );
262 $default = $this->get_default_form_settings();
263 $settings = wp_parse_args( [
264 'message' => $message,
265 ], $default );
266
267 if ( $this->submit_text ) {
268 $settings['submit_text'] = $this->submit_text;
269 }
270
271 return $settings;
272 }
273
274 /**
275 * Get form notifications
276 *
277 * @param object $form
278 *
279 * @return array
280 */
281 public function get_form_notifications( $form ) {
282 $notifications = [];
283 $all_settings = get_option( 'nf_form_' . $form->get_id(), true );
284
285 foreach ( $all_settings['actions'] as $actions ) {
286 if ( 'Email Notification' == $actions['settings']['label'] ) {
287 $action_settings = $actions['settings'];
288 } elseif ( 'Email Confirmation' == $actions['settings']['label'] ) {
289 $action_settings2 = $actions['settings'];
290 }
291 }
292
293 $sub = str_replace( '{field:name}', '{site_name}', $action_settings['email_subject'] );
294
295 $notifications = [
296 [
297 'active' => $action_settings['active'] ? 'true' : 'false',
298 'name' => 'Admin Notification',
299 'subject' => str_replace( '[your-subject]', '{field:your-subject}', $sub ),
300 'to' => '{field:your-email}',
301 'replyTo' => '{field:your-email}',
302 'message' => '{all_fields}',
303 'fromName' => '{site_name}',
304 'fromAddress' => '{admin_email}',
305 'cc' => '',
306 'bcc' => '',
307 ],
308 ];
309
310 $sender_match = $this->get_notification_sender_match( get_option( 'admin_email' ) );
311
312 if ( !empty( $sender_match['fromName'] ) ) {
313 $form_notifications[0]['fromName'] = $sender_match['fromName'];
314 }
315
316 if ( isset( $sender_match['fromAddress'] ) ) {
317 $form_notifications[0]['fromAddress'] = $sender_match['fromAddress'];
318 }
319
320 if ( $action_settings2['active'] ) {
321 $notifications[] = [
322 'active' => $action_settings2['active'] ? 'true' : 'false',
323 'name' => 'Admin Notification',
324 'subject' => str_replace( '[your-subject]', $action_settings2['subject'], $action_settings2['subject'] ),
325 'to' => '{field:your-email}',
326 'replyTo' => '{field:your-email}',
327 'message' => '{all_fields}',
328 'fromName' => '{site_name}',
329 'fromAddress' => '{admin_email}',
330 'cc' => '',
331 'bcc' => '',
332 ];
333 }
334
335 $sender_match = $this->get_notification_sender_match( get_option( 'admin_email' ) );
336
337 if ( !empty( $sender_match['fromName'] ) ) {
338 $form_notifications[1]['fromName'] = $sender_match['fromName'];
339 }
340
341 if ( isset( $sender_match['fromAddress'] ) ) {
342 $form_notifications[1]['fromAddress'] = $sender_match['fromAddress'];
343 }
344
345 return $notifications;
346 }
347
348 /**
349 * Match the sender
350 *
351 * @param array $mail
352 *
353 * @return array
354 */
355 public function get_notification_sender_match( $mail ) {
356 if ( !isset( $mail['sender'] ) ) {
357 return;
358 }
359 $sender = [ 'fromName' => '', 'fromAddress' => '' ];
360 $sender_match = [];
361
362 preg_match( '/([^<"]*)"?\s*<(\S*)>/', $mail['sender'], $sender_match );
363
364 if ( isset( $sender_match[1] ) ) {
365 $sender['fromName'] = $sender_match[1];
366 }
367
368 if ( isset( $sender_match[2] ) ) {
369 $sender['fromAddress'] = $sender_match[2];
370 }
371
372 return $sender;
373 }
374
375 /**
376 * Try to find out the input label
377 *
378 * Loop through all the label tags and try to find out
379 * if the field is inside that tag. Then strip out the field and find out label
380 *
381 * @param string $content
382 * @param string $type
383 * @param string $fieldname
384 *
385 * @return string
386 */
387 private function find_label( $content, $type, $fieldname ) {
388 return $content; // i guess, we don't need this :/ will remove later
389
390 // find all enclosing label fields
391 $pattern = '/<label>([ \w\S\r\n\t]+?)<\/label>/';
392 preg_match_all( $pattern, $content, $matches );
393
394 foreach ( $matches[1] as $key => $match ) {
395 $match = trim( str_replace( "\n", '', $match ) );
396
397 preg_match( '/\[(?:' . preg_quote( $type ) . ') ' . $fieldname . '(?:[ ](.*?))?(?:[\r\n\t ](\/))?\]/', $match, $input_match );
398
399 if ( $input_match ) {
400 $label = strip_tags( str_replace( $input_match[0], '', $match ) );
401
402 return trim( $label );
403 }
404 }
405
406 return $fieldname;
407 }
408
409 /**
410 * Get file type for upload files
411 *
412 * @param string $extension
413 *
414 * @return bool|string
415 */
416 private function get_file_type( $extension ) {
417 $allowed_extensions = weforms_allowed_extensions();
418
419 foreach ( $allowed_extensions as $type => $extensions ) {
420 $_extensions = explode( ',', $extensions['ext'] );
421
422 if ( in_array( $extension, $_extensions ) ) {
423 return $type;
424 }
425 }
426
427 return false;
428 }
429
430 /**
431 * Translate to wpuf field options array
432 *
433 * @param object $field
434 *
435 * @return array
436 */
437 private function get_options( $field ) {
438 $options = [];
439
440 if ( !is_array( $field ) ) {
441 return $options;
442 }
443
444 foreach ( $field as $value ) {
445 $options[ $value['value'] ] = $value['label'];
446 }
447
448 return $options;
449 }
450 }
451