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/helpers/FrmXMLHelper.php +357 -278 6.26.1 → trunk View file →
@@ -46,10 +46,10 @@
46 46
47 47 if ( ! class_exists( 'DOMDocument' ) ) {
48 48 $error_message = sprintf(
49 49 /* translators: 1: Documentation link */
50 - __( 'In order to install XML, your server must have DOMDocument installed. Follow our documentation on %1$s to ensure DOMDocument is properly set up and XML support is enabled.', 'formidable' ),
51 - '<a href="https://formidableforms.com/knowledgebase/import-forms-entries-and-views/#kb-your-server-does-not-have-xml-enabled" target="_blank">Importing Forms, Entries, and Views</a>'
50 + __( 'In order to install XML, your server must have DOMDocument installed. Follow our documentation on %1$s to ensure DOMDocument is properly set up and XML support is enabled.', 'formidable' ), // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
51 + '<a href="https://formidableforms.com/knowledgebase/import-forms-entries-and-views/#kb-your-server-does-not-have-xml-enabled" target="_blank">Importing Forms, Entries, and Views</a>' // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
52 52 );
53 53
54 54 return new WP_Error( 'SimpleXML_parse_error', $error_message, libxml_get_errors() );
55 55 }
@@ -73,9 +73,9 @@
73 73
74 74 $xml = simplexml_import_dom( $dom );
75 75 unset( $dom );
76 76
77 - // halt if loading produces an error
77 + // Halt if loading produces an error
78 78 if ( ! $xml ) {
79 79 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
80 80 }
81 81
@@ -89,9 +89,9 @@
89 89 *
90 90 * @return void
91 91 */
92 92 private static function maybe_fix_xml( &$xml_string ) {
93 - if ( '<?xml' !== substr( $xml_string, 0, 5 ) ) {
93 + if ( ! str_starts_with( $xml_string, '<?xml' ) ) {
94 94 // Some XML files have may have unexpected characters at the start.
95 95 $xml_string = substr( $xml_string, strpos( $xml_string, '<?xml' ) );
96 96 }
97 97
@@ -99,17 +99,19 @@
99 99 // Strip that out as it breaks the XML import.
100 100 $channel_start_position = strpos( $xml_string, '<channel>' );
101 101 $content_before_channel_tag = substr( $xml_string, 0, $channel_start_position );
102 102
103 - if ( 0 !== strpos( $content_before_channel_tag, '<meta name="generator" ' ) ) {
104 - $content_before_channel_tag = preg_replace(
105 - '/<meta\s+[^>]*name="generator"[^>]*\/>/i',
106 - '',
107 - $content_before_channel_tag,
108 - 1
109 - );
110 - $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position );
103 + if ( str_starts_with( $content_before_channel_tag, '<meta name="generator" ' ) ) {
104 + return;
111 105 }
106 +
107 + $content_before_channel_tag = preg_replace(
108 + '/<meta\s+[^>]*name="generator"[^>]*\/>/i',
109 + '',
110 + $content_before_channel_tag,
111 + 1
112 + );
113 + $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position );
112 114 }
113 115
114 116 /**
115 117 * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order
@@ -130,13 +132,15 @@
130 132 $imported = self::pre_import_data();
131 133
132 134 foreach ( array( 'term', 'form', 'view' ) as $item_type ) {
133 135 // Grab cats, tags, and terms, or forms or posts.
134 - if ( isset( $xml->{$item_type} ) ) {
135 - $function_name = 'import_xml_' . $item_type . 's';
136 - $imported = self::$function_name( $xml->{$item_type}, $imported );
137 - unset( $function_name, $xml->{$item_type} );
136 + if ( ! isset( $xml->{$item_type} ) ) {
137 + continue;
138 138 }
139 +
140 + $function_name = 'import_xml_' . $item_type . 's';
141 + $imported = self::$function_name( $xml->{$item_type}, $imported );
142 + unset( $function_name, $xml->{$item_type} );
139 143 }
140 144
141 145 $imported = apply_filters( 'frm_importing_xml', $imported, $xml );
142 146
@@ -193,9 +197,9 @@
193 197 (string) $t->term_taxonomy,
194 198 array(
195 199 'slug' => (string) $t->term_slug,
196 200 'description' => (string) $t->term_description,
197 - 'parent' => empty( $parent ) ? 0 : $parent,
201 + 'parent' => $parent ? $parent : 0,
198 202 )
199 203 );
200 204
201 205 if ( $term && is_array( $term ) ) {
@@ -218,16 +222,11 @@
218 222 */
219 223 private static function get_term_parent_id( $t ) {
220 224 $parent = (string) $t->term_parent;
221 225
222 - if ( ! empty( $parent ) ) {
226 + if ( $parent ) {
223 227 $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy );
224 -
225 - if ( $parent ) {
226 - $parent = $parent['term_id'];
227 - } else {
228 - $parent = 0;
229 - }
228 + $parent = $parent ? $parent['term_id'] : 0;
230 229 }
231 230
232 231 return $parent;
233 232 }
@@ -248,14 +247,13 @@
248 247 $form = self::fill_form( $item );
249 248
250 249 self::update_custom_style_setting_on_import( $form );
251 250
252 - $this_form = self::maybe_get_form( $form );
253 -
251 + $this_form = self::maybe_get_form( $form );
254 252 $old_id = false;
255 253 $form_fields = false;
256 254
257 - if ( ! empty( $this_form ) ) {
255 + if ( $this_form ) {
258 256 $form_id = $this_form->id;
259 257 $old_id = $this_form->id;
260 258 self::update_form( $this_form, $form, $imported );
261 259
@@ -296,8 +294,23 @@
296 294 }//end foreach
297 295
298 296 self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms );
299 297
298 + /**
299 + * Fires after all forms in the XML have been imported.
300 + *
301 + * Child forms are imported before their parent form, so per-form fix-ups
302 + * cannot resolve conditional logic that references fields in a form
303 + * imported later. Use this action for cross-form fix-ups that require
304 + * the fully populated `$frm_duplicate_ids` map.
305 + *
306 + * @since 6.31
307 + *
308 + * @param array $imported Summary of imported items, including
309 + * 'forms' => array( old_form_id => new_form_id ).
310 + */
311 + do_action( 'frm_after_import_forms', $imported );
312 +
300 313 return $imported;
301 314 }
302 315
303 316 /**
@@ -339,9 +352,9 @@
339 352 *
340 353 * @return false|object
341 354 */
342 355 private static function maybe_get_form( $form ) {
343 - // if template, allow to edit if form keys match, otherwise, creation date must also match
356 + // If template, allow to edit if form keys match, otherwise, creation date must also match
344 357 $edit_query = array(
345 358 'form_key' => $form['form_key'],
346 359 'is_template' => $form['is_template'],
347 360 );
@@ -350,11 +363,10 @@
350 363 $edit_query['created_at'] = $form['created_at'];
351 364 }
352 365
353 366 $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form );
367 + $form = FrmForm::getAll( $edit_query, '', 1 );
354 368
355 - $form = FrmForm::getAll( $edit_query, '', 1 );
356 -
357 369 if ( is_object( $form ) && $form->status === 'trash' ) {
358 370 FrmForm::destroy( $form->id );
359 371 return false;
360 372 }
@@ -396,11 +408,9 @@
396 408 $old_fields[ $f->field_key ] = $f->id;
397 409 unset( $f );
398 410 }
399 411
400 - $form_fields = $old_fields;
401 -
402 - return $form_fields;
412 + return $old_fields;
403 413 }
404 414
405 415 /**
406 416 * Delete any fields attached to this form that were not included in the template
@@ -409,15 +419,17 @@
409 419 *
410 420 * @return void
411 421 */
412 422 private static function delete_removed_fields( $form_fields ) {
413 - if ( ! empty( $form_fields ) ) {
414 - foreach ( $form_fields as $field ) {
415 - if ( is_object( $field ) ) {
416 - FrmField::destroy( $field->id );
417 - }
418 - unset( $field );
423 + if ( ! $form_fields ) {
424 + return;
425 + }
426 +
427 + foreach ( $form_fields as $field ) {
428 + if ( is_object( $field ) ) {
429 + FrmField::destroy( $field->id );
419 430 }
431 + unset( $field );
420 432 }
421 433 }
422 434
423 435 /**
@@ -469,14 +481,16 @@
469 481 * @param array $child_forms
470 482 */
471 483 private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) {
472 484 foreach ( $child_forms as $child_form_id => $old_parent_form_id ) {
473 - if ( isset( $imported_forms[ $old_parent_form_id ] ) && (int) $imported_forms[ $old_parent_form_id ] !== (int) $old_parent_form_id ) {
474 - // Update all children with this old parent_form_id
475 - $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
476 - FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
477 - do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id );
485 + if ( ! isset( $imported_forms[ $old_parent_form_id ] ) || (int) $imported_forms[ $old_parent_form_id ] === (int) $old_parent_form_id ) {
486 + continue;
478 487 }
488 +
489 + // Update all children with this old parent_form_id
490 + $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
491 + FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
492 + do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id );
479 493 }
480 494 }
481 495
482 496 /**
@@ -505,10 +519,10 @@
505 519 self::maybe_update_form_select( $f, $imported );
506 520 self::maybe_update_get_values_form_setting( $imported, $f );
507 521 self::migrate_placeholders( $f );
508 522
509 - if ( ! empty( $this_form ) ) {
510 - // check for field to edit by field id
523 + if ( $this_form ) {
524 + // Check for field to edit by field id
511 525 if ( isset( $form_fields[ $f['id'] ] ) ) {
512 526 FrmField::update( $f['id'], $f );
513 527 ++$imported['updated']['fields'];
514 528
@@ -519,12 +533,11 @@
519 533 unset( $form_fields[ $f['field_key'] ] );
520 534 }
521 535 } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) {
522 536 $keys_by_original_field_id[ $f['id'] ] = $f['field_key'];
537 + $old_field_id = $f['id'];
523 538
524 - $old_field_id = $f['id'];
525 -
526 - // check for field to edit by field key
539 + // Check for field to edit by field key
527 540 unset( $f['id'] );
528 541
529 542 FrmField::update( $form_fields[ $f['field_key'] ], $f );
530 543 ++$imported['updated']['fields'];
@@ -540,9 +553,8 @@
540 553 // If no matching field id or key in this form, create the field.
541 554 self::create_imported_field( $f, $imported );
542 555 }//end if
543 556 } else {
544 -
545 557 self::create_imported_field( $f, $imported );
546 558 }//end if
547 559 }//end foreach
548 560
@@ -607,13 +619,40 @@
607 619 'field_order' => (int) $field->field_order,
608 620 'form_id' => (int) $form_id,
609 621 'required' => (int) $field->required,
610 622 'options' => FrmAppHelper::maybe_json_decode( (string) $field->options ),
611 - 'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options ),
623 + 'field_options' => self::fill_field_options( $field ),
612 624 );
613 625 }
614 626
615 627 /**
628 + * Reads a field's options out of the file as an array.
629 + *
630 + * Field options are a settings map, and every step of the import reads them
631 + * as one. A file whose value there cannot be read, because it is empty or
632 + * was written by something other than an export, used to end the whole
633 + * import with a fatal error on the first field it reached. An empty set of
634 + * options is recoverable, since the defaults for the field type fill the
635 + * gaps, so prefer that over stopping.
636 + *
637 + * Unserialize is tried as well as JSON so options written by an older
638 + * version are still read rather than thrown away.
639 + *
640 + * @since 6.35
641 + *
642 + * @param object $field Field element from the file.
643 + *
644 + * @return array
645 + */
646 + private static function fill_field_options( $field ) {
647 + $options = (string) $field->field_options;
648 +
649 + FrmAppHelper::unserialize_or_decode( $options );
650 +
651 + return is_array( $options ) ? $options : array();
652 + }
653 +
654 + /**
616 655 * @since 4.06
617 656 *
618 657 * @param array $f
619 658 *
@@ -632,15 +671,17 @@
632 671 'password',
633 672 'tag',
634 673 );
635 674
636 - if ( is_array( $f['default_value'] ) && in_array( $f['type'], $has_default, true ) ) {
637 - if ( count( $f['default_value'] ) === 1 ) {
638 - $f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
639 - } else {
640 - $f['default_value'] = reset( $f['default_value'] );
641 - }
675 + if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) {
676 + return;
642 677 }
678 +
679 + if ( count( $f['default_value'] ) === 1 ) {
680 + $f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
681 + } else {
682 + $f['default_value'] = reset( $f['default_value'] );
683 + }
643 684 }
644 685
645 686 /**
646 687 * Make sure the required indicator is set.
@@ -668,9 +709,9 @@
668 709 * @return void
669 710 */
670 711 private static function maybe_update_in_section_variable( &$in_section, &$f ) {
671 712 // If we're at the end of a section, switch $in_section is 0
672 - if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) {
713 + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ), true ) ) {
673 714 $in_section = 0;
674 715 }
675 716
676 717 // Update the current field's in_section value
@@ -696,17 +737,21 @@
696 737 if ( ! isset( $imported['forms'] ) ) {
697 738 return;
698 739 }
699 740
700 - if ( $f['type'] === 'form' || ( $f['type'] === 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
701 - if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
702 - $form_select = (int) $f['field_options']['form_select'];
741 + if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
742 + return;
743 + }
703 744
704 - if ( isset( $imported['forms'][ $form_select ] ) ) {
705 - $f['field_options']['form_select'] = $imported['forms'][ $form_select ];
706 - }
707 - }
745 + if ( ! FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
746 + return;
708 747 }
748 +
749 + $form_select = (int) $f['field_options']['form_select'];
750 +
751 + if ( isset( $imported['forms'][ $form_select ] ) ) {
752 + $f['field_options']['form_select'] = $imported['forms'][ $form_select ];
753 + }
709 754 }
710 755
711 756 /**
712 757 * Update the get_values_form setting if the form was imported
@@ -722,14 +767,16 @@
722 767 if ( ! isset( $imported['forms'] ) ) {
723 768 return;
724 769 }
725 770
726 - if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
727 - $old_form = $f['field_options']['get_values_form'];
771 + if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
772 + return;
773 + }
728 774
729 - if ( isset( $imported['forms'][ $old_form ] ) ) {
730 - $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
731 - }
775 + $old_form = $f['field_options']['get_values_form'];
776 +
777 + if ( isset( $imported['forms'][ $old_form ] ) ) {
778 + $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
732 779 }
733 780 }
734 781
735 782 /**
@@ -782,28 +829,33 @@
782 829
783 830 // If a dropdown placeholder was used, remove the option so it won't be included twice.
784 831 $options = $field['options'];
785 832
786 - if ( $type === 'default_blank' && is_array( $options ) ) {
787 - $default_value = $field['default_value'];
833 + if ( $type !== 'default_blank' || ! is_array( $options ) ) {
834 + return $changes;
835 + }
788 836
789 - if ( is_array( $default_value ) ) {
790 - $default_value = reset( $default_value );
837 + $default_value = $field['default_value'];
838 +
839 + if ( is_array( $default_value ) ) {
840 + $default_value = reset( $default_value );
841 + }
842 +
843 + foreach ( $options as $opt_key => $opt ) {
844 + if ( is_array( $opt ) ) {
845 + $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
791 846 }
792 847
793 - foreach ( $options as $opt_key => $opt ) {
794 - if ( is_array( $opt ) ) {
795 - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
796 - }
797 -
798 - if ( $opt == $default_value ) {
799 - unset( $options[ $opt_key ] );
800 - break;
801 - }
848 + // phpcs:ignore Universal.Operators.StrictComparisons
849 + if ( $opt == $default_value ) {
850 + unset( $options[ $opt_key ] );
851 + break;
802 852 }
803 - $changes['options'] = $options;
804 853 }
805 854
855 + $changes['options'] = $options;
856 + // end if
857 +
806 858 return $changes;
807 859 }
808 860
809 861 /**
@@ -824,12 +876,14 @@
824 876 }
825 877
826 878 $new_id = FrmField::create( $f );
827 879
828 - if ( $new_id != false ) {
829 - ++$imported['imported']['fields'];
830 - do_action( 'frm_after_field_is_imported', $f, $new_id );
880 + if ( ! $new_id ) {
881 + return;
831 882 }
883 +
884 + ++$imported['imported']['fields'];
885 + do_action( 'frm_after_field_is_imported', $f, $new_id );
832 886 }
833 887
834 888 /**
835 889 * Import images for radio buttons and checkboxes from image src if available.
@@ -895,15 +949,17 @@
895 949 $field = (array) $field;
896 950 $frm_duplicate_ids = $keys_by_original_field_id;
897 951 $after = FrmFieldsHelper::switch_field_ids( $field );
898 952
953 + if ( $before['field_options'] === $after['field_options'] ) {
954 + continue;
955 + }
956 +
957 + $frm_duplicate_ids = $field_id_by_key;
958 + $after = FrmFieldsHelper::switch_field_ids( $after );
959 +
899 960 if ( $before['field_options'] !== $after['field_options'] ) {
900 - $frm_duplicate_ids = $field_id_by_key;
901 - $after = FrmFieldsHelper::switch_field_ids( $after );
902 -
903 - if ( $before['field_options'] !== $after['field_options'] ) {
904 - FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
905 - }
961 + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
906 962 }
907 963 }
908 964
909 965 $frm_duplicate_ids = $former_duplicate_ids;
@@ -926,29 +982,32 @@
926 982
927 983 if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
928 984 // Set to default
929 985 $form['options']['custom_style'] = 1;
930 - } else {
931 - // Replace the style name with the style ID on import
932 - global $wpdb;
933 - $table = $wpdb->prefix . 'posts';
934 - $where = array(
935 - 'post_name' => $form['options']['custom_style'],
936 - 'post_type' => 'frm_styles',
937 - );
938 - $select = 'ID';
939 - $style_id = FrmDb::get_var( $table, $where, $select );
940 986
941 - if ( $style_id ) {
942 - $form['options']['custom_style'] = $style_id;
943 - } else {
944 - // save the old style to maybe update after styles import
945 - $form['options']['old_style'] = $form['options']['custom_style'];
987 + return;
988 + }
946 989
947 - // Set to default
948 - $form['options']['custom_style'] = 1;
949 - }
950 - }//end if
990 + // Replace the style name with the style ID on import
991 + global $wpdb;
992 + $table = $wpdb->prefix . 'posts';
993 + $where = array(
994 + 'post_name' => $form['options']['custom_style'],
995 + 'post_type' => 'frm_styles',
996 + );
997 + $select = 'ID';
998 + $style_id = FrmDb::get_var( $table, $where, $select );
999 +
1000 + if ( $style_id ) {
1001 + $form['options']['custom_style'] = $style_id;
1002 + return;
1003 + }
1004 +
1005 + // Save the old style to maybe update after styles import
1006 + $form['options']['old_style'] = $form['options']['custom_style'];
1007 +
1008 + // Set to default
1009 + $form['options']['custom_style'] = 1;
951 1010 }
952 1011
953 1012 /**
954 1013 * After styles are imported, check for any forms that were linked
@@ -962,18 +1021,20 @@
962 1021 */
963 1022 private static function update_custom_style_setting_after_import( $form_id ) {
964 1023 $form = FrmForm::getOne( $form_id );
965 1024
966 - if ( $form && isset( $form->options['old_style'] ) ) {
967 - $form = (array) $form;
968 - $saved_style = $form['options']['custom_style'];
969 - $form['options']['custom_style'] = $form['options']['old_style'];
970 - self::update_custom_style_setting_on_import( $form );
971 - $has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] );
1025 + if ( ! $form || ! isset( $form->options['old_style'] ) ) {
1026 + return;
1027 + }
972 1028
973 - if ( $has_changed ) {
974 - FrmForm::update( $form['id'], $form );
975 - }
1029 + $form = (array) $form;
1030 + $saved_style = $form['options']['custom_style'];
1031 + $form['options']['custom_style'] = $form['options']['old_style'];
1032 + self::update_custom_style_setting_on_import( $form );
1033 + $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong
1034 +
1035 + if ( $has_changed ) {
1036 + FrmForm::update( $form['id'], $form );
976 1037 }
977 1038 }
978 1039
979 1040 /**
@@ -983,9 +1044,9 @@
983 1044 * @param array $imported
984 1045 *
985 1046 * @return array
986 1047 */
987 - public static function import_xml_views( $views, $imported ) {
1048 + public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
988 1049 $imported['posts'] = array();
989 1050 $form_action_type = FrmFormActionsController::$action_post_type;
990 1051
991 1052 $post_types = array(
@@ -1064,9 +1125,9 @@
1064 1125 if ( ! is_numeric( $post_id ) ) {
1065 1126 continue;
1066 1127 }
1067 1128
1068 - if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
1129 + if ( str_contains( $post['post_content'], '[display-frm-data' ) || str_contains( $post['post_content'], '[formidable' ) ) {
1069 1130 $posts_with_shortcodes[ $post_id ] = $post;
1070 1131 }
1071 1132
1072 1133 self::update_postmeta( $post, $post_id );
@@ -1077,9 +1138,9 @@
1077 1138 if ( isset( $post_types[ $post['post_type'] ] ) ) {
1078 1139 $this_type = $post_types[ $post['post_type'] ];
1079 1140 }
1080 1141
1081 - if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
1142 + if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) {
1082 1143 ++$imported['updated'][ $this_type ];
1083 1144 } else {
1084 1145 ++$imported['imported'][ $this_type ];
1085 1146 }
@@ -1100,9 +1161,9 @@
1100 1161 }
1101 1162 unset( $posts_with_shortcodes, $view_ids );
1102 1163
1103 1164 if ( ! empty( $imported['forms'] ) ) {
1104 - // clear imported forms style cache to make sure the new styles are applied to the forms
1165 + // Clear imported forms style cache to make sure the new styles are applied to the forms
1105 1166 self::clear_forms_style_caches( $imported['forms'] );
1106 1167 }
1107 1168
1108 1169 self::maybe_update_stylesheet( $imported );
@@ -1137,10 +1198,9 @@
1137 1198 'post_name' => $form->options['old_style'],
1138 1199 'post_type' => FrmStylesController::$post_type,
1139 1200 );
1140 1201
1141 - $select = 'ID';
1142 -
1202 + $select = 'ID';
1143 1203 $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
1144 1204 FrmDb::delete_cache_and_transient( $cache_key, 'post' );
1145 1205 }
1146 1206 }
@@ -1153,9 +1213,9 @@
1153 1213 *
1154 1214 * @return string
1155 1215 */
1156 1216 private static function switch_form_ids( $string, $form_ids ) {
1157 - if ( false === strpos( $string, '[formidable' ) ) {
1217 + if ( ! str_contains( $string, '[formidable' ) ) {
1158 1218 // Skip string replacing if there are no form shortcodes in string.
1159 1219 return $string;
1160 1220 }
1161 1221
@@ -1211,9 +1271,9 @@
1211 1271 *
1212 1272 * @return string
1213 1273 */
1214 1274 private static function switch_view_ids( $string, $view_ids ) {
1215 - if ( false === strpos( $string, '[display-frm-data' ) ) {
1275 + if ( ! str_contains( $string, '[display-frm-data' ) ) {
1216 1276 // Skip string replacing if there are no view shortcodes in string.
1217 1277 return $string;
1218 1278 }
1219 1279
@@ -1249,8 +1309,9 @@
1249 1309
1250 1310 if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) {
1251 1311 return FrmAppHelper::prepare_and_encode( $maybe_decoded );
1252 1312 }
1313 +
1253 1314 return $content;
1254 1315 }
1255 1316
1256 1317 /**
@@ -1264,15 +1325,15 @@
1264 1325 if ( isset( $item->attachment_url ) ) {
1265 1326 $post['attachment_url'] = (string) $item->attachment_url;
1266 1327 }
1267 1328
1268 - if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1269 - // update to new form id
1329 + if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1330 + // Update to new form id
1270 1331 $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
1271 1332 }
1272 1333
1273 1334 // Don't allow default styles to take over a site's default style
1274 - if ( 'frm_styles' == $post['post_type'] ) {
1335 + if ( 'frm_styles' === $post['post_type'] ) {
1275 1336 $post['menu_order'] = 0;
1276 1337 }
1277 1338
1278 1339 foreach ( $item->postmeta as $meta ) {
@@ -1294,9 +1355,9 @@
1294 1355 * @param array $post
1295 1356 * @param stdClass $meta
1296 1357 * @param array $imported
1297 1358 */
1298 - private static function populate_postmeta( &$post, $meta, $imported ) {
1359 + private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong
1299 1360 global $frm_duplicate_ids;
1300 1361
1301 1362 $m = array(
1302 1363 'key' => (string) $meta->meta_key,
@@ -1308,14 +1369,13 @@
1308 1369 $m['value'] = $imported['forms'][ (int) $m['value'] ];
1309 1370 } else {
1310 1371 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1311 1372
1312 - if ( ! empty( $frm_duplicate_ids ) ) {
1373 + if ( $frm_duplicate_ids ) {
1313 1374 if ( 'frm_dyncontent' === $m['key'] ) {
1314 1375 $m['value'] = self::maybe_prepare_json_view_content( $m['value'] );
1315 1376 $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
1316 1377 } elseif ( 'frm_options' === $m['key'] ) {
1317 -
1318 1378 foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
1319 1379 if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
1320 1380 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1321 1381 }
@@ -1339,8 +1399,9 @@
1339 1399
1340 1400 if ( ! empty( $m['value']['timeline_options'] ) ) {
1341 1401 foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) {
1342 1402 foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) {
1403 + // @mago-expect lint:excessive-nesting
1343 1404 if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) {
1344 1405 $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ];
1345 1406 }
1346 1407 }
@@ -1448,10 +1509,11 @@
1448 1509 }
1449 1510
1450 1511 $editing = get_posts( $match_by );
1451 1512
1452 - if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
1453 - // set the id of the post to edit
1513 + // phpcs:ignore Universal.Operators.StrictComparisons
1514 + if ( $editing && current( $editing )->post_date == $post['post_date'] ) {
1515 + // Set the id of the post to edit
1454 1516 $post['ID'] = current( $editing )->ID;
1455 1517 }
1456 1518 }
1457 1519
@@ -1496,15 +1558,17 @@
1496 1558 * @param array $post
1497 1559 * @param int $post_id
1498 1560 */
1499 1561 private static function update_layout( &$post, $post_id ) {
1500 - if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1501 - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1502 - $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1562 + if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1563 + return;
1564 + }
1503 1565
1504 - if ( $listing_layout || $detail_layout ) {
1505 - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1506 - }
1566 + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1567 + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1568 +
1569 + if ( $listing_layout || $detail_layout ) {
1570 + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1507 1571 }
1508 1572 }
1509 1573
1510 1574 /**
@@ -1515,18 +1579,20 @@
1515 1579 private static function maybe_update_stylesheet( $imported ) {
1516 1580 $new_styles = ! empty( $imported['imported']['styles'] );
1517 1581 $updated_styles = ! empty( $imported['updated']['styles'] );
1518 1582
1519 - if ( $new_styles || $updated_styles ) {
1520 - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1521 - $frm_style = new FrmStyle();
1522 - $frm_style->update( 'default' );
1523 - }
1583 + if ( ! $new_styles && ! $updated_styles ) {
1584 + return;
1585 + }
1524 1586
1525 - foreach ( $imported['forms'] as $form_id ) {
1526 - self::update_custom_style_setting_after_import( $form_id );
1527 - }
1587 + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1588 + $frm_style = new FrmStyle();
1589 + $frm_style->update( 'default' );
1528 1590 }
1591 +
1592 + foreach ( $imported['forms'] as $form_id ) {
1593 + self::update_custom_style_setting_after_import( $form_id );
1594 + }
1529 1595 }
1530 1596
1531 1597 /**
1532 1598 * @param mixed $result
@@ -1531,8 +1597,10 @@
1531 1597 /**
1532 1598 * @param mixed $result
1533 1599 * @param string $message
1534 1600 * @param array $errors
1601 + *
1602 + * @return void
1535 1603 */
1536 1604 public static function parse_message( $result, &$message, &$errors ) {
1537 1605 if ( is_wp_error( $result ) ) {
1538 1606 $errors[] = $result->get_error_message();
@@ -1538,14 +1606,13 @@
1538 1606 $errors[] = $result->get_error_message();
1539 1607
1540 1608 // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1541 1609 // displaying duplicate error messages from $result->get_error_message()
1542 - $error_codes = $result->get_error_codes();
1543 1610 $error_details = array();
1544 1611
1545 - foreach ( $error_codes as $error_code ) {
1612 + foreach ( $result->get_error_codes() as $error_code ) {
1546 1613 // Clone WP_Error data because WP_Error removes all error messages and data
1547 - // associated with the specified error code when an item is removed.
1614 + // Associated with the specified error code when an item is removed.
1548 1615 // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1549 1616 $error_details = $result->get_error_data( $error_code );
1550 1617
1551 1618 if ( $error_code === 'SimpleXML_parse_error' ) {
@@ -1553,9 +1620,9 @@
1553 1620 break;
1554 1621 }
1555 1622 }
1556 1623
1557 - if ( ! empty( $error_details ) ) {
1624 + if ( $error_details ) {
1558 1625 $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1559 1626 }
1560 1627
1561 1628 return;
@@ -1578,9 +1645,9 @@
1578 1645 $message = '<ul>';
1579 1646
1580 1647 foreach ( $result as $type => $results ) {
1581 1648 if ( ! isset( $t_strings[ $type ] ) ) {
1582 - // only print imported and updated
1649 + // Only print imported and updated
1583 1650 continue;
1584 1651 }
1585 1652
1586 1653 $s_message = array();
@@ -1589,30 +1656,34 @@
1589 1656 self::item_count_message( $m, $k, $s_message );
1590 1657 unset( $k, $m );
1591 1658 }
1592 1659
1593 - if ( ! empty( $s_message ) ) {
1594 - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1595 - $message .= implode( ', ', $s_message );
1596 - $message .= '</li>';
1660 + if ( ! $s_message ) {
1661 + continue;
1597 1662 }
1598 - }
1599 1663
1664 + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1665 + $message .= implode( ', ', $s_message );
1666 + $message .= '</li>';
1667 + }//end foreach
1668 +
1600 1669 if ( $message === '<ul>' ) {
1601 1670 $message = '';
1602 1671 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1603 - } else {
1604 - self::add_form_link_to_message( $result, $message );
1605 1672
1606 - /**
1607 - * @since 5.3
1608 - *
1609 - * @param string $message
1610 - * @param array $result
1611 - */
1612 - $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1613 - $message .= '</ul>';
1673 + return;
1614 1674 }
1675 +
1676 + self::add_form_link_to_message( $result, $message );
1677 +
1678 + /**
1679 + * @since 5.3
1680 + *
1681 + * @param string $message
1682 + * @param array $result
1683 + */
1684 + $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1685 + $message .= '</ul>';
1615 1686 }
1616 1687
1617 1688 /**
1618 1689 * @param int $m
@@ -1617,8 +1688,10 @@
1617 1688 /**
1618 1689 * @param int $m
1619 1690 * @param string $type
1620 1691 * @param array<string> $s_message
1692 + *
1693 + * @return void
1621 1694 */
1622 1695 public static function item_count_message( $m, $type, &$s_message ) {
1623 1696 if ( ! $m ) {
1624 1697 return;
@@ -1644,21 +1717,22 @@
1644 1717 );
1645 1718
1646 1719 if ( isset( $strings[ $type ] ) ) {
1647 1720 $s_message[] = $strings[ $type ];
1648 - } else {
1649 - $string = ' ' . $m . ' ' . ucfirst( $type );
1721 + return;
1722 + }
1650 1723
1651 - /**
1652 - * @since 5.3
1653 - *
1654 - * @param string $string Message string for imported item.
1655 - * @param int $m Number of item that was imported.
1656 - * }
1657 - */
1658 - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1659 - $s_message[] = $string;
1660 - }
1724 + $string = ' ' . $m . ' ' . ucfirst( $type );
1725 +
1726 + /**
1727 + * @since 5.3
1728 + *
1729 + * @param string $string Message string for imported item.
1730 + * @param int $m Number of item that was imported.
1731 + * }
1732 + */
1733 + $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1734 + $s_message[] = $string;
1661 1735 }
1662 1736
1663 1737 /**
1664 1738 * If a single form was imported, include a link in the success message.
@@ -1676,14 +1750,15 @@
1676 1750 }
1677 1751
1678 1752 $primary_form = reset( $result['forms'] );
1679 1753
1680 - if ( ! empty( $primary_form ) ) {
1681 - $primary_form = FrmForm::getOne( $primary_form );
1682 - $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id;
1754 + if ( ! $primary_form ) {
1755 + return;
1756 + }
1683 1757
1684 - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1685 - }
1758 + $primary_form = FrmForm::getOne( $primary_form );
1759 + $form_id = ! empty( $primary_form->parent_form_id ) ? $primary_form->parent_form_id : $primary_form->id;
1760 + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1686 1761 }
1687 1762
1688 1763 /**
1689 1764 * Prepare the form options for export
@@ -1696,23 +1771,17 @@
1696 1771 */
1697 1772 public static function prepare_form_options_for_export( $options ) {
1698 1773 FrmAppHelper::unserialize_or_decode( $options );
1699 1774 // Change custom_style to the post_name instead of ID (1 may be a string)
1700 - $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style'];
1775 + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; // phpcs:ignore Universal.Operators.StrictComparisons
1701 1776
1702 1777 if ( $not_default ) {
1703 1778 global $wpdb;
1704 - $table = $wpdb->prefix . 'posts';
1705 - $where = array( 'ID' => $options['custom_style'] );
1706 - $select = 'post_name';
1707 -
1708 - $style_name = FrmDb::get_var( $table, $where, $select );
1709 -
1710 - if ( $style_name ) {
1711 - $options['custom_style'] = $style_name;
1712 - } else {
1713 - $options['custom_style'] = 1;
1714 - }
1779 + $table = $wpdb->prefix . 'posts';
1780 + $where = array( 'ID' => $options['custom_style'] );
1781 + $select = 'post_name';
1782 + $style_name = FrmDb::get_var( $table, $where, $select );
1783 + $options['custom_style'] = $style_name ? $style_name : 1;
1715 1784 }
1716 1785 self::remove_default_form_options( $options );
1717 1786 $options = serialize( $options );
1718 1787
@@ -1796,9 +1865,9 @@
1796 1865 *
1797 1866 * @return void
1798 1867 */
1799 1868 private static function add_image_src_to_image_options( $field ) {
1800 - if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1869 + if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) {
1801 1870 return;
1802 1871 }
1803 1872
1804 1873 $updated = false;
@@ -1809,12 +1878,14 @@
1809 1878 return;
1810 1879 }
1811 1880
1812 1881 foreach ( $options as $key => $option ) {
1813 - if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1814 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1815 - $updated = true;
1882 + if ( ! is_array( $option ) || empty( $option['image'] ) ) {
1883 + continue;
1816 1884 }
1885 +
1886 + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1887 + $updated = true;
1817 1888 }
1818 1889
1819 1890 if ( $updated ) {
1820 1891 $field->options = maybe_serialize( $options );
@@ -1833,8 +1904,9 @@
1833 1904
1834 1905 if ( empty( $defaults['custom_html'] ) ) {
1835 1906 $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type );
1836 1907 }
1908 +
1837 1909 return $defaults;
1838 1910 }
1839 1911
1840 1912 /**
@@ -1874,11 +1946,11 @@
1874 1946
1875 1947 $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
1876 1948 $default_html = $defaults[ $html_name ];
1877 1949
1950 + // phpcs:ignore Universal.Operators.StrictComparisons
1878 1951 if ( $old_html == $default_html ) {
1879 1952 unset( $options[ $html_name ] );
1880 -
1881 1953 return;
1882 1954 }
1883 1955
1884 1956 // Account for some of the older field default HTML.
@@ -1909,11 +1981,9 @@
1909 1981
1910 1982 self::remove_invalid_characters_from_xml( $str );
1911 1983
1912 1984 // $str = ent2ncr(esc_html( $str));
1913 - $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1914 -
1915 - return $str;
1985 + return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1916 1986 }
1917 1987
1918 1988 /**
1919 1989 * Remove <US> character (unit separator) from exported strings
@@ -1964,9 +2034,9 @@
1964 2034 * @param array $imported
1965 2035 * @param bool $switch
1966 2036 */
1967 2037 private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
1968 - if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
2038 + if ( empty( $form_options['create_post'] ) ) {
1969 2039 return;
1970 2040 }
1971 2041
1972 2042 $new_action = array(
@@ -2018,8 +2088,9 @@
2018 2088 $array_fields = array( 'post_category', 'post_custom_fields' );
2019 2089
2020 2090 $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
2021 2091 }
2092 +
2022 2093 $new_action['post_content'] = json_encode( $new_action['post_content'] );
2023 2094
2024 2095 $exists = get_posts(
2025 2096 array(
@@ -2029,13 +2100,15 @@
2029 2100 'numberposts' => 1,
2030 2101 )
2031 2102 );
2032 2103
2033 - if ( ! $exists ) {
2034 - // this isn't an email, but we need to use a class that will always be included
2035 - FrmDb::save_json_post( $new_action );
2036 - ++$imported['imported']['actions'];
2104 + if ( $exists ) {
2105 + return;
2037 2106 }
2107 +
2108 + // This isn't an email, but we need to use a class that will always be included
2109 + FrmDb::save_json_post( $new_action );
2110 + ++$imported['imported']['actions'];
2038 2111 }
2039 2112
2040 2113 /**
2041 2114 * Switch old field IDs for new field IDs in emails and post
@@ -2052,9 +2125,9 @@
2052 2125 global $frm_duplicate_ids;
2053 2126
2054 2127 // If there aren't IDs that were switched, end now
2055 2128 if ( ! $frm_duplicate_ids ) {
2056 - return;
2129 + return null;
2057 2130 }
2058 2131
2059 2132 // Get old IDs
2060 2133 $old = array_keys( $frm_duplicate_ids );
@@ -2063,11 +2136,13 @@
2063 2136 $new = array_values( $frm_duplicate_ids );
2064 2137
2065 2138 // Do a str_replace with each item to set the new IDs
2066 2139 foreach ( $post_content as $key => $setting ) {
2140 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2067 2141 if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
2068 2142 // Replace old IDs with new IDs
2069 2143 $post_content[ $key ] = str_replace( $old, $new, $setting );
2144 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2070 2145 } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
2071 2146 foreach ( $setting as $k => $val ) {
2072 2147 // Replace old IDs with new IDs
2073 2148 $post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
@@ -2104,9 +2179,9 @@
2104 2179
2105 2180 // Migrate autoresponders
2106 2181 self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
2107 2182
2108 - if ( empty( $notifications ) ) {
2183 + if ( ! $notifications ) {
2109 2184 return;
2110 2185 }
2111 2186
2112 2187 foreach ( $notifications as $new_notification ) {
@@ -2117,9 +2192,8 @@
2117 2192 $new_notification['post_status'] = 'publish';
2118 2193
2119 2194 // Switch field IDs and keys, if needed
2120 2195 if ( $switch ) {
2121 -
2122 2196 // Switch field IDs in email conditional logic
2123 2197 self::switch_email_condition_field_ids( $new_notification['post_content'] );
2124 2198
2125 2199 // Switch all other field IDs in email
@@ -2124,8 +2198,9 @@
2124 2198
2125 2199 // Switch all other field IDs in email
2126 2200 $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
2127 2201 }
2202 +
2128 2203 $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
2129 2204
2130 2205 $exists = get_posts(
2131 2206 array(
@@ -2135,9 +2210,9 @@
2135 2210 'numberposts' => 1,
2136 2211 )
2137 2212 );
2138 2213
2139 - if ( empty( $exists ) ) {
2214 + if ( ! $exists ) {
2140 2215 FrmDb::save_json_post( $new_notification );
2141 2216 ++$imported['imported']['actions'];
2142 2217 }
2143 2218 unset( $new_notification );
@@ -2163,8 +2238,9 @@
2163 2238 if ( isset( $form_options[ $index ] ) ) {
2164 2239 unset( $form_options[ $index ] );
2165 2240 }
2166 2241 }
2242 +
2167 2243 FrmForm::update( $form_id, array( 'options' => $form_options ) );
2168 2244 }
2169 2245
2170 2246 /**
@@ -2177,18 +2253,17 @@
2177 2253 * @return void
2178 2254 */
2179 2255 private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) {
2180 2256 if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) {
2181 - // add old settings into notification array
2257 + // Add old settings into notification array
2182 2258 $form_options['notification'] = array( 0 => $form_options );
2183 2259 } elseif ( isset( $form_options['notification']['email_to'] ) ) {
2184 - // make sure it's in the correct format
2260 + // Make sure it's in the correct format
2185 2261 $form_options['notification'] = array( 0 => $form_options['notification'] );
2186 2262 }
2187 2263
2188 2264 if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
2189 2265 foreach ( $form_options['notification'] as $email_key => $notification ) {
2190 -
2191 2266 $atts = array(
2192 2267 'email_to' => '',
2193 2268 'reply_to' => '',
2194 2269 'reply_to_name' => '',
@@ -2199,9 +2274,9 @@
2199 2274
2200 2275 // Format the email data
2201 2276 self::format_email_data( $atts, $notification );
2202 2277
2203 - if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
2278 + if ( ! empty( $notification['twilio'] ) ) {
2204 2279 do_action( 'frm_create_twilio_action', $atts, $notification );
2205 2280 }
2206 2281
2207 2282 // Setup the new notification
@@ -2234,11 +2309,11 @@
2234 2309 foreach ( $reply_fields as $f => $val ) {
2235 2310 if ( isset( $notification[ $f ] ) ) {
2236 2311 $atts[ $f ] = $notification[ $f ];
2237 2312
2238 - if ( 'custom' == $notification[ $f ] ) {
2313 + if ( 'custom' === $notification[ $f ] ) {
2239 2314 $atts[ $f ] = $notification[ 'cust_' . $f ];
2240 - } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
2315 + } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) {
2241 2316 $atts[ $f ] = '[' . $atts[ $f ] . ']';
2242 2317 }
2243 2318 }
2244 2319 unset( $f, $val );
@@ -2246,11 +2321,12 @@
2246 2321
2247 2322 // Format event
2248 2323 $atts['event'] = array( 'create' );
2249 2324
2325 + // phpcs:ignore Universal.Operators.StrictComparisons
2250 2326 if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
2251 2327 $atts['event'][] = 'update';
2252 - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
2328 + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons
2253 2329 $atts['event'] = array( 'update' );
2254 2330 }
2255 2331 }
2256 2332
@@ -2262,13 +2338,9 @@
2262 2338 *
2263 2339 * @return void
2264 2340 */
2265 2341 private static function format_email_to_data( &$atts, $notification ) {
2266 - if ( isset( $notification['email_to'] ) ) {
2267 - $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] );
2268 - } else {
2269 - $atts['email_to'] = array();
2270 - }
2342 + $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array();
2271 2343
2272 2344 if ( isset( $notification['also_email_to'] ) ) {
2273 2345 $email_fields = (array) $notification['also_email_to'];
2274 2346 $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
@@ -2275,22 +2347,24 @@
2275 2347 unset( $email_fields );
2276 2348 }
2277 2349
2278 2350 foreach ( $atts['email_to'] as $key => $email_field ) {
2279 -
2280 2351 if ( is_numeric( $email_field ) ) {
2281 2352 $atts['email_to'][ $key ] = '[' . $email_field . ']';
2282 2353 }
2283 2354
2284 - if ( strpos( $email_field, '|' ) ) {
2285 - $email_opt = explode( '|', $email_field );
2355 + if ( ! str_contains( $email_field, '|' ) ) {
2356 + continue;
2357 + }
2286 2358
2287 - if ( isset( $email_opt[0] ) ) {
2288 - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2289 - }
2290 - unset( $email_opt );
2359 + $email_opt = explode( '|', $email_field );
2360 +
2361 + if ( isset( $email_opt[0] ) ) {
2362 + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2291 2363 }
2364 + unset( $email_opt );
2292 2365 }
2366 +
2293 2367 $atts['email_to'] = implode( ', ', $atts['email_to'] );
2294 2368 }
2295 2369
2296 2370 /**
@@ -2317,9 +2391,9 @@
2317 2391
2318 2392 foreach ( $add_fields as $add_field ) {
2319 2393 if ( isset( $notification[ $add_field ] ) ) {
2320 2394 $new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
2321 - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
2395 + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) {
2322 2396 $new_notification['post_content'][ $add_field ] = 0;
2323 2397 } else {
2324 2398 $new_notification['post_content'][ $add_field ] = '';
2325 2399 }
@@ -2330,9 +2404,9 @@
2330 2404 $new_notification['post_content']['reply_to'] = $atts['reply_to'];
2331 2405
2332 2406 // Set from
2333 2407 if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) {
2334 - $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>';
2408 + $new_notification['post_content']['from'] = ( ! empty( $atts['reply_to_name'] ) ? $atts['reply_to_name'] : '[sitename]' ) . ' <' . ( ! empty( $atts['reply_to'] ) ? $atts['reply_to'] : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
2335 2409 }
2336 2410 }
2337 2411
2338 2412 /**
@@ -2343,15 +2417,17 @@
2343 2417 * @return void
2344 2418 */
2345 2419 private static function switch_email_condition_field_ids( &$post_content ) {
2346 2420 // Switch field IDs in conditional logic
2347 - if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) {
2348 - foreach ( $post_content['conditions'] as $email_key => $val ) {
2349 - if ( is_numeric( $email_key ) ) {
2350 - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2351 - }
2352 - unset( $email_key, $val );
2421 + if ( ! isset( $post_content['conditions'] ) || ! is_array( $post_content['conditions'] ) ) {
2422 + return;
2423 + }
2424 +
2425 + foreach ( $post_content['conditions'] as $email_key => $val ) {
2426 + if ( is_numeric( $email_key ) ) {
2427 + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2353 2428 }
2429 + unset( $email_key, $val );
2354 2430 }
2355 2431 }
2356 2432
2357 2433 /**
@@ -2363,52 +2439,54 @@
2363 2439 *
2364 2440 * @return void
2365 2441 */
2366 2442 private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) {
2367 - if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) {
2368 - // migrate autoresponder
2443 + if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) {
2444 + return;
2445 + }
2369 2446
2370 - $email_field = $form_options['ar_email_to'] ?? 0;
2447 + // Migrate autoresponder
2371 2448
2372 - if ( strpos( $email_field, '|' ) ) {
2373 - // data from entries field
2374 - $email_field = explode( '|', $email_field );
2449 + $email_field = $form_options['ar_email_to'] ?? 0;
2375 2450
2376 - if ( isset( $email_field[1] ) ) {
2377 - $email_field = $email_field[1];
2378 - }
2451 + if ( str_contains( $email_field, '|' ) ) {
2452 + // Data from entries field
2453 + $email_field = explode( '|', $email_field );
2454 +
2455 + if ( isset( $email_field[1] ) ) {
2456 + $email_field = $email_field[1];
2379 2457 }
2458 + }
2380 2459
2381 - if ( is_numeric( $email_field ) && ! empty( $email_field ) ) {
2382 - $email_field = '[' . $email_field . ']';
2383 - }
2460 + if ( is_numeric( $email_field ) && $email_field ) {
2461 + $email_field = '[' . $email_field . ']';
2462 + }
2384 2463
2385 - $notification = $form_options;
2386 - $new_notification2 = array(
2387 - 'post_content' => array(
2388 - 'email_message' => $notification['ar_email_message'],
2389 - 'email_subject' => $notification['ar_email_subject'] ?? '',
2390 - 'email_to' => $email_field,
2391 - 'plain_text' => $notification['ar_plain_text'] ?? 0,
2392 - 'inc_user_info' => 0,
2393 - ),
2394 - 'post_name' => $form_id . '_email_' . count( $notifications ),
2395 - );
2464 + $notification = $form_options;
2465 + $new_notification2 = array(
2466 + 'post_content' => array(
2467 + 'email_message' => $notification['ar_email_message'],
2468 + 'email_subject' => $notification['ar_email_subject'] ?? '',
2469 + 'email_to' => $email_field,
2470 + 'plain_text' => $notification['ar_plain_text'] ?? 0,
2471 + 'inc_user_info' => 0,
2472 + ),
2473 + 'post_name' => $form_id . '_email_' . count( $notifications ),
2474 + );
2396 2475
2397 - $reply_to = $notification['ar_reply_to'] ?? '';
2398 - $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2476 + $reply_to = $notification['ar_reply_to'] ?? '';
2477 + $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2399 2478
2400 - if ( ! empty( $reply_to ) ) {
2401 - $new_notification2['post_content']['reply_to'] = $reply_to;
2402 - }
2479 + if ( $reply_to ) {
2480 + $new_notification2['post_content']['reply_to'] = $reply_to;
2481 + }
2403 2482
2404 - if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) {
2405 - $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>';
2406 - }
2483 + if ( $reply_to || $reply_to_name ) {
2484 + $new_notification2['post_content']['from'] = ( $reply_to_name ? $reply_to_name : '[sitename]' ) . ' <' . ( $reply_to ? $reply_to : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
2485 + }
2407 2486
2408 - $notifications[] = $new_notification2;
2409 - unset( $new_notification2 );
2410 - }//end if
2487 + $notifications[] = $new_notification2;
2488 + unset( $new_notification2 );
2411 2489 }
2412 2490
2413 2491 /**
2414 2492 * PHP 8 backward compatibility for the libxml_disable_entity_loader function
@@ -2448,7 +2526,8 @@
2448 2526 if ( FrmAppHelper::pro_is_installed() ) {
2449 2527 // CSV Importing is only available in Pro.
2450 2528 $file_types[] = '.csv';
2451 2529 }
2530 +
2452 2531 return $file_types;
2453 2532 }
2454 2533 }