PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.20
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.20
6.35 6.34 6.33.1 6.33 6.32.1 6.32 6.31 6.25 6.25.1 6.26 6.26.1 6.27 6.28 6.29 6.3 6.3.1 6.3.2 6.30 6.4 6.4.1 6.4.2 6.5 6.5.1 6.5.2 6.5.3 All 141 releases
← All changes | classes/models/FrmFormMigrator.php +36 -179 6.276.20 View file →
@@ -4,54 +4,22 @@
4 4 }
5 5
6 6 abstract class FrmFormMigrator {
7 7
8 - /**
9 - * @var bool
10 - */
11 8 public $source_active;
12 9
13 - /**
14 - * @var string
15 - */
16 10 public $slug;
17 -
18 - /**
19 - * @var string
20 - */
21 11 public $path;
22 -
23 - /**
24 - * @var string
25 - */
26 12 public $name;
27 13
28 - /**
29 - * @var array
30 - */
31 14 public $response = array();
32 -
33 - /**
34 - * @var string
35 - */
36 15 public $tracking = 'frm_forms_imported';
37 16
38 - /**
39 - * @var array
40 - */
41 - protected $fields_map = array();
42 -
43 - /**
44 - * @var mixed
45 - */
17 + protected $fields_map = array();
46 18 protected $current_source_form;
19 + protected $current_section = array();
47 20
48 21 /**
49 - * @var array
50 - */
51 - protected $current_section = array();
52 -
53 - /**
54 22 * Define required properties.
55 23 */
56 24 public function __construct() {
57 25 if ( ! is_admin() ) {
@@ -62,9 +30,8 @@
62 30 require_once ABSPATH . '/wp-admin/includes/plugin.php';
63 31 }
64 32
65 33 $this->source_active = is_plugin_active( $this->path );
66 -
67 34 if ( ! $this->source_active ) {
68 35 // if source plugin is not installed, do nothing
69 36 return;
70 37 }
@@ -76,21 +43,14 @@
76 43 'unsupported' => array(),
77 44 );
78 45 }
79 46
80 - /**
81 - * @return void
82 - */
83 47 private function maybe_add_to_import_page() {
84 48 add_action( 'frm_import_settings', array( $this, 'import_page' ) );
85 49 add_action( 'wp_ajax_frm_import_' . $this->slug, array( $this, 'import_forms' ) );
86 50 }
87 51
88 - /**
89 - * @return void
90 - */
91 52 public function import_page() {
92 - $forms = $this->get_forms();
93 53 ?>
94 54 <div class="wrap">
95 55 <h2 class="frm-h2"><?php echo esc_html( $this->name ); ?> Importer</h2>
96 56 <p class="howto">Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>.</p>
@@ -105,13 +65,13 @@
105 65 <input type="hidden" name="slug" value="<?php echo esc_attr( $this->slug ); ?>" />
106 66 <input type="hidden" name="action" value="frm_import_<?php echo esc_attr( $this->slug ); ?>" />
107 67 <div style="max-width:400px;text-align:left;">
108 68 <?php
109 - if ( ! $forms ) {
69 + if ( empty( $this->get_forms() ) ) {
110 70 esc_html_e( 'No Forms Found.', 'formidable' );
111 71 }
112 72 ?>
113 - <?php foreach ( $forms as $form_id => $name ) { ?>
73 + <?php foreach ( $this->get_forms() as $form_id => $name ) { ?>
114 74 <p>
115 75 <label>
116 76 <input type="checkbox" name="form_id[]"
117 77 value="<?php echo esc_attr( $form_id ); ?>" checked="checked" />
@@ -125,23 +85,9 @@
125 85 </label>
126 86 </p>
127 87 <?php } ?>
128 88 </div>
129 - <?php
130 - $button_atts = array(
131 - 'type' => 'submit',
132 - 'class' => 'button button-primary frm-button-primary',
133 - );
134 -
135 - if ( ! $forms ) {
136 - $button_atts['disabled'] = 'disabled';
137 - }
138 - ?>
139 - <p class="submit">
140 - <button <?php FrmAppHelper::array_to_html_params( $button_atts, true ); ?>>
141 - <?php esc_html_e( 'Start Import', 'formidable' ); ?>
142 - </button>
143 - </p>
89 + <p class="submit"><button type="submit" class="button button-primary frm-button-primary">Start Import</button></p>
144 90 </form>
145 91 <div id="frm-importer-process" class="frm-importer-process frm_hidden">
146 92
147 93 <p class="process-count">
@@ -150,15 +96,10 @@
150 96 from <?php echo esc_html( $this->name ); ?>.
151 97 </p>
152 98
153 99 <p class="process-completed" class="frm_hidden">
154 - <?php
155 - printf(
156 - // translators: %s is the number of forms that were imported.
157 - esc_html__( 'The import process has finished! We have successfully imported %s forms. You can review the results below.', 'formidable' ),
158 - '<span class="forms-completed"></span>'
159 - );
160 - ?>
100 + The import process has finished! We have successfully imported
101 + <span class="forms-completed"></span> forms. You can review the results below.
161 102 </p>
162 103
163 104 <div class="status"></div>
164 105
@@ -170,12 +111,11 @@
170 111 }
171 112
172 113 /**
173 114 * Import all forms using ajax
174 - *
175 - * @return void
176 115 */
177 116 public function import_forms() {
117 +
178 118 check_ajax_referer( 'frm_ajax', 'nonce' );
179 119 FrmAppHelper::permission_check( 'frm_edit_forms' );
180 120
181 121 $forms = FrmAppHelper::get_simple_request(
@@ -187,9 +127,8 @@
187 127 );
188 128
189 129 if ( is_array( $forms ) ) {
190 130 $imported = array();
191 -
192 131 foreach ( $forms as $form_id ) {
193 132 $imported[] = $this->import_form( $form_id );
194 133 }
195 134 } else {
@@ -200,14 +139,11 @@
200 139 }
201 140
202 141 /**
203 142 * Import a single form
204 - *
205 - * @param int $source_id Source form ID.
206 - *
207 - * @return array
208 143 */
209 144 protected function import_form( $source_id ) {
145 +
210 146 $source_form = $this->get_form( $source_id );
211 147 $source_form_name = $this->get_form_name( $source_form );
212 148 $source_fields = $this->get_form_fields( $source_form );
213 149 $this->maybe_add_end_fields( $source_fields );
@@ -214,9 +150,9 @@
214 150
215 151 $this->current_source_form = $source_form;
216 152
217 153 // If form does not contain fields, bail.
218 - if ( ! $source_fields ) {
154 + if ( empty( $source_fields ) ) {
219 155 wp_send_json_success(
220 156 array(
221 157 'error' => true,
222 158 'name' => esc_html( $source_form_name ),
@@ -238,14 +174,8 @@
238 174
239 175 return $response;
240 176 }
241 177
242 - /**
243 - * @param int|string $source_id
244 - * @param string $source_form_name
245 - *
246 - * @return array
247 - */
248 178 protected function prepare_new_form( $source_id, $source_form_name ) {
249 179 return array(
250 180 'import_form_id' => $source_id,
251 181 'fields' => array(),
@@ -255,29 +185,18 @@
255 185 'actions' => array(),
256 186 );
257 187 }
258 188
259 - /**
260 - * @param array|object $form
261 - * @param array $new_form
262 - *
263 - * @return void
264 - */
265 189 protected function prepare_form( $form, &$new_form ) {
266 190 // customize this function
267 191 }
268 192
269 - /**
270 - * @param array $fields
271 - * @param array $form
272 - *
273 - * @return void
274 - */
275 193 protected function prepare_fields( $fields, &$form ) {
276 194 $field_order = 1;
277 195
278 196 foreach ( $fields as $field ) {
279 197 $field = (array) $field;
198 +
280 199 $label = $this->get_field_label( $field );
281 200 $type = $this->get_field_type( $field );
282 201
283 202 // check if field is unsupported. If unsupported make note and continue
@@ -290,9 +209,10 @@
290 209 $this->response['upgrade_omit'][] = $label;
291 210 continue;
292 211 }
293 212
294 - $new_type = $this->convert_field_type( $type, $field );
213 + $new_type = $this->convert_field_type( $type, $field );
214 +
295 215 $new_field = FrmFieldsHelper::setup_new_vars( $new_type );
296 216 $new_field['name'] = $label;
297 217 $new_field['field_order'] = $field_order;
298 218 $new_field['original'] = $type;
@@ -298,10 +218,9 @@
298 218 $new_field['original'] = $type;
299 219
300 220 $this->prepare_field( $field, $new_field );
301 221
302 - $in_section = ! empty( $this->current_section ) && ! in_array( $new_type, $this->fields_with_end(), true ) && $new_type !== 'break';
303 -
222 + $in_section = ! empty( $this->current_section ) && ! in_array( $new_type, $this->fields_with_end() ) && $new_type !== 'break';
304 223 if ( $in_section ) {
305 224 $new_field['field_options']['in_section'] = $this->current_section['id'];
306 225 }
307 226
@@ -306,9 +225,9 @@
306 225 }
307 226
308 227 $form['fields'][] = $new_field;
309 228
310 - if ( in_array( $new_type, $this->fields_with_end(), true ) ) {
229 + if ( in_array( $new_type, $this->fields_with_end() ) ) {
311 230 $this->current_section = $field;
312 231 } elseif ( $new_type === 'break' || $new_type === 'end_divider' ) {
313 232 $this->current_section = array();
314 233 }
@@ -326,14 +245,8 @@
326 245 }
327 246 }//end foreach
328 247 }
329 248
330 - /**
331 - * @param array|object $field
332 - * @param array $new_field
333 - *
334 - * @return void
335 - */
336 249 protected function prepare_field( $field, &$new_field ) {
337 250 // customize this function
338 251 }
339 252
@@ -340,10 +253,8 @@
340 253 /**
341 254 * Add any field types that will need an end section field.
342 255 *
343 256 * @since 4.04.03
344 - *
345 - * @return array
346 257 */
347 258 protected function fields_with_end() {
348 259 return array( 'divider' );
349 260 }
@@ -349,43 +260,37 @@
349 260 }
350 261
351 262 /**
352 263 * @since 4.04.03
353 - *
354 - * @param array $fields
355 - *
356 - * @return void
357 264 */
358 265 protected function maybe_add_end_fields( &$fields ) {
359 266 $with_end = $this->fields_with_end();
360 -
361 - if ( ! $with_end ) {
267 + if ( empty( $with_end ) ) {
362 268 return;
363 269 }
364 270
365 - $open = array();
271 + $open = array();
272 +
366 273 $order = 0;
367 -
368 274 foreach ( $fields as $field ) {
369 275 ++$order;
370 276 $type = $this->get_field_type( $field );
371 277 $new_type = $this->convert_field_type( $type, $field );
372 -
373 - if ( ! in_array( $new_type, $with_end, true ) && $new_type !== 'break' ) {
278 + if ( ! in_array( $new_type, $with_end ) && $new_type !== 'break' ) {
374 279 continue;
375 280 }
376 281
377 - if ( $open ) {
282 + if ( ! empty( $open ) ) {
378 283 $this->insert_end_section( $fields, $order );
379 284 $open = array();
380 285 }
381 286
382 - if ( in_array( $new_type, $with_end, true ) ) {
287 + if ( in_array( $new_type, $with_end ) ) {
383 288 $open = $field;
384 289 }
385 290 }
386 291
387 - if ( $open ) {
292 + if ( ! empty( $open ) ) {
388 293 $this->insert_end_section( $fields, $order );
389 294 }
390 295 }
391 296
@@ -390,13 +295,8 @@
390 295 }
391 296
392 297 /**
393 298 * @since 4.04.03
394 - *
395 - * @param array $fields
396 - * @param int $order
397 - *
398 - * @return void
399 299 */
400 300 protected function insert_end_section( &$fields, &$order ) {
401 301 $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' );
402 302 $sub['name'] = __( 'Section Buttons', 'formidable' );
@@ -409,15 +309,8 @@
409 309 * Replace the original combo field with a group.
410 310 * This switches the name field to individual fields.
411 311 *
412 312 * @since 4.04.03
413 - *
414 - * @param array $subs
415 - * @param int $start
416 - * @param int $remove
417 - * @param array $fields
418 - *
419 - * @return void
420 313 */
421 314 protected function insert_fields_in_array( $subs, $start, $remove, &$fields ) {
422 315 array_splice( $fields, $start, $remove, $subs );
423 316 }
@@ -429,13 +322,11 @@
429 322 * This also eases the recursive use of the method,
430 323 * particularly the overrides in child classes, as
431 324 * there will be no need to rebuild the converter
432 325 * array at usage locations.
433 - *
434 - * @return string
435 326 */
436 327 protected function convert_field_type( $type, $field = array(), $use = '' ) {
437 - if ( ! $field ) {
328 + if ( empty( $field ) ) {
438 329 // For reverse compatibility.
439 330 return $type;
440 331 }
441 332
@@ -442,20 +333,19 @@
442 333 return $use ? $use : $field['type'];
443 334 }
444 335
445 336 /**
446 - * Add the new form to the database and return AJAX data.
337 + * Add the new form to the database and return AJAX data.å
447 338 *
448 339 * @param array $form Form to import.
449 340 * @param array $upgrade_omit No field alternative.
450 - *
451 - * @return array
452 341 */
453 342 protected function add_form( $form, $upgrade_omit = array() ) {
343 +
454 344 // Create empty form so we have an ID to work with.
455 345 $form_id = $this->create_form( $form );
456 346
457 - if ( ! $form_id ) {
347 + if ( empty( $form_id ) ) {
458 348 return $this->form_creation_error_response( $form );
459 349 }
460 350
461 351 $this->create_fields( $form_id, $form );
@@ -490,12 +380,8 @@
490 380 }
491 381
492 382 /**
493 383 * @since 4.04.03
494 - *
495 - * @param array $form
496 - *
497 - * @return array
498 384 */
499 385 protected function form_creation_error_response( $form ) {
500 386 return array(
501 387 'error' => true,
@@ -508,10 +394,8 @@
508 394 * @since 4.04.03
509 395 *
510 396 * @param int $form_id
511 397 * @param array $form
512 - *
513 - * @return void
514 398 */
515 399 protected function create_fields( $form_id, &$form ) {
516 400 foreach ( $form['fields'] as $key => $new_field ) {
517 401 $new_field['form_id'] = $form_id;
@@ -521,12 +405,9 @@
521 405
522 406 /**
523 407 * @since 4.04.03
524 408 *
525 - * @param array $form
526 - * @param int|string $form_id
527 - *
528 - * @return void
409 + * @param array $form
529 410 */
530 411 protected function create_emails( $form, $form_id ) {
531 412 foreach ( $form['actions'] as $action ) {
532 413 $this->save_action( $action, $form, $form_id );
@@ -538,10 +419,8 @@
538 419 *
539 420 * @param array $action
540 421 * @param array $form
541 422 * @param int $form_id
542 - *
543 - * @return int|WP_Error
544 423 */
545 424 protected function save_action( $action, $form, $form_id ) {
546 425 /**
547 426 * @var FrmFormAction
@@ -548,9 +427,8 @@
548 427 */
549 428 $action_control = FrmFormActionsController::get_form_actions( $action['type'] );
550 429 unset( $action['type'] );
551 430 $new_action = $action_control->prepare_new( $form_id );
552 -
553 431 foreach ( $action as $key => $value ) {
554 432 if ( $key === 'post_title' ) {
555 433 $new_action->post_title = $value;
556 434 } elseif ( $key === 'ID' ) {
@@ -573,12 +451,11 @@
573 451 * been imported.
574 452 *
575 453 * @param int $source_id Imported plugin form ID.
576 454 * @param int $new_form_id Formidable form ID.
577 - *
578 - * @return void
579 455 */
580 456 protected function track_import( $source_id, $new_form_id ) {
457 +
581 458 $imported = $this->get_tracked_import();
582 459
583 460 $imported[ $this->slug ][ $new_form_id ] = $source_id;
584 461
@@ -599,18 +476,14 @@
599 476 */
600 477 private function is_imported( $source_id ) {
601 478 $imported = $this->get_tracked_import();
602 479 $new_form_id = 0;
603 -
604 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
605 480 if ( ! isset( $imported[ $this->slug ] ) || ! in_array( $source_id, $imported[ $this->slug ] ) ) {
606 481 return $new_form_id;
607 482 }
608 483
609 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
610 484 $new_form_id = array_search( $source_id, array_reverse( $imported[ $this->slug ], true ) );
611 -
612 - if ( $new_form_id && ! FrmForm::get_key_by_id( $new_form_id ) ) {
485 + if ( ! empty( $new_form_id ) && empty( FrmForm::get_key_by_id( $new_form_id ) ) ) {
613 486 // Allow reimport if the form was deleted.
614 487 $new_form_id = 0;
615 488 }
616 489
@@ -625,15 +498,12 @@
625 498 protected function unsupported_field_types() {
626 499 return array();
627 500 }
628 501
629 - /**
630 - * @param string $type
631 - *
632 - * @return bool
633 - */
634 502 private function is_unsupported_field( $type ) {
635 - return in_array( $type, $this->unsupported_field_types(), true );
503 + $fields = $this->unsupported_field_types();
504 +
505 + return in_array( $type, $fields, true );
636 506 }
637 507
638 508 /**
639 509 * Strict PRO fields with no Lite alternatives.
@@ -643,19 +513,12 @@
643 513 protected function skip_pro_fields() {
644 514 return array();
645 515 }
646 516
647 - /**
648 - * @param string $type
649 - *
650 - * @return bool
651 - */
652 517 protected function should_skip_field( $type ) {
653 - if ( FrmAppHelper::pro_is_installed() ) {
654 - return false;
655 - }
518 + $skip_pro_fields = $this->skip_pro_fields();
656 519
657 - return in_array( $type, $this->skip_pro_fields(), true );
520 + return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) );
658 521 }
659 522
660 523 /**
661 524 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
@@ -677,13 +540,8 @@
677 540 public function get_forms() {
678 541 return array();
679 542 }
680 543
681 - /**
682 - * @param int|string $id
683 - *
684 - * @return array
685 - */
686 544 public function get_form( $id ) {
687 545 return array();
688 546 }
689 547
@@ -719,11 +577,10 @@
719 577 *
720 578 * @return string
721 579 */
722 580 protected function get_field_label( $field ) {
723 - $label = $field['label'] ?? '';
724 -
725 - if ( $label ) {
581 + $label = isset( $field['label'] ) ? $field['label'] : '';
582 + if ( ! empty( $label ) ) {
726 583 return $label;
727 584 }
728 585
729 586 $type = $this->get_field_type( $field );