PluginProbe
weForms – Easy Drag & Drop Contact Form Builder For WordPress / 1.3.9
weForms – Easy Drag & Drop Contact Form Builder For WordPress v1.3.9
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
← All changes | includes/importer/class-importer-abstract.php +983 -981 1.6.81.3.9 View file →
@@ -1,981 +1,983 @@
1 -<?php
2 -
3 -/**
4 - * Importer Abstract Class
5 - */
6 -abstract class WeForms_Importer_Abstract {
7 -
8 - /**
9 - * The importer ID
10 - *
11 - * @var string
12 - */
13 - public $id = '';
14 -
15 - /**
16 - * The importer title
17 - *
18 - * @var string
19 - */
20 - public $title = '';
21 -
22 - /**
23 - * The shortcode (without bracket)
24 - *
25 - * @var string
26 - */
27 - public $shortcode = '';
28 -
29 - /**
30 - * The ajax action string
31 - *
32 - * @var string
33 - */
34 - protected $action = '';
35 -
36 - /**
37 - * Conditional stub
38 - *
39 - * @var array
40 - */
41 - public $conditionals = [
42 - 'condition_status' => 'no',
43 - 'cond_field' => [],
44 - 'cond_operator' => [ '=' ],
45 - 'cond_option' => [ '- select -' ],
46 - 'cond_logic' => 'all',
47 - ];
48 -
49 - public function __construct() {
50 - add_action( 'admin_notices', [ $this, 'maybe_show_notice' ] );
51 -
52 - add_action( 'wp_ajax_weforms_import_xdismiss_' . $this->id, [ $this, 'dismiss_notice' ] );
53 - add_action( 'wp_ajax_weforms_import_xforms_' . $this->id, [ $this, 'import_forms' ] );
54 - add_action( 'wp_ajax_weforms_import_xreplace_' . $this->id, [ $this, 'replace_action' ] );
55 - }
56 -
57 - /**
58 - * Get the importer name
59 - *
60 - * @return string
61 - */
62 - public function get_importer_name() {
63 - return $this->title;
64 - }
65 -
66 - /**
67 - * Get the shortcode ready for regular expression
68 - *
69 - * @return string
70 - */
71 - public function get_shortcode( $regex = false ) {
72 - if ( $regex ) {
73 - return preg_quote( $this->shortcode );
74 - }
75 -
76 - return $this->shortcode;
77 - }
78 -
79 - /**
80 - * Get all the forms
81 - *
82 - * @return array
83 - */
84 - abstract protected function get_forms();
85 -
86 - /**
87 - * Get the form name
88 - *
89 - * @param mixed $form
90 - *
91 - * @return string
92 - */
93 - abstract protected function get_form_name( $form );
94 -
95 - /**
96 - * Get the form id
97 - *
98 - * @param mixed $form
99 - *
100 - * @return int
101 - */
102 - abstract protected function get_form_id( $form );
103 -
104 - /**
105 - * Get all form fields of a form
106 - *
107 - * @param mixed $form
108 - *
109 - * @return array
110 - */
111 - abstract protected function get_form_fields( $form );
112 -
113 - /**
114 - * Get form settings of a form
115 - *
116 - * @param mixed $form
117 - *
118 - * @return array
119 - */
120 - abstract protected function get_form_settings( $form );
121 -
122 - /**
123 - * Get form notifications of a form
124 - *
125 - * @param mixed $form
126 - *
127 - * @return array
128 - */
129 - abstract protected function get_form_notifications( $form );
130 -
131 - /**
132 - * Check if the plugin exists
133 - *
134 - * @return bool
135 - */
136 - abstract protected function plugin_exists();
137 -
138 - /**
139 - * Show notice if the plugin found
140 - *
141 - * @return void
142 - */
143 - public function maybe_show_notice() {
144 - if ( !$this->plugin_exists() ) {
145 - return;
146 - }
147 -
148 - if ( $this->is_dimissed() || !current_user_can( 'manage_options' ) ) {
149 - return;
150 - } ?>
151 - <div class="notice notice-info">
152 - <p><strong><?php printf( esc_html__( '%s Detected', 'weforms' ), wp_kses_post( $this->get_importer_name() ) ); ?></strong></p>
153 - <p><?php printf( wp_kses_post( 'Hey, looks like you have <strong>%s</strong> installed. Would you like to <strong>import</strong> the forms into weForms?', 'weforms' ), wp_kses_post ( $this->get_importer_name() ) ); ?></p>
154 -
155 - <p>
156 - <a href="#" class="button button-primary weforms-import-<?php echo esc_attr( $this->id );?>" id="weforms-import-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'Import Forms', 'weforms' ); ?></a>
157 - <a href="#" class="button weforms-import-<?php echo esc_attr( $this->id ) ;?>" id="weforms-dimiss-<?php echo esc_attr( $this->id );?>"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a>
158 - </p>
159 - </div>
160 -
161 - <script type="text/javascript">
162 - jQuery(function($) {
163 - $('.notice').on('click', 'a#weforms-import-<?php echo esc_attr( $this->id );?>', function(e) {
164 - e.preventDefault();
165 -
166 - var self = $(this);
167 - self.addClass('updating-message');
168 - wp.ajax.send( 'weforms_import_xforms_<?php echo esc_attr( $this->id );?>', {
169 - data: {
170 - type: self.data('type'),
171 - _wpnonce: '<?php echo esc_attr( wp_create_nonce( 'weforms' ) ); ?>'
172 - },
173 -
174 - success: function(response) {
175 - var html = '<p><strong>' + response.title + '</strong></p>' +
176 - '<p>' + response.message + '</p>' +
177 - '<p>' + response.action + '</p>';
178 -
179 - html += '<ul>';
180 - _.each(response.refs, function(el, index) {
181 - html += '<li><a target="_blank" href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit">' + el.title + '</a> - <a href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit" target="_blank" class="button button-small"><span class="dashicons dashicons-external"></span> Edit</a></li>';
182 - });
183 -
184 - html += '</ul>';
185 -
186 - html += '<p>' + '<a href="#" class="button button-primary weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="replace"><?php esc_html_e( 'Replace Shortcodes', 'weforms' ); ?></a>&nbsp;' +
187 - '<a href="#" class="button weforms-<?php echo esc_attr( $this->id );?>-replace-action" data-type="skip"><?php esc_html_e( 'No Thanks', 'weforms' ); ?></a></p>';
188 -
189 - self.closest('.notice').removeClass('notice-info').addClass('notice-success').html( html );
190 - },
191 -
192 - error: function(error) {
193 - var html = '<p><strong>' + error.title + '</strong></p>' +
194 - '<p>' + error.message + '</p>';
195 -
196 - self.closest('.notice').removeClass('notice-info').addClass('notice-error').html( html );
197 - },
198 -
199 - complete: function() {
200 - self.removeClass('updating-message');
201 - }
202 - });
203 - });
204 -
205 - $('.notice').on('click', '#weforms-dimiss-<?php echo esc_attr( $this->id ) ;?>', function(e) {
206 - e.preventDefault();
207 -
208 - $(this).closest('.notice').remove();
209 - wp.ajax.send('weforms_import_xdismiss_<?php echo esc_attr( $this->id );?>');
210 - });
211 -
212 - $('.notice').on('click', 'a.weforms-<?php echo esc_attr( $this->id );?>-replace-action', function(e) {
213 - e.preventDefault();
214 -
215 - var self = $(this);
216 - var notice = self.closest('.notice');
217 -
218 - self.addClass('updating-message');
219 -
220 - wp.ajax.send( 'weforms_import_xreplace_<?php echo esc_attr( $this->id ) ;?>', {
221 - data: {
222 - type: self.data('type'),
223 - _wpnonce: '<?php echo esc_attr ( wp_create_nonce( 'weforms' ) ); ?>'
224 - },
225 -
226 - success: function(response) {
227 - notice.remove();
228 -
229 - if ( 'replace' === self.data('type') ) {
230 - alert( response );
231 - }
232 - },
233 -
234 - error: function(error) {
235 - notice.remove();
236 - alert( error );
237 - },
238 -
239 - complete: function() {
240 - self.removeClass('updating-message');
241 - }
242 - });
243 -
244 - });
245 - });
246 - </script>
247 - <?php
248 - }
249 -
250 - /**
251 - * Import forms
252 - *
253 - * @return void
254 - */
255 - public function import_forms() {
256 - check_ajax_referer( 'weforms' );
257 -
258 - $this->check_caps();
259 -
260 - // check if plugin installed
261 - if ( !$this->plugin_exists() ) {
262 - wp_send_json_error( [
263 - 'title' => __( 'Uh oh!', 'weforms' ),
264 - 'message' => sprintf( __( '%s is not installed.', 'weforms' ), $this->get_importer_name() ),
265 - ] );
266 - }
267 -
268 - $imported = 0;
269 - $refs = [];
270 - $forms = $this->get_forms();
271 -
272 - if ( !$forms ) {
273 - wp_send_json_error( [
274 - 'title' => __( 'Uh oh!', 'weforms' ),
275 - 'message' => __( 'No forms found!', 'weforms' ),
276 - ] );
277 -
278 - $this->dismiss_prompt();
279 - }
280 -
281 - if ( $forms ) {
282 - foreach ( $forms as $form ) {
283 - $form_name = $this->get_form_name( $form );
284 - $form_fields = $this->get_form_fields( $form );
285 - $settings = $this->get_form_settings( $form );
286 - $notifications = $this->get_form_notifications( $form );
287 -
288 - if ( $form_fields ) {
289 - $form_id = $this->insert_form( $form_name );
290 -
291 - if ( is_wp_error( $form_id ) ) {
292 - continue;
293 - }
294 -
295 - foreach ( $form_fields as $menu_order => $form_field ) {
296 - $this->insert_form_field( $form_field, $form_id, $menu_order );
297 - }
298 -
299 - $this->update_settings( $form_id, $settings );
300 - $this->update_notification( $form_id, $notifications );
301 -
302 - $imported++;
303 -
304 - $refs[ $this->get_form_id( $form ) ] = [
305 - 'imported_id' => $this->get_form_id( $form ),
306 - 'weforms_id' => $form_id,
307 - 'title' => $form_name,
308 - ];
309 - }
310 - }
311 - }
312 -
313 - $this->dismiss_prompt();
314 - $this->update_refs( $refs );
315 -
316 - wp_send_json_success( [
317 - 'title' => sprintf( _n( '%s form imported', '%s forms imported', $imported, 'weforms' ), $imported ),
318 - 'message' => __( 'We have successfully imported these forms into weForms. You could check and edit in-case anything weird happended.', 'weforms' ),
319 - 'action' => sprintf( __( 'Do you want to <strong>replace</strong> %s shortcodes with weForms?', 'weforms' ), $this->get_importer_name() ),
320 - 'refs' => $refs,
321 - ] );
322 - }
323 -
324 - /**
325 - * Replace contact form 7 shortcodes
326 - *
327 - * @return void
328 - */
329 - public function replace_action() {
330 - check_ajax_referer( 'weforms' );
331 -
332 - $this->check_caps();
333 -
334 - if ( ! in_array( isset( $_POST['type'] ), array( 'skip', 'replace' ) ) ) {
335 - wp_send_json_error( __( 'Not a valid action type.', 'weforms' ) );
336 - }
337 -
338 - if ( 'skip' == $_POST['type'] ) {
339 - wp_send_json_success();
340 - }
341 -
342 - $pages_query = new WP_Query( [
343 - 'post_type' => 'page',
344 - 'posts_per_page' => -1,
345 - 's' => '[' . $this->get_shortcode(),
346 - ] );
347 -
348 - if ( !$pages_query->found_posts ) {
349 - wp_send_json_error( __( 'No pages found with shortcode. Skipped!', 'weforms' ) );
350 - }
351 -
352 - $count = 0;
353 - $refs = $this->get_refs();
354 - $pages = $pages_query->get_posts();
355 -
356 - foreach ( $pages as $page ) {
357 - preg_match_all( '/\[(\[?)(' . $this->get_shortcode( true ) . '|)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $page->post_content, $matches, PREG_SET_ORDER );
358 -
359 - if ( empty( $matches ) ) {
360 - continue;
361 - }
362 -
363 - foreach ( $matches as $shortcode ) {
364 - $atts = shortcode_parse_atts( $shortcode[0] );
365 -
366 - if ( isset( $atts['id'] ) && array_key_exists( $atts['id'], $refs ) ) {
367 - $replace = sprintf( '[weforms id="%d"]', $refs[ $atts['id'] ]['weforms_id'] );
368 -
369 - $post_content = str_replace( $shortcode[0], $replace, $page->post_content );
370 -
371 - wp_update_post( [
372 - 'ID' => $page->ID,
373 - 'post_content' => $post_content,
374 - ] );
375 -
376 - $count++;
377 - }
378 - }
379 - }
380 -
381 - wp_send_json_success( sprintf( _n( 'Replaced %d form', 'Replaced %d forms', $count, 'weforms' ), $count ) );
382 - }
383 -
384 - /**
385 - * Check capability if able to process
386 - *
387 - * @return void
388 - */
389 - public function check_caps() {
390 - if ( !current_user_can( 'manage_options' ) ) {
391 - wp_send_json_error( __( 'You are not allowed.', 'weforms' ) );
392 - }
393 - }
394 -
395 - /**
396 - * If the prompt is dismissed
397 - *
398 - * @return bool
399 - */
400 - public function is_dimissed() {
401 - return 'yes' == get_option( 'weforms_dismiss_xnotice_' . $this->id );
402 - }
403 -
404 - /**
405 - * Dismiss the prompt
406 - *
407 - * @return void
408 - */
409 - public function dismiss_prompt() {
410 - update_option( 'weforms_dismiss_xnotice_' . $this->id, 'yes' );
411 - }
412 -
413 - /**
414 - * Dismiss the notice
415 - *
416 - * @return void
417 - */
418 - public function dismiss_notice() {
419 - $this->dismiss_prompt();
420 -
421 - wp_send_json_success();
422 - }
423 -
424 - /**
425 - * Get the imported form refs
426 - *
427 - * @return array
428 - */
429 - public function get_refs() {
430 - return get_option( 'weforms_imported_xforms_' . $this->id, [] );
431 - }
432 -
433 - /**
434 - * Update the imported reference forms
435 - *
436 - * @param array $refs
437 - *
438 - * @return void
439 - */
440 - public function update_refs( $refs ) {
441 - update_option( 'weforms_imported_xforms_' . $this->id, $refs );
442 - }
443 -
444 - /**
445 - * Get form field
446 - *
447 - * @param string $type
448 - * @param array $args [description]
449 - *
450 - * @return array
451 - */
452 - public function get_form_field( $type, $args = [] ) {
453 - $defaults = [
454 - 'required' => 'no',
455 - 'label' => '',
456 - 'name' => '',
457 - 'help' => '',
458 - 'css_class' => '',
459 - 'placeholder' => '',
460 - 'value' => '',
461 - 'default' => '',
462 - 'options' => [],
463 - 'step' => '',
464 - 'min' => '',
465 - 'max' => '',
466 - 'extension' => '',
467 - 'max_size' => '', // file size
468 - 'size' => '', // file size
469 - 'first_placeholder' => '',
470 - 'first_default' => '',
471 - 'middle_placeholder' => '',
472 - 'middle_default' => '',
473 - 'last_placeholder' => '',
474 - 'last_default' => '',
475 - 'first_name' => '',
476 - 'middle_name' => '',
477 - 'last_name' => '',
478 - ];
479 -
480 - $args = wp_parse_args( $args, $defaults );
481 -
482 - switch ( $type ) {
483 - case 'text':
484 - $field_content = [
485 - 'input_type' => 'text',
486 - 'template' => 'text_field',
487 - 'required' => $args['required'],
488 - 'label' => $args['label'],
489 - 'name' => $args['name'],
490 - 'is_meta' => 'yes',
491 - 'help' => $args['help'],
492 - 'css' => $args['css_class'],
493 - 'placeholder' => $args['placeholder'],
494 - 'default' => $args['default'],
495 - 'size' => $args['size'],
496 - 'word_restriction' => '',
497 - 'wpuf_cond' => $this->conditionals,
498 - ];
499 - break;
500 -
501 - case 'email':
502 - $field_content = [
503 - 'input_type' => 'email',
504 - 'template' => 'email_address',
505 - 'required' => $args['required'],
506 - 'label' => $args['label'],
507 - 'name' => $args['name'],
508 - 'is_meta' => 'yes',
509 - 'help' => $args['help'],
510 - 'css' => $args['css_class'],
511 - 'placeholder' => $args['placeholder'],
512 - 'default' => $args['default'],
513 - 'size' => $args['size'],
514 - 'word_restriction' => '',
515 - 'wpuf_cond' => $this->conditionals,
516 - ];
517 - break;
518 -
519 - case 'textarea':
520 - $field_content = [
521 - 'input_type' => 'textarea',
522 - 'template' => 'textarea_field',
523 - 'required' => $args['required'],
524 - 'label' => $args['label'],
525 - 'name' => $args['name'],
526 - 'is_meta' => 'yes',
527 - 'help' => $args['help'],
528 - 'css' => $args['css_class'],
529 - 'rows' => 5,
530 - 'cols' => 25,
531 - 'placeholder' => $args['placeholder'],
532 - 'default' => $args['default'],
533 - 'rich' => 'no',
534 - 'word_restriction' => '',
535 - 'wpuf_cond' => $this->conditionals,
536 - ];
537 - break;
538 -
539 - case 'select':
540 - $field_content = [
541 - 'input_type' => 'select',
542 - 'template' => 'dropdown_field',
543 - 'required' => $args['required'],
544 - 'label' => $args['label'],
545 - 'name' => $args['name'],
546 - 'is_meta' => 'yes',
547 - 'help' => '',
548 - 'css' => $args['css_class'],
549 - 'selected' => '',
550 - 'inline' => 'no',
551 - 'options' => $args['options'],
552 - 'wpuf_cond' => $this->conditionals,
553 - ];
554 - break;
555 -
556 - case 'multiselect':
557 - $field_content = [
558 - 'input_type' => 'multiselect',
559 - 'template' => 'multiple_select',
560 - 'required' => $args['required'],
561 - 'label' => $args['label'],
562 - 'name' => $args['name'],
563 - 'is_meta' => 'yes',
564 - 'help' => '',
565 - 'css' => $args['css_class'],
566 - 'selected' => '',
567 - 'first' => __( '- select -', 'weforms' ),
568 - 'options' => $args['options'],
569 - 'wpuf_cond' => $this->conditionals,
570 - ];
571 - break;
572 -
573 - case 'date':
574 - $field_content = [
575 - 'input_type' => 'date',
576 - 'template' => 'date_field',
577 - 'required' => $args['required'],
578 - 'label' => $args['label'],
579 - 'name' => $args['name'],
580 - 'is_meta' => 'yes',
581 - 'help' => '',
582 - 'css' => $args['css_class'],
583 - 'format' => 'dd/mm/yy',
584 - 'time' => '',
585 - 'is_publish_time' => '',
586 - 'wpuf_cond' => $this->conditionals,
587 - ];
588 - break;
589 -
590 - case 'range':
591 - case 'number':
592 -
593 - $field_content = [
594 - 'input_type' => 'numeric_text',
595 - 'template' => 'numeric_text_field',
596 - 'required' => $args['required'],
597 - 'label' => $args['label'],
598 - 'name' => $args['name'],
599 - 'is_meta' => 'yes',
600 - 'help' => '',
601 - 'css' => $args['css_class'],
602 - 'placeholder' => $args['placeholder'],
603 - 'default' => $args['value'],
604 - 'size' => 40,
605 - 'step_text_field' => $args['step'],
606 - 'min_value_field' => $args['min'],
607 - 'max_value_field' => $args['max'],
608 - 'wpuf_cond' => $this->conditionals,
609 - ];
610 -
611 - break;
612 -
613 - case 'url':
614 - $field_content = [
615 - 'input_type' => 'url',
616 - 'template' => 'website_url',
617 - 'required' => $args['required'],
618 - 'label' => $args['label'],
619 - 'name' => $args['name'],
620 - 'is_meta' => 'yes',
621 - 'help' => '',
622 - 'css' => $args['css_class'],
623 - 'placeholder' => '',
624 - 'default' => '',
625 - 'size' => 40,
626 - 'word_restriction' => '',
627 - 'wpuf_cond' => $this->conditionals,
628 - ];
629 -
630 - break;
631 -
632 - case 'checkbox':
633 - $field_content = [
634 - 'input_type' => 'checkbox',
635 - 'template' => 'checkbox_field',
636 - 'required' => $args['required'],
637 - 'label' => $args['label'],
638 - 'name' => $args['name'],
639 - 'is_meta' => 'yes',
640 - 'help' => '',
641 - 'css' => $args['css_class'],
642 - 'selected' => '',
643 - 'inline' => 'no',
644 - 'options' => $args['options'],
645 - 'wpuf_cond' => $this->conditionals,
646 - ];
647 - break;
648 -
649 - case 'radio':
650 - $field_content = [
651 - 'input_type' => 'radio',
652 - 'template' => 'radio_field',
653 - 'required' => $args['required'],
654 - 'label' => $args['label'],
655 - 'name' => $args['name'],
656 - 'is_meta' => 'yes',
657 - 'help' => '',
658 - 'css' => $args['css_class'],
659 - 'selected' => '',
660 - 'inline' => 'no',
661 - 'options' => $args['options'],
662 - 'wpuf_cond' => $this->conditionals,
663 - ];
664 - break;
665 -
666 - case 'hidden':
667 - $field_content = [
668 - 'input_type' => 'hidden',
669 - 'template' => 'custom_hidden_field',
670 - 'label' => $args['label'],
671 - 'name' => $args['name'],
672 - 'is_meta' => 'yes',
673 - 'wpuf_cond' => $this->conditionals,
674 - ];
675 - break;
676 -
677 - case 'section_break':
678 - $field_content = [
679 - 'input_type' => 'section_break',
680 - 'template' => 'section_break',
681 - 'label' => $args['label'],
682 - 'name' => $args['name'],
683 - 'wpuf_cond' => $this->conditionals,
684 - ];
685 - break;
686 -
687 - case 'html':
688 - $field_content = [
689 - 'input_type' => 'html',
690 - 'template' => 'custom_html',
691 - 'label' => $args['label'],
692 - 'name' => $args['name'],
693 - 'html' => $args['default'],
694 - 'wpuf_cond' => $this->conditionals,
695 - ];
696 - break;
697 -
698 - case 'toc':
699 - $field_content = [
700 - 'input_type' => 'toc',
701 - 'template' => 'toc',
702 - 'required' => $args['required'],
703 - 'name' => $args['name'],
704 - 'description' => $args['label'],
705 - 'is_meta' => 'yes',
706 - 'show_checkbox' => true,
707 - 'wpuf_cond' => $this->conditionals,
708 - ];
709 - break;
710 -
711 - case 'recaptcha':
712 - $field_content = [
713 - 'input_type' => 'recaptcha',
714 - 'template' => 'recaptcha',
715 - 'required' => $args['required'],
716 - 'label' => $args['label'],
717 - 'name' => $args['name'],
718 - 'recaptcha_type' => 'enable_no_captcha',
719 - 'is_meta' => 'yes',
720 - 'help' => '',
721 - 'css' => $args['css_class'],
722 - 'placeholder' => '',
723 - 'default' => '',
724 - 'size' => 40,
725 - 'word_restriction' => '',
726 - 'wpuf_cond' => $this->conditionals,
727 - ];
728 - break;
729 -
730 - case 'file':
731 - $field_content = [
732 - 'input_type' => 'file_upload',
733 - 'template' => 'file_upload',
734 - 'required' => $args['required'],
735 - 'label' => $args['label'],
736 - 'name' => $args['name'],
737 - 'is_meta' => 'yes',
738 - 'help' => $args['help'],
739 - 'css' => $args['css_class'],
740 - 'max_size' => $args['max_size'],
741 - 'count' => '1',
742 - 'extension' => $args['extension'],
743 - 'wpuf_cond' => $this->conditionals,
744 - ];
745 - break;
746 -
747 - case 'name':
748 - $field_content = [
749 - 'input_type' => 'name',
750 - 'template' => 'name_field',
751 - 'required' => $args['required'],
752 - 'label' => $args['label'],
753 - 'name' => $args['name'],
754 - 'is_meta' => 'yes',
755 - 'format' => $args['format'],
756 - 'first_name' => $args['first_name'],
757 - 'middle_name' => $args['middle_name'],
758 - 'last_name' => $args['last_name'],
759 - 'hide_subs' => false,
760 - 'help' => $args['help'],
761 - 'css' => $args['css_class'],
762 - 'wpuf_cond' => $this->conditionals,
763 - ];
764 - break;
765 -
766 - case 'ratings':
767 - $field_content = [
768 - 'input_type' => 'ratings',
769 - 'template' => 'ratings',
770 - 'required' => $args['required'],
771 - 'label' => $args['label'],
772 - 'name' => $args['name'],
773 - 'is_meta' => 'yes',
774 - 'help' => '',
775 - 'css' => $args['css_class'],
776 - 'options' => $args['options'],
777 - 'wpuf_cond' => $this->conditionals,
778 - ];
779 - break;
780 -
781 - case 'linear_scale':
782 - $field_content = [
783 - 'input_type' => 'linear_scale',
784 - 'template' => 'linear_scale',
785 - 'required' => $args['required'],
786 - 'label' => $args['label'],
787 - 'name' => $args['name'],
788 - 'is_meta' => 'yes',
789 - 'help' => '',
790 - 'css' => $args['css_class'],
791 - 'options' => $args['options'],
792 - 'wpuf_cond' => $this->conditionals,
793 - ];
794 - break;
795 -
796 - case 'checkbox_grid':
797 - $field_content = [
798 - 'input_type' => 'checkbox_grid',
799 - 'template' => 'checkbox_grid',
800 - 'required' => $args['required'],
801 - 'label' => $args['label'],
802 - 'name' => $args['name'],
803 - 'is_meta' => 'yes',
804 - 'help' => '',
805 - 'css' => $args['css_class'],
806 - 'options' => $args['options'],
807 - 'wpuf_cond' => $this->conditionals,
808 - ];
809 - break;
810 -
811 - case 'multiple_choice_grid':
812 - $field_content = [
813 - 'input_type' => 'multiple_choice_grid',
814 - 'template' => 'multiple_choice_grid',
815 - 'required' => $args['required'],
816 - 'label' => $args['label'],
817 - 'name' => $args['name'],
818 - 'is_meta' => 'yes',
819 - 'help' => '',
820 - 'css' => $args['css_class'],
821 - 'options' => $args['options'],
822 - 'wpuf_cond' => $this->conditionals,
823 - ];
824 - break;
825 -
826 - case 'single_product':
827 - $field_content = [
828 - 'input_type' => 'single_product',
829 - 'template' => 'single_product',
830 - 'required' => $args['required'],
831 - 'label' => $args['label'],
832 - 'name' => $args['name'],
833 - 'is_meta' => 'yes',
834 - 'help' => '',
835 - 'css' => $args['css_class'],
836 - 'options' => $args['options'],
837 - 'wpuf_cond' => $this->conditionals,
838 - ];
839 - break;
840 -
841 - case 'multiple_product':
842 - $field_content = [
843 - 'input_type' => 'multiple_product',
844 - 'template' => 'multiple_product',
845 - 'required' => $args['required'],
846 - 'label' => $args['label'],
847 - 'name' => $args['name'],
848 - 'is_meta' => 'yes',
849 - 'help' => '',
850 - 'css' => $args['css_class'],
851 - 'options' => $args['options'],
852 - 'wpuf_cond' => $this->conditionals,
853 - ];
854 - break;
855 -
856 - case 'payment_method':
857 - $field_content = [
858 - 'input_type' => 'payment_method',
859 - 'template' => 'payment_method',
860 - 'required' => $args['required'],
861 - 'label' => $args['label'],
862 - 'name' => $args['name'],
863 - 'is_meta' => 'yes',
864 - 'help' => '',
865 - 'css' => $args['css_class'],
866 - 'options' => $args['options'],
867 - 'wpuf_cond' => $this->conditionals,
868 - ];
869 - break;
870 -
871 - case 'total':
872 - $field_content = [
873 - 'input_type' => 'total',
874 - 'template' => 'total',
875 - 'required' => $args['required'],
876 - 'label' => $args['label'],
877 - 'name' => $args['name'],
878 - 'is_meta' => 'yes',
879 - 'help' => '',
880 - 'css' => $args['css_class'],
881 - 'options' => $args['options'],
882 - 'wpuf_cond' => $this->conditionals,
883 - ];
884 - break;
885 - }
886 -
887 - return $field_content;
888 - }
889 -
890 - /**
891 - * Default form settings
892 - *
893 - * @return array
894 - */
895 - public function get_default_form_settings() {
896 - $form_settings = [
897 - 'redirect_to' => 'same',
898 - 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ),
899 - 'page_id' => '',
900 - 'url' => '',
901 - 'submit_text' => __( 'Submit Query', 'weforms' ),
902 - 'schedule_form' => 'false',
903 - 'schedule_start' => '',
904 - 'schedule_end' => '',
905 - 'humanpresence_enabled' => false,
906 - 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ),
907 - 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ),
908 - 'require_login' => 'false',
909 - 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ),
910 - 'limit_entries' => 'false',
911 - 'limit_number' => '1000',
912 - 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ),
913 - 'label_position' => 'above',
914 - ];
915 -
916 - return $form_settings;
917 - }
918 -
919 - /**
920 - * Insert a form
921 - *
922 - * @param string $form_name
923 - *
924 - * @return ID|WP_Error
925 - */
926 - public function insert_form( $form_name ) {
927 - $weforms_form = [
928 - 'post_title' => sprintf( '[%s] %s', strtoupper( $this->id ), $form_name ),
929 - 'post_type' => 'wpuf_contact_form',
930 - 'post_status' => 'publish',
931 - 'post_author' => get_current_user_id(),
932 - ];
933 -
934 - return wp_insert_post( $weforms_form );
935 - }
936 -
937 - /**
938 - * Insert a form field
939 - *
940 - * @param array $field
941 - * @param int $form_id
942 - * @param int $menu_order
943 - *
944 - * @return int|WP_Error
945 - */
946 - public function insert_form_field( $field, $form_id, $menu_order ) {
947 - $form_field = [
948 - 'post_type' => 'wpuf_input',
949 - 'post_status' => 'publish',
950 - 'post_content' => maybe_serialize( $field ),
951 - 'post_parent' => $form_id,
952 - 'menu_order' => $menu_order,
953 - ];
954 -
955 - return wp_insert_post( $form_field );
956 - }
957 -
958 - /**
959 - * Update form settings
960 - *
961 - * @param int $form_id
962 - * @param array $settings
963 - *
964 - * @return void
965 - */
966 - public function update_settings( $form_id, $settings ) {
967 - update_post_meta( $form_id, 'wpuf_form_settings', $settings );
968 - }
969 -
970 - /**
971 - * Update Notification
972 - *
973 - * @param int $form_id
974 - * @param array $notification
975 - *
976 - * @return void
977 - */
978 - public function update_notification( $form_id, $notifications ) {
979 - update_post_meta( $form_id, 'notifications', $notifications );
980 - }
981 -}
1 +<?php
2 +
3 +/**
4 + * Importer Abstract Class
5 + */
6 +abstract class WeForms_Importer_Abstract {
7 +
8 + /**
9 + * The importer ID
10 + *
11 + * @var string
12 + */
13 + public $id = '';
14 +
15 + /**
16 + * The importer title
17 + *
18 + * @var string
19 + */
20 + public $title = '';
21 +
22 + /**
23 + * The shortcode (without bracket)
24 + *
25 + * @var string
26 + */
27 + public $shortcode = '';
28 +
29 + /**
30 + * The ajax action string
31 + *
32 + * @var string
33 + */
34 + protected $action = '';
35 +
36 + /**
37 + * Conditional stub
38 + *
39 + * @var array
40 + */
41 + public $conditionals = array(
42 + 'condition_status' => 'no',
43 + 'cond_field' => array(),
44 + 'cond_operator' => array( '=' ),
45 + 'cond_option' => array( '- select -' ),
46 + 'cond_logic' => 'all'
47 + );
48 +
49 + public function __construct() {
50 + add_action( 'admin_notices', array( $this, 'maybe_show_notice' ) );
51 +
52 + add_action( 'wp_ajax_weforms_import_xdismiss_' . $this->id, array( $this, 'dismiss_notice' ) );
53 + add_action( 'wp_ajax_weforms_import_xforms_' . $this->id, array( $this, 'import_forms' ) );
54 + add_action( 'wp_ajax_weforms_import_xreplace_' . $this->id, array( $this, 'replace_action' ) );
55 + }
56 +
57 + /**
58 + * Get the importer name
59 + *
60 + * @return string
61 + */
62 + public function get_importer_name() {
63 + return $this->title;
64 + }
65 +
66 + /**
67 + * Get the shortcode ready for regular expression
68 + *
69 + * @return string
70 + */
71 + public function get_shortcode( $regex = false ) {
72 +
73 + if ( $regex ) {
74 + return preg_quote( $this->shortcode );
75 + }
76 +
77 + return $this->shortcode;
78 + }
79 +
80 + /**
81 + * Get all the forms
82 + *
83 + * @return array
84 + */
85 + abstract protected function get_forms();
86 +
87 + /**
88 + * Get the form name
89 + *
90 + * @param mixed $form
91 + *
92 + * @return string
93 + */
94 + abstract protected function get_form_name( $form );
95 +
96 + /**
97 + * Get the form id
98 + *
99 + * @param mixed $form
100 + *
101 + * @return int
102 + */
103 + abstract protected function get_form_id( $form );
104 +
105 + /**
106 + * Get all form fields of a form
107 + *
108 + * @param mixed $form
109 + *
110 + * @return array
111 + */
112 + abstract protected function get_form_fields( $form );
113 +
114 + /**
115 + * Get form settings of a form
116 + *
117 + * @param mixed $form
118 + *
119 + * @return array
120 + */
121 + abstract protected function get_form_settings( $form );
122 +
123 + /**
124 + * Get form notifications of a form
125 + *
126 + * @param mixed $form
127 + *
128 + * @return array
129 + */
130 + abstract protected function get_form_notifications( $form );
131 +
132 + /**
133 + * Check if the plugin exists
134 + *
135 + * @return boolean
136 + */
137 + abstract protected function plugin_exists();
138 +
139 + /**
140 + * Show notice if the plugin found
141 + *
142 + * @return void
143 + */
144 + public function maybe_show_notice() {
145 + if ( ! $this->plugin_exists() ) {
146 + return;
147 + }
148 +
149 + if ( $this->is_dimissed() || !current_user_can( 'manage_options' ) ) {
150 + return;
151 + }
152 +
153 + ?>
154 + <div class="notice notice-info">
155 + <p><strong><?php printf( __( '%s Detected', 'weforms' ), $this->get_importer_name() ); ?></strong></p>
156 + <p><?php printf( __( 'Hey, looks like you have <strong>%s</strong> installed. Would you like to <strong>import</strong> the forms into weForms?', 'weforms' ), $this->get_importer_name() ); ?></p>
157 +
158 + <p>
159 + <a href="#" class="button button-primary weforms-import-<?php echo $this->id ;?>" id="weforms-import-<?php echo $this->id ;?>"><?php _e( 'Import Forms', 'weforms' ); ?></a>
160 + <a href="#" class="button weforms-import-<?php echo $this->id ;?>" id="weforms-dimiss-<?php echo $this->id ;?>"><?php _e( 'No Thanks', 'weforms' ); ?></a>
161 + </p>
162 + </div>
163 +
164 + <script type="text/javascript">
165 + jQuery(function($) {
166 + $('.notice').on('click', 'a#weforms-import-<?php echo $this->id ;?>', function(e) {
167 + e.preventDefault();
168 +
169 + var self = $(this);
170 + self.addClass('updating-message');
171 +
172 + wp.ajax.send( 'weforms_import_xforms_<?php echo $this->id ;?>', {
173 + data: {
174 + type: self.data('type'),
175 + _wpnonce: '<?php echo wp_create_nonce( 'weforms' ); ?>'
176 + },
177 +
178 + success: function(response) {
179 + var html = '<p><strong>' + response.title + '</strong></p>' +
180 + '<p>' + response.message + '</p>' +
181 + '<p>' + response.action + '</p>';
182 +
183 + html += '<ul>';
184 + _.each(response.refs, function(el, index) {
185 + html += '<li><a target="_blank" href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit">' + el.title + '</a> - <a href="admin.php?page=weforms#/form/' + el.weforms_id + '/edit" target="_blank" class="button button-small"><span class="dashicons dashicons-external"></span> Edit</a></li>';
186 + });
187 +
188 + html += '</ul>';
189 + html += '<p>' + '<a href="#" class="button button-primary weforms-<?php echo $this->id ;?>-replace-action" data-type="replace"><?php _e( 'Replace Shortcodes', 'weforms' ); ?></a>&nbsp;' +
190 + '<a href="#" class="button weforms-<?php echo $this->id ;?>-replace-action" data-type="skip"><?php _e( 'No Thanks', 'weforms' ); ?></a></p>';
191 +
192 + self.closest('.notice').removeClass('notice-info').addClass('notice-success').html( html );
193 + },
194 +
195 + error: function(error) {
196 + var html = '<p><strong>' + error.title + '</strong></p>' +
197 + '<p>' + error.message + '</p>';
198 +
199 + self.closest('.notice').removeClass('notice-info').addClass('notice-error').html( html );
200 + },
201 +
202 + complete: function() {
203 + self.removeClass('updating-message');
204 + }
205 + });
206 + });
207 +
208 + $('.notice').on('click', '#weforms-dimiss-<?php echo $this->id ;?>', function(e) {
209 + e.preventDefault();
210 +
211 + $(this).closest('.notice').remove();
212 + wp.ajax.send('weforms_import_xdismiss_<?php echo $this->id ;?>');
213 + });
214 +
215 + $('.notice').on('click', 'a.weforms-<?php echo $this->id ;?>-replace-action', function(e) {
216 + e.preventDefault();
217 +
218 + var self = $(this);
219 + var notice = self.closest('.notice');
220 +
221 + self.addClass('updating-message');
222 +
223 + wp.ajax.send( 'weforms_import_xreplace_<?php echo $this->id ;?>', {
224 + data: {
225 + type: self.data('type'),
226 + _wpnonce: '<?php echo wp_create_nonce( 'weforms' ); ?>'
227 + },
228 +
229 + success: function(response) {
230 + notice.remove();
231 +
232 + if ( 'replace' === self.data('type') ) {
233 + alert( response );
234 + }
235 + },
236 +
237 + error: function(error) {
238 + notice.remove();
239 + alert( error );
240 + },
241 +
242 + complete: function() {
243 + self.removeClass('updating-message');
244 + }
245 + });
246 +
247 + });
248 + });
249 + </script>
250 + <?php
251 + }
252 +
253 + /**
254 + * Import forms
255 + *
256 + * @return void
257 + */
258 + public function import_forms() {
259 + check_ajax_referer( 'weforms' );
260 +
261 + $this->check_caps();
262 +
263 + // check if plugin installed
264 + if ( ! $this->plugin_exists() ) {
265 + wp_send_json_error( array(
266 + 'title' => __( 'Uh oh!', 'weforms' ),
267 + 'message' => sprintf( __( '%s is not installed.', 'weforms' ), $this->get_importer_name() )
268 + ) );
269 + }
270 +
271 + $imported = 0;
272 + $refs = array();
273 + $forms = $this->get_forms();
274 +
275 + if ( ! $forms ) {
276 + wp_send_json_error( array(
277 + 'title' => __( 'Uh oh!', 'weforms' ),
278 + 'message' => __( 'No forms found!', 'weforms' )
279 + ) );
280 +
281 + $this->dismiss_prompt();
282 + }
283 +
284 + if ( $forms ) {
285 + foreach ($forms as $form) {
286 + $form_name = $this->get_form_name( $form );
287 + $form_fields = $this->get_form_fields( $form );
288 + $settings = $this->get_form_settings( $form );
289 + $notifications = $this->get_form_notifications( $form );
290 +
291 + if ( $form_fields ) {
292 + $form_id = $this->insert_form( $form_name );
293 +
294 + if ( is_wp_error( $form_id ) ) {
295 + continue;
296 + }
297 +
298 + foreach ($form_fields as $menu_order => $form_field) {
299 + $this->insert_form_field( $form_field, $form_id, $menu_order );
300 + }
301 +
302 + $this->update_settings( $form_id, $settings );
303 + $this->update_notification( $form_id, $notifications );
304 +
305 + $imported++;
306 +
307 + $refs[ $this->get_form_id( $form ) ] = array(
308 + 'imported_id' => $this->get_form_id( $form ),
309 + 'weforms_id' => $form_id,
310 + 'title' => $form_name
311 + );
312 + }
313 + }
314 + }
315 +
316 + $this->dismiss_prompt();
317 + $this->update_refs( $refs );
318 +
319 + wp_send_json_success( array(
320 + 'title' => sprintf( _n( '%s form imported', '%s forms imported', $imported, 'weforms' ), $imported ),
321 + 'message' => __( 'We have successfully imported these forms into weForms. You could check and edit in-case anything weird happended.', 'weforms' ),
322 + 'action' => sprintf( __( 'Do you want to <strong>replace</strong> %s shortcodes with weForms?', 'weforms' ), $this->get_importer_name() ),
323 + 'refs' => $refs
324 + ) );
325 + }
326 +
327 + /**
328 + * Replace contact form 7 shortcodes
329 + *
330 + * @return void
331 + */
332 + public function replace_action() {
333 + check_ajax_referer( 'weforms' );
334 +
335 + $this->check_caps();
336 +
337 + if ( ! in_array( $_POST['type'], array( 'skip', 'replace' ) ) ) {
338 + wp_send_json_error( __( 'Not a valid action type.', 'weforms' ) );
339 + }
340 +
341 + if ( 'skip' == $_POST['type'] ) {
342 + wp_send_json_success();
343 + }
344 +
345 + $pages_query = new WP_Query( array(
346 + 'post_type' => 'page',
347 + 'posts_per_page' => -1,
348 + 's' => '[' . $this->get_shortcode()
349 + ) );
350 +
351 + if ( ! $pages_query->found_posts ) {
352 + wp_send_json_error( __( 'No pages found with shortcode. Skipped!', 'weforms' ) );
353 + }
354 +
355 + $count = 0;
356 + $refs = $this->get_refs();
357 + $pages = $pages_query->get_posts();
358 +
359 + foreach ($pages as $page) {
360 + preg_match_all( '/\[(\[?)(' . $this->get_shortcode( true ) . '|)(?![\w-])([^\]\/]*(?:\/(?!\])[^\]\/]*)*?)(?:(\/)\]|\](?:([^\[]*+(?:\[(?!\/\2\])[^\[]*+)*+)\[\/\2\])?)(\]?)/s', $page->post_content, $matches, PREG_SET_ORDER );
361 +
362 + if ( empty( $matches ) ) {
363 + continue;
364 + }
365 +
366 + foreach ( $matches as $shortcode ) {
367 + $atts = shortcode_parse_atts( $shortcode[0] );
368 +
369 + if ( isset( $atts['id'] ) && array_key_exists( $atts['id'], $refs ) ) {
370 + $replace = sprintf( '[weforms id="%d"]', $refs[ $atts['id'] ]['weforms_id'] );
371 +
372 + $post_content = str_replace( $shortcode[0], $replace, $page->post_content );
373 +
374 + wp_update_post( array(
375 + 'ID' => $page->ID,
376 + 'post_content' => $post_content
377 + ) );
378 +
379 + $count++;
380 + }
381 + }
382 + }
383 +
384 + wp_send_json_success( sprintf( _n( 'Replaced %d form', 'Replaced %d forms', $count, 'weforms' ), $count ) );
385 + }
386 +
387 + /**
388 + * Check capability if able to process
389 + *
390 + * @return void
391 + */
392 + public function check_caps() {
393 + if ( ! current_user_can( 'manage_options' ) ) {
394 + wp_send_json_error( __( 'You are not allowed.', 'weforms' ) );
395 + }
396 + }
397 +
398 + /**
399 + * If the prompt is dismissed
400 + *
401 + * @return boolean
402 + */
403 + public function is_dimissed() {
404 + return 'yes' == get_option( 'weforms_dismiss_xnotice_' . $this->id );
405 + }
406 +
407 + /**
408 + * Dismiss the prompt
409 + *
410 + * @return void
411 + */
412 + public function dismiss_prompt() {
413 + update_option( 'weforms_dismiss_xnotice_' . $this->id, 'yes' );
414 + }
415 +
416 + /**
417 + * Dismiss the notice
418 + *
419 + * @return void
420 + */
421 + public function dismiss_notice() {
422 + $this->dismiss_prompt();
423 +
424 + wp_send_json_success();
425 + }
426 +
427 + /**
428 + * Get the imported form refs
429 + *
430 + * @return array
431 + */
432 + public function get_refs() {
433 + return get_option( 'weforms_imported_xforms_' . $this->id, array() );
434 + }
435 +
436 + /**
437 + * Update the imported reference forms
438 + *
439 + * @param array $refs
440 + *
441 + * @return void
442 + */
443 + public function update_refs( $refs ) {
444 + update_option( 'weforms_imported_xforms_' . $this->id, $refs );
445 + }
446 +
447 + /**
448 + * Get form field
449 + *
450 + * @param string $type
451 + * @param array $args [description]
452 + *
453 + * @return array
454 + */
455 + public function get_form_field( $type, $args = array() ) {
456 + $defaults = array(
457 + 'required' => 'no',
458 + 'label' => '',
459 + 'name' => '',
460 + 'help' => '',
461 + 'css_class' => '',
462 + 'placeholder' => '',
463 + 'value' => '',
464 + 'default' => '',
465 + 'options' => array(),
466 + 'step' => '',
467 + 'min' => '',
468 + 'max' => '',
469 + 'extension' => '',
470 + 'max_size' => '', // file size
471 + 'size' => '', // file size
472 + 'first_placeholder' => '',
473 + 'first_default' => '',
474 + 'middle_placeholder' => '',
475 + 'middle_default' => '',
476 + 'last_placeholder' => '',
477 + 'last_default' => '',
478 + 'first_name' => '',
479 + 'middle_name' => '',
480 + 'last_name' => '',
481 + );
482 +
483 + $args = wp_parse_args( $args, $defaults );
484 +
485 + switch ( $type ) {
486 + case 'text':
487 + $field_content = array(
488 + 'input_type' => 'text',
489 + 'template' => 'text_field',
490 + 'required' => $args['required'],
491 + 'label' => $args['label'],
492 + 'name' => $args['name'],
493 + 'is_meta' => 'yes',
494 + 'help' => $args['help'],
495 + 'css' => $args['css_class'],
496 + 'placeholder' => $args['placeholder'],
497 + 'default' => $args['default'],
498 + 'size' => $args['size'],
499 + 'word_restriction' => '',
500 + 'wpuf_cond' => $this->conditionals
501 + );
502 + break;
503 +
504 + case 'email':
505 + $field_content = array(
506 + 'input_type' => 'email',
507 + 'template' => 'email_address',
508 + 'required' => $args['required'],
509 + 'label' => $args['label'],
510 + 'name' => $args['name'],
511 + 'is_meta' => 'yes',
512 + 'help' => $args['help'],
513 + 'css' => $args['css_class'],
514 + 'placeholder' => $args['placeholder'],
515 + 'default' => $args['default'],
516 + 'size' => $args['size'],
517 + 'word_restriction' => '',
518 + 'wpuf_cond' => $this->conditionals
519 + );
520 + break;
521 +
522 + case 'textarea':
523 + $field_content = array(
524 + 'input_type' => 'textarea',
525 + 'template' => 'textarea_field',
526 + 'required' => $args['required'],
527 + 'label' => $args['label'],
528 + 'name' => $args['name'],
529 + 'is_meta' => 'yes',
530 + 'help' => $args['help'],
531 + 'css' => $args['css_class'],
532 + 'rows' => 5,
533 + 'cols' => 25,
534 + 'placeholder' => $args['placeholder'],
535 + 'default' => $args['default'],
536 + 'rich' => 'no',
537 + 'word_restriction' => '',
538 + 'wpuf_cond' => $this->conditionals
539 + );
540 + break;
541 +
542 + case 'select':
543 + $field_content = array(
544 + 'input_type' => 'select',
545 + 'template' => 'dropdown_field',
546 + 'required' => $args['required'],
547 + 'label' => $args['label'],
548 + 'name' => $args['name'],
549 + 'is_meta' => 'yes',
550 + 'help' => '',
551 + 'css' => $args['css_class'],
552 + 'selected' => '',
553 + 'inline' => 'no',
554 + 'options' => $args['options'],
555 + 'wpuf_cond' => $this->conditionals
556 + );
557 + break;
558 +
559 + case 'multiselect':
560 + $field_content = array(
561 + 'input_type' => 'multiselect',
562 + 'template' => 'multiple_select',
563 + 'required' => $args['required'],
564 + 'label' => $args['label'],
565 + 'name' => $args['name'],
566 + 'is_meta' => 'yes',
567 + 'help' => '',
568 + 'css' => $args['css_class'],
569 + 'selected' => '',
570 + 'first' => __( '- select -', 'weforms' ),
571 + 'options' => $args['options'],
572 + 'wpuf_cond' => $this->conditionals
573 + );
574 + break;
575 +
576 + case 'date':
577 + $field_content = array(
578 + 'input_type' => 'date',
579 + 'template' => 'date_field',
580 + 'required' => $args['required'],
581 + 'label' => $args['label'],
582 + 'name' => $args['name'],
583 + 'is_meta' => 'yes',
584 + 'help' => '',
585 + 'css' => $args['css_class'],
586 + 'format' => 'dd/mm/yy',
587 + 'time' => '',
588 + 'is_publish_time' => '',
589 + 'wpuf_cond' => $this->conditionals
590 + );
591 + break;
592 +
593 + case 'range':
594 + case 'number':
595 +
596 + $field_content = array(
597 + 'input_type' => 'numeric_text',
598 + 'template' => 'numeric_text_field',
599 + 'required' => $args['required'],
600 + 'label' => $args['label'],
601 + 'name' => $args['name'],
602 + 'is_meta' => 'yes',
603 + 'help' => '',
604 + 'css' => $args['css_class'],
605 + 'placeholder' => $args['placeholder'],
606 + 'default' => $args['value'],
607 + 'size' => 40,
608 + 'step_text_field' => $args['step'],
609 + 'min_value_field' => $args['min'],
610 + 'max_value_field' => $args['max'],
611 + 'wpuf_cond' => $this->conditionals
612 + );
613 +
614 + break;
615 +
616 + case 'url':
617 + $field_content = array(
618 + 'input_type' => 'url',
619 + 'template' => 'website_url',
620 + 'required' => $args['required'],
621 + 'label' => $args['label'],
622 + 'name' => $args['name'],
623 + 'is_meta' => 'yes',
624 + 'help' => '',
625 + 'css' => $args['css_class'],
626 + 'placeholder' => '',
627 + 'default' => '',
628 + 'size' => 40,
629 + 'word_restriction' => '',
630 + 'wpuf_cond' => $this->conditionals
631 + );
632 +
633 + break;
634 +
635 + case 'checkbox':
636 + $field_content = array(
637 + 'input_type' => 'checkbox',
638 + 'template' => 'checkbox_field',
639 + 'required' => $args['required'],
640 + 'label' => $args['label'],
641 + 'name' => $args['name'],
642 + 'is_meta' => 'yes',
643 + 'help' => '',
644 + 'css' => $args['css_class'],
645 + 'selected' => '',
646 + 'inline' => 'no',
647 + 'options' => $args['options'],
648 + 'wpuf_cond' => $this->conditionals
649 + );
650 + break;
651 +
652 + case 'radio':
653 + $field_content = array(
654 + 'input_type' => 'radio',
655 + 'template' => 'radio_field',
656 + 'required' => $args['required'],
657 + 'label' => $args['label'],
658 + 'name' => $args['name'],
659 + 'is_meta' => 'yes',
660 + 'help' => '',
661 + 'css' => $args['css_class'],
662 + 'selected' => '',
663 + 'inline' => 'no',
664 + 'options' => $args['options'],
665 + 'wpuf_cond' => $this->conditionals
666 + );
667 + break;
668 +
669 + case 'hidden':
670 + $field_content = array(
671 + 'input_type' => 'hidden',
672 + 'template' => 'custom_hidden_field',
673 + 'label' => $args['label'],
674 + 'name' => $args['name'],
675 + 'is_meta' => 'yes',
676 + 'wpuf_cond' => $this->conditionals
677 + );
678 + break;
679 +
680 + case 'section_break':
681 + $field_content = array(
682 + 'input_type' => 'section_break',
683 + 'template' => 'section_break',
684 + 'label' => $args['label'],
685 + 'name' => $args['name'],
686 + 'wpuf_cond' => $this->conditionals
687 + );
688 + break;
689 +
690 + case 'html':
691 + $field_content = array(
692 + 'input_type' => 'html',
693 + 'template' => 'custom_html',
694 + 'label' => $args['label'],
695 + 'name' => $args['name'],
696 + 'html' => $args['default'],
697 + 'wpuf_cond' => $this->conditionals
698 + );
699 + break;
700 +
701 + case 'toc':
702 + $field_content = array(
703 + 'input_type' => 'toc',
704 + 'template' => 'toc',
705 + 'required' => $args['required'],
706 + 'name' => $args['name'],
707 + 'description' => $args['label'],
708 + 'is_meta' => 'yes',
709 + 'show_checkbox' => true,
710 + 'wpuf_cond' => $this->conditionals
711 + );
712 + break;
713 +
714 + case 'recaptcha':
715 + $field_content = array(
716 + 'input_type' => 'recaptcha',
717 + 'template' => 'recaptcha',
718 + 'required' => $args['required'],
719 + 'label' => $args['label'],
720 + 'name' => $args['name'],
721 + 'recaptcha_type' => 'enable_no_captcha',
722 + 'is_meta' => 'yes',
723 + 'help' => '',
724 + 'css' => $args['css_class'],
725 + 'placeholder' => '',
726 + 'default' => '',
727 + 'size' => 40,
728 + 'word_restriction' => '',
729 + 'wpuf_cond' => $this->conditionals
730 + );
731 + break;
732 +
733 + case 'file':
734 + $field_content = array(
735 + 'input_type' => 'file_upload',
736 + 'template' => 'file_upload',
737 + 'required' => $args['required'],
738 + 'label' => $args['label'],
739 + 'name' => $args['name'],
740 + 'is_meta' => 'yes',
741 + 'help' => $args['help'],
742 + 'css' => $args['css_class'],
743 + 'max_size' => $args['max_size'],
744 + 'count' => '1',
745 + 'extension' => $args['extension'],
746 + 'wpuf_cond' => $this->conditionals
747 + );
748 + break;
749 +
750 + case 'name':
751 + $field_content = array(
752 + 'input_type' => 'name',
753 + 'template' => 'name_field',
754 + 'required' => $args['required'],
755 + 'label' => $args['label'],
756 + 'name' => $args['name'],
757 + 'is_meta' => 'yes',
758 + 'format' => $args['format'],
759 + 'first_name' => $args['first_name'],
760 + 'middle_name' => $args['middle_name'],
761 + 'last_name' => $args['last_name'],
762 + 'hide_subs' => false,
763 + 'help' => $args['help'],
764 + 'css' => $args['css_class'],
765 + 'wpuf_cond' => $this->conditionals,
766 + );
767 + break;
768 +
769 + case 'ratings':
770 + $field_content = array(
771 + 'input_type' => 'ratings',
772 + 'template' => 'ratings',
773 + 'required' => $args['required'],
774 + 'label' => $args['label'],
775 + 'name' => $args['name'],
776 + 'is_meta' => 'yes',
777 + 'help' => '',
778 + 'css' => $args['css_class'],
779 + 'options' => $args['options'],
780 + 'wpuf_cond' => $this->conditionals
781 + );
782 + break;
783 +
784 + case 'linear_scale':
785 + $field_content = array(
786 + 'input_type' => 'linear_scale',
787 + 'template' => 'linear_scale',
788 + 'required' => $args['required'],
789 + 'label' => $args['label'],
790 + 'name' => $args['name'],
791 + 'is_meta' => 'yes',
792 + 'help' => '',
793 + 'css' => $args['css_class'],
794 + 'options' => $args['options'],
795 + 'wpuf_cond' => $this->conditionals
796 + );
797 + break;
798 +
799 + case 'checkbox_grid':
800 + $field_content = array(
801 + 'input_type' => 'checkbox_grid',
802 + 'template' => 'checkbox_grid',
803 + 'required' => $args['required'],
804 + 'label' => $args['label'],
805 + 'name' => $args['name'],
806 + 'is_meta' => 'yes',
807 + 'help' => '',
808 + 'css' => $args['css_class'],
809 + 'options' => $args['options'],
810 + 'wpuf_cond' => $this->conditionals
811 + );
812 + break;
813 +
814 + case 'multiple_choice_grid':
815 + $field_content = array(
816 + 'input_type' => 'multiple_choice_grid',
817 + 'template' => 'multiple_choice_grid',
818 + 'required' => $args['required'],
819 + 'label' => $args['label'],
820 + 'name' => $args['name'],
821 + 'is_meta' => 'yes',
822 + 'help' => '',
823 + 'css' => $args['css_class'],
824 + 'options' => $args['options'],
825 + 'wpuf_cond' => $this->conditionals
826 + );
827 + break;
828 +
829 + case 'single_product':
830 + $field_content = array(
831 + 'input_type' => 'single_product',
832 + 'template' => 'single_product',
833 + 'required' => $args['required'],
834 + 'label' => $args['label'],
835 + 'name' => $args['name'],
836 + 'is_meta' => 'yes',
837 + 'help' => '',
838 + 'css' => $args['css_class'],
839 + 'options' => $args['options'],
840 + 'wpuf_cond' => $this->conditionals
841 + );
842 + break;
843 +
844 + case 'multiple_product':
845 + $field_content = array(
846 + 'input_type' => 'multiple_product',
847 + 'template' => 'multiple_product',
848 + 'required' => $args['required'],
849 + 'label' => $args['label'],
850 + 'name' => $args['name'],
851 + 'is_meta' => 'yes',
852 + 'help' => '',
853 + 'css' => $args['css_class'],
854 + 'options' => $args['options'],
855 + 'wpuf_cond' => $this->conditionals
856 + );
857 + break;
858 +
859 + case 'payment_method':
860 + $field_content = array(
861 + 'input_type' => 'payment_method',
862 + 'template' => 'payment_method',
863 + 'required' => $args['required'],
864 + 'label' => $args['label'],
865 + 'name' => $args['name'],
866 + 'is_meta' => 'yes',
867 + 'help' => '',
868 + 'css' => $args['css_class'],
869 + 'options' => $args['options'],
870 + 'wpuf_cond' => $this->conditionals
871 + );
872 + break;
873 +
874 + case 'total':
875 + $field_content = array(
876 + 'input_type' => 'total',
877 + 'template' => 'total',
878 + 'required' => $args['required'],
879 + 'label' => $args['label'],
880 + 'name' => $args['name'],
881 + 'is_meta' => 'yes',
882 + 'help' => '',
883 + 'css' => $args['css_class'],
884 + 'options' => $args['options'],
885 + 'wpuf_cond' => $this->conditionals
886 + );
887 + break;
888 + }
889 +
890 + return $field_content;
891 + }
892 +
893 + /**
894 + * Default form settings
895 + *
896 + * @return array
897 + */
898 + public function get_default_form_settings() {
899 + $form_settings = array(
900 + 'redirect_to' => 'same',
901 + 'message' => __( 'Thanks for contacting us! We will get in touch with you shortly.', 'weforms' ),
902 + 'page_id' => '',
903 + 'url' => '',
904 + 'submit_text' => __( 'Submit Query', 'weforms' ),
905 + 'schedule_form' => 'false',
906 + 'schedule_start' => '',
907 + 'schedule_end' => '',
908 + 'sc_pending_message' => __( 'Form submission hasn\'t been started yet', 'weforms' ),
909 + 'sc_expired_message' => __( 'Form submission is now closed.', 'weforms' ),
910 + 'require_login' => 'false',
911 + 'req_login_message' => __( 'You need to login to submit a query.', 'weforms' ),
912 + 'limit_entries' => 'false',
913 + 'limit_number' => '1000',
914 + 'limit_message' => __( 'Sorry, we have reached the maximum number of submissions.', 'weforms' ),
915 + 'label_position' => 'above',
916 + );
917 +
918 + return $form_settings;
919 + }
920 +
921 + /**
922 + * Insert a form
923 + *
924 + * @param string $form_name
925 + *
926 + * @return ID|WP_Error
927 + */
928 + public function insert_form( $form_name ) {
929 + $weforms_form = array(
930 + 'post_title' => sprintf( '[%s] %s', strtoupper( $this->id ), $form_name ),
931 + 'post_type' => 'wpuf_contact_form',
932 + 'post_status' => 'publish',
933 + 'post_author' => get_current_user_id()
934 + );
935 +
936 + return wp_insert_post( $weforms_form );
937 + }
938 +
939 + /**
940 + * Insert a form field
941 + *
942 + * @param array $field
943 + * @param int $form_id
944 + * @param int $menu_order
945 + *
946 + * @return int|WP_Error
947 + */
948 + public function insert_form_field( $field, $form_id, $menu_order ) {
949 + $form_field = array(
950 + 'post_type' => 'wpuf_input',
951 + 'post_status' => 'publish',
952 + 'post_content' => maybe_serialize( $field ),
953 + 'post_parent' => $form_id,
954 + 'menu_order' => $menu_order
955 + );
956 +
957 + return wp_insert_post( $form_field );
958 + }
959 +
960 + /**
961 + * Update form settings
962 + *
963 + * @param int $form_id
964 + * @param array $settings
965 + *
966 + * @return void
967 + */
968 + public function update_settings( $form_id, $settings ) {
969 + update_post_meta( $form_id, 'wpuf_form_settings', $settings );
970 + }
971 +
972 + /**
973 + * Update Notification
974 + *
975 + * @param int $form_id
976 + * @param array $notification
977 + *
978 + * @return void
979 + */
980 + public function update_notification( $form_id, $notifications ) {
981 + update_post_meta( $form_id, 'notifications', $notifications );
982 + }
983 +}