| @@ -32,25 +32,15 @@ | ||
| 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 | 39 | $dom = new DOMDocument(); |
| 49 | - | |
| 50 | 40 | // LIBXML_COMPACT activates small nodes allocation optimization. |
| 51 | 41 | // 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 ); | |
| 42 | + $success = $dom->loadXML( file_get_contents( $file ), LIBXML_COMPACT | LIBXML_PARSEHUGE ); | |
| 53 | 43 | if ( ! $success ) { |
| 54 | 44 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 55 | 45 | } |
| 56 | 46 | |
| @@ -69,35 +59,8 @@ | ||
| 69 | 59 | return self::import_xml_now( $xml ); |
| 70 | 60 | } |
| 71 | 61 | |
| 72 | 62 | /** |
| 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 | 63 | * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
| 101 | 64 | * |
| 102 | 65 | * @since 3.06 |
| 103 | 66 | * |
| @@ -303,15 +266,9 @@ | ||
| 303 | 266 | } |
| 304 | 267 | |
| 305 | 268 | $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
| 306 | 269 | |
| 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; | |
| 270 | + return FrmForm::getAll( $edit_query, '', 1 ); | |
| 314 | 271 | } |
| 315 | 272 | |
| 316 | 273 | private static function update_form( $this_form, $form, &$imported ) { |
| 317 | 274 | $form_id = $this_form->id; |
| @@ -769,9 +726,9 @@ | ||
| 769 | 726 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 770 | 727 | return; |
| 771 | 728 | } |
| 772 | 729 | |
| 773 | - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) { | |
| 730 | + if ( is_numeric( $form['options']['custom_style'] ) ) { | |
| 774 | 731 | // Set to default |
| 775 | 732 | $form['options']['custom_style'] = 1; |
| 776 | 733 | } else { |
| 777 | 734 | // Replace the style name with the style ID on import |
| @@ -870,12 +827,14 @@ | ||
| 870 | 827 | unset( $action_control ); |
| 871 | 828 | } elseif ( $post['post_type'] === 'frm_styles' ) { |
| 872 | 829 | // Properly encode post content before inserting the post |
| 873 | 830 | $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] ); |
| 831 | + $custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : ''; | |
| 874 | 832 | $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] ); |
| 875 | 833 | |
| 876 | 834 | // Create/update post now |
| 877 | 835 | $post_id = wp_insert_post( $post ); |
| 836 | + self::maybe_update_custom_css( $custom_css ); | |
| 878 | 837 | } else { |
| 879 | 838 | if ( $post['post_type'] === 'frm_display' ) { |
| 880 | 839 | $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] ); |
| 881 | 840 | } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) { |
| @@ -922,13 +881,8 @@ | ||
| 922 | 881 | self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ); |
| 923 | 882 | } |
| 924 | 883 | unset( $posts_with_shortcodes, $view_ids ); |
| 925 | 884 | |
| 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 | 885 | self::maybe_update_stylesheet( $imported ); |
| 932 | 886 | |
| 933 | 887 | flush_rewrite_rules(); |
| 934 | 888 | |
| @@ -935,37 +889,8 @@ | ||
| 935 | 889 | return $imported; |
| 936 | 890 | } |
| 937 | 891 | |
| 938 | 892 | /** |
| 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 | 893 | * Replace old form ids with new ones in a string. |
| 969 | 894 | * |
| 970 | 895 | * @param string $string |
| 971 | 896 | * @param array<int> $form_ids new form ids indexed by old form id. |
| @@ -1270,8 +1195,25 @@ | ||
| 1270 | 1195 | } |
| 1271 | 1196 | } |
| 1272 | 1197 | } |
| 1273 | 1198 | |
| 1199 | + /** | |
| 1200 | + * If a template includes custom css, let's include it. | |
| 1201 | + * The custom css is included on the default style. | |
| 1202 | + * | |
| 1203 | + * @since 2.03 | |
| 1204 | + */ | |
| 1205 | + private static function maybe_update_custom_css( $custom_css ) { | |
| 1206 | + if ( empty( $custom_css ) ) { | |
| 1207 | + return; | |
| 1208 | + } | |
| 1209 | + | |
| 1210 | + $frm_style = new FrmStyle(); | |
| 1211 | + $default_style = $frm_style->get_default_style(); | |
| 1212 | + $default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css; | |
| 1213 | + $frm_style->save( $default_style ); | |
| 1214 | + } | |
| 1215 | + | |
| 1274 | 1216 | private static function maybe_update_stylesheet( $imported ) { |
| 1275 | 1217 | $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); |
| 1276 | 1218 | $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); |
| 1277 | 1219 | if ( $new_styles || $updated_styles ) { |
| @@ -1290,29 +1232,8 @@ | ||
| 1290 | 1232 | */ |
| 1291 | 1233 | public static function parse_message( $result, &$message, &$errors ) { |
| 1292 | 1234 | if ( is_wp_error( $result ) ) { |
| 1293 | 1235 | $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 | 1236 | } elseif ( ! $result ) { |
| 1316 | 1237 | return; |
| 1317 | 1238 | } |
| 1318 | 1239 | |
| @@ -1606,9 +1527,9 @@ | ||
| 1606 | 1527 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1607 | 1528 | if ( is_array( $str ) ) { |
| 1608 | 1529 | $str = json_encode( $str ); |
| 1609 | 1530 | } elseif ( seems_utf8( $str ) === false ) { |
| 1610 | - $str = FrmAppHelper::maybe_utf8_encode( $str ); | |
| 1531 | + $str = utf8_encode( $str ); | |
| 1611 | 1532 | } |
| 1612 | 1533 | |
| 1613 | 1534 | if ( is_numeric( $str ) ) { |
| 1614 | 1535 | return $str; |