| @@ -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,12 +789,14 @@ | ||
| 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 | 802 | } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) { |
| @@ -888,9 +809,9 @@ | ||
| 888 | 809 | if ( ! is_numeric( $post_id ) ) { |
| 889 | 810 | continue; |
| 890 | 811 | } |
| 891 | 812 | |
| 892 | - if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) { | |
| 813 | + if ( false !== strpos( $post['post_content'], '[frm-display-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) { | |
| 893 | 814 | $posts_with_shortcodes[ $post_id ] = $post; |
| 894 | 815 | } |
| 895 | 816 | |
| 896 | 817 | self::update_postmeta( $post, $post_id ); |
| @@ -922,13 +843,8 @@ | ||
| 922 | 843 | self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ); |
| 923 | 844 | } |
| 924 | 845 | unset( $posts_with_shortcodes, $view_ids ); |
| 925 | 846 | |
| 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 | 847 | self::maybe_update_stylesheet( $imported ); |
| 932 | 848 | |
| 933 | 849 | flush_rewrite_rules(); |
| 934 | 850 | |
| @@ -935,37 +851,8 @@ | ||
| 935 | 851 | return $imported; |
| 936 | 852 | } |
| 937 | 853 | |
| 938 | 854 | /** |
| 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 | 855 | * Replace old form ids with new ones in a string. |
| 969 | 856 | * |
| 970 | 857 | * @param string $string |
| 971 | 858 | * @param array<int> $form_ids new form ids indexed by old form id. |
| @@ -1270,8 +1157,25 @@ | ||
| 1270 | 1157 | } |
| 1271 | 1158 | } |
| 1272 | 1159 | } |
| 1273 | 1160 | |
| 1161 | + /** | |
| 1162 | + * If a template includes custom css, let's include it. | |
| 1163 | + * The custom css is included on the default style. | |
| 1164 | + * | |
| 1165 | + * @since 2.03 | |
| 1166 | + */ | |
| 1167 | + private static function maybe_update_custom_css( $custom_css ) { | |
| 1168 | + if ( empty( $custom_css ) ) { | |
| 1169 | + return; | |
| 1170 | + } | |
| 1171 | + | |
| 1172 | + $frm_style = new FrmStyle(); | |
| 1173 | + $default_style = $frm_style->get_default_style(); | |
| 1174 | + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css; | |
| 1175 | + $frm_style->save( $default_style ); | |
| 1176 | + } | |
| 1177 | + | |
| 1274 | 1178 | private static function maybe_update_stylesheet( $imported ) { |
| 1275 | 1179 | $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); |
| 1276 | 1180 | $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); |
| 1277 | 1181 | if ( $new_styles || $updated_styles ) { |
| @@ -1290,29 +1194,8 @@ | ||
| 1290 | 1194 | */ |
| 1291 | 1195 | public static function parse_message( $result, &$message, &$errors ) { |
| 1292 | 1196 | if ( is_wp_error( $result ) ) { |
| 1293 | 1197 | $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 | 1198 | } elseif ( ! $result ) { |
| 1316 | 1199 | return; |
| 1317 | 1200 | } |
| 1318 | 1201 | |
| @@ -1485,9 +1368,8 @@ | ||
| 1485 | 1368 | * @since 3.06 |
| 1486 | 1369 | */ |
| 1487 | 1370 | public static function prepare_field_for_export( &$field ) { |
| 1488 | 1371 | self::remove_default_field_options( $field ); |
| 1489 | - self::add_image_src_to_image_options( $field ); | |
| 1490 | 1372 | } |
| 1491 | 1373 | |
| 1492 | 1374 | /** |
| 1493 | 1375 | * Remove defaults from field options too |
| @@ -1519,41 +1401,8 @@ | ||
| 1519 | 1401 | $field->field_options = serialize( $options ); |
| 1520 | 1402 | } |
| 1521 | 1403 | |
| 1522 | 1404 | /** |
| 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 | 1405 | * @since 3.06.03 |
| 1557 | 1406 | */ |
| 1558 | 1407 | private static function default_field_options( $type ) { |
| 1559 | 1408 | $defaults = FrmFieldsHelper::get_default_field_options( $type ); |
| @@ -1606,9 +1455,9 @@ | ||
| 1606 | 1455 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1607 | 1456 | if ( is_array( $str ) ) { |
| 1608 | 1457 | $str = json_encode( $str ); |
| 1609 | 1458 | } elseif ( seems_utf8( $str ) === false ) { |
| 1610 | - $str = FrmAppHelper::maybe_utf8_encode( $str ); | |
| 1459 | + $str = utf8_encode( $str ); | |
| 1611 | 1460 | } |
| 1612 | 1461 | |
| 1613 | 1462 | if ( is_numeric( $str ) ) { |
| 1614 | 1463 | return $str; |