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