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