PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 5.3
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v5.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 +35 -200 6.3.1 → 5.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 *
@@ -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
@@ -864,17 +789,17 @@
864 789 unset( $action_control );
865 790 } elseif ( $post['post_type'] === 'frm_styles' ) {
866 791 // Properly encode post content before inserting the post
867 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'] : '';
868 794 $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] );
869 795
870 796 // Create/update post now
871 797 $post_id = wp_insert_post( $post );
798 + self::maybe_update_custom_css( $custom_css );
872 799 } else {
873 800 if ( $post['post_type'] === 'frm_display' ) {
874 801 $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 802 }
878 803 // Create/update post now
879 804 $post_id = wp_insert_post( $post );
880 805 }
@@ -882,9 +807,9 @@
882 807 if ( ! is_numeric( $post_id ) ) {
883 808 continue;
884 809 }
885 810
886 - 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' ) ) {
887 812 $posts_with_shortcodes[ $post_id ] = $post;
888 813 }
889 814
890 815 self::update_postmeta( $post, $post_id );
@@ -916,50 +841,14 @@
916 841 self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids );
917 842 }
918 843 unset( $posts_with_shortcodes, $view_ids );
919 844
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 845 self::maybe_update_stylesheet( $imported );
926 846
927 - flush_rewrite_rules();
928 -
929 847 return $imported;
930 848 }
931 849
932 850 /**
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 851 * Replace old form ids with new ones in a string.
963 852 *
964 853 * @param string $string
965 854 * @param array<int> $form_ids new form ids indexed by old form id.
@@ -1214,40 +1103,24 @@
1214 1103 $post['ID'] = current( $editing )->ID;
1215 1104 }
1216 1105 }
1217 1106
1218 - /**
1219 - * @param array $post
1220 - * @param int $post_id
1221 - * @return void
1222 - */
1223 1107 private static function update_postmeta( &$post, $post_id ) {
1224 1108 foreach ( $post['postmeta'] as $k => $v ) {
1225 - switch ( $k ) {
1226 - case '_edit_last':
1227 - $v = FrmAppHelper::get_user_id_param( $v );
1228 - 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 + }
1229 1116
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;
1117 + if ( 'frm_dyncontent' === $k && is_array( $v ) ) {
1118 + $v = json_encode( $v );
1247 1119 }
1248 1120
1249 1121 update_post_meta( $post_id, $k, $v );
1122 +
1250 1123 unset( $k, $v );
1251 1124 }
1252 1125 }
1253 1126
@@ -1264,8 +1137,25 @@
1264 1137 }
1265 1138 }
1266 1139 }
1267 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 +
1268 1158 private static function maybe_update_stylesheet( $imported ) {
1269 1159 $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] );
1270 1160 $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] );
1271 1161 if ( $new_styles || $updated_styles ) {
@@ -1284,29 +1174,8 @@
1284 1174 */
1285 1175 public static function parse_message( $result, &$message, &$errors ) {
1286 1176 if ( is_wp_error( $result ) ) {
1287 1177 $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 1178 } elseif ( ! $result ) {
1310 1179 return;
1311 1180 }
1312 1181
@@ -1479,9 +1348,8 @@
1479 1348 * @since 3.06
1480 1349 */
1481 1350 public static function prepare_field_for_export( &$field ) {
1482 1351 self::remove_default_field_options( $field );
1483 - self::add_image_src_to_image_options( $field );
1484 1352 }
1485 1353
1486 1354 /**
1487 1355 * Remove defaults from field options too
@@ -1513,41 +1381,8 @@
1513 1381 $field->field_options = serialize( $options );
1514 1382 }
1515 1383
1516 1384 /**
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 1385 * @since 3.06.03
1551 1386 */
1552 1387 private static function default_field_options( $type ) {
1553 1388 $defaults = FrmFieldsHelper::get_default_field_options( $type );
@@ -1600,9 +1435,9 @@
1600 1435 FrmAppHelper::unserialize_or_decode( $str );
1601 1436 if ( is_array( $str ) ) {
1602 1437 $str = json_encode( $str );
1603 1438 } elseif ( seems_utf8( $str ) === false ) {
1604 - $str = FrmAppHelper::maybe_utf8_encode( $str );
1439 + $str = utf8_encode( $str );
1605 1440 }
1606 1441
1607 1442 if ( is_numeric( $str ) ) {
1608 1443 return $str;