PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.2.06
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.2.06
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 +39 -338 6.35.2.06 View file →
@@ -32,25 +32,13 @@
32 32 define( 'WP_IMPORTING', true );
33 33 }
34 34
35 35 if ( ! class_exists( 'DOMDocument' ) ) {
36 - $error_message = sprintf(
37 - /* translators: 1: Documentation link */
38 - __( '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' ),
39 - '<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>'
40 - );
41 -
42 - return new WP_Error( 'SimpleXML_parse_error', $error_message, libxml_get_errors() );
36 + return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() );
43 37 }
44 38
45 - $xml_string = file_get_contents( $file );
46 - self::maybe_fix_xml( $xml_string );
47 -
48 - $dom = new DOMDocument();
49 -
50 - // LIBXML_COMPACT activates small nodes allocation optimization.
51 - // Use LIBXML_PARSEHUGE to avoid "parser error : internal error: Huge input lookup" for large (300MB) files.
52 - $success = $dom->loadXML( $xml_string, LIBXML_COMPACT | LIBXML_PARSEHUGE );
39 + $dom = new DOMDocument();
40 + $success = $dom->loadXML( file_get_contents( $file ) );
53 41 if ( ! $success ) {
54 42 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
55 43 }
56 44
@@ -69,35 +57,8 @@
69 57 return self::import_xml_now( $xml );
70 58 }
71 59
72 60 /**
73 - * @since 6.2.3
74 - *
75 - * @param string $xml_string
76 - * @return void
77 - */
78 - private static function maybe_fix_xml( &$xml_string ) {
79 - if ( '<?xml' !== substr( $xml_string, 0, 5 ) ) {
80 - // Some XML files have may have unexpected characters at the start.
81 - $xml_string = substr( $xml_string, strpos( $xml_string, '<?xml' ) );
82 - }
83 -
84 - // The Equity theme adds a <meta name="generator" content="Equity 1.7.13" /> tag using the "the_generator" filter.
85 - // Strip that out as it breaks the XML import.
86 - $channel_start_position = strpos( $xml_string, '<channel>' );
87 - $content_before_channel_tag = substr( $xml_string, 0, $channel_start_position );
88 - if ( 0 !== strpos( $content_before_channel_tag, '<meta name="generator" ' ) ) {
89 - $content_before_channel_tag = preg_replace(
90 - '/<meta\s+[^>]*name="generator"[^>]*\/>/i',
91 - '',
92 - $content_before_channel_tag,
93 - 1
94 - );
95 - $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position );
96 - }
97 - }
98 -
99 - /**
100 61 * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order
101 62 *
102 63 * @since 3.06
103 64 *
@@ -126,9 +87,9 @@
126 87
127 88 if ( ! isset( $imported['form_status'] ) || empty( $imported['form_status'] ) ) {
128 89 // Check for an error message in the XML.
129 90 if ( isset( $xml->Code ) && isset( $xml->Message ) ) { // phpcs:ignore WordPress.NamingConventions
130 - $imported['error'] = (string) $xml->Message; // phpcs:ignore WordPress.NamingConventions
91 + $imported['error'] = reset( $xml->Message ); // phpcs:ignore WordPress.NamingConventions
131 92 }
132 93 }
133 94
134 95 return $imported;
@@ -667,12 +628,8 @@
667 628 private static function create_imported_field( $f, &$imported ) {
668 629 $defaults = self::default_field_options( $f['type'] );
669 630 $f['field_options'] = array_merge( $defaults, $f['field_options'] );
670 631
671 - if ( is_callable( 'FrmProFileImport::import_attachment' ) ) {
672 - $f = self::maybe_import_images_for_options( $f );
673 - }
674 -
675 632 $new_id = FrmField::create( $f );
676 633 if ( $new_id != false ) {
677 634 $imported['imported']['fields'] ++;
678 635 do_action( 'frm_after_field_is_imported', $f, $new_id );
@@ -679,40 +636,8 @@
679 636 }
680 637 }
681 638
682 639 /**
683 - * Import images for radio buttons and checkboxes from image src if available.
684 - *
685 - * @since 5.5.1
686 - *
687 - * @param array $field
688 - * @return array
689 - */
690 - private static function maybe_import_images_for_options( $field ) {
691 - if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) {
692 - return $field;
693 - }
694 -
695 - foreach ( $field['options'] as $key => $option ) {
696 - if ( ! is_array( $option ) || empty( $option['src'] ) ) {
697 - continue;
698 - }
699 -
700 - $field_object = (object) $field;
701 - $field_object->type = 'file'; // Fake the file type as FrmProImport::import_attachment checks for file type.
702 -
703 - $image_id = FrmProFileImport::import_attachment( $option['src'], $field_object );
704 - unset( $field['options'][ $key ]['src'] ); // Remove the src from options as it isn't required after import.
705 -
706 - if ( is_numeric( $image_id ) ) {
707 - $field['options'][ $key ]['image'] = $image_id;
708 - }
709 - }
710 -
711 - return $field;
712 - }
713 -
714 - /**
715 640 * Fix field ids for fields that already exist prior to import.
716 641 *
717 642 * @since 4.07
718 643 * @param int $form_id
@@ -763,9 +688,9 @@
763 688 if ( ! isset( $form['options']['custom_style'] ) ) {
764 689 return;
765 690 }
766 691
767 - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
692 + if ( is_numeric( $form['options']['custom_style'] ) ) {
768 693 // Set to default
769 694 $form['options']['custom_style'] = 1;
770 695 } else {
771 696 // Replace the style name with the style ID on import
@@ -820,11 +745,8 @@
820 745 $form_action_type => 'actions',
821 746 'frm_styles' => 'styles',
822 747 );
823 748
824 - $view_ids = array();
825 - $posts_with_shortcodes = array();
826 -
827 749 foreach ( $views as $item ) {
828 750 $post = array(
829 751 'post_title' => (string) $item->title,
830 752 'post_name' => (string) $item->post_name,
@@ -847,10 +769,8 @@
847 769 'layout' => array(),
848 770 'tax_input' => array(),
849 771 );
850 772
851 - $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] );
852 -
853 773 $old_id = $post['post_id'];
854 774 self::populate_post( $post, $item, $imported );
855 775
856 776 unset( $item );
@@ -855,26 +775,26 @@
855 775
856 776 unset( $item );
857 777
858 778 $post_id = false;
859 - if ( $post['post_type'] === $form_action_type ) {
779 + if ( $post['post_type'] == $form_action_type ) {
860 780 $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] );
861 781 if ( $action_control && is_object( $action_control ) ) {
862 782 $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] );
863 783 }
864 784 unset( $action_control );
865 - } elseif ( $post['post_type'] === 'frm_styles' ) {
785 + } elseif ( $post['post_type'] == 'frm_styles' ) {
866 786 // Properly encode post content before inserting the post
867 787 $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] );
788 + $custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : '';
868 789 $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] );
869 790
870 791 // Create/update post now
871 792 $post_id = wp_insert_post( $post );
793 + self::maybe_update_custom_css( $custom_css );
872 794 } else {
873 795 if ( $post['post_type'] === 'frm_display' ) {
874 796 $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] );
875 - } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) {
876 - $post['post_parent'] = $imported['posts'][ $post['post_parent'] ];
877 797 }
878 798 // Create/update post now
879 799 $post_id = wp_insert_post( $post );
880 800 }
@@ -882,12 +802,8 @@
882 802 if ( ! is_numeric( $post_id ) ) {
883 803 continue;
884 804 }
885 805
886 - if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
887 - $posts_with_shortcodes[ $post_id ] = $post;
888 - }
889 -
890 806 self::update_postmeta( $post, $post_id );
891 807 self::update_layout( $post, $post_id );
892 808
893 809 $this_type = 'posts';
@@ -902,154 +818,19 @@
902 818 }
903 819
904 820 $imported['posts'][ (int) $old_id ] = $post_id;
905 821
906 - if ( $post['post_type'] === 'frm_display' ) {
907 - $view_ids[ (int) $old_id ] = $post_id;
908 - }
909 -
910 822 do_action( 'frm_after_import_view', $post_id, $post );
911 823
912 824 unset( $post );
913 825 }
914 826
915 - if ( $posts_with_shortcodes && $view_ids ) {
916 - self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids );
917 - }
918 - unset( $posts_with_shortcodes, $view_ids );
919 -
920 - if ( ! empty( $imported['forms'] ) ) {
921 - // clear imported forms style cache to make sure the new styles are applied to the forms
922 - self::clear_forms_style_caches( $imported['forms'] );
923 - }
924 -
925 827 self::maybe_update_stylesheet( $imported );
926 828
927 - flush_rewrite_rules();
928 -
929 829 return $imported;
930 830 }
931 831
932 832 /**
933 - * Clears styles from cache for imported forms
934 - *
935 - * @param array $imported_forms
936 - */
937 - private static function clear_forms_style_caches( $imported_forms ) {
938 - $where = array(
939 - 'id' => $imported_forms,
940 - 'options LIKE' => '"old_style"',
941 - );
942 - $forms = FrmDb::get_results( 'frm_forms', $where );
943 -
944 - foreach ( $forms as $form ) {
945 - FrmAppHelper::unserialize_or_decode( $form->options );
946 - if ( ! $form->options ) {
947 - continue;
948 - }
949 - $where = array(
950 - 'post_name' => $form->options['old_style'],
951 - 'post_type' => FrmStylesController::$post_type,
952 - );
953 -
954 - $select = 'ID';
955 -
956 - $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
957 - FrmDb::delete_cache_and_transient( $cache_key, 'post' );
958 - }
959 - }
960 -
961 - /**
962 - * Replace old form ids with new ones in a string.
963 - *
964 - * @param string $string
965 - * @param array<int> $form_ids new form ids indexed by old form id.
966 - * @return string
967 - */
968 - private static function switch_form_ids( $string, $form_ids ) {
969 - if ( false === strpos( $string, '[formidable' ) ) {
970 - // Skip string replacing if there are no form shortcodes in string.
971 - return $string;
972 - }
973 -
974 - foreach ( $form_ids as $old_id => $new_id ) {
975 - $string = str_replace(
976 - array(
977 - '[formidable id="' . $old_id . '"',
978 - '[formidable id=' . $old_id . ']',
979 - '[formidable id=' . $old_id . ' ',
980 - '"formId":"' . $old_id . '"',
981 - ),
982 - array(
983 - '[formidable id="' . $new_id . '"',
984 - '[formidable id=' . $new_id . ']',
985 - '[formidable id=' . $new_id . ' ',
986 - '"formId":"' . $new_id . '"',
987 - ),
988 - $string
989 - );
990 - }
991 -
992 - return $string;
993 - }
994 -
995 - /**
996 - * @param array<array> $posts_with_shortcodes indexed by current post id.
997 - * @param array<int> $view_ids new view ids indexed by old view id.
998 - * @return void
999 - */
1000 - private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) {
1001 - foreach ( $posts_with_shortcodes as $imported_post_id => $post ) {
1002 - $post_content = self::switch_view_ids( $post['post_content'], $view_ids );
1003 - if ( $post_content === $post['post_content'] ) {
1004 - continue;
1005 - }
1006 -
1007 - wp_update_post(
1008 - array(
1009 - 'ID' => $imported_post_id,
1010 - 'post_content' => $post_content,
1011 - )
1012 - );
1013 - }
1014 - }
1015 -
1016 - /**
1017 - * Replace old view ids with new ones in a string.
1018 - *
1019 - * @param string $string
1020 - * @param array<int> $view_ids new view ids indexed by old view id.
1021 - * @return string
1022 - */
1023 - private static function switch_view_ids( $string, $view_ids ) {
1024 - if ( false === strpos( $string, '[display-frm-data' ) ) {
1025 - // Skip string replacing if there are no view shortcodes in string.
1026 - return $string;
1027 - }
1028 -
1029 - foreach ( $view_ids as $old_id => $new_id ) {
1030 - $string = str_replace(
1031 - array(
1032 - '[display-frm-data id="' . $old_id . '"',
1033 - '[display-frm-data id=' . $old_id . ']',
1034 - '[display-frm-data id=' . $old_id . ' ',
1035 - '"viewId":"' . $old_id . '"',
1036 - ),
1037 - array(
1038 - '[display-frm-data id="' . $new_id . '"',
1039 - '[display-frm-data id=' . $new_id . ']',
1040 - '[display-frm-data id=' . $new_id . ' ',
1041 - '"viewId":"' . $new_id . '"',
1042 - ),
1043 - $string
1044 - );
1045 - unset( $old_id, $new_id );
1046 - }
1047 -
1048 - return $string;
1049 - }
1050 -
1051 - /**
1052 833 * @param string $content
1053 834 * @return string
1054 835 */
1055 836 private static function maybe_prepare_json_view_content( $content ) {
@@ -1214,40 +995,24 @@
1214 995 $post['ID'] = current( $editing )->ID;
1215 996 }
1216 997 }
1217 998
1218 - /**
1219 - * @param array $post
1220 - * @param int $post_id
1221 - * @return void
1222 - */
1223 999 private static function update_postmeta( &$post, $post_id ) {
1224 1000 foreach ( $post['postmeta'] as $k => $v ) {
1225 - switch ( $k ) {
1226 - case '_edit_last':
1227 - $v = FrmAppHelper::get_user_id_param( $v );
1228 - break;
1001 + if ( '_edit_last' == $k ) {
1002 + $v = FrmAppHelper::get_user_id_param( $v );
1003 + } elseif ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) {
1004 + // Change the attachment ID.
1005 + $field_obj = FrmFieldFactory::get_field_type( 'file' );
1006 + $v = $field_obj->get_file_id( $v );
1007 + }
1229 1008
1230 - case '_thumbnail_id':
1231 - if ( FrmAppHelper::pro_is_installed() ) {
1232 - // Change the attachment ID.
1233 - $field_obj = FrmFieldFactory::get_field_type( 'file' );
1234 - $v = $field_obj->get_file_id( $v );
1235 - }
1236 - break;
1237 -
1238 - case 'frm_dyncontent':
1239 - if ( is_array( $v ) ) {
1240 - $v = json_encode( $v );
1241 - }
1242 - break;
1243 -
1244 - case 'frm_param':
1245 - add_rewrite_endpoint( $v, EP_PERMALINK | EP_PAGES );
1246 - break;
1009 + if ( 'frm_dyncontent' === $k && is_array( $v ) ) {
1010 + $v = json_encode( $v );
1247 1011 }
1248 1012
1249 1013 update_post_meta( $post_id, $k, $v );
1014 +
1250 1015 unset( $k, $v );
1251 1016 }
1252 1017 }
1253 1018
@@ -1264,8 +1029,25 @@
1264 1029 }
1265 1030 }
1266 1031 }
1267 1032
1033 + /**
1034 + * If a template includes custom css, let's include it.
1035 + * The custom css is included on the default style.
1036 + *
1037 + * @since 2.03
1038 + */
1039 + private static function maybe_update_custom_css( $custom_css ) {
1040 + if ( empty( $custom_css ) ) {
1041 + return;
1042 + }
1043 +
1044 + $frm_style = new FrmStyle();
1045 + $default_style = $frm_style->get_default_style();
1046 + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css;
1047 + $frm_style->save( $default_style );
1048 + }
1049 +
1268 1050 private static function maybe_update_stylesheet( $imported ) {
1269 1051 $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] );
1270 1052 $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] );
1271 1053 if ( $new_styles || $updated_styles ) {
@@ -1284,29 +1066,8 @@
1284 1066 */
1285 1067 public static function parse_message( $result, &$message, &$errors ) {
1286 1068 if ( is_wp_error( $result ) ) {
1287 1069 $errors[] = $result->get_error_message();
1288 -
1289 - // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1290 - // displaying duplicate error messages from $result->get_error_message()
1291 - $error_codes = $result->get_error_codes();
1292 - $error_details = array();
1293 - foreach ( $error_codes as $error_code ) {
1294 - // Clone WP_Error data because WP_Error removes all error messages and data
1295 - // associated with the specified error code when an item is removed.
1296 - // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1297 - $error_details = $result->get_error_data( $error_code );
1298 - if ( $error_code === 'SimpleXML_parse_error' ) {
1299 - $result->remove( $error_code );
1300 - break;
1301 - }
1302 - }
1303 -
1304 - if ( ! empty( $error_details ) ) {
1305 - $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1306 - }
1307 -
1308 - return;
1309 1070 } elseif ( ! $result ) {
1310 1071 return;
1311 1072 }
1312 1073
@@ -1346,24 +1107,12 @@
1346 1107 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1347 1108 } else {
1348 1109 self::add_form_link_to_message( $result, $message );
1349 1110
1350 - /**
1351 - * @since 5.3
1352 - *
1353 - * @param string $message
1354 - * @param array $result
1355 - */
1356 - $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1357 1111 $message .= '</ul>';
1358 1112 }
1359 1113 }
1360 1114
1361 - /**
1362 - * @param int $m
1363 - * @param string $type
1364 - * @param array<string> $s_message
1365 - */
1366 1115 public static function item_count_message( $m, $type, &$s_message ) {
1367 1116 if ( ! $m ) {
1368 1117 return;
1369 1118 }
@@ -1377,9 +1126,9 @@
1377 1126 'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ),
1378 1127 /* translators: %1$s: Number of items */
1379 1128 'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ),
1380 1129 /* translators: %1$s: Number of items */
1381 - 'posts' => sprintf( _n( '%1$s Page/Post', '%1$s Pages/Posts', $m, 'formidable' ), $m ),
1130 + 'posts' => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ),
1382 1131 /* translators: %1$s: Number of items */
1383 1132 'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ),
1384 1133 /* translators: %1$s: Number of items */
1385 1134 'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ),
@@ -1386,23 +1135,9 @@
1386 1135 /* translators: %1$s: Number of items */
1387 1136 'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ),
1388 1137 );
1389 1138
1390 - if ( isset( $strings[ $type ] ) ) {
1391 - $s_message[] = $strings[ $type ];
1392 - } else {
1393 - $string = ' ' . $m . ' ' . ucfirst( $type );
1394 -
1395 - /**
1396 - * @since 5.3
1397 - *
1398 - * @param string $string Message string for imported item.
1399 - * @param int $m Number of item that was imported.
1400 - * }
1401 - */
1402 - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1403 - $s_message[] = $string;
1404 - }
1139 + $s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type );
1405 1140 }
1406 1141
1407 1142 /**
1408 1143 * If a single form was imported, include a link in the success message.
@@ -1479,9 +1214,8 @@
1479 1214 * @since 3.06
1480 1215 */
1481 1216 public static function prepare_field_for_export( &$field ) {
1482 1217 self::remove_default_field_options( $field );
1483 - self::add_image_src_to_image_options( $field );
1484 1218 }
1485 1219
1486 1220 /**
1487 1221 * Remove defaults from field options too
@@ -1513,41 +1247,8 @@
1513 1247 $field->field_options = serialize( $options );
1514 1248 }
1515 1249
1516 1250 /**
1517 - * Add image "src" key to each image option so the image can be imported to another website.
1518 - *
1519 - * @since 5.5.1
1520 - *
1521 - * @param stdClass $field
1522 - * @return void
1523 - */
1524 - private static function add_image_src_to_image_options( $field ) {
1525 - if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1526 - return;
1527 - }
1528 -
1529 - $updated = false;
1530 - $options = $field->options;
1531 - FrmAppHelper::unserialize_or_decode( $options );
1532 -
1533 - if ( ! $options || ! is_array( $options ) ) {
1534 - return;
1535 - }
1536 -
1537 - foreach ( $options as $key => $option ) {
1538 - if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1539 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1540 - $updated = true;
1541 - }
1542 - }
1543 -
1544 - if ( $updated ) {
1545 - $field->options = maybe_serialize( $options );
1546 - }
1547 - }
1548 -
1549 - /**
1550 1251 * @since 3.06.03
1551 1252 */
1552 1253 private static function default_field_options( $type ) {
1553 1254 $defaults = FrmFieldsHelper::get_default_field_options( $type );
@@ -1600,9 +1301,9 @@
1600 1301 FrmAppHelper::unserialize_or_decode( $str );
1601 1302 if ( is_array( $str ) ) {
1602 1303 $str = json_encode( $str );
1603 1304 } elseif ( seems_utf8( $str ) === false ) {
1604 - $str = FrmAppHelper::maybe_utf8_encode( $str );
1305 + $str = utf8_encode( $str );
1605 1306 }
1606 1307
1607 1308 if ( is_numeric( $str ) ) {
1608 1309 return $str;