PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.0
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 +69 -221 6.346.0 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();
17 + protected $fields_map = array();
18 + protected $current_source_form = null;
19 + protected $current_section = array();
42 20
43 21 /**
44 - * @var mixed
45 - */
46 - protected $current_source_form;
47 -
48 - /**
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() ) {
@@ -58,15 +26,14 @@
58 26 return;
59 27 }
60 28
61 29 if ( ! function_exists( 'is_plugin_active' ) ) {
62 - require_once ABSPATH . '/wp-admin/includes/plugin.php';
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,28 +43,20 @@
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 - // phpcs:disable Generic.WhiteSpace.ScopeIndent
94 53 ?>
95 54 <div class="wrap">
96 55 <h2 class="frm-h2"><?php echo esc_html( $this->name ); ?> Importer</h2>
97 56 <p class="howto">Import forms and settings automatically from <?php echo esc_html( $this->name ); ?>.</p>
98 - <div id="welcome-panel">
99 - <div style="margin-bottom:10px;">
57 + <div class="welcome-panel" id="welcome-panel">
58 + <div class="welcome-panel-content" style="text-align:center;margin-bottom:10px;">
100 59 <p class="about-description">
101 60 Select the forms to import.
102 61 </p>
103 62 <form class="frm_form_importer" method="post"
@@ -104,15 +63,15 @@
104 63 action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
105 64 <?php wp_nonce_field( 'nonce', 'frm_ajax' ); ?>
106 65 <input type="hidden" name="slug" value="<?php echo esc_attr( $this->slug ); ?>" />
107 66 <input type="hidden" name="action" value="frm_import_<?php echo esc_attr( $this->slug ); ?>" />
108 - <div style="max-width:400px;text-align:left;">
67 + <div style="margin:10px auto;max-width:400px;text-align:left;">
109 68 <?php
110 - if ( ! $forms ) {
69 + if ( empty( $this->get_forms() ) ) {
111 70 esc_html_e( 'No Forms Found.', 'formidable' );
112 71 }
113 72 ?>
114 - <?php foreach ( $forms as $form_id => $name ) { ?>
73 + <?php foreach ( $this->get_forms() as $form_id => $name ) { ?>
115 74 <p>
116 75 <label>
117 76 <input type="checkbox" name="form_id[]"
118 77 value="<?php echo esc_attr( $form_id ); ?>" checked="checked" />
@@ -126,23 +85,9 @@
126 85 </label>
127 86 </p>
128 87 <?php } ?>
129 88 </div>
130 - <?php
131 - $button_atts = array(
132 - 'type' => 'submit',
133 - 'class' => 'button button-primary frm-button-primary',
134 - );
135 -
136 - if ( ! $forms ) {
137 - $button_atts['disabled'] = 'disabled';
138 - }
139 - ?>
140 - <p class="submit">
141 - <button <?php FrmAppHelper::array_to_html_params( $button_atts, true ); ?>>
142 - <?php esc_html_e( 'Start Import', 'formidable' ); ?>
143 - </button>
144 - </p>
89 + <button type="submit" class="button button-primary button-hero">Start Import</button>
145 90 </form>
146 91 <div id="frm-importer-process" class="frm-importer-process frm_hidden">
147 92
148 93 <p class="process-count">
@@ -151,15 +96,10 @@
151 96 from <?php echo esc_html( $this->name ); ?>.
152 97 </p>
153 98
154 99 <p class="process-completed" class="frm_hidden">
155 - <?php
156 - printf(
157 - // translators: %s is the number of forms that were imported.
158 - esc_html__( 'The import process has finished! We have successfully imported %s forms. You can review the results below.', 'formidable' ),
159 - '<span class="forms-completed"></span>'
160 - );
161 - ?>
100 + The import process has finished! We have successfully imported
101 + <span class="forms-completed"></span> forms. You can review the results below.
162 102 </p>
163 103
164 104 <div class="status"></div>
165 105
@@ -167,17 +107,15 @@
167 107 </div>
168 108 </div>
169 109 </div>
170 110 <?php
171 - // phpcs:enable Generic.WhiteSpace.ScopeIndent
172 111 }
173 112
174 113 /**
175 114 * Import all forms using ajax
176 - *
177 - * @return void
178 115 */
179 116 public function import_forms() {
117 +
180 118 check_ajax_referer( 'frm_ajax', 'nonce' );
181 119 FrmAppHelper::permission_check( 'frm_edit_forms' );
182 120
183 121 $forms = FrmAppHelper::get_simple_request(
@@ -189,10 +127,9 @@
189 127 );
190 128
191 129 if ( is_array( $forms ) ) {
192 130 $imported = array();
193 -
194 - foreach ( $forms as $form_id ) {
131 + foreach ( (array) $forms as $form_id ) {
195 132 $imported[] = $this->import_form( $form_id );
196 133 }
197 134 } else {
198 135 $imported = $this->import_form( $forms );
@@ -202,14 +139,11 @@
202 139 }
203 140
204 141 /**
205 142 * Import a single form
206 - *
207 - * @param int $source_id Source form ID.
208 - *
209 - * @return array
210 143 */
211 144 protected function import_form( $source_id ) {
145 +
212 146 $source_form = $this->get_form( $source_id );
213 147 $source_form_name = $this->get_form_name( $source_form );
214 148 $source_fields = $this->get_form_fields( $source_form );
215 149 $this->maybe_add_end_fields( $source_fields );
@@ -216,9 +150,9 @@
216 150
217 151 $this->current_source_form = $source_form;
218 152
219 153 // If form does not contain fields, bail.
220 - if ( ! $source_fields ) {
154 + if ( empty( $source_fields ) ) {
221 155 wp_send_json_success(
222 156 array(
223 157 'error' => true,
224 158 'name' => esc_html( $source_form_name ),
@@ -240,14 +174,8 @@
240 174
241 175 return $response;
242 176 }
243 177
244 - /**
245 - * @param int|string $source_id
246 - * @param string $source_form_name
247 - *
248 - * @return array
249 - */
250 178 protected function prepare_new_form( $source_id, $source_form_name ) {
251 179 return array(
252 180 'import_form_id' => $source_id,
253 181 'fields' => array(),
@@ -257,33 +185,22 @@
257 185 'actions' => array(),
258 186 );
259 187 }
260 188
261 - /**
262 - * @param array|object $form
263 - * @param array $new_form
264 - *
265 - * @return void
266 - */
267 189 protected function prepare_form( $form, &$new_form ) {
268 - // Customize this function
190 + // customize this function
269 191 }
270 192
271 - /**
272 - * @param array $fields
273 - * @param array $form
274 - *
275 - * @return void
276 - */
277 193 protected function prepare_fields( $fields, &$form ) {
278 194 $field_order = 1;
279 195
280 196 foreach ( $fields as $field ) {
281 197 $field = (array) $field;
198 +
282 199 $label = $this->get_field_label( $field );
283 200 $type = $this->get_field_type( $field );
284 201
285 - // Check if field is unsupported. If unsupported make note and continue
202 + // check if field is unsupported. If unsupported make note and continue
286 203 if ( $this->is_unsupported_field( $type ) ) {
287 204 $this->response['unsupported'][] = $label;
288 205 continue;
289 206 }
@@ -292,9 +209,10 @@
292 209 $this->response['upgrade_omit'][] = $label;
293 210 continue;
294 211 }
295 212
296 - $new_type = $this->convert_field_type( $type, $field );
213 + $new_type = $this->convert_field_type( $type, $field );
214 +
297 215 $new_field = FrmFieldsHelper::setup_new_vars( $new_type );
298 216 $new_field['name'] = $label;
299 217 $new_field['field_order'] = $field_order;
300 218 $new_field['original'] = $type;
@@ -300,10 +218,9 @@
300 218 $new_field['original'] = $type;
301 219
302 220 $this->prepare_field( $field, $new_field );
303 221
304 - $in_section = $this->current_section && ! in_array( $new_type, $this->fields_with_end(), true ) && $new_type !== 'break';
305 -
222 + $in_section = ! empty( $this->current_section ) && ! in_array( $new_type, $this->fields_with_end() ) && $new_type !== 'break';
306 223 if ( $in_section ) {
307 224 $new_field['field_options']['in_section'] = $this->current_section['id'];
308 225 }
309 226
@@ -308,38 +225,30 @@
308 225 }
309 226
310 227 $form['fields'][] = $new_field;
311 228
312 - if ( in_array( $new_type, $this->fields_with_end(), true ) ) {
229 + if ( in_array( $new_type, $this->fields_with_end() ) ) {
313 230 $this->current_section = $field;
314 231 } elseif ( $new_type === 'break' || $new_type === 'end_divider' ) {
315 232 $this->current_section = array();
316 233 }
317 234
318 - // 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.
320 - ++$field_order;
235 + // This may occassionally skip one level/order e.g. after adding a
236 + // list field, as field_order would already be prepared to be used.
237 + $field_order ++;
321 238
322 - if ( empty( $new_field['fields'] ) || ! is_array( $new_field['fields'] ) ) {
323 - continue;
239 + if ( isset( $new_field['fields'] ) && is_array( $new_field['fields'] ) && ! empty( $new_field['fields'] ) ) {
240 + // we have (inner) fields to merge
241 +
242 + $form['fields'] = array_merge( $form['fields'], $new_field['fields'] );
243 + // set the new field_order as it would have changed
244 + $field_order = $new_field['current_order'];
324 245 }
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 - }//end foreach
246 + }
332 247 }
333 248
334 - /**
335 - * @param array|object $field
336 - * @param array $new_field
337 - *
338 - * @return void
339 - */
340 249 protected function prepare_field( $field, &$new_field ) {
341 - // Customize this function
250 + // customize this function
342 251 }
343 252
344 253 /**
345 254 * Add any field types that will need an end section field.
@@ -344,10 +253,8 @@
344 253 /**
345 254 * Add any field types that will need an end section field.
346 255 *
347 256 * @since 4.04.03
348 - *
349 - * @return array
350 257 */
351 258 protected function fields_with_end() {
352 259 return array( 'divider' );
353 260 }
@@ -353,43 +260,37 @@
353 260 }
354 261
355 262 /**
356 263 * @since 4.04.03
357 - *
358 - * @param array $fields
359 - *
360 - * @return void
361 264 */
362 265 protected function maybe_add_end_fields( &$fields ) {
363 266 $with_end = $this->fields_with_end();
364 -
365 - if ( ! $with_end ) {
267 + if ( empty( $with_end ) ) {
366 268 return;
367 269 }
368 270
369 - $open = array();
271 + $open = array();
272 +
370 273 $order = 0;
371 -
372 274 foreach ( $fields as $field ) {
373 - ++$order;
275 + $order ++;
374 276 $type = $this->get_field_type( $field );
375 277 $new_type = $this->convert_field_type( $type, $field );
376 -
377 - if ( ! in_array( $new_type, $with_end, true ) && $new_type !== 'break' ) {
278 + if ( ! in_array( $new_type, $with_end ) && $new_type !== 'break' ) {
378 279 continue;
379 280 }
380 281
381 - if ( $open ) {
282 + if ( ! empty( $open ) ) {
382 283 $this->insert_end_section( $fields, $order );
383 284 $open = array();
384 285 }
385 286
386 - if ( in_array( $new_type, $with_end, true ) ) {
287 + if ( in_array( $new_type, $with_end ) ) {
387 288 $open = $field;
388 289 }
389 290 }
390 291
391 - if ( $open ) {
292 + if ( ! empty( $open ) ) {
392 293 $this->insert_end_section( $fields, $order );
393 294 }
394 295 }
395 296
@@ -394,13 +295,8 @@
394 295 }
395 296
396 297 /**
397 298 * @since 4.04.03
398 - *
399 - * @param array $fields
400 - * @param int $order
401 - *
402 - * @return void
403 299 */
404 300 protected function insert_end_section( &$fields, &$order ) {
405 301 $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' );
406 302 $sub['name'] = __( 'Section Buttons', 'formidable' );
@@ -405,9 +301,9 @@
405 301 $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' );
406 302 $sub['name'] = __( 'Section Buttons', 'formidable' );
407 303 $subs = array( $sub );
408 304 $this->insert_fields_in_array( $subs, $order, 0, $fields );
409 - ++$order;
305 + $order ++;
410 306 }
411 307
412 308 /**
413 309 * Replace the original combo field with a group.
@@ -413,15 +309,8 @@
413 309 * Replace the original combo field with a group.
414 310 * This switches the name field to individual fields.
415 311 *
416 312 * @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 313 */
425 314 protected function insert_fields_in_array( $subs, $start, $remove, &$fields ) {
426 315 array_splice( $fields, $start, $remove, $subs );
427 316 }
@@ -433,14 +322,12 @@
433 322 * This also eases the recursive use of the method,
434 323 * particularly the overrides in child classes, as
435 324 * there will be no need to rebuild the converter
436 325 * array at usage locations.
437 - *
438 - * @return string
439 326 */
440 327 protected function convert_field_type( $type, $field = array(), $use = '' ) {
441 - if ( ! $field ) {
442 - // For reverse compatibility.
328 + if ( empty( $field ) ) {
329 + // For reverse compatability.
443 330 return $type;
444 331 }
445 332
446 333 return $use ? $use : $field['type'];
@@ -446,20 +333,19 @@
446 333 return $use ? $use : $field['type'];
447 334 }
448 335
449 336 /**
450 - * Add the new form to the database and return AJAX data.
337 + * Add the new form to the database and return AJAX data.å
451 338 *
452 339 * @param array $form Form to import.
453 - * @param array $upgrade_omit No field alternative.
454 - *
455 - * @return array
340 + * @param array $upgrade_omit No field alternative
456 341 */
457 342 protected function add_form( $form, $upgrade_omit = array() ) {
343 +
458 344 // Create empty form so we have an ID to work with.
459 345 $form_id = $this->create_form( $form );
460 346
461 - if ( ! $form_id ) {
347 + if ( empty( $form_id ) ) {
462 348 return $this->form_creation_error_response( $form );
463 349 }
464 350
465 351 $this->create_fields( $form_id, $form );
@@ -483,9 +369,9 @@
483 369 * @param array $form parameters for the new form to be created. Only
484 370 * the name key is a must. The keys are the column
485 371 * names of the forms table in the DB.
486 372 *
487 - * @return bool|int The ID of the newly created form or false on failure.
373 + * @return int The ID of the newly created form.
488 374 */
489 375 protected function create_form( $form ) {
490 376 $form['form_key'] = $form['name'];
491 377 $form['status'] = 'published';
@@ -494,12 +380,8 @@
494 380 }
495 381
496 382 /**
497 383 * @since 4.04.03
498 - *
499 - * @param array $form
500 - *
501 - * @return array
502 384 */
503 385 protected function form_creation_error_response( $form ) {
504 386 return array(
505 387 'error' => true,
@@ -510,12 +392,10 @@
510 392
511 393 /**
512 394 * @since 4.04.03
513 395 *
514 - * @param int $form_id
396 + * @param int $form_id
515 397 * @param array $form
516 - *
517 - * @return void
518 398 */
519 399 protected function create_fields( $form_id, &$form ) {
520 400 foreach ( $form['fields'] as $key => $new_field ) {
521 401 $new_field['form_id'] = $form_id;
@@ -525,12 +405,9 @@
525 405
526 406 /**
527 407 * @since 4.04.03
528 408 *
529 - * @param array $form
530 - * @param int|string $form_id
531 - *
532 - * @return void
409 + * @param array $form
533 410 */
534 411 protected function create_emails( $form, $form_id ) {
535 412 foreach ( $form['actions'] as $action ) {
536 413 $this->save_action( $action, $form, $form_id );
@@ -539,22 +416,14 @@
539 416
540 417 /**
541 418 * @since 4.04.03
542 419 *
543 - * @param array $action
544 420 * @param array $form
545 - * @param int $form_id
546 - *
547 - * @return int|WP_Error
548 421 */
549 422 protected function save_action( $action, $form, $form_id ) {
550 - /**
551 - * @var FrmFormAction
552 - */
553 423 $action_control = FrmFormActionsController::get_form_actions( $action['type'] );
554 424 unset( $action['type'] );
555 425 $new_action = $action_control->prepare_new( $form_id );
556 -
557 426 foreach ( $action as $key => $value ) {
558 427 if ( $key === 'post_title' ) {
559 428 $new_action->post_title = $value;
560 429 } elseif ( $key === 'ID' ) {
@@ -575,14 +444,13 @@
575 444 * After a form has been successfully imported we track it, so that in the
576 445 * future we can alert users if they try to import a form that has already
577 446 * been imported.
578 447 *
579 - * @param int $source_id Imported plugin form ID.
580 - * @param int $new_form_id Formidable form ID.
581 - *
582 - * @return void
448 + * @param int $source_id Imported plugin form ID
449 + * @param int $new_form_id Formidable form ID
583 450 */
584 451 protected function track_import( $source_id, $new_form_id ) {
452 +
585 453 $imported = $this->get_tracked_import();
586 454
587 455 $imported[ $this->slug ][ $new_form_id ] = $source_id;
588 456
@@ -596,9 +464,9 @@
596 464 return get_option( $this->tracking, array() );
597 465 }
598 466
599 467 /**
600 - * @param int $source_id Imported plugin form ID.
468 + * @param int $source_id Imported plugin form ID
601 469 *
602 470 * @return int the ID of the created form or 0
603 471 */
604 472 private function is_imported( $source_id ) {
@@ -603,20 +471,16 @@
603 471 */
604 472 private function is_imported( $source_id ) {
605 473 $imported = $this->get_tracked_import();
606 474 $new_form_id = 0;
607 -
608 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
609 475 if ( ! isset( $imported[ $this->slug ] ) || ! in_array( $source_id, $imported[ $this->slug ] ) ) {
610 476 return $new_form_id;
611 477 }
612 478
613 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
614 479 $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 ) ) {
480 + if ( ! empty( $new_form_id ) && empty( FrmForm::get_key_by_id( $new_form_id ) ) ) {
617 481 // Allow reimport if the form was deleted.
618 - return 0;
482 + $new_form_id = 0;
619 483 }
620 484
621 485 return $new_form_id;
622 486 }
@@ -629,15 +493,12 @@
629 493 protected function unsupported_field_types() {
630 494 return array();
631 495 }
632 496
633 - /**
634 - * @param string $type
635 - *
636 - * @return bool
637 - */
638 497 private function is_unsupported_field( $type ) {
639 - return in_array( $type, $this->unsupported_field_types(), true );
498 + $fields = $this->unsupported_field_types();
499 +
500 + return in_array( $type, $fields, true );
640 501 }
641 502
642 503 /**
643 504 * Strict PRO fields with no Lite alternatives.
@@ -647,19 +508,12 @@
647 508 protected function skip_pro_fields() {
648 509 return array();
649 510 }
650 511
651 - /**
652 - * @param string $type
653 - *
654 - * @return bool
655 - */
656 512 protected function should_skip_field( $type ) {
657 - if ( FrmAppHelper::pro_is_installed() ) {
658 - return false;
659 - }
513 + $skip_pro_fields = $this->skip_pro_fields();
660 514
661 - return in_array( $type, $this->skip_pro_fields(), true );
515 + return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) );
662 516 }
663 517
664 518 /**
665 519 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
@@ -664,9 +518,9 @@
664 518 /**
665 519 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
666 520 *
667 521 * @param string $string String to process the smart tag in.
668 - * @param array $fields List of fields for the form.
522 + * @param array $fields List of fields for the form.
669 523 *
670 524 * @return string
671 525 */
672 526 protected function replace_smart_tags( $string, $fields ) {
@@ -681,19 +535,14 @@
681 535 public function get_forms() {
682 536 return array();
683 537 }
684 538
685 - /**
686 - * @param int|string $id
687 - *
688 - * @return array
689 - */
690 539 public function get_form( $id ) {
691 540 return array();
692 541 }
693 542
694 543 /**
695 - * @param array|object $source_form
544 + * @param object|array $source_form
696 545 *
697 546 * @return string
698 547 */
699 548 protected function get_form_name( $source_form ) {
@@ -700,9 +549,9 @@
700 549 return __( 'Default Form', 'formidable' );
701 550 }
702 551
703 552 /**
704 - * @param array|object $source_form
553 + * @param object|array $source_form
705 554 *
706 555 * @return array
707 556 */
708 557 protected function get_form_fields( $source_form ) {
@@ -723,11 +572,10 @@
723 572 *
724 573 * @return string
725 574 */
726 575 protected function get_field_label( $field ) {
727 - $label = $field['label'] ?? '';
728 -
729 - if ( $label ) {
576 + $label = isset( $field['label'] ) ? $field['label'] : '';
577 + if ( ! empty( $label ) ) {
730 578 return $label;
731 579 }
732 580
733 581 $type = $this->get_field_type( $field );