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 -292 6.26 → 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,20 +767,20 @@
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 /**
736 - * If field settings have been migrated, update the values during import.
737 - *
738 783 * @since 4.0
739 784 *
740 785 * @param array $f
741 786 *
@@ -740,20 +785,8 @@
740 785 * @param array $f
741 786 *
742 787 * @return void
743 788 */
744 - private static function run_field_migrations( &$f ) {
745 - self::migrate_placeholders( $f );
746 - $f = apply_filters( 'frm_import_xml_field', $f );
747 - }
748 -
749 - /**
750 - * @since 4.0
751 - *
752 - * @param array $f
753 - *
754 - * @return void
755 - */
756 789 private static function migrate_placeholders( &$f ) {
757 790 $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' );
758 791
759 792 foreach ( $update_values as $k => $v ) {
@@ -796,28 +829,33 @@
796 829
797 830 // If a dropdown placeholder was used, remove the option so it won't be included twice.
798 831 $options = $field['options'];
799 832
800 - if ( $type === 'default_blank' && is_array( $options ) ) {
801 - $default_value = $field['default_value'];
833 + if ( $type !== 'default_blank' || ! is_array( $options ) ) {
834 + return $changes;
835 + }
802 836
803 - if ( is_array( $default_value ) ) {
804 - $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 );
805 846 }
806 847
807 - foreach ( $options as $opt_key => $opt ) {
808 - if ( is_array( $opt ) ) {
809 - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
810 - }
811 -
812 - if ( $opt == $default_value ) {
813 - unset( $options[ $opt_key ] );
814 - break;
815 - }
848 + // phpcs:ignore Universal.Operators.StrictComparisons
849 + if ( $opt == $default_value ) {
850 + unset( $options[ $opt_key ] );
851 + break;
816 852 }
817 - $changes['options'] = $options;
818 853 }
819 854
855 + $changes['options'] = $options;
856 + // end if
857 +
820 858 return $changes;
821 859 }
822 860
823 861 /**
@@ -838,12 +876,14 @@
838 876 }
839 877
840 878 $new_id = FrmField::create( $f );
841 879
842 - if ( $new_id != false ) {
843 - ++$imported['imported']['fields'];
844 - do_action( 'frm_after_field_is_imported', $f, $new_id );
880 + if ( ! $new_id ) {
881 + return;
845 882 }
883 +
884 + ++$imported['imported']['fields'];
885 + do_action( 'frm_after_field_is_imported', $f, $new_id );
846 886 }
847 887
848 888 /**
849 889 * Import images for radio buttons and checkboxes from image src if available.
@@ -909,15 +949,17 @@
909 949 $field = (array) $field;
910 950 $frm_duplicate_ids = $keys_by_original_field_id;
911 951 $after = FrmFieldsHelper::switch_field_ids( $field );
912 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 +
913 960 if ( $before['field_options'] !== $after['field_options'] ) {
914 - $frm_duplicate_ids = $field_id_by_key;
915 - $after = FrmFieldsHelper::switch_field_ids( $after );
916 -
917 - if ( $before['field_options'] !== $after['field_options'] ) {
918 - FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
919 - }
961 + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
920 962 }
921 963 }
922 964
923 965 $frm_duplicate_ids = $former_duplicate_ids;
@@ -940,29 +982,32 @@
940 982
941 983 if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
942 984 // Set to default
943 985 $form['options']['custom_style'] = 1;
944 - } else {
945 - // Replace the style name with the style ID on import
946 - global $wpdb;
947 - $table = $wpdb->prefix . 'posts';
948 - $where = array(
949 - 'post_name' => $form['options']['custom_style'],
950 - 'post_type' => 'frm_styles',
951 - );
952 - $select = 'ID';
953 - $style_id = FrmDb::get_var( $table, $where, $select );
954 986
955 - if ( $style_id ) {
956 - $form['options']['custom_style'] = $style_id;
957 - } else {
958 - // save the old style to maybe update after styles import
959 - $form['options']['old_style'] = $form['options']['custom_style'];
987 + return;
988 + }
960 989
961 - // Set to default
962 - $form['options']['custom_style'] = 1;
963 - }
964 - }//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;
965 1010 }
966 1011
967 1012 /**
968 1013 * After styles are imported, check for any forms that were linked
@@ -976,18 +1021,20 @@
976 1021 */
977 1022 private static function update_custom_style_setting_after_import( $form_id ) {
978 1023 $form = FrmForm::getOne( $form_id );
979 1024
980 - if ( $form && isset( $form->options['old_style'] ) ) {
981 - $form = (array) $form;
982 - $saved_style = $form['options']['custom_style'];
983 - $form['options']['custom_style'] = $form['options']['old_style'];
984 - self::update_custom_style_setting_on_import( $form );
985 - $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 + }
986 1028
987 - if ( $has_changed ) {
988 - FrmForm::update( $form['id'], $form );
989 - }
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 );
990 1037 }
991 1038 }
992 1039
993 1040 /**
@@ -997,9 +1044,9 @@
997 1044 * @param array $imported
998 1045 *
999 1046 * @return array
1000 1047 */
1001 - public static function import_xml_views( $views, $imported ) {
1048 + public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
1002 1049 $imported['posts'] = array();
1003 1050 $form_action_type = FrmFormActionsController::$action_post_type;
1004 1051
1005 1052 $post_types = array(
@@ -1078,9 +1125,9 @@
1078 1125 if ( ! is_numeric( $post_id ) ) {
1079 1126 continue;
1080 1127 }
1081 1128
1082 - 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' ) ) {
1083 1130 $posts_with_shortcodes[ $post_id ] = $post;
1084 1131 }
1085 1132
1086 1133 self::update_postmeta( $post, $post_id );
@@ -1091,9 +1138,9 @@
1091 1138 if ( isset( $post_types[ $post['post_type'] ] ) ) {
1092 1139 $this_type = $post_types[ $post['post_type'] ];
1093 1140 }
1094 1141
1095 - if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
1142 + if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) {
1096 1143 ++$imported['updated'][ $this_type ];
1097 1144 } else {
1098 1145 ++$imported['imported'][ $this_type ];
1099 1146 }
@@ -1114,9 +1161,9 @@
1114 1161 }
1115 1162 unset( $posts_with_shortcodes, $view_ids );
1116 1163
1117 1164 if ( ! empty( $imported['forms'] ) ) {
1118 - // 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
1119 1166 self::clear_forms_style_caches( $imported['forms'] );
1120 1167 }
1121 1168
1122 1169 self::maybe_update_stylesheet( $imported );
@@ -1151,10 +1198,9 @@
1151 1198 'post_name' => $form->options['old_style'],
1152 1199 'post_type' => FrmStylesController::$post_type,
1153 1200 );
1154 1201
1155 - $select = 'ID';
1156 -
1202 + $select = 'ID';
1157 1203 $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
1158 1204 FrmDb::delete_cache_and_transient( $cache_key, 'post' );
1159 1205 }
1160 1206 }
@@ -1167,9 +1213,9 @@
1167 1213 *
1168 1214 * @return string
1169 1215 */
1170 1216 private static function switch_form_ids( $string, $form_ids ) {
1171 - if ( false === strpos( $string, '[formidable' ) ) {
1217 + if ( ! str_contains( $string, '[formidable' ) ) {
1172 1218 // Skip string replacing if there are no form shortcodes in string.
1173 1219 return $string;
1174 1220 }
1175 1221
@@ -1225,9 +1271,9 @@
1225 1271 *
1226 1272 * @return string
1227 1273 */
1228 1274 private static function switch_view_ids( $string, $view_ids ) {
1229 - if ( false === strpos( $string, '[display-frm-data' ) ) {
1275 + if ( ! str_contains( $string, '[display-frm-data' ) ) {
1230 1276 // Skip string replacing if there are no view shortcodes in string.
1231 1277 return $string;
1232 1278 }
1233 1279
@@ -1263,8 +1309,9 @@
1263 1309
1264 1310 if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) {
1265 1311 return FrmAppHelper::prepare_and_encode( $maybe_decoded );
1266 1312 }
1313 +
1267 1314 return $content;
1268 1315 }
1269 1316
1270 1317 /**
@@ -1278,15 +1325,15 @@
1278 1325 if ( isset( $item->attachment_url ) ) {
1279 1326 $post['attachment_url'] = (string) $item->attachment_url;
1280 1327 }
1281 1328
1282 - if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1283 - // 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
1284 1331 $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
1285 1332 }
1286 1333
1287 1334 // Don't allow default styles to take over a site's default style
1288 - if ( 'frm_styles' == $post['post_type'] ) {
1335 + if ( 'frm_styles' === $post['post_type'] ) {
1289 1336 $post['menu_order'] = 0;
1290 1337 }
1291 1338
1292 1339 foreach ( $item->postmeta as $meta ) {
@@ -1308,9 +1355,9 @@
1308 1355 * @param array $post
1309 1356 * @param stdClass $meta
1310 1357 * @param array $imported
1311 1358 */
1312 - 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
1313 1360 global $frm_duplicate_ids;
1314 1361
1315 1362 $m = array(
1316 1363 'key' => (string) $meta->meta_key,
@@ -1322,14 +1369,13 @@
1322 1369 $m['value'] = $imported['forms'][ (int) $m['value'] ];
1323 1370 } else {
1324 1371 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1325 1372
1326 - if ( ! empty( $frm_duplicate_ids ) ) {
1373 + if ( $frm_duplicate_ids ) {
1327 1374 if ( 'frm_dyncontent' === $m['key'] ) {
1328 1375 $m['value'] = self::maybe_prepare_json_view_content( $m['value'] );
1329 1376 $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
1330 1377 } elseif ( 'frm_options' === $m['key'] ) {
1331 -
1332 1378 foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
1333 1379 if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
1334 1380 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1335 1381 }
@@ -1353,8 +1399,9 @@
1353 1399
1354 1400 if ( ! empty( $m['value']['timeline_options'] ) ) {
1355 1401 foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) {
1356 1402 foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) {
1403 + // @mago-expect lint:excessive-nesting
1357 1404 if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) {
1358 1405 $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ];
1359 1406 }
1360 1407 }
@@ -1462,10 +1509,11 @@
1462 1509 }
1463 1510
1464 1511 $editing = get_posts( $match_by );
1465 1512
1466 - if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
1467 - // 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
1468 1516 $post['ID'] = current( $editing )->ID;
1469 1517 }
1470 1518 }
1471 1519
@@ -1510,15 +1558,17 @@
1510 1558 * @param array $post
1511 1559 * @param int $post_id
1512 1560 */
1513 1561 private static function update_layout( &$post, $post_id ) {
1514 - if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1515 - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1516 - $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 + }
1517 1565
1518 - if ( $listing_layout || $detail_layout ) {
1519 - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1520 - }
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 );
1521 1571 }
1522 1572 }
1523 1573
1524 1574 /**
@@ -1529,18 +1579,20 @@
1529 1579 private static function maybe_update_stylesheet( $imported ) {
1530 1580 $new_styles = ! empty( $imported['imported']['styles'] );
1531 1581 $updated_styles = ! empty( $imported['updated']['styles'] );
1532 1582
1533 - if ( $new_styles || $updated_styles ) {
1534 - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1535 - $frm_style = new FrmStyle();
1536 - $frm_style->update( 'default' );
1537 - }
1583 + if ( ! $new_styles && ! $updated_styles ) {
1584 + return;
1585 + }
1538 1586
1539 - foreach ( $imported['forms'] as $form_id ) {
1540 - self::update_custom_style_setting_after_import( $form_id );
1541 - }
1587 + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1588 + $frm_style = new FrmStyle();
1589 + $frm_style->update( 'default' );
1542 1590 }
1591 +
1592 + foreach ( $imported['forms'] as $form_id ) {
1593 + self::update_custom_style_setting_after_import( $form_id );
1594 + }
1543 1595 }
1544 1596
1545 1597 /**
1546 1598 * @param mixed $result
@@ -1545,8 +1597,10 @@
1545 1597 /**
1546 1598 * @param mixed $result
1547 1599 * @param string $message
1548 1600 * @param array $errors
1601 + *
1602 + * @return void
1549 1603 */
1550 1604 public static function parse_message( $result, &$message, &$errors ) {
1551 1605 if ( is_wp_error( $result ) ) {
1552 1606 $errors[] = $result->get_error_message();
@@ -1552,14 +1606,13 @@
1552 1606 $errors[] = $result->get_error_message();
1553 1607
1554 1608 // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1555 1609 // displaying duplicate error messages from $result->get_error_message()
1556 - $error_codes = $result->get_error_codes();
1557 1610 $error_details = array();
1558 1611
1559 - foreach ( $error_codes as $error_code ) {
1612 + foreach ( $result->get_error_codes() as $error_code ) {
1560 1613 // Clone WP_Error data because WP_Error removes all error messages and data
1561 - // associated with the specified error code when an item is removed.
1614 + // Associated with the specified error code when an item is removed.
1562 1615 // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1563 1616 $error_details = $result->get_error_data( $error_code );
1564 1617
1565 1618 if ( $error_code === 'SimpleXML_parse_error' ) {
@@ -1567,9 +1620,9 @@
1567 1620 break;
1568 1621 }
1569 1622 }
1570 1623
1571 - if ( ! empty( $error_details ) ) {
1624 + if ( $error_details ) {
1572 1625 $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1573 1626 }
1574 1627
1575 1628 return;
@@ -1592,9 +1645,9 @@
1592 1645 $message = '<ul>';
1593 1646
1594 1647 foreach ( $result as $type => $results ) {
1595 1648 if ( ! isset( $t_strings[ $type ] ) ) {
1596 - // only print imported and updated
1649 + // Only print imported and updated
1597 1650 continue;
1598 1651 }
1599 1652
1600 1653 $s_message = array();
@@ -1603,30 +1656,34 @@
1603 1656 self::item_count_message( $m, $k, $s_message );
1604 1657 unset( $k, $m );
1605 1658 }
1606 1659
1607 - if ( ! empty( $s_message ) ) {
1608 - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1609 - $message .= implode( ', ', $s_message );
1610 - $message .= '</li>';
1660 + if ( ! $s_message ) {
1661 + continue;
1611 1662 }
1612 - }
1613 1663
1664 + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1665 + $message .= implode( ', ', $s_message );
1666 + $message .= '</li>';
1667 + }//end foreach
1668 +
1614 1669 if ( $message === '<ul>' ) {
1615 1670 $message = '';
1616 1671 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1617 - } else {
1618 - self::add_form_link_to_message( $result, $message );
1619 1672
1620 - /**
1621 - * @since 5.3
1622 - *
1623 - * @param string $message
1624 - * @param array $result
1625 - */
1626 - $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1627 - $message .= '</ul>';
1673 + return;
1628 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>';
1629 1686 }
1630 1687
1631 1688 /**
1632 1689 * @param int $m
@@ -1631,8 +1688,10 @@
1631 1688 /**
1632 1689 * @param int $m
1633 1690 * @param string $type
1634 1691 * @param array<string> $s_message
1692 + *
1693 + * @return void
1635 1694 */
1636 1695 public static function item_count_message( $m, $type, &$s_message ) {
1637 1696 if ( ! $m ) {
1638 1697 return;
@@ -1658,21 +1717,22 @@
1658 1717 );
1659 1718
1660 1719 if ( isset( $strings[ $type ] ) ) {
1661 1720 $s_message[] = $strings[ $type ];
1662 - } else {
1663 - $string = ' ' . $m . ' ' . ucfirst( $type );
1721 + return;
1722 + }
1664 1723
1665 - /**
1666 - * @since 5.3
1667 - *
1668 - * @param string $string Message string for imported item.
1669 - * @param int $m Number of item that was imported.
1670 - * }
1671 - */
1672 - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1673 - $s_message[] = $string;
1674 - }
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;
1675 1735 }
1676 1736
1677 1737 /**
1678 1738 * If a single form was imported, include a link in the success message.
@@ -1690,14 +1750,15 @@
1690 1750 }
1691 1751
1692 1752 $primary_form = reset( $result['forms'] );
1693 1753
1694 - if ( ! empty( $primary_form ) ) {
1695 - $primary_form = FrmForm::getOne( $primary_form );
1696 - $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id;
1754 + if ( ! $primary_form ) {
1755 + return;
1756 + }
1697 1757
1698 - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1699 - }
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>';
1700 1761 }
1701 1762
1702 1763 /**
1703 1764 * Prepare the form options for export
@@ -1710,23 +1771,17 @@
1710 1771 */
1711 1772 public static function prepare_form_options_for_export( $options ) {
1712 1773 FrmAppHelper::unserialize_or_decode( $options );
1713 1774 // Change custom_style to the post_name instead of ID (1 may be a string)
1714 - $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
1715 1776
1716 1777 if ( $not_default ) {
1717 1778 global $wpdb;
1718 - $table = $wpdb->prefix . 'posts';
1719 - $where = array( 'ID' => $options['custom_style'] );
1720 - $select = 'post_name';
1721 -
1722 - $style_name = FrmDb::get_var( $table, $where, $select );
1723 -
1724 - if ( $style_name ) {
1725 - $options['custom_style'] = $style_name;
1726 - } else {
1727 - $options['custom_style'] = 1;
1728 - }
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;
1729 1784 }
1730 1785 self::remove_default_form_options( $options );
1731 1786 $options = serialize( $options );
1732 1787
@@ -1810,9 +1865,9 @@
1810 1865 *
1811 1866 * @return void
1812 1867 */
1813 1868 private static function add_image_src_to_image_options( $field ) {
1814 - if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1869 + if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) {
1815 1870 return;
1816 1871 }
1817 1872
1818 1873 $updated = false;
@@ -1823,12 +1878,14 @@
1823 1878 return;
1824 1879 }
1825 1880
1826 1881 foreach ( $options as $key => $option ) {
1827 - if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1828 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1829 - $updated = true;
1882 + if ( ! is_array( $option ) || empty( $option['image'] ) ) {
1883 + continue;
1830 1884 }
1885 +
1886 + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1887 + $updated = true;
1831 1888 }
1832 1889
1833 1890 if ( $updated ) {
1834 1891 $field->options = maybe_serialize( $options );
@@ -1847,8 +1904,9 @@
1847 1904
1848 1905 if ( empty( $defaults['custom_html'] ) ) {
1849 1906 $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type );
1850 1907 }
1908 +
1851 1909 return $defaults;
1852 1910 }
1853 1911
1854 1912 /**
@@ -1888,11 +1946,11 @@
1888 1946
1889 1947 $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
1890 1948 $default_html = $defaults[ $html_name ];
1891 1949
1950 + // phpcs:ignore Universal.Operators.StrictComparisons
1892 1951 if ( $old_html == $default_html ) {
1893 1952 unset( $options[ $html_name ] );
1894 -
1895 1953 return;
1896 1954 }
1897 1955
1898 1956 // Account for some of the older field default HTML.
@@ -1923,11 +1981,9 @@
1923 1981
1924 1982 self::remove_invalid_characters_from_xml( $str );
1925 1983
1926 1984 // $str = ent2ncr(esc_html( $str));
1927 - $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1928 -
1929 - return $str;
1985 + return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1930 1986 }
1931 1987
1932 1988 /**
1933 1989 * Remove <US> character (unit separator) from exported strings
@@ -1978,9 +2034,9 @@
1978 2034 * @param array $imported
1979 2035 * @param bool $switch
1980 2036 */
1981 2037 private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
1982 - if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
2038 + if ( empty( $form_options['create_post'] ) ) {
1983 2039 return;
1984 2040 }
1985 2041
1986 2042 $new_action = array(
@@ -2032,8 +2088,9 @@
2032 2088 $array_fields = array( 'post_category', 'post_custom_fields' );
2033 2089
2034 2090 $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
2035 2091 }
2092 +
2036 2093 $new_action['post_content'] = json_encode( $new_action['post_content'] );
2037 2094
2038 2095 $exists = get_posts(
2039 2096 array(
@@ -2043,13 +2100,15 @@
2043 2100 'numberposts' => 1,
2044 2101 )
2045 2102 );
2046 2103
2047 - if ( ! $exists ) {
2048 - // this isn't an email, but we need to use a class that will always be included
2049 - FrmDb::save_json_post( $new_action );
2050 - ++$imported['imported']['actions'];
2104 + if ( $exists ) {
2105 + return;
2051 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'];
2052 2111 }
2053 2112
2054 2113 /**
2055 2114 * Switch old field IDs for new field IDs in emails and post
@@ -2066,9 +2125,9 @@
2066 2125 global $frm_duplicate_ids;
2067 2126
2068 2127 // If there aren't IDs that were switched, end now
2069 2128 if ( ! $frm_duplicate_ids ) {
2070 - return;
2129 + return null;
2071 2130 }
2072 2131
2073 2132 // Get old IDs
2074 2133 $old = array_keys( $frm_duplicate_ids );
@@ -2077,11 +2136,13 @@
2077 2136 $new = array_values( $frm_duplicate_ids );
2078 2137
2079 2138 // Do a str_replace with each item to set the new IDs
2080 2139 foreach ( $post_content as $key => $setting ) {
2140 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2081 2141 if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
2082 2142 // Replace old IDs with new IDs
2083 2143 $post_content[ $key ] = str_replace( $old, $new, $setting );
2144 + // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2084 2145 } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
2085 2146 foreach ( $setting as $k => $val ) {
2086 2147 // Replace old IDs with new IDs
2087 2148 $post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
@@ -2118,9 +2179,9 @@
2118 2179
2119 2180 // Migrate autoresponders
2120 2181 self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
2121 2182
2122 - if ( empty( $notifications ) ) {
2183 + if ( ! $notifications ) {
2123 2184 return;
2124 2185 }
2125 2186
2126 2187 foreach ( $notifications as $new_notification ) {
@@ -2131,9 +2192,8 @@
2131 2192 $new_notification['post_status'] = 'publish';
2132 2193
2133 2194 // Switch field IDs and keys, if needed
2134 2195 if ( $switch ) {
2135 -
2136 2196 // Switch field IDs in email conditional logic
2137 2197 self::switch_email_condition_field_ids( $new_notification['post_content'] );
2138 2198
2139 2199 // Switch all other field IDs in email
@@ -2138,8 +2198,9 @@
2138 2198
2139 2199 // Switch all other field IDs in email
2140 2200 $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
2141 2201 }
2202 +
2142 2203 $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
2143 2204
2144 2205 $exists = get_posts(
2145 2206 array(
@@ -2149,9 +2210,9 @@
2149 2210 'numberposts' => 1,
2150 2211 )
2151 2212 );
2152 2213
2153 - if ( empty( $exists ) ) {
2214 + if ( ! $exists ) {
2154 2215 FrmDb::save_json_post( $new_notification );
2155 2216 ++$imported['imported']['actions'];
2156 2217 }
2157 2218 unset( $new_notification );
@@ -2177,8 +2238,9 @@
2177 2238 if ( isset( $form_options[ $index ] ) ) {
2178 2239 unset( $form_options[ $index ] );
2179 2240 }
2180 2241 }
2242 +
2181 2243 FrmForm::update( $form_id, array( 'options' => $form_options ) );
2182 2244 }
2183 2245
2184 2246 /**
@@ -2191,18 +2253,17 @@
2191 2253 * @return void
2192 2254 */
2193 2255 private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) {
2194 2256 if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) {
2195 - // add old settings into notification array
2257 + // Add old settings into notification array
2196 2258 $form_options['notification'] = array( 0 => $form_options );
2197 2259 } elseif ( isset( $form_options['notification']['email_to'] ) ) {
2198 - // make sure it's in the correct format
2260 + // Make sure it's in the correct format
2199 2261 $form_options['notification'] = array( 0 => $form_options['notification'] );
2200 2262 }
2201 2263
2202 2264 if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
2203 2265 foreach ( $form_options['notification'] as $email_key => $notification ) {
2204 -
2205 2266 $atts = array(
2206 2267 'email_to' => '',
2207 2268 'reply_to' => '',
2208 2269 'reply_to_name' => '',
@@ -2213,9 +2274,9 @@
2213 2274
2214 2275 // Format the email data
2215 2276 self::format_email_data( $atts, $notification );
2216 2277
2217 - if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
2278 + if ( ! empty( $notification['twilio'] ) ) {
2218 2279 do_action( 'frm_create_twilio_action', $atts, $notification );
2219 2280 }
2220 2281
2221 2282 // Setup the new notification
@@ -2248,11 +2309,11 @@
2248 2309 foreach ( $reply_fields as $f => $val ) {
2249 2310 if ( isset( $notification[ $f ] ) ) {
2250 2311 $atts[ $f ] = $notification[ $f ];
2251 2312
2252 - if ( 'custom' == $notification[ $f ] ) {
2313 + if ( 'custom' === $notification[ $f ] ) {
2253 2314 $atts[ $f ] = $notification[ 'cust_' . $f ];
2254 - } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
2315 + } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) {
2255 2316 $atts[ $f ] = '[' . $atts[ $f ] . ']';
2256 2317 }
2257 2318 }
2258 2319 unset( $f, $val );
@@ -2260,11 +2321,12 @@
2260 2321
2261 2322 // Format event
2262 2323 $atts['event'] = array( 'create' );
2263 2324
2325 + // phpcs:ignore Universal.Operators.StrictComparisons
2264 2326 if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
2265 2327 $atts['event'][] = 'update';
2266 - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
2328 + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons
2267 2329 $atts['event'] = array( 'update' );
2268 2330 }
2269 2331 }
2270 2332
@@ -2276,13 +2338,9 @@
2276 2338 *
2277 2339 * @return void
2278 2340 */
2279 2341 private static function format_email_to_data( &$atts, $notification ) {
2280 - if ( isset( $notification['email_to'] ) ) {
2281 - $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] );
2282 - } else {
2283 - $atts['email_to'] = array();
2284 - }
2342 + $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array();
2285 2343
2286 2344 if ( isset( $notification['also_email_to'] ) ) {
2287 2345 $email_fields = (array) $notification['also_email_to'];
2288 2346 $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
@@ -2289,22 +2347,24 @@
2289 2347 unset( $email_fields );
2290 2348 }
2291 2349
2292 2350 foreach ( $atts['email_to'] as $key => $email_field ) {
2293 -
2294 2351 if ( is_numeric( $email_field ) ) {
2295 2352 $atts['email_to'][ $key ] = '[' . $email_field . ']';
2296 2353 }
2297 2354
2298 - if ( strpos( $email_field, '|' ) ) {
2299 - $email_opt = explode( '|', $email_field );
2355 + if ( ! str_contains( $email_field, '|' ) ) {
2356 + continue;
2357 + }
2300 2358
2301 - if ( isset( $email_opt[0] ) ) {
2302 - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2303 - }
2304 - 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] . ']';
2305 2363 }
2364 + unset( $email_opt );
2306 2365 }
2366 +
2307 2367 $atts['email_to'] = implode( ', ', $atts['email_to'] );
2308 2368 }
2309 2369
2310 2370 /**
@@ -2331,9 +2391,9 @@
2331 2391
2332 2392 foreach ( $add_fields as $add_field ) {
2333 2393 if ( isset( $notification[ $add_field ] ) ) {
2334 2394 $new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
2335 - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
2395 + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) {
2336 2396 $new_notification['post_content'][ $add_field ] = 0;
2337 2397 } else {
2338 2398 $new_notification['post_content'][ $add_field ] = '';
2339 2399 }
@@ -2344,9 +2404,9 @@
2344 2404 $new_notification['post_content']['reply_to'] = $atts['reply_to'];
2345 2405
2346 2406 // Set from
2347 2407 if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) {
2348 - $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
2349 2409 }
2350 2410 }
2351 2411
2352 2412 /**
@@ -2357,15 +2417,17 @@
2357 2417 * @return void
2358 2418 */
2359 2419 private static function switch_email_condition_field_ids( &$post_content ) {
2360 2420 // Switch field IDs in conditional logic
2361 - if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) {
2362 - foreach ( $post_content['conditions'] as $email_key => $val ) {
2363 - if ( is_numeric( $email_key ) ) {
2364 - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2365 - }
2366 - 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' ) );
2367 2428 }
2429 + unset( $email_key, $val );
2368 2430 }
2369 2431 }
2370 2432
2371 2433 /**
@@ -2377,52 +2439,54 @@
2377 2439 *
2378 2440 * @return void
2379 2441 */
2380 2442 private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) {
2381 - if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) {
2382 - // migrate autoresponder
2443 + if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) {
2444 + return;
2445 + }
2383 2446
2384 - $email_field = $form_options['ar_email_to'] ?? 0;
2447 + // Migrate autoresponder
2385 2448
2386 - if ( strpos( $email_field, '|' ) ) {
2387 - // data from entries field
2388 - $email_field = explode( '|', $email_field );
2449 + $email_field = $form_options['ar_email_to'] ?? 0;
2389 2450
2390 - if ( isset( $email_field[1] ) ) {
2391 - $email_field = $email_field[1];
2392 - }
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];
2393 2457 }
2458 + }
2394 2459
2395 - if ( is_numeric( $email_field ) && ! empty( $email_field ) ) {
2396 - $email_field = '[' . $email_field . ']';
2397 - }
2460 + if ( is_numeric( $email_field ) && $email_field ) {
2461 + $email_field = '[' . $email_field . ']';
2462 + }
2398 2463
2399 - $notification = $form_options;
2400 - $new_notification2 = array(
2401 - 'post_content' => array(
2402 - 'email_message' => $notification['ar_email_message'],
2403 - 'email_subject' => $notification['ar_email_subject'] ?? '',
2404 - 'email_to' => $email_field,
2405 - 'plain_text' => $notification['ar_plain_text'] ?? 0,
2406 - 'inc_user_info' => 0,
2407 - ),
2408 - 'post_name' => $form_id . '_email_' . count( $notifications ),
2409 - );
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 + );
2410 2475
2411 - $reply_to = $notification['ar_reply_to'] ?? '';
2412 - $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2476 + $reply_to = $notification['ar_reply_to'] ?? '';
2477 + $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2413 2478
2414 - if ( ! empty( $reply_to ) ) {
2415 - $new_notification2['post_content']['reply_to'] = $reply_to;
2416 - }
2479 + if ( $reply_to ) {
2480 + $new_notification2['post_content']['reply_to'] = $reply_to;
2481 + }
2417 2482
2418 - if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) {
2419 - $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>';
2420 - }
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 + }
2421 2486
2422 - $notifications[] = $new_notification2;
2423 - unset( $new_notification2 );
2424 - }//end if
2487 + $notifications[] = $new_notification2;
2488 + unset( $new_notification2 );
2425 2489 }
2426 2490
2427 2491 /**
2428 2492 * PHP 8 backward compatibility for the libxml_disable_entity_loader function
@@ -2462,7 +2526,8 @@
2462 2526 if ( FrmAppHelper::pro_is_installed() ) {
2463 2527 // CSV Importing is only available in Pro.
2464 2528 $file_types[] = '.csv';
2465 2529 }
2530 +
2466 2531 return $file_types;
2467 2532 }
2468 2533 }