PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.24.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.24.1
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 +43 -171 6.33 → 6.24.1 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,11 +30,10 @@
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 - // If source plugin is not installed, do nothing
35 + // if source plugin is not installed, do nothing
69 36 return;
70 37 }
71 38
72 39 $this->maybe_add_to_import_page();
@@ -76,22 +43,15 @@
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 53 $forms = $this->get_forms();
93 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
94 54 ?>
95 55 <div class="wrap">
96 56 <h2 class="frm-h2"><?php echo esc_html( $this->name ); ?> Importer</h2>
97 57 <p class="howto">Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>.</p>
@@ -131,9 +91,8 @@
131 91 $button_atts = array(
132 92 'type' => 'submit',
133 93 'class' => 'button button-primary frm-button-primary',
134 94 );
135 -
136 95 if ( ! $forms ) {
137 96 $button_atts['disabled'] = 'disabled';
138 97 }
139 98 ?>
@@ -167,17 +126,15 @@
167 126 </div>
168 127 </div>
169 128 </div>
170 129 <?php
171 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
172 130 }
173 131
174 132 /**
175 133 * Import all forms using ajax
176 - *
177 - * @return void
178 134 */
179 135 public function import_forms() {
136 +
180 137 check_ajax_referer( 'frm_ajax', 'nonce' );
181 138 FrmAppHelper::permission_check( 'frm_edit_forms' );
182 139
183 140 $forms = FrmAppHelper::get_simple_request(
@@ -189,9 +146,8 @@
189 146 );
190 147
191 148 if ( is_array( $forms ) ) {
192 149 $imported = array();
193 -
194 150 foreach ( $forms as $form_id ) {
195 151 $imported[] = $this->import_form( $form_id );
196 152 }
197 153 } else {
@@ -202,14 +158,11 @@
202 158 }
203 159
204 160 /**
205 161 * Import a single form
206 - *
207 - * @param int $source_id Source form ID.
208 - *
209 - * @return array
210 162 */
211 163 protected function import_form( $source_id ) {
164 +
212 165 $source_form = $this->get_form( $source_id );
213 166 $source_form_name = $this->get_form_name( $source_form );
214 167 $source_fields = $this->get_form_fields( $source_form );
215 168 $this->maybe_add_end_fields( $source_fields );
@@ -216,9 +169,9 @@
216 169
217 170 $this->current_source_form = $source_form;
218 171
219 172 // If form does not contain fields, bail.
220 - if ( ! $source_fields ) {
173 + if ( empty( $source_fields ) ) {
221 174 wp_send_json_success(
222 175 array(
223 176 'error' => true,
224 177 'name' => esc_html( $source_form_name ),
@@ -240,14 +193,8 @@
240 193
241 194 return $response;
242 195 }
243 196
244 - /**
245 - * @param int|string $source_id
246 - * @param string $source_form_name
247 - *
248 - * @return array
249 - */
250 197 protected function prepare_new_form( $source_id, $source_form_name ) {
251 198 return array(
252 199 'import_form_id' => $source_id,
253 200 'fields' => array(),
@@ -257,33 +204,22 @@
257 204 'actions' => array(),
258 205 );
259 206 }
260 207
261 - /**
262 - * @param array|object $form
263 - * @param array $new_form
264 - *
265 - * @return void
266 - */
267 208 protected function prepare_form( $form, &$new_form ) {
268 - // Customize this function
209 + // customize this function
269 210 }
270 211
271 - /**
272 - * @param array $fields
273 - * @param array $form
274 - *
275 - * @return void
276 - */
277 212 protected function prepare_fields( $fields, &$form ) {
278 213 $field_order = 1;
279 214
280 215 foreach ( $fields as $field ) {
281 216 $field = (array) $field;
217 +
282 218 $label = $this->get_field_label( $field );
283 219 $type = $this->get_field_type( $field );
284 220
285 - // Check if field is unsupported. If unsupported make note and continue
221 + // check if field is unsupported. If unsupported make note and continue
286 222 if ( $this->is_unsupported_field( $type ) ) {
287 223 $this->response['unsupported'][] = $label;
288 224 continue;
289 225 }
@@ -292,9 +228,10 @@
292 228 $this->response['upgrade_omit'][] = $label;
293 229 continue;
294 230 }
295 231
296 - $new_type = $this->convert_field_type( $type, $field );
232 + $new_type = $this->convert_field_type( $type, $field );
233 +
297 234 $new_field = FrmFieldsHelper::setup_new_vars( $new_type );
298 235 $new_field['name'] = $label;
299 236 $new_field['field_order'] = $field_order;
300 237 $new_field['original'] = $type;
@@ -300,10 +237,9 @@
300 237 $new_field['original'] = $type;
301 238
302 239 $this->prepare_field( $field, $new_field );
303 240
304 - $in_section = $this->current_section && ! in_array( $new_type, $this->fields_with_end(), true ) && $new_type !== 'break';
305 -
241 + $in_section = ! empty( $this->current_section ) && ! in_array( $new_type, $this->fields_with_end() ) && $new_type !== 'break';
306 242 if ( $in_section ) {
307 243 $new_field['field_options']['in_section'] = $this->current_section['id'];
308 244 }
309 245
@@ -308,9 +244,9 @@
308 244 }
309 245
310 246 $form['fields'][] = $new_field;
311 247
312 - if ( in_array( $new_type, $this->fields_with_end(), true ) ) {
248 + if ( in_array( $new_type, $this->fields_with_end() ) ) {
313 249 $this->current_section = $field;
314 250 } elseif ( $new_type === 'break' || $new_type === 'end_divider' ) {
315 251 $this->current_section = array();
316 252 }
@@ -315,31 +251,23 @@
315 251 $this->current_section = array();
316 252 }
317 253
318 254 // This may occasionally skip one level/order e.g. after adding a
319 - // List field, as field_order would already be prepared to be used.
255 + // list field, as field_order would already be prepared to be used.
320 256 ++$field_order;
321 257
322 - if ( empty( $new_field['fields'] ) || ! is_array( $new_field['fields'] ) ) {
323 - continue;
258 + if ( ! empty( $new_field['fields'] ) && is_array( $new_field['fields'] ) ) {
259 + // we have (inner) fields to merge
260 +
261 + $form['fields'] = array_merge( $form['fields'], $new_field['fields'] );
262 + // set the new field_order as it would have changed
263 + $field_order = $new_field['current_order'];
324 264 }
325 -
326 - // we have (inner) fields to merge
327 -
328 - $form['fields'] = array_merge( $form['fields'], $new_field['fields'] );
329 - // Set the new field_order as it would have changed
330 - $field_order = $new_field['current_order'];
331 265 }//end foreach
332 266 }
333 267
334 - /**
335 - * @param array|object $field
336 - * @param array $new_field
337 - *
338 - * @return void
339 - */
340 268 protected function prepare_field( $field, &$new_field ) {
341 - // Customize this function
269 + // customize this function
342 270 }
343 271
344 272 /**
345 273 * Add any field types that will need an end section field.
@@ -344,10 +272,8 @@
344 272 /**
345 273 * Add any field types that will need an end section field.
346 274 *
347 275 * @since 4.04.03
348 - *
349 - * @return array
350 276 */
351 277 protected function fields_with_end() {
352 278 return array( 'divider' );
353 279 }
@@ -353,43 +279,37 @@
353 279 }
354 280
355 281 /**
356 282 * @since 4.04.03
357 - *
358 - * @param array $fields
359 - *
360 - * @return void
361 283 */
362 284 protected function maybe_add_end_fields( &$fields ) {
363 285 $with_end = $this->fields_with_end();
364 -
365 - if ( ! $with_end ) {
286 + if ( empty( $with_end ) ) {
366 287 return;
367 288 }
368 289
369 - $open = array();
290 + $open = array();
291 +
370 292 $order = 0;
371 -
372 293 foreach ( $fields as $field ) {
373 294 ++$order;
374 295 $type = $this->get_field_type( $field );
375 296 $new_type = $this->convert_field_type( $type, $field );
376 -
377 - if ( ! in_array( $new_type, $with_end, true ) && $new_type !== 'break' ) {
297 + if ( ! in_array( $new_type, $with_end ) && $new_type !== 'break' ) {
378 298 continue;
379 299 }
380 300
381 - if ( $open ) {
301 + if ( ! empty( $open ) ) {
382 302 $this->insert_end_section( $fields, $order );
383 303 $open = array();
384 304 }
385 305
386 - if ( in_array( $new_type, $with_end, true ) ) {
306 + if ( in_array( $new_type, $with_end ) ) {
387 307 $open = $field;
388 308 }
389 309 }
390 310
391 - if ( $open ) {
311 + if ( ! empty( $open ) ) {
392 312 $this->insert_end_section( $fields, $order );
393 313 }
394 314 }
395 315
@@ -394,13 +314,8 @@
394 314 }
395 315
396 316 /**
397 317 * @since 4.04.03
398 - *
399 - * @param array $fields
400 - * @param int $order
401 - *
402 - * @return void
403 318 */
404 319 protected function insert_end_section( &$fields, &$order ) {
405 320 $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' );
406 321 $sub['name'] = __( 'Section Buttons', 'formidable' );
@@ -413,15 +328,8 @@
413 328 * Replace the original combo field with a group.
414 329 * This switches the name field to individual fields.
415 330 *
416 331 * @since 4.04.03
417 - *
418 - * @param array $subs
419 - * @param int $start
420 - * @param int $remove
421 - * @param array $fields
422 - *
423 - * @return void
424 332 */
425 333 protected function insert_fields_in_array( $subs, $start, $remove, &$fields ) {
426 334 array_splice( $fields, $start, $remove, $subs );
427 335 }
@@ -433,13 +341,11 @@
433 341 * This also eases the recursive use of the method,
434 342 * particularly the overrides in child classes, as
435 343 * there will be no need to rebuild the converter
436 344 * array at usage locations.
437 - *
438 - * @return string
439 345 */
440 346 protected function convert_field_type( $type, $field = array(), $use = '' ) {
441 - if ( ! $field ) {
347 + if ( empty( $field ) ) {
442 348 // For reverse compatibility.
443 349 return $type;
444 350 }
445 351
@@ -446,20 +352,19 @@
446 352 return $use ? $use : $field['type'];
447 353 }
448 354
449 355 /**
450 - * Add the new form to the database and return AJAX data.
356 + * Add the new form to the database and return AJAX data.å
451 357 *
452 358 * @param array $form Form to import.
453 359 * @param array $upgrade_omit No field alternative.
454 - *
455 - * @return array
456 360 */
457 361 protected function add_form( $form, $upgrade_omit = array() ) {
362 +
458 363 // Create empty form so we have an ID to work with.
459 364 $form_id = $this->create_form( $form );
460 365
461 - if ( ! $form_id ) {
366 + if ( empty( $form_id ) ) {
462 367 return $this->form_creation_error_response( $form );
463 368 }
464 369
465 370 $this->create_fields( $form_id, $form );
@@ -494,12 +399,8 @@
494 399 }
495 400
496 401 /**
497 402 * @since 4.04.03
498 - *
499 - * @param array $form
500 - *
501 - * @return array
502 403 */
503 404 protected function form_creation_error_response( $form ) {
504 405 return array(
505 406 'error' => true,
@@ -512,10 +413,8 @@
512 413 * @since 4.04.03
513 414 *
514 415 * @param int $form_id
515 416 * @param array $form
516 - *
517 - * @return void
518 417 */
519 418 protected function create_fields( $form_id, &$form ) {
520 419 foreach ( $form['fields'] as $key => $new_field ) {
521 420 $new_field['form_id'] = $form_id;
@@ -525,12 +424,9 @@
525 424
526 425 /**
527 426 * @since 4.04.03
528 427 *
529 - * @param array $form
530 - * @param int|string $form_id
531 - *
532 - * @return void
428 + * @param array $form
533 429 */
534 430 protected function create_emails( $form, $form_id ) {
535 431 foreach ( $form['actions'] as $action ) {
536 432 $this->save_action( $action, $form, $form_id );
@@ -542,10 +438,8 @@
542 438 *
543 439 * @param array $action
544 440 * @param array $form
545 441 * @param int $form_id
546 - *
547 - * @return int|WP_Error
548 442 */
549 443 protected function save_action( $action, $form, $form_id ) {
550 444 /**
551 445 * @var FrmFormAction
@@ -552,9 +446,8 @@
552 446 */
553 447 $action_control = FrmFormActionsController::get_form_actions( $action['type'] );
554 448 unset( $action['type'] );
555 449 $new_action = $action_control->prepare_new( $form_id );
556 -
557 450 foreach ( $action as $key => $value ) {
558 451 if ( $key === 'post_title' ) {
559 452 $new_action->post_title = $value;
560 453 } elseif ( $key === 'ID' ) {
@@ -577,12 +470,11 @@
577 470 * been imported.
578 471 *
579 472 * @param int $source_id Imported plugin form ID.
580 473 * @param int $new_form_id Formidable form ID.
581 - *
582 - * @return void
583 474 */
584 475 protected function track_import( $source_id, $new_form_id ) {
476 +
585 477 $imported = $this->get_tracked_import();
586 478
587 479 $imported[ $this->slug ][ $new_form_id ] = $source_id;
588 480
@@ -603,20 +495,16 @@
603 495 */
604 496 private function is_imported( $source_id ) {
605 497 $imported = $this->get_tracked_import();
606 498 $new_form_id = 0;
607 -
608 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
609 499 if ( ! isset( $imported[ $this->slug ] ) || ! in_array( $source_id, $imported[ $this->slug ] ) ) {
610 500 return $new_form_id;
611 501 }
612 502
613 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
614 503 $new_form_id = array_search( $source_id, array_reverse( $imported[ $this->slug ], true ) );
615 -
616 - if ( $new_form_id && ! FrmForm::get_key_by_id( $new_form_id ) ) {
504 + if ( ! empty( $new_form_id ) && empty( FrmForm::get_key_by_id( $new_form_id ) ) ) {
617 505 // Allow reimport if the form was deleted.
618 - return 0;
506 + $new_form_id = 0;
619 507 }
620 508
621 509 return $new_form_id;
622 510 }
@@ -629,15 +517,12 @@
629 517 protected function unsupported_field_types() {
630 518 return array();
631 519 }
632 520
633 - /**
634 - * @param string $type
635 - *
636 - * @return bool
637 - */
638 521 private function is_unsupported_field( $type ) {
639 - return in_array( $type, $this->unsupported_field_types(), true );
522 + $fields = $this->unsupported_field_types();
523 +
524 + return in_array( $type, $fields, true );
640 525 }
641 526
642 527 /**
643 528 * Strict PRO fields with no Lite alternatives.
@@ -647,19 +532,12 @@
647 532 protected function skip_pro_fields() {
648 533 return array();
649 534 }
650 535
651 - /**
652 - * @param string $type
653 - *
654 - * @return bool
655 - */
656 536 protected function should_skip_field( $type ) {
657 - if ( FrmAppHelper::pro_is_installed() ) {
658 - return false;
659 - }
537 + $skip_pro_fields = $this->skip_pro_fields();
660 538
661 - return in_array( $type, $this->skip_pro_fields(), true );
539 + return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) );
662 540 }
663 541
664 542 /**
665 543 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
@@ -681,13 +559,8 @@
681 559 public function get_forms() {
682 560 return array();
683 561 }
684 562
685 - /**
686 - * @param int|string $id
687 - *
688 - * @return array
689 - */
690 563 public function get_form( $id ) {
691 564 return array();
692 565 }
693 566
@@ -723,11 +596,10 @@
723 596 *
724 597 * @return string
725 598 */
726 599 protected function get_field_label( $field ) {
727 - $label = $field['label'] ?? '';
728 -
729 - if ( $label ) {
600 + $label = isset( $field['label'] ) ? $field['label'] : '';
601 + if ( ! empty( $label ) ) {
730 602 return $label;
731 603 }
732 604
733 605 $type = $this->get_field_type( $field );