PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.0
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.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 +59 -207 6.275.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,13 +26,12 @@
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 35 // if source plugin is not installed, do nothing
69 36 return;
70 37 }
@@ -76,27 +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 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>
97 - <div id="welcome-panel">
98 - <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;">
99 59 <p class="about-description">
100 60 Select the forms to import.
101 61 </p>
102 62 <form class="frm_form_importer" method="post"
@@ -103,15 +63,15 @@
103 63 action="<?php echo esc_url( admin_url( 'admin-ajax.php' ) ); ?>">
104 64 <?php wp_nonce_field( 'nonce', 'frm_ajax' ); ?>
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 - <div style="max-width:400px;text-align:left;">
67 + <div style="margin:10px auto;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 + <button type="submit" class="button button-primary button-hero">Start Import</button>
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,10 +127,9 @@
187 127 );
188 128
189 129 if ( is_array( $forms ) ) {
190 130 $imported = array();
191 -
192 - foreach ( $forms as $form_id ) {
131 + foreach ( (array) $forms as $form_id ) {
193 132 $imported[] = $this->import_form( $form_id );
194 133 }
195 134 } else {
196 135 $imported = $this->import_form( $forms );
@@ -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,34 +225,28 @@
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 }
315 234
316 - // This may occasionally skip one level/order e.g. after adding a
235 + // This may occassionally skip one level/order e.g. after adding a
317 236 // list field, as field_order would already be prepared to be used.
318 - ++$field_order;
237 + $field_order ++;
319 238
320 - if ( ! empty( $new_field['fields'] ) && is_array( $new_field['fields'] ) ) {
239 + if ( isset( $new_field['fields'] ) && is_array( $new_field['fields'] ) && ! empty( $new_field['fields'] ) ) {
321 240 // we have (inner) fields to merge
322 241
323 242 $form['fields'] = array_merge( $form['fields'], $new_field['fields'] );
324 243 // set the new field_order as it would have changed
325 - $field_order = $new_field['current_order'];
244 + $field_order = $new_field['current_order'];
326 245 }
327 - }//end foreach
246 + }
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 - ++$order;
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' );
@@ -401,9 +301,9 @@
401 301 $sub = FrmFieldsHelper::setup_new_vars( 'end_divider' );
402 302 $sub['name'] = __( 'Section Buttons', 'formidable' );
403 303 $subs = array( $sub );
404 304 $this->insert_fields_in_array( $subs, $order, 0, $fields );
405 - ++$order;
305 + $order ++;
406 306 }
407 307
408 308 /**
409 309 * Replace the original combo field with a group.
@@ -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,14 +322,12 @@
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 ) {
438 - // For reverse compatibility.
328 + if ( empty( $field ) ) {
329 + // For reverse compatability.
439 330 return $type;
440 331 }
441 332
442 333 return $use ? $use : $field['type'];
@@ -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 - * @param array $upgrade_omit No field alternative.
450 - *
451 - * @return array
340 + * @param array $upgrade_omit No field alternative
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 );
@@ -479,9 +369,9 @@
479 369 * @param array $form parameters for the new form to be created. Only
480 370 * the name key is a must. The keys are the column
481 371 * names of the forms table in the DB.
482 372 *
483 - * @return bool|int The ID of the newly created form or false on failure.
373 + * @return int The ID of the newly created form.
484 374 */
485 375 protected function create_form( $form ) {
486 376 $form['form_key'] = $form['name'];
487 377 $form['status'] = 'published';
@@ -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,
@@ -506,12 +392,10 @@
506 392
507 393 /**
508 394 * @since 4.04.03
509 395 *
510 - * @param int $form_id
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 );
@@ -535,22 +416,14 @@
535 416
536 417 /**
537 418 * @since 4.04.03
538 419 *
539 - * @param array $action
540 420 * @param array $form
541 - * @param int $form_id
542 - *
543 - * @return int|WP_Error
544 421 */
545 422 protected function save_action( $action, $form, $form_id ) {
546 - /**
547 - * @var FrmFormAction
548 - */
549 423 $action_control = FrmFormActionsController::get_form_actions( $action['type'] );
550 424 unset( $action['type'] );
551 425 $new_action = $action_control->prepare_new( $form_id );
552 -
553 426 foreach ( $action as $key => $value ) {
554 427 if ( $key === 'post_title' ) {
555 428 $new_action->post_title = $value;
556 429 } elseif ( $key === 'ID' ) {
@@ -571,14 +444,13 @@
571 444 * After a form has been successfully imported we track it, so that in the
572 445 * future we can alert users if they try to import a form that has already
573 446 * been imported.
574 447 *
575 - * @param int $source_id Imported plugin form ID.
576 - * @param int $new_form_id Formidable form ID.
577 - *
578 - * @return void
448 + * @param int $source_id Imported plugin form ID
449 + * @param int $new_form_id Formidable form ID
579 450 */
580 451 protected function track_import( $source_id, $new_form_id ) {
452 +
581 453 $imported = $this->get_tracked_import();
582 454
583 455 $imported[ $this->slug ][ $new_form_id ] = $source_id;
584 456
@@ -592,9 +464,9 @@
592 464 return get_option( $this->tracking, array() );
593 465 }
594 466
595 467 /**
596 - * @param int $source_id Imported plugin form ID.
468 + * @param int $source_id Imported plugin form ID
597 469 *
598 470 * @return int the ID of the created form or 0
599 471 */
600 472 private function is_imported( $source_id ) {
@@ -599,18 +471,14 @@
599 471 */
600 472 private function is_imported( $source_id ) {
601 473 $imported = $this->get_tracked_import();
602 474 $new_form_id = 0;
603 -
604 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
605 475 if ( ! isset( $imported[ $this->slug ] ) || ! in_array( $source_id, $imported[ $this->slug ] ) ) {
606 476 return $new_form_id;
607 477 }
608 478
609 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
610 479 $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 ) ) {
480 + if ( ! empty( $new_form_id ) && empty( FrmForm::get_key_by_id( $new_form_id ) ) ) {
613 481 // Allow reimport if the form was deleted.
614 482 $new_form_id = 0;
615 483 }
616 484
@@ -625,15 +493,12 @@
625 493 protected function unsupported_field_types() {
626 494 return array();
627 495 }
628 496
629 - /**
630 - * @param string $type
631 - *
632 - * @return bool
633 - */
634 497 private function is_unsupported_field( $type ) {
635 - return in_array( $type, $this->unsupported_field_types(), true );
498 + $fields = $this->unsupported_field_types();
499 +
500 + return in_array( $type, $fields, true );
636 501 }
637 502
638 503 /**
639 504 * Strict PRO fields with no Lite alternatives.
@@ -643,19 +508,12 @@
643 508 protected function skip_pro_fields() {
644 509 return array();
645 510 }
646 511
647 - /**
648 - * @param string $type
649 - *
650 - * @return bool
651 - */
652 512 protected function should_skip_field( $type ) {
653 - if ( FrmAppHelper::pro_is_installed() ) {
654 - return false;
655 - }
513 + $skip_pro_fields = $this->skip_pro_fields();
656 514
657 - return in_array( $type, $this->skip_pro_fields(), true );
515 + return ( ! FrmAppHelper::pro_is_installed() && in_array( $type, $skip_pro_fields, true ) );
658 516 }
659 517
660 518 /**
661 519 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
@@ -660,9 +518,9 @@
660 518 /**
661 519 * Replace 3rd-party form provider tags/shortcodes with our own Tags.
662 520 *
663 521 * @param string $string String to process the smart tag in.
664 - * @param array $fields List of fields for the form.
522 + * @param array $fields List of fields for the form.
665 523 *
666 524 * @return string
667 525 */
668 526 protected function replace_smart_tags( $string, $fields ) {
@@ -677,19 +535,14 @@
677 535 public function get_forms() {
678 536 return array();
679 537 }
680 538
681 - /**
682 - * @param int|string $id
683 - *
684 - * @return array
685 - */
686 539 public function get_form( $id ) {
687 540 return array();
688 541 }
689 542
690 543 /**
691 - * @param array|object $source_form
544 + * @param object|array $source_form
692 545 *
693 546 * @return string
694 547 */
695 548 protected function get_form_name( $source_form ) {
@@ -696,9 +549,9 @@
696 549 return __( 'Default Form', 'formidable' );
697 550 }
698 551
699 552 /**
700 - * @param array|object $source_form
553 + * @param object|array $source_form
701 554 *
702 555 * @return array
703 556 */
704 557 protected function get_form_fields( $source_form ) {
@@ -719,11 +572,10 @@
719 572 *
720 573 * @return string
721 574 */
722 575 protected function get_field_label( $field ) {
723 - $label = $field['label'] ?? '';
724 -
725 - if ( $label ) {
576 + $label = isset( $field['label'] ) ? $field['label'] : '';
577 + if ( ! empty( $label ) ) {
726 578 return $label;
727 579 }
728 580
729 581 $type = $this->get_field_type( $field );