| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | /** |
| 9 | 9 | * @return void |
| 10 | 10 | */ |
| 11 | 11 | public static function menu() { |
| 12 | - add_submenu_page( 'formidable', 'Formidable | ' . __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route' ); | |
| 12 | + add_submenu_page( 'formidable', 'Formidable | ' . __( 'Import/Export', 'formidable' ), __( 'Import/Export', 'formidable' ), 'frm_edit_forms', 'formidable-import', 'FrmXMLController::route' ); // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 13 | 13 | } |
| 14 | 14 | |
| 15 | 15 | /** |
| 16 | 16 | * @return void |
| @@ -22,11 +22,10 @@ | ||
| 22 | 22 | } |
| 23 | 23 | |
| 24 | 24 | $set_err = libxml_use_internal_errors( true ); |
| 25 | 25 | $loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 26 | + $files = apply_filters( 'frm_default_templates_files', array() ); | |
| 26 | 27 | |
| 27 | - $files = apply_filters( 'frm_default_templates_files', array() ); | |
| 28 | - | |
| 29 | 28 | foreach ( (array) $files as $file ) { |
| 30 | 29 | FrmXMLHelper::import_xml( $file ); |
| 31 | 30 | unset( $file ); |
| 32 | 31 | } |
| @@ -85,10 +84,9 @@ | ||
| 85 | 84 | $imported = FrmXMLHelper::import_xml_now( $xml, true ); |
| 86 | 85 | |
| 87 | 86 | if ( ! empty( $imported['form_status'] ) ) { |
| 88 | 87 | // Get the last form id in case there are child forms. |
| 89 | - end( $imported['form_status'] ); | |
| 90 | - $form_id = key( $imported['form_status'] ); | |
| 88 | + $form_id = array_key_last( $imported['form_status'] ); | |
| 91 | 89 | $response = array( |
| 92 | 90 | 'id' => $form_id, |
| 93 | 91 | 'redirect' => FrmForm::get_edit_link( $form_id ) . '&new_template=true', |
| 94 | 92 | 'success' => 1, |
| @@ -98,9 +96,9 @@ | ||
| 98 | 96 | // Return the link to the last page created. |
| 99 | 97 | $pages = $imported['posts']; |
| 100 | 98 | } |
| 101 | 99 | |
| 102 | - if ( ! empty( $form ) ) { | |
| 100 | + if ( $form ) { | |
| 103 | 101 | // Create selected pages with the correct shortcodes. |
| 104 | 102 | $pages = self::create_pages_for_import( $form ); |
| 105 | 103 | } |
| 106 | 104 | |
| @@ -108,18 +106,12 @@ | ||
| 108 | 106 | $post_id = end( $pages ); |
| 109 | 107 | $response['redirect'] = get_permalink( $post_id ); |
| 110 | 108 | } |
| 111 | 109 | } else { |
| 112 | - if ( isset( $imported['error'] ) ) { | |
| 113 | - $message = $imported['error']; | |
| 114 | - } else { | |
| 115 | - $message = __( 'There was an error importing form', 'formidable' ); | |
| 116 | - } | |
| 117 | - | |
| 110 | + $message = $imported['error'] ?? __( 'There was an error importing form', 'formidable' ); | |
| 118 | 111 | $response = array( |
| 119 | 112 | 'message' => $message, |
| 120 | 113 | ); |
| 121 | - | |
| 122 | 114 | }//end if |
| 123 | 115 | |
| 124 | 116 | /** |
| 125 | 117 | * @since 6.18 Added `url` to the $args. |
| @@ -150,15 +142,9 @@ | ||
| 150 | 142 | * @return mixed |
| 151 | 143 | */ |
| 152 | 144 | private static function get_posted_form() { |
| 153 | 145 | $form = FrmAppHelper::get_param( 'form', '', 'post', 'wp_unslash' ); |
| 154 | - | |
| 155 | - if ( empty( $form ) ) { | |
| 156 | - return $form; | |
| 157 | - } | |
| 158 | - | |
| 159 | - $form = json_decode( $form, true ); | |
| 160 | - return $form; | |
| 146 | + return $form ? json_decode( $form, true ) : $form; | |
| 161 | 147 | } |
| 162 | 148 | |
| 163 | 149 | /** |
| 164 | 150 | * Get a different URL depending on the selection in the form. |
| @@ -172,15 +158,15 @@ | ||
| 172 | 158 | */ |
| 173 | 159 | private static function override_url( $form, &$url ) { |
| 174 | 160 | $selected_form = self::get_selected_in_form( $form, 'form' ); |
| 175 | 161 | |
| 176 | - if ( empty( $selected_form ) ) { | |
| 162 | + if ( ! $selected_form ) { | |
| 177 | 163 | return; |
| 178 | 164 | } |
| 179 | 165 | |
| 180 | 166 | $selected_xml = isset( $form['xml'] ) && isset( $form['xml'][ $selected_form ] ) ? $form['xml'][ $selected_form ] : ''; |
| 181 | 167 | |
| 182 | - if ( empty( $selected_xml ) || strpos( $selected_xml, 'http' ) !== 0 ) { | |
| 168 | + if ( ! $selected_xml || ! str_starts_with( $selected_xml, 'http' ) ) { | |
| 183 | 169 | return; |
| 184 | 170 | } |
| 185 | 171 | |
| 186 | 172 | $url = $selected_xml; |
| @@ -191,12 +177,12 @@ | ||
| 191 | 177 | * |
| 192 | 178 | * @param array $form |
| 193 | 179 | * @param string $value |
| 194 | 180 | * |
| 195 | - * @return string | |
| 181 | + * @return array|string | |
| 196 | 182 | */ |
| 197 | 183 | private static function get_selected_in_form( $form, $value = 'form' ) { |
| 198 | - if ( ! empty( $form ) && ! empty( $form[ $value ] ) ) { | |
| 184 | + if ( $form && ! empty( $form[ $value ] ) ) { | |
| 199 | 185 | return $form[ $value ]; |
| 200 | 186 | } |
| 201 | 187 | |
| 202 | 188 | return ''; |
| @@ -215,13 +201,12 @@ | ||
| 215 | 201 | } |
| 216 | 202 | |
| 217 | 203 | $form_key = self::get_selected_in_form( $form, 'form' ); |
| 218 | 204 | $view_keys = self::get_selected_in_form( $form, 'view' ); |
| 205 | + $page_ids = array(); | |
| 219 | 206 | |
| 220 | - $page_ids = array(); | |
| 221 | - | |
| 222 | 207 | foreach ( (array) $form['pages'] as $for => $name ) { |
| 223 | - if ( empty( $name ) ) { | |
| 208 | + if ( ! $name ) { | |
| 224 | 209 | // Don't create a page if no title is given. |
| 225 | 210 | continue; |
| 226 | 211 | } |
| 227 | 212 | |
| @@ -353,11 +338,10 @@ | ||
| 353 | 338 | /** |
| 354 | 339 | * @return void |
| 355 | 340 | */ |
| 356 | 341 | public static function import_xml() { |
| 357 | - $errors = array(); | |
| 358 | - $message = ''; | |
| 359 | - | |
| 342 | + $errors = array(); | |
| 343 | + $message = ''; | |
| 360 | 344 | $permission_error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'import-xml', 'import-xml-nonce' ); |
| 361 | 345 | |
| 362 | 346 | if ( false !== $permission_error ) { |
| 363 | 347 | $errors[] = $permission_error; |
| @@ -365,9 +349,9 @@ | ||
| 365 | 349 | |
| 366 | 350 | return; |
| 367 | 351 | } |
| 368 | 352 | |
| 369 | - // phpcs:ignore WordPress.Security.NonceVerification.Missing | |
| 353 | + // phpcs:ignore WordPress.Security.NonceVerification.Missing, SlevomatCodingStandard.Files.LineLength.LineTooLong | |
| 370 | 354 | $has_file = ! empty( $_FILES['frm_import_file'] ) && ! empty( $_FILES['frm_import_file']['name'] ) && ! empty( $_FILES['frm_import_file']['size'] ) && (int) $_FILES['frm_import_file']['size'] > 0; |
| 371 | 355 | |
| 372 | 356 | if ( ! $has_file ) { |
| 373 | 357 | $errors[] = __( 'Oops, you didn\'t select a file.', 'formidable' ); |
| @@ -400,9 +384,9 @@ | ||
| 400 | 384 | $file_type = sanitize_option( 'upload_path', $_FILES['frm_import_file']['name'] ); |
| 401 | 385 | $file_type = strtolower( pathinfo( $file_type, PATHINFO_EXTENSION ) ); |
| 402 | 386 | |
| 403 | 387 | if ( 'xml' !== $file_type && isset( $export_format[ $file_type ] ) ) { |
| 404 | - // allow other file types to be imported | |
| 388 | + // Allow other file types to be imported | |
| 405 | 389 | do_action( 'frm_before_import_' . $file_type ); |
| 406 | 390 | |
| 407 | 391 | return; |
| 408 | 392 | } |
| @@ -416,10 +400,9 @@ | ||
| 416 | 400 | } |
| 417 | 401 | |
| 418 | 402 | $set_err = libxml_use_internal_errors( true ); |
| 419 | 403 | $loader = FrmXMLHelper::maybe_libxml_disable_entity_loader( true ); |
| 420 | - | |
| 421 | - $result = FrmXMLHelper::import_xml( $file ); | |
| 404 | + $result = FrmXMLHelper::import_xml( $file ); | |
| 422 | 405 | FrmXMLHelper::parse_message( $result, $message, $errors ); |
| 423 | 406 | |
| 424 | 407 | unset( $file ); |
| 425 | 408 | |
| @@ -434,9 +417,9 @@ | ||
| 434 | 417 | */ |
| 435 | 418 | public static function export_xml() { |
| 436 | 419 | $error = FrmAppHelper::permission_nonce_error( 'frm_edit_forms', 'export-xml', 'export-xml-nonce' ); |
| 437 | 420 | |
| 438 | - if ( ! empty( $error ) ) { | |
| 421 | + if ( $error ) { | |
| 439 | 422 | wp_die( esc_html( $error ) ); |
| 440 | 423 | } |
| 441 | 424 | |
| 442 | 425 | $ids = FrmAppHelper::get_post_param( 'frm_export_forms', array(), 'sanitize_text_field' ); |
| @@ -443,9 +426,9 @@ | ||
| 443 | 426 | $type = FrmAppHelper::get_post_param( 'type', array(), 'sanitize_text_field' ); |
| 444 | 427 | $format = FrmAppHelper::get_post_param( 'format', 'xml', 'sanitize_title' ); |
| 445 | 428 | |
| 446 | 429 | if ( ! headers_sent() && ! $type ) { |
| 447 | - wp_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) ); | |
| 430 | + wp_safe_redirect( esc_url_raw( admin_url( 'admin.php?page=formidable-import' ) ) ); | |
| 448 | 431 | die(); |
| 449 | 432 | } |
| 450 | 433 | |
| 451 | 434 | if ( 'xml' === $format ) { |
| @@ -466,9 +449,9 @@ | ||
| 466 | 449 | * @psalm-param array{ids?: mixed} $args |
| 467 | 450 | * |
| 468 | 451 | * @return void |
| 469 | 452 | */ |
| 470 | - public static function generate_xml( $type, $args = array() ) { | |
| 453 | + public static function generate_xml( $type, $args = array() ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh | |
| 471 | 454 | global $wpdb; |
| 472 | 455 | |
| 473 | 456 | self::prepare_types_array( $type ); |
| 474 | 457 | |
| @@ -492,9 +475,9 @@ | ||
| 492 | 475 | ); |
| 493 | 476 | $args = wp_parse_args( $args, $defaults ); |
| 494 | 477 | |
| 495 | 478 | // Make sure ids are numeric. |
| 496 | - if ( is_array( $args['ids'] ) && ! empty( $args['ids'] ) ) { | |
| 479 | + if ( is_array( $args['ids'] ) && $args['ids'] ) { | |
| 497 | 480 | $args['ids'] = array_filter( $args['ids'], 'is_numeric' ); |
| 498 | 481 | } |
| 499 | 482 | |
| 500 | 483 | $records = array(); |
| @@ -499,12 +482,11 @@ | ||
| 499 | 482 | |
| 500 | 483 | $records = array(); |
| 501 | 484 | |
| 502 | 485 | foreach ( $type as $tb_type ) { |
| 503 | - $where = array(); | |
| 504 | - $join = ''; | |
| 505 | - $table = $tables[ $tb_type ]; | |
| 506 | - | |
| 486 | + $where = array(); | |
| 487 | + $join = ''; | |
| 488 | + $table = $tables[ $tb_type ]; | |
| 507 | 489 | $select = $table . '.id'; |
| 508 | 490 | $query_vars = array(); |
| 509 | 491 | |
| 510 | 492 | switch ( $tb_type ) { |
| @@ -558,9 +540,9 @@ | ||
| 558 | 540 | $select = $table . '.ID'; |
| 559 | 541 | $where['post_type'] = 'frm_styles'; |
| 560 | 542 | |
| 561 | 543 | // Only export selected styles. |
| 562 | - if ( ! empty( $style_ids ) ) { | |
| 544 | + if ( $style_ids ) { | |
| 563 | 545 | $where['ID'] = $style_ids; |
| 564 | 546 | } |
| 565 | 547 | break; |
| 566 | 548 | default: |
| @@ -605,12 +587,11 @@ | ||
| 605 | 587 | if ( ! $parent_term_ids ) { |
| 606 | 588 | return $parent_slugs; |
| 607 | 589 | } |
| 608 | 590 | |
| 609 | - $results = FrmDb::get_results( 'terms', array( 'term_id' => $parent_term_ids ), 'term_id, slug' ); | |
| 610 | - $parent_slugs = wp_list_pluck( $results, 'slug', 'term_id' ); | |
| 591 | + $results = FrmDb::get_results( 'terms', array( 'term_id' => $parent_term_ids ), 'term_id, slug' ); | |
| 611 | 592 | |
| 612 | - return $parent_slugs; | |
| 593 | + return wp_list_pluck( $results, 'slug', 'term_id' ); | |
| 613 | 594 | } |
| 614 | 595 | |
| 615 | 596 | /** |
| 616 | 597 | * Prepare the types array. |
| @@ -622,14 +603,14 @@ | ||
| 622 | 603 | private static function prepare_types_array( &$type ) { |
| 623 | 604 | $type = (array) $type; |
| 624 | 605 | |
| 625 | 606 | if ( ! in_array( 'forms', $type, true ) && ( in_array( 'items', $type, true ) || in_array( 'posts', $type, true ) ) ) { |
| 626 | - // make sure the form is included if there are entries | |
| 607 | + // Make sure the form is included if there are entries | |
| 627 | 608 | $type[] = 'forms'; |
| 628 | 609 | } |
| 629 | 610 | |
| 630 | 611 | if ( in_array( 'forms', $type, true ) ) { |
| 631 | - // include actions with forms | |
| 612 | + // Include actions with forms | |
| 632 | 613 | $type[] = 'actions'; |
| 633 | 614 | } |
| 634 | 615 | } |
| 635 | 616 | |
| @@ -647,9 +628,9 @@ | ||
| 647 | 628 | private static function get_file_name( $args, $records ) { |
| 648 | 629 | $has_one_form = ! empty( $records['forms'] ) && count( $args['ids'] ) === 1; |
| 649 | 630 | |
| 650 | 631 | if ( $has_one_form ) { |
| 651 | - // one form is being exported | |
| 632 | + // One form is being exported | |
| 652 | 633 | $selected_form_id = reset( $args['ids'] ); |
| 653 | 634 | $filename = 'form-' . $selected_form_id . '.xml'; |
| 654 | 635 | |
| 655 | 636 | foreach ( $records['forms'] as $form_id ) { |
| @@ -664,9 +645,9 @@ | ||
| 664 | 645 | } |
| 665 | 646 | } else { |
| 666 | 647 | $sitename = sanitize_key( get_bloginfo( 'name' ) ); |
| 667 | 648 | |
| 668 | - if ( ! empty( $sitename ) ) { | |
| 649 | + if ( $sitename ) { | |
| 669 | 650 | $sitename .= '.'; |
| 670 | 651 | } |
| 671 | 652 | |
| 672 | 653 | $filename = $sitename . 'formidable.' . gmdate( 'Y-m-d' ) . '.xml'; |
| @@ -687,9 +668,9 @@ | ||
| 687 | 668 | */ |
| 688 | 669 | public static function generate_csv( $atts ) { |
| 689 | 670 | $form_ids = $atts['ids']; |
| 690 | 671 | |
| 691 | - if ( empty( $form_ids ) ) { | |
| 672 | + if ( ! $form_ids ) { | |
| 692 | 673 | wp_die( esc_html__( 'Please select a form', 'formidable' ) ); |
| 693 | 674 | } |
| 694 | 675 | self::csv( reset( $form_ids ) ); |
| 695 | 676 | } |
| @@ -709,9 +690,9 @@ | ||
| 709 | 690 | FrmAppHelper::permission_check( 'frm_view_entries' ); |
| 710 | 691 | |
| 711 | 692 | if ( ! $form_id ) { |
| 712 | 693 | $form_id = FrmAppHelper::get_param( 'form', '', 'get', 'sanitize_text_field' ); |
| 713 | - $search = FrmAppHelper::get_param( ( isset( $_REQUEST['s'] ) ? 's' : 'search' ), '', 'get', 'sanitize_text_field' ); | |
| 694 | + $search = FrmAppHelper::get_param( isset( $_REQUEST['s'] ) ? 's' : 'search', '', 'get', 'sanitize_text_field' ); | |
| 714 | 695 | $fid = FrmAppHelper::get_param( 'fid', '', 'get', 'sanitize_text_field' ); |
| 715 | 696 | } |
| 716 | 697 | |
| 717 | 698 | // Remove time limit to execute this function. |
| @@ -735,12 +716,11 @@ | ||
| 735 | 716 | } |
| 736 | 717 | |
| 737 | 718 | $form_id = $form->id; |
| 738 | 719 | $form_cols = self::get_fields_for_csv_export( $form_id, $form ); |
| 720 | + $item_id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'sanitize_text_field' ); | |
| 739 | 721 | |
| 740 | - $item_id = FrmAppHelper::get_param( 'item_id', 0, 'get', 'sanitize_text_field' ); | |
| 741 | - | |
| 742 | - if ( ! empty( $item_id ) ) { | |
| 722 | + if ( $item_id ) { | |
| 743 | 723 | $item_id = explode( ',', $item_id ); |
| 744 | 724 | } |
| 745 | 725 | |
| 746 | 726 | $query = array( |
| @@ -761,12 +741,12 @@ | ||
| 761 | 741 | |
| 762 | 742 | $entry_ids = FrmDb::get_col( $wpdb->prefix . 'frm_items it', $query ); |
| 763 | 743 | unset( $query ); |
| 764 | 744 | |
| 765 | - if ( empty( $entry_ids ) ) { | |
| 745 | + if ( $entry_ids ) { | |
| 746 | + FrmCSVExportHelper::generate_csv( compact( 'form', 'entry_ids', 'form_cols' ) ); | |
| 747 | + } else { | |
| 766 | 748 | esc_html_e( 'There are no entries for that form.', 'formidable' ); |
| 767 | - } else { | |
| 768 | - FrmCSVExportHelper::generate_csv( compact( 'form', 'entry_ids', 'form_cols' ) ); | |
| 769 | 749 | } |
| 770 | 750 | |
| 771 | 751 | wp_die(); |
| 772 | 752 | } |
| @@ -779,9 +759,9 @@ | ||
| 779 | 759 | * |
| 780 | 760 | * @param int $form_id |
| 781 | 761 | * @param object $form |
| 782 | 762 | * |
| 783 | - * @return array $csv_fields | |
| 763 | + * @return array CSV fields. | |
| 784 | 764 | */ |
| 785 | 765 | public static function get_fields_for_csv_export( $form_id, $form ) { |
| 786 | 766 | $csv_fields = FrmField::get_all_for_form( $form_id, '', 'include', 'include' ); |
| 787 | 767 | $no_export_fields = FrmField::no_save_fields(); |
| @@ -801,14 +781,14 @@ | ||
| 801 | 781 | * @return array |
| 802 | 782 | */ |
| 803 | 783 | public static function allow_mime( $mimes ) { |
| 804 | 784 | if ( ! isset( $mimes['csv'] ) ) { |
| 805 | - // allow csv files | |
| 785 | + // Allow csv files | |
| 806 | 786 | $mimes['csv'] = 'text/csv'; |
| 807 | 787 | } |
| 808 | 788 | |
| 809 | 789 | if ( ! isset( $mimes['xml'] ) ) { |
| 810 | - // allow xml | |
| 790 | + // Allow xml | |
| 811 | 791 | $mimes['xml'] = 'text/xml'; |
| 812 | 792 | } |
| 813 | 793 | |
| 814 | 794 | return $mimes; |