PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.0.4
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.0.4
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.0.4, at includes/importer/class-importer-ninja-forms.php

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