PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.4.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.4.3
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 +36 -207 6.5.25.4.3 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 *
@@ -303,15 +264,9 @@
303 264 }
304 265
305 266 $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form );
306 267
307 - $form = FrmForm::getAll( $edit_query, '', 1 );
308 - if ( is_object( $form ) && $form->status === 'trash' ) {
309 - FrmForm::destroy( $form->id );
310 - return false;
311 - }
312 -
313 - return $form;
268 + return FrmForm::getAll( $edit_query, '', 1 );
314 269 }
315 270
316 271 private static function update_form( $this_form, $form, &$imported ) {
317 272 $form_id = $this_form->id;
@@ -673,12 +628,8 @@
673 628 private static function create_imported_field( $f, &$imported ) {
674 629 $defaults = self::default_field_options( $f['type'] );
675 630 $f['field_options'] = array_merge( $defaults, $f['field_options'] );
676 631
677 - if ( is_callable( 'FrmProFileImport::import_attachment' ) ) {
678 - $f = self::maybe_import_images_for_options( $f );
679 - }
680 -
681 632 $new_id = FrmField::create( $f );
682 633 if ( $new_id != false ) {
683 634 $imported['imported']['fields'] ++;
684 635 do_action( 'frm_after_field_is_imported', $f, $new_id );
@@ -685,40 +636,8 @@
685 636 }
686 637 }
687 638
688 639 /**
689 - * Import images for radio buttons and checkboxes from image src if available.
690 - *
691 - * @since 5.5.1
692 - *
693 - * @param array $field
694 - * @return array
695 - */
696 - private static function maybe_import_images_for_options( $field ) {
697 - if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) {
698 - return $field;
699 - }
700 -
701 - foreach ( $field['options'] as $key => $option ) {
702 - if ( ! is_array( $option ) || empty( $option['src'] ) ) {
703 - continue;
704 - }
705 -
706 - $field_object = (object) $field;
707 - $field_object->type = 'file'; // Fake the file type as FrmProImport::import_attachment checks for file type.
708 -
709 - $image_id = FrmProFileImport::import_attachment( $option['src'], $field_object );
710 - unset( $field['options'][ $key ]['src'] ); // Remove the src from options as it isn't required after import.
711 -
712 - if ( is_numeric( $image_id ) ) {
713 - $field['options'][ $key ]['image'] = $image_id;
714 - }
715 - }
716 -
717 - return $field;
718 - }
719 -
720 - /**
721 640 * Fix field ids for fields that already exist prior to import.
722 641 *
723 642 * @since 4.07
724 643 * @param int $form_id
@@ -769,9 +688,9 @@
769 688 if ( ! isset( $form['options']['custom_style'] ) ) {
770 689 return;
771 690 }
772 691
773 - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
692 + if ( is_numeric( $form['options']['custom_style'] ) ) {
774 693 // Set to default
775 694 $form['options']['custom_style'] = 1;
776 695 } else {
777 696 // Replace the style name with the style ID on import
@@ -870,17 +789,17 @@
870 789 unset( $action_control );
871 790 } elseif ( $post['post_type'] === 'frm_styles' ) {
872 791 // Properly encode post content before inserting the post
873 792 $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] );
793 + $custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : '';
874 794 $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] );
875 795
876 796 // Create/update post now
877 797 $post_id = wp_insert_post( $post );
798 + self::maybe_update_custom_css( $custom_css );
878 799 } else {
879 800 if ( $post['post_type'] === 'frm_display' ) {
880 801 $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] );
881 - } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) {
882 - $post['post_parent'] = $imported['posts'][ $post['post_parent'] ];
883 802 }
884 803 // Create/update post now
885 804 $post_id = wp_insert_post( $post );
886 805 }
@@ -888,9 +807,9 @@
888 807 if ( ! is_numeric( $post_id ) ) {
889 808 continue;
890 809 }
891 810
892 - if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
811 + if ( false !== strpos( $post['post_content'], '[frm-display-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
893 812 $posts_with_shortcodes[ $post_id ] = $post;
894 813 }
895 814
896 815 self::update_postmeta( $post, $post_id );
@@ -922,50 +841,14 @@
922 841 self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids );
923 842 }
924 843 unset( $posts_with_shortcodes, $view_ids );
925 844
926 - if ( ! empty( $imported['forms'] ) ) {
927 - // clear imported forms style cache to make sure the new styles are applied to the forms
928 - self::clear_forms_style_caches( $imported['forms'] );
929 - }
930 -
931 845 self::maybe_update_stylesheet( $imported );
932 846
933 - flush_rewrite_rules();
934 -
935 847 return $imported;
936 848 }
937 849
938 850 /**
939 - * Clears styles from cache for imported forms
940 - *
941 - * @param array $imported_forms
942 - */
943 - private static function clear_forms_style_caches( $imported_forms ) {
944 - $where = array(
945 - 'id' => $imported_forms,
946 - 'options LIKE' => '"old_style"',
947 - );
948 - $forms = FrmDb::get_results( 'frm_forms', $where );
949 -
950 - foreach ( $forms as $form ) {
951 - FrmAppHelper::unserialize_or_decode( $form->options );
952 - if ( ! $form->options ) {
953 - continue;
954 - }
955 - $where = array(
956 - 'post_name' => $form->options['old_style'],
957 - 'post_type' => FrmStylesController::$post_type,
958 - );
959 -
960 - $select = 'ID';
961 -
962 - $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
963 - FrmDb::delete_cache_and_transient( $cache_key, 'post' );
964 - }
965 - }
966 -
967 - /**
968 851 * Replace old form ids with new ones in a string.
969 852 *
970 853 * @param string $string
971 854 * @param array<int> $form_ids new form ids indexed by old form id.
@@ -1220,40 +1103,24 @@
1220 1103 $post['ID'] = current( $editing )->ID;
1221 1104 }
1222 1105 }
1223 1106
1224 - /**
1225 - * @param array $post
1226 - * @param int $post_id
1227 - * @return void
1228 - */
1229 1107 private static function update_postmeta( &$post, $post_id ) {
1230 1108 foreach ( $post['postmeta'] as $k => $v ) {
1231 - switch ( $k ) {
1232 - case '_edit_last':
1233 - $v = FrmAppHelper::get_user_id_param( $v );
1234 - break;
1109 + if ( '_edit_last' == $k ) {
1110 + $v = FrmAppHelper::get_user_id_param( $v );
1111 + } elseif ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) {
1112 + // Change the attachment ID.
1113 + $field_obj = FrmFieldFactory::get_field_type( 'file' );
1114 + $v = $field_obj->get_file_id( $v );
1115 + }
1235 1116
1236 - case '_thumbnail_id':
1237 - if ( FrmAppHelper::pro_is_installed() ) {
1238 - // Change the attachment ID.
1239 - $field_obj = FrmFieldFactory::get_field_type( 'file' );
1240 - $v = $field_obj->get_file_id( $v );
1241 - }
1242 - break;
1243 -
1244 - case 'frm_dyncontent':
1245 - if ( is_array( $v ) ) {
1246 - $v = json_encode( $v );
1247 - }
1248 - break;
1249 -
1250 - case 'frm_param':
1251 - add_rewrite_endpoint( $v, EP_PERMALINK | EP_PAGES );
1252 - break;
1117 + if ( 'frm_dyncontent' === $k && is_array( $v ) ) {
1118 + $v = json_encode( $v );
1253 1119 }
1254 1120
1255 1121 update_post_meta( $post_id, $k, $v );
1122 +
1256 1123 unset( $k, $v );
1257 1124 }
1258 1125 }
1259 1126
@@ -1270,8 +1137,25 @@
1270 1137 }
1271 1138 }
1272 1139 }
1273 1140
1141 + /**
1142 + * If a template includes custom css, let's include it.
1143 + * The custom css is included on the default style.
1144 + *
1145 + * @since 2.03
1146 + */
1147 + private static function maybe_update_custom_css( $custom_css ) {
1148 + if ( empty( $custom_css ) ) {
1149 + return;
1150 + }
1151 +
1152 + $frm_style = new FrmStyle();
1153 + $default_style = $frm_style->get_default_style();
1154 + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css;
1155 + $frm_style->save( $default_style );
1156 + }
1157 +
1274 1158 private static function maybe_update_stylesheet( $imported ) {
1275 1159 $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] );
1276 1160 $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] );
1277 1161 if ( $new_styles || $updated_styles ) {
@@ -1290,29 +1174,8 @@
1290 1174 */
1291 1175 public static function parse_message( $result, &$message, &$errors ) {
1292 1176 if ( is_wp_error( $result ) ) {
1293 1177 $errors[] = $result->get_error_message();
1294 -
1295 - // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1296 - // displaying duplicate error messages from $result->get_error_message()
1297 - $error_codes = $result->get_error_codes();
1298 - $error_details = array();
1299 - foreach ( $error_codes as $error_code ) {
1300 - // Clone WP_Error data because WP_Error removes all error messages and data
1301 - // associated with the specified error code when an item is removed.
1302 - // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1303 - $error_details = $result->get_error_data( $error_code );
1304 - if ( $error_code === 'SimpleXML_parse_error' ) {
1305 - $result->remove( $error_code );
1306 - break;
1307 - }
1308 - }
1309 -
1310 - if ( ! empty( $error_details ) ) {
1311 - $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1312 - }
1313 -
1314 - return;
1315 1178 } elseif ( ! $result ) {
1316 1179 return;
1317 1180 }
1318 1181
@@ -1485,9 +1348,8 @@
1485 1348 * @since 3.06
1486 1349 */
1487 1350 public static function prepare_field_for_export( &$field ) {
1488 1351 self::remove_default_field_options( $field );
1489 - self::add_image_src_to_image_options( $field );
1490 1352 }
1491 1353
1492 1354 /**
1493 1355 * Remove defaults from field options too
@@ -1519,41 +1381,8 @@
1519 1381 $field->field_options = serialize( $options );
1520 1382 }
1521 1383
1522 1384 /**
1523 - * Add image "src" key to each image option so the image can be imported to another website.
1524 - *
1525 - * @since 5.5.1
1526 - *
1527 - * @param stdClass $field
1528 - * @return void
1529 - */
1530 - private static function add_image_src_to_image_options( $field ) {
1531 - if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1532 - return;
1533 - }
1534 -
1535 - $updated = false;
1536 - $options = $field->options;
1537 - FrmAppHelper::unserialize_or_decode( $options );
1538 -
1539 - if ( ! $options || ! is_array( $options ) ) {
1540 - return;
1541 - }
1542 -
1543 - foreach ( $options as $key => $option ) {
1544 - if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1545 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1546 - $updated = true;
1547 - }
1548 - }
1549 -
1550 - if ( $updated ) {
1551 - $field->options = maybe_serialize( $options );
1552 - }
1553 - }
1554 -
1555 - /**
1556 1385 * @since 3.06.03
1557 1386 */
1558 1387 private static function default_field_options( $type ) {
1559 1388 $defaults = FrmFieldsHelper::get_default_field_options( $type );
@@ -1606,9 +1435,9 @@
1606 1435 FrmAppHelper::unserialize_or_decode( $str );
1607 1436 if ( is_array( $str ) ) {
1608 1437 $str = json_encode( $str );
1609 1438 } elseif ( seems_utf8( $str ) === false ) {
1610 - $str = FrmAppHelper::maybe_utf8_encode( $str );
1439 + $str = utf8_encode( $str );
1611 1440 }
1612 1441
1613 1442 if ( is_numeric( $str ) ) {
1614 1443 return $str;