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