| @@ -5,9 +5,9 @@ | ||
| 5 | 5 | |
| 6 | 6 | class FrmXMLHelper { |
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | - * @var bool true if importing an XML from API, false if importing an XML file manually. | |
| 9 | + * @var bool $installing_template true if importing an XML from API, false if importing an XML file manually. | |
| 10 | 10 | */ |
| 11 | 11 | private static $installing_template = false; |
| 12 | 12 | |
| 13 | 13 | public static function get_xml_values( $opt, $padding ) { |
| @@ -26,12 +26,8 @@ | ||
| 26 | 26 | echo self::cdata( $opt ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped |
| 27 | 27 | } |
| 28 | 28 | } |
| 29 | 29 | |
| 30 | - /** | |
| 31 | - * @param string $file | |
| 32 | - * @return array|WP_Error The items imported | |
| 33 | - */ | |
| 34 | 30 | public static function import_xml( $file ) { |
| 35 | 31 | if ( ! defined( 'WP_IMPORTING' ) ) { |
| 36 | 32 | define( 'WP_IMPORTING', true ); |
| 37 | 33 | } |
| @@ -36,25 +32,13 @@ | ||
| 36 | 32 | define( 'WP_IMPORTING', true ); |
| 37 | 33 | } |
| 38 | 34 | |
| 39 | 35 | if ( ! class_exists( 'DOMDocument' ) ) { |
| 40 | - $error_message = sprintf( | |
| 41 | - /* translators: 1: Documentation link */ | |
| 42 | - __( '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' ), | |
| 43 | - '<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>' | |
| 44 | - ); | |
| 45 | - | |
| 46 | - 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() ); | |
| 47 | 37 | } |
| 48 | 38 | |
| 49 | - $xml_string = file_get_contents( $file ); | |
| 50 | - self::maybe_fix_xml( $xml_string ); | |
| 51 | - | |
| 52 | - $dom = new DOMDocument(); | |
| 53 | - | |
| 54 | - // LIBXML_COMPACT activates small nodes allocation optimization. | |
| 55 | - // Use LIBXML_PARSEHUGE to avoid "parser error : internal error: Huge input lookup" for large (300MB) files. | |
| 56 | - $success = $dom->loadXML( $xml_string, LIBXML_COMPACT | LIBXML_PARSEHUGE ); | |
| 39 | + $dom = new DOMDocument(); | |
| 40 | + $success = $dom->loadXML( file_get_contents( $file ) ); | |
| 57 | 41 | if ( ! $success ) { |
| 58 | 42 | return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
| 59 | 43 | } |
| 60 | 44 | |
| @@ -73,35 +57,8 @@ | ||
| 73 | 57 | return self::import_xml_now( $xml ); |
| 74 | 58 | } |
| 75 | 59 | |
| 76 | 60 | /** |
| 77 | - * @since 6.2.3 | |
| 78 | - * | |
| 79 | - * @param string $xml_string | |
| 80 | - * @return void | |
| 81 | - */ | |
| 82 | - private static function maybe_fix_xml( &$xml_string ) { | |
| 83 | - if ( '<?xml' !== substr( $xml_string, 0, 5 ) ) { | |
| 84 | - // Some XML files have may have unexpected characters at the start. | |
| 85 | - $xml_string = substr( $xml_string, strpos( $xml_string, '<?xml' ) ); | |
| 86 | - } | |
| 87 | - | |
| 88 | - // The Equity theme adds a <meta name="generator" content="Equity 1.7.13" /> tag using the "the_generator" filter. | |
| 89 | - // Strip that out as it breaks the XML import. | |
| 90 | - $channel_start_position = strpos( $xml_string, '<channel>' ); | |
| 91 | - $content_before_channel_tag = substr( $xml_string, 0, $channel_start_position ); | |
| 92 | - if ( 0 !== strpos( $content_before_channel_tag, '<meta name="generator" ' ) ) { | |
| 93 | - $content_before_channel_tag = preg_replace( | |
| 94 | - '/<meta\s+[^>]*name="generator"[^>]*\/>/i', | |
| 95 | - '', | |
| 96 | - $content_before_channel_tag, | |
| 97 | - 1 | |
| 98 | - ); | |
| 99 | - $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position ); | |
| 100 | - } | |
| 101 | - } | |
| 102 | - | |
| 103 | - /** | |
| 104 | 61 | * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
| 105 | 62 | * |
| 106 | 63 | * @since 3.06 |
| 107 | 64 | * |
| @@ -127,12 +84,12 @@ | ||
| 127 | 84 | } |
| 128 | 85 | |
| 129 | 86 | $imported = apply_filters( 'frm_importing_xml', $imported, $xml ); |
| 130 | 87 | |
| 131 | - if ( empty( $imported['form_status'] ) ) { | |
| 88 | + if ( ! isset( $imported['form_status'] ) || empty( $imported['form_status'] ) ) { | |
| 132 | 89 | // Check for an error message in the XML. |
| 133 | 90 | if ( isset( $xml->Code ) && isset( $xml->Message ) ) { // phpcs:ignore WordPress.NamingConventions |
| 134 | - $imported['error'] = (string) $xml->Message; // phpcs:ignore WordPress.NamingConventions | |
| 91 | + $imported['error'] = reset( $xml->Message ); // phpcs:ignore WordPress.NamingConventions | |
| 135 | 92 | } |
| 136 | 93 | } |
| 137 | 94 | |
| 138 | 95 | return $imported; |
| @@ -160,13 +117,8 @@ | ||
| 160 | 117 | 'terms' => array(), |
| 161 | 118 | ); |
| 162 | 119 | } |
| 163 | 120 | |
| 164 | - /** | |
| 165 | - * @param SimpleXMLElement $terms | |
| 166 | - * @param array $imported | |
| 167 | - * @return array | |
| 168 | - */ | |
| 169 | 121 | public static function import_xml_terms( $terms, $imported ) { |
| 170 | 122 | foreach ( $terms as $t ) { |
| 171 | 123 | if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) { |
| 172 | 124 | continue; |
| @@ -184,14 +136,14 @@ | ||
| 184 | 136 | ) |
| 185 | 137 | ); |
| 186 | 138 | |
| 187 | 139 | if ( $term && is_array( $term ) ) { |
| 188 | - ++$imported['imported']['terms']; | |
| 140 | + $imported['imported']['terms'] ++; | |
| 189 | 141 | $imported['terms'][ (int) $t->term_id ] = $term['term_id']; |
| 190 | 142 | } |
| 191 | 143 | |
| 192 | 144 | unset( $term, $t ); |
| 193 | - }//end foreach | |
| 145 | + } | |
| 194 | 146 | |
| 195 | 147 | return $imported; |
| 196 | 148 | } |
| 197 | 149 | |
| @@ -196,11 +148,8 @@ | ||
| 196 | 148 | } |
| 197 | 149 | |
| 198 | 150 | /** |
| 199 | 151 | * @since 2.0.8 |
| 200 | - * | |
| 201 | - * @param object $t | |
| 202 | - * @return int|string | |
| 203 | 152 | */ |
| 204 | 153 | private static function get_term_parent_id( $t ) { |
| 205 | 154 | $parent = (string) $t->term_parent; |
| 206 | 155 | if ( ! empty( $parent ) ) { |
| @@ -214,13 +163,8 @@ | ||
| 214 | 163 | |
| 215 | 164 | return $parent; |
| 216 | 165 | } |
| 217 | 166 | |
| 218 | - /** | |
| 219 | - * @param SimpleXMLElement $forms | |
| 220 | - * @param array $imported | |
| 221 | - * @return array | |
| 222 | - */ | |
| 223 | 167 | public static function import_xml_forms( $forms, $imported ) { |
| 224 | 168 | $child_forms = array(); |
| 225 | 169 | |
| 226 | 170 | // Import child forms first |
| @@ -245,9 +189,9 @@ | ||
| 245 | 189 | $form_id = FrmForm::create( $form ); |
| 246 | 190 | if ( $form_id ) { |
| 247 | 191 | if ( empty( $form['parent_form_id'] ) ) { |
| 248 | 192 | // Don't include the repeater form in the imported count. |
| 249 | - ++$imported['imported']['forms']; | |
| 193 | + $imported['imported']['forms'] ++; | |
| 250 | 194 | } |
| 251 | 195 | |
| 252 | 196 | // Keep track of whether this specific form was updated or not. |
| 253 | 197 | $imported['form_status'][ $form_id ] = 'imported'; |
| @@ -272,9 +216,9 @@ | ||
| 272 | 216 | |
| 273 | 217 | do_action( 'frm_after_import_form', $form_id, $form ); |
| 274 | 218 | |
| 275 | 219 | unset( $form, $item ); |
| 276 | - }//end foreach | |
| 220 | + } | |
| 277 | 221 | |
| 278 | 222 | self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms ); |
| 279 | 223 | |
| 280 | 224 | return $imported; |
| @@ -320,15 +264,9 @@ | ||
| 320 | 264 | } |
| 321 | 265 | |
| 322 | 266 | $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
| 323 | 267 | |
| 324 | - $form = FrmForm::getAll( $edit_query, '', 1 ); | |
| 325 | - if ( is_object( $form ) && $form->status === 'trash' ) { | |
| 326 | - FrmForm::destroy( $form->id ); | |
| 327 | - return false; | |
| 328 | - } | |
| 329 | - | |
| 330 | - return $form; | |
| 268 | + return FrmForm::getAll( $edit_query, '', 1 ); | |
| 331 | 269 | } |
| 332 | 270 | |
| 333 | 271 | private static function update_form( $this_form, $form, &$imported ) { |
| 334 | 272 | $form_id = $this_form->id; |
| @@ -334,9 +272,9 @@ | ||
| 334 | 272 | $form_id = $this_form->id; |
| 335 | 273 | FrmForm::update( $form_id, $form ); |
| 336 | 274 | if ( empty( $form['parent_form_id'] ) ) { |
| 337 | 275 | // Don't include the repeater form in the updated count. |
| 338 | - ++$imported['updated']['forms']; | |
| 276 | + $imported['updated']['forms'] ++; | |
| 339 | 277 | } |
| 340 | 278 | |
| 341 | 279 | // Keep track of whether this specific form was updated or not |
| 342 | 280 | $imported['form_status'][ $form_id ] = 'updated'; |
| @@ -397,10 +335,10 @@ | ||
| 397 | 335 | * Keep track of all imported child forms |
| 398 | 336 | * |
| 399 | 337 | * @since 2.0.16 |
| 400 | 338 | * |
| 401 | - * @param int $form_id | |
| 402 | - * @param int $parent_form_id | |
| 339 | + * @param int $form_id | |
| 340 | + * @param int $parent_form_id | |
| 403 | 341 | * @param array $child_forms |
| 404 | 342 | */ |
| 405 | 343 | private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) { |
| 406 | 344 | if ( $parent_form_id ) { |
| @@ -452,13 +390,13 @@ | ||
| 452 | 390 | if ( ! empty( $this_form ) ) { |
| 453 | 391 | // check for field to edit by field id |
| 454 | 392 | if ( isset( $form_fields[ $f['id'] ] ) ) { |
| 455 | 393 | FrmField::update( $f['id'], $f ); |
| 456 | - ++$imported['updated']['fields']; | |
| 394 | + $imported['updated']['fields'] ++; | |
| 457 | 395 | |
| 458 | 396 | unset( $form_fields[ $f['id'] ] ); |
| 459 | 397 | |
| 460 | - // Unset old field key. | |
| 398 | + //unset old field key | |
| 461 | 399 | if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| 462 | 400 | unset( $form_fields[ $f['field_key'] ] ); |
| 463 | 401 | } |
| 464 | 402 | } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| @@ -463,32 +401,25 @@ | ||
| 463 | 401 | } |
| 464 | 402 | } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) { |
| 465 | 403 | $keys_by_original_field_id[ $f['id'] ] = $f['field_key']; |
| 466 | 404 | |
| 467 | - $old_field_id = $f['id']; | |
| 468 | - | |
| 469 | 405 | // check for field to edit by field key |
| 470 | 406 | unset( $f['id'] ); |
| 471 | 407 | |
| 472 | 408 | FrmField::update( $form_fields[ $f['field_key'] ], $f ); |
| 473 | - ++$imported['updated']['fields']; | |
| 409 | + $imported['updated']['fields'] ++; | |
| 474 | 410 | |
| 475 | - self::do_after_field_imported_action( $f, $form_fields, $old_field_id ); | |
| 476 | - | |
| 477 | - // Unset old field id. | |
| 478 | - unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); | |
| 479 | - | |
| 480 | - // Unset old field key. | |
| 481 | - unset( $form_fields[ $f['field_key'] ] ); | |
| 411 | + unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); //unset old field id | |
| 412 | + unset( $form_fields[ $f['field_key'] ] ); //unset old field key | |
| 482 | 413 | } else { |
| 483 | - // If no matching field id or key in this form, create the field. | |
| 414 | + // if no matching field id or key in this form, create the field | |
| 484 | 415 | self::create_imported_field( $f, $imported ); |
| 485 | - }//end if | |
| 416 | + } | |
| 486 | 417 | } else { |
| 487 | 418 | |
| 488 | 419 | self::create_imported_field( $f, $imported ); |
| 489 | - }//end if | |
| 490 | - }//end foreach | |
| 420 | + } | |
| 421 | + } | |
| 491 | 422 | |
| 492 | 423 | if ( $keys_by_original_field_id ) { |
| 493 | 424 | self::maybe_update_field_ids( $form_id, $keys_by_original_field_id ); |
| 494 | 425 | } |
| @@ -493,46 +424,8 @@ | ||
| 493 | 424 | self::maybe_update_field_ids( $form_id, $keys_by_original_field_id ); |
| 494 | 425 | } |
| 495 | 426 | } |
| 496 | 427 | |
| 497 | - /** | |
| 498 | - * @since 6.8.4 | |
| 499 | - * | |
| 500 | - * @param array $field_array | |
| 501 | - * @return array | |
| 502 | - */ | |
| 503 | - private static function update_field_options_with_defaults( $field_array ) { | |
| 504 | - $defaults = self::default_field_options( $field_array['type'] ); | |
| 505 | - $field_array['field_options'] = array_merge( $defaults, $field_array['field_options'] ); | |
| 506 | - | |
| 507 | - return $field_array; | |
| 508 | - } | |
| 509 | - | |
| 510 | - /** | |
| 511 | - * @since 6.8.4 | |
| 512 | - * | |
| 513 | - * @param array $field_array | |
| 514 | - * @param array $form_fields | |
| 515 | - * @param int $old_field_id | |
| 516 | - * | |
| 517 | - * @return void | |
| 518 | - */ | |
| 519 | - private static function do_after_field_imported_action( $field_array, $form_fields, $old_field_id ) { | |
| 520 | - // Assign field array the update field's ID. | |
| 521 | - $field_array['id'] = $form_fields[ $field_array['field_key'] ]; | |
| 522 | - | |
| 523 | - /** | |
| 524 | - * Fires when an existing field is imported. | |
| 525 | - * | |
| 526 | - * @since 6.8.4 | |
| 527 | - * | |
| 528 | - * @param array $field_array | |
| 529 | - * @param int $field_id | |
| 530 | - * @param int $old_field_id | |
| 531 | - */ | |
| 532 | - do_action( 'frm_after_existing_field_is_imported', $field_array, $form_fields[ $field_array['field_key'] ], $old_field_id ); | |
| 533 | - } | |
| 534 | - | |
| 535 | 428 | private static function fill_field( $field, $form_id ) { |
| 536 | 429 | return array( |
| 537 | 430 | 'id' => (int) $field->id, |
| 538 | 431 | 'field_key' => (string) $field->field_key, |
| @@ -588,9 +481,9 @@ | ||
| 588 | 481 | /** |
| 589 | 482 | * Update the current in_section value at the beginning of the field loop |
| 590 | 483 | * |
| 591 | 484 | * @since 2.0.25 |
| 592 | - * @param int $in_section | |
| 485 | + * @param int $in_section | |
| 593 | 486 | * @param array $f |
| 594 | 487 | */ |
| 595 | 488 | private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
| 596 | 489 | // If we're at the end of a section, switch $in_section is 0 |
| @@ -603,9 +496,9 @@ | ||
| 603 | 496 | $f['field_options']['in_section'] = $in_section; |
| 604 | 497 | } |
| 605 | 498 | |
| 606 | 499 | // If we're starting a new section, switch $in_section to ID of divider |
| 607 | - if ( $f['type'] === 'divider' ) { | |
| 500 | + if ( $f['type'] == 'divider' ) { | |
| 608 | 501 | $in_section = $f['id']; |
| 609 | 502 | } |
| 610 | 503 | } |
| 611 | 504 | |
| @@ -621,9 +514,9 @@ | ||
| 621 | 514 | if ( ! isset( $imported['forms'] ) ) { |
| 622 | 515 | return; |
| 623 | 516 | } |
| 624 | 517 | |
| 625 | - if ( $f['type'] === 'form' || ( $f['type'] === 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 518 | + if ( $f['type'] == 'form' || ( $f['type'] == 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { | |
| 626 | 519 | if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { |
| 627 | 520 | $form_select = (int) $f['field_options']['form_select']; |
| 628 | 521 | if ( isset( $imported['forms'][ $form_select ] ) ) { |
| 629 | 522 | $f['field_options']['form_select'] = $imported['forms'][ $form_select ]; |
| @@ -685,9 +578,9 @@ | ||
| 685 | 578 | * @since 4.0 |
| 686 | 579 | * @return array |
| 687 | 580 | */ |
| 688 | 581 | public static function migrate_field_placeholder( $field, $type ) { |
| 689 | - $field = (array) $field; | |
| 582 | + $field = (array) $field; | |
| 690 | 583 | $field_options = $field['field_options']; |
| 691 | 584 | if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) { |
| 692 | 585 | return array(); |
| 693 | 586 | } |
| @@ -709,9 +602,9 @@ | ||
| 709 | 602 | } |
| 710 | 603 | |
| 711 | 604 | foreach ( $options as $opt_key => $opt ) { |
| 712 | 605 | if ( is_array( $opt ) ) { |
| 713 | - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt ); | |
| 606 | + $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) ); | |
| 714 | 607 | } |
| 715 | 608 | |
| 716 | 609 | if ( $opt == $default_value ) { |
| 717 | 610 | unset( $options[ $opt_key ] ); |
| @@ -732,60 +625,23 @@ | ||
| 732 | 625 | * @param array $f |
| 733 | 626 | * @param array $imported |
| 734 | 627 | */ |
| 735 | 628 | private static function create_imported_field( $f, &$imported ) { |
| 736 | - $f = self::update_field_options_with_defaults( $f ); | |
| 629 | + $defaults = self::default_field_options( $f['type'] ); | |
| 630 | + $f['field_options'] = array_merge( $defaults, $f['field_options'] ); | |
| 737 | 631 | |
| 738 | - if ( is_callable( 'FrmProFileImport::import_attachment' ) ) { | |
| 739 | - $f = self::maybe_import_images_for_options( $f ); | |
| 740 | - } | |
| 741 | - | |
| 742 | 632 | $new_id = FrmField::create( $f ); |
| 743 | 633 | if ( $new_id != false ) { |
| 744 | - ++$imported['imported']['fields']; | |
| 634 | + $imported['imported']['fields'] ++; | |
| 745 | 635 | do_action( 'frm_after_field_is_imported', $f, $new_id ); |
| 746 | 636 | } |
| 747 | 637 | } |
| 748 | 638 | |
| 749 | 639 | /** |
| 750 | - * Import images for radio buttons and checkboxes from image src if available. | |
| 751 | - * | |
| 752 | - * @since 5.5.1 | |
| 753 | - * | |
| 754 | - * @param array $field | |
| 755 | - * @return array | |
| 756 | - */ | |
| 757 | - private static function maybe_import_images_for_options( $field ) { | |
| 758 | - if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) { | |
| 759 | - return $field; | |
| 760 | - } | |
| 761 | - | |
| 762 | - foreach ( $field['options'] as $key => $option ) { | |
| 763 | - if ( ! is_array( $option ) || empty( $option['src'] ) ) { | |
| 764 | - continue; | |
| 765 | - } | |
| 766 | - | |
| 767 | - $field_object = (object) $field; | |
| 768 | - // Fake the file type as FrmProImport::import_attachment checks for file type. | |
| 769 | - $field_object->type = 'file'; | |
| 770 | - | |
| 771 | - $image_id = FrmProFileImport::import_attachment( $option['src'], $field_object ); | |
| 772 | - // Remove the src from options as it isn't required after import. | |
| 773 | - unset( $field['options'][ $key ]['src'] ); | |
| 774 | - | |
| 775 | - if ( is_numeric( $image_id ) ) { | |
| 776 | - $field['options'][ $key ]['image'] = $image_id; | |
| 777 | - } | |
| 778 | - } | |
| 779 | - | |
| 780 | - return $field; | |
| 781 | - } | |
| 782 | - | |
| 783 | - /** | |
| 784 | 640 | * Fix field ids for fields that already exist prior to import. |
| 785 | 641 | * |
| 786 | 642 | * @since 4.07 |
| 787 | - * @param int $form_id | |
| 643 | + * @param int $form_id | |
| 788 | 644 | * @param array $keys_by_original_field_id |
| 789 | 645 | */ |
| 790 | 646 | protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) { |
| 791 | 647 | global $frm_duplicate_ids; |
| @@ -832,9 +688,9 @@ | ||
| 832 | 688 | if ( ! isset( $form['options']['custom_style'] ) ) { |
| 833 | 689 | return; |
| 834 | 690 | } |
| 835 | 691 | |
| 836 | - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) { | |
| 692 | + if ( is_numeric( $form['options']['custom_style'] ) ) { | |
| 837 | 693 | // Set to default |
| 838 | 694 | $form['options']['custom_style'] = 1; |
| 839 | 695 | } else { |
| 840 | 696 | // Replace the style name with the style ID on import |
| @@ -855,9 +711,9 @@ | ||
| 855 | 711 | |
| 856 | 712 | // Set to default |
| 857 | 713 | $form['options']['custom_style'] = 1; |
| 858 | 714 | } |
| 859 | - }//end if | |
| 715 | + } | |
| 860 | 716 | } |
| 861 | 717 | |
| 862 | 718 | /** |
| 863 | 719 | * After styles are imported, check for any forms that were linked |
| @@ -879,15 +735,8 @@ | ||
| 879 | 735 | } |
| 880 | 736 | } |
| 881 | 737 | } |
| 882 | 738 | |
| 883 | - /** | |
| 884 | - * Handle import for all posts. This includes views, styles, pages, and form actions. | |
| 885 | - * | |
| 886 | - * @param SimpleXMLElement $views | |
| 887 | - * @param array $imported | |
| 888 | - * @return array | |
| 889 | - */ | |
| 890 | 739 | public static function import_xml_views( $views, $imported ) { |
| 891 | 740 | $imported['posts'] = array(); |
| 892 | 741 | $form_action_type = FrmFormActionsController::$action_post_type; |
| 893 | 742 | |
| @@ -896,11 +745,8 @@ | ||
| 896 | 745 | $form_action_type => 'actions', |
| 897 | 746 | 'frm_styles' => 'styles', |
| 898 | 747 | ); |
| 899 | 748 | |
| 900 | - $view_ids = array(); | |
| 901 | - $posts_with_shortcodes = array(); | |
| 902 | - | |
| 903 | 749 | foreach ( $views as $item ) { |
| 904 | 750 | $post = array( |
| 905 | 751 | 'post_title' => (string) $item->title, |
| 906 | 752 | 'post_name' => (string) $item->post_name, |
| @@ -923,15 +769,8 @@ | ||
| 923 | 769 | 'layout' => array(), |
| 924 | 770 | 'tax_input' => array(), |
| 925 | 771 | ); |
| 926 | 772 | |
| 927 | - $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] ); | |
| 928 | - | |
| 929 | - // Fix issue with line breaks appearing in descriptions as "rn". | |
| 930 | - if ( $post['post_type'] === $form_action_type ) { | |
| 931 | - $post['post_content'] = str_replace( '\\\\r\\\\n', '\\r\\n', $post['post_content'] ); | |
| 932 | - } | |
| 933 | - | |
| 934 | 773 | $old_id = $post['post_id']; |
| 935 | 774 | self::populate_post( $post, $item, $imported ); |
| 936 | 775 | |
| 937 | 776 | unset( $item ); |
| @@ -936,41 +775,35 @@ | ||
| 936 | 775 | |
| 937 | 776 | unset( $item ); |
| 938 | 777 | |
| 939 | 778 | $post_id = false; |
| 940 | - if ( $post['post_type'] === $form_action_type ) { | |
| 779 | + if ( $post['post_type'] == $form_action_type ) { | |
| 941 | 780 | $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
| 942 | - if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) { | |
| 781 | + if ( $action_control && is_object( $action_control ) ) { | |
| 943 | 782 | $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
| 944 | 783 | } |
| 945 | 784 | unset( $action_control ); |
| 946 | - } elseif ( $post['post_type'] === 'frm_styles' ) { | |
| 785 | + } elseif ( $post['post_type'] == 'frm_styles' ) { | |
| 947 | 786 | // Properly encode post content before inserting the post |
| 948 | 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'] : ''; | |
| 949 | 789 | $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] ); |
| 950 | - // Store template data additionally to a postmeta that can be used to revert the style to default template style. | |
| 951 | - $post['postmeta']['frm_style_default'] = $post['post_content']; | |
| 952 | 790 | |
| 953 | 791 | // Create/update post now |
| 954 | 792 | $post_id = wp_insert_post( $post ); |
| 793 | + self::maybe_update_custom_css( $custom_css ); | |
| 955 | 794 | } else { |
| 956 | 795 | if ( $post['post_type'] === 'frm_display' ) { |
| 957 | 796 | $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] ); |
| 958 | - } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) { | |
| 959 | - $post['post_parent'] = $imported['posts'][ $post['post_parent'] ]; | |
| 960 | 797 | } |
| 961 | 798 | // Create/update post now |
| 962 | 799 | $post_id = wp_insert_post( $post ); |
| 963 | - }//end if | |
| 800 | + } | |
| 964 | 801 | |
| 965 | 802 | if ( ! is_numeric( $post_id ) ) { |
| 966 | 803 | continue; |
| 967 | 804 | } |
| 968 | 805 | |
| 969 | - if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) { | |
| 970 | - $posts_with_shortcodes[ $post_id ] = $post; | |
| 971 | - } | |
| 972 | - | |
| 973 | 806 | self::update_postmeta( $post, $post_id ); |
| 974 | 807 | self::update_layout( $post, $post_id ); |
| 975 | 808 | |
| 976 | 809 | $this_type = 'posts'; |
| @@ -978,161 +811,26 @@ | ||
| 978 | 811 | $this_type = $post_types[ $post['post_type'] ]; |
| 979 | 812 | } |
| 980 | 813 | |
| 981 | 814 | if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) { |
| 982 | - ++$imported['updated'][ $this_type ]; | |
| 815 | + $imported['updated'][ $this_type ] ++; | |
| 983 | 816 | } else { |
| 984 | - ++$imported['imported'][ $this_type ]; | |
| 817 | + $imported['imported'][ $this_type ] ++; | |
| 985 | 818 | } |
| 986 | 819 | |
| 987 | - $imported['posts'][ $old_id ] = $post_id; | |
| 820 | + $imported['posts'][ (int) $old_id ] = $post_id; | |
| 988 | 821 | |
| 989 | - if ( $post['post_type'] === 'frm_display' ) { | |
| 990 | - $view_ids[ $old_id ] = $post_id; | |
| 991 | - } | |
| 992 | - | |
| 993 | 822 | do_action( 'frm_after_import_view', $post_id, $post ); |
| 994 | 823 | |
| 995 | 824 | unset( $post ); |
| 996 | - }//end foreach | |
| 997 | - | |
| 998 | - if ( $posts_with_shortcodes && $view_ids ) { | |
| 999 | - self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ); | |
| 1000 | 825 | } |
| 1001 | - unset( $posts_with_shortcodes, $view_ids ); | |
| 1002 | 826 | |
| 1003 | - if ( ! empty( $imported['forms'] ) ) { | |
| 1004 | - // clear imported forms style cache to make sure the new styles are applied to the forms | |
| 1005 | - self::clear_forms_style_caches( $imported['forms'] ); | |
| 1006 | - } | |
| 1007 | - | |
| 1008 | 827 | self::maybe_update_stylesheet( $imported ); |
| 1009 | 828 | |
| 1010 | - flush_rewrite_rules(); | |
| 1011 | - | |
| 1012 | 829 | return $imported; |
| 1013 | 830 | } |
| 1014 | 831 | |
| 1015 | 832 | /** |
| 1016 | - * Clears styles from cache for imported forms | |
| 1017 | - * | |
| 1018 | - * @param array $imported_forms | |
| 1019 | - */ | |
| 1020 | - private static function clear_forms_style_caches( $imported_forms ) { | |
| 1021 | - $where = array( | |
| 1022 | - 'id' => $imported_forms, | |
| 1023 | - 'options LIKE' => '"old_style"', | |
| 1024 | - ); | |
| 1025 | - $forms = FrmDb::get_results( 'frm_forms', $where ); | |
| 1026 | - | |
| 1027 | - foreach ( $forms as $form ) { | |
| 1028 | - FrmAppHelper::unserialize_or_decode( $form->options ); | |
| 1029 | - if ( ! $form->options ) { | |
| 1030 | - continue; | |
| 1031 | - } | |
| 1032 | - $where = array( | |
| 1033 | - 'post_name' => $form->options['old_style'], | |
| 1034 | - 'post_type' => FrmStylesController::$post_type, | |
| 1035 | - ); | |
| 1036 | - | |
| 1037 | - $select = 'ID'; | |
| 1038 | - | |
| 1039 | - $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' ); | |
| 1040 | - FrmDb::delete_cache_and_transient( $cache_key, 'post' ); | |
| 1041 | - } | |
| 1042 | - } | |
| 1043 | - | |
| 1044 | - /** | |
| 1045 | - * Replace old form ids with new ones in a string. | |
| 1046 | - * | |
| 1047 | - * @param string $string | |
| 1048 | - * @param array<int> $form_ids new form ids indexed by old form id. | |
| 1049 | - * @return string | |
| 1050 | - */ | |
| 1051 | - private static function switch_form_ids( $string, $form_ids ) { | |
| 1052 | - if ( false === strpos( $string, '[formidable' ) ) { | |
| 1053 | - // Skip string replacing if there are no form shortcodes in string. | |
| 1054 | - return $string; | |
| 1055 | - } | |
| 1056 | - | |
| 1057 | - foreach ( $form_ids as $old_id => $new_id ) { | |
| 1058 | - $string = str_replace( | |
| 1059 | - array( | |
| 1060 | - '[formidable id="' . $old_id . '"', | |
| 1061 | - '[formidable id=' . $old_id . ']', | |
| 1062 | - '[formidable id=' . $old_id . ' ', | |
| 1063 | - '"formId":"' . $old_id . '"', | |
| 1064 | - ), | |
| 1065 | - array( | |
| 1066 | - '[formidable id="' . $new_id . '"', | |
| 1067 | - '[formidable id=' . $new_id . ']', | |
| 1068 | - '[formidable id=' . $new_id . ' ', | |
| 1069 | - '"formId":"' . $new_id . '"', | |
| 1070 | - ), | |
| 1071 | - $string | |
| 1072 | - ); | |
| 1073 | - } | |
| 1074 | - | |
| 1075 | - return $string; | |
| 1076 | - } | |
| 1077 | - | |
| 1078 | - /** | |
| 1079 | - * @param array<array> $posts_with_shortcodes indexed by current post id. | |
| 1080 | - * @param array<int> $view_ids new view ids indexed by old view id. | |
| 1081 | - * @return void | |
| 1082 | - */ | |
| 1083 | - private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) { | |
| 1084 | - foreach ( $posts_with_shortcodes as $imported_post_id => $post ) { | |
| 1085 | - $post_content = self::switch_view_ids( $post['post_content'], $view_ids ); | |
| 1086 | - if ( $post_content === $post['post_content'] ) { | |
| 1087 | - continue; | |
| 1088 | - } | |
| 1089 | - | |
| 1090 | - wp_update_post( | |
| 1091 | - array( | |
| 1092 | - 'ID' => $imported_post_id, | |
| 1093 | - 'post_content' => $post_content, | |
| 1094 | - ) | |
| 1095 | - ); | |
| 1096 | - } | |
| 1097 | - } | |
| 1098 | - | |
| 1099 | - /** | |
| 1100 | - * Replace old view ids with new ones in a string. | |
| 1101 | - * | |
| 1102 | - * @param string $string | |
| 1103 | - * @param array<int> $view_ids new view ids indexed by old view id. | |
| 1104 | - * @return string | |
| 1105 | - */ | |
| 1106 | - private static function switch_view_ids( $string, $view_ids ) { | |
| 1107 | - if ( false === strpos( $string, '[display-frm-data' ) ) { | |
| 1108 | - // Skip string replacing if there are no view shortcodes in string. | |
| 1109 | - return $string; | |
| 1110 | - } | |
| 1111 | - | |
| 1112 | - foreach ( $view_ids as $old_id => $new_id ) { | |
| 1113 | - $string = str_replace( | |
| 1114 | - array( | |
| 1115 | - '[display-frm-data id="' . $old_id . '"', | |
| 1116 | - '[display-frm-data id=' . $old_id . ']', | |
| 1117 | - '[display-frm-data id=' . $old_id . ' ', | |
| 1118 | - '"viewId":"' . $old_id . '"', | |
| 1119 | - ), | |
| 1120 | - array( | |
| 1121 | - '[display-frm-data id="' . $new_id . '"', | |
| 1122 | - '[display-frm-data id=' . $new_id . ']', | |
| 1123 | - '[display-frm-data id=' . $new_id . ' ', | |
| 1124 | - '"viewId":"' . $new_id . '"', | |
| 1125 | - ), | |
| 1126 | - $string | |
| 1127 | - ); | |
| 1128 | - unset( $old_id, $new_id ); | |
| 1129 | - } | |
| 1130 | - | |
| 1131 | - return $string; | |
| 1132 | - } | |
| 1133 | - | |
| 1134 | - /** | |
| 1135 | 833 | * @param string $content |
| 1136 | 834 | * @return string |
| 1137 | 835 | */ |
| 1138 | 836 | private static function maybe_prepare_json_view_content( $content ) { |
| @@ -1185,9 +883,9 @@ | ||
| 1185 | 883 | 'key' => (string) $meta->meta_key, |
| 1186 | 884 | 'value' => (string) $meta->meta_value, |
| 1187 | 885 | ); |
| 1188 | 886 | |
| 1189 | - // Switch old form and field ids to new ones. | |
| 887 | + //switch old form and field ids to new ones | |
| 1190 | 888 | if ( 'frm_form_id' === $m['key'] && isset( $imported['forms'][ (int) $m['value'] ] ) ) { |
| 1191 | 889 | $m['value'] = $imported['forms'][ (int) $m['value'] ]; |
| 1192 | 890 | } else { |
| 1193 | 891 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| @@ -1203,36 +901,10 @@ | ||
| 1203 | 901 | $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
| 1204 | 902 | } |
| 1205 | 903 | } |
| 1206 | 904 | |
| 1207 | - if ( ! empty( $m['value']['map_address_fields'] ) ) { | |
| 1208 | - foreach ( $m['value']['map_address_fields'] as $address_field_key => $address_field_id ) { | |
| 1209 | - if ( isset( $frm_duplicate_ids[ $address_field_id ] ) ) { | |
| 1210 | - $m['value']['map_address_fields'][ $address_field_key ] = $frm_duplicate_ids[ $address_field_id ]; | |
| 1211 | - } | |
| 1212 | - } | |
| 1213 | - } | |
| 1214 | - | |
| 1215 | - if ( ! empty( $m['value']['calendar_options'] ) ) { | |
| 1216 | - foreach ( $m['value']['calendar_options'] as $calendar_option_group_key => $calendar_option ) { | |
| 1217 | - if ( isset( $frm_duplicate_ids[ $calendar_option['value'] ] ) ) { | |
| 1218 | - $m['value']['calendar_options'][ $calendar_option_group_key ]['value'] = $frm_duplicate_ids[ $calendar_option['value'] ]; | |
| 1219 | - } | |
| 1220 | - } | |
| 1221 | - } | |
| 1222 | - | |
| 1223 | - if ( ! empty( $m['value']['timeline_options'] ) ) { | |
| 1224 | - foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) { | |
| 1225 | - foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) { | |
| 1226 | - if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) { | |
| 1227 | - $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ]; | |
| 1228 | - } | |
| 1229 | - } | |
| 1230 | - } | |
| 1231 | - } | |
| 1232 | - | |
| 1233 | 905 | $check_dup_array = array(); |
| 1234 | - if ( ! empty( $m['value']['order_by'] ) ) { | |
| 906 | + if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) { | |
| 1235 | 907 | if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
| 1236 | 908 | $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
| 1237 | 909 | } elseif ( is_array( $m['value']['order_by'] ) ) { |
| 1238 | 910 | $check_dup_array[] = 'order_by'; |
| @@ -1238,9 +910,9 @@ | ||
| 1238 | 910 | $check_dup_array[] = 'order_by'; |
| 1239 | 911 | } |
| 1240 | 912 | } |
| 1241 | 913 | |
| 1242 | - if ( ! empty( $m['value']['where'] ) ) { | |
| 914 | + if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) { | |
| 1243 | 915 | $check_dup_array[] = 'where'; |
| 1244 | 916 | } |
| 1245 | 917 | |
| 1246 | 918 | foreach ( $check_dup_array as $check_k ) { |
| @@ -1250,11 +922,11 @@ | ||
| 1250 | 922 | } |
| 1251 | 923 | unset( $mk, $mv ); |
| 1252 | 924 | } |
| 1253 | 925 | } |
| 1254 | - }//end if | |
| 1255 | - }//end if | |
| 1256 | - }//end if | |
| 926 | + } | |
| 927 | + } | |
| 928 | + } | |
| 1257 | 929 | |
| 1258 | 930 | if ( ! is_array( $m['value'] ) ) { |
| 1259 | 931 | $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] ); |
| 1260 | 932 | } |
| @@ -1268,10 +940,10 @@ | ||
| 1268 | 940 | |
| 1269 | 941 | /** |
| 1270 | 942 | * Add terms to post |
| 1271 | 943 | * |
| 1272 | - * @param array $post By reference. | |
| 1273 | - * @param object $item The XML object data. | |
| 944 | + * @param array $post by reference | |
| 945 | + * @param object $item The XML object data | |
| 1274 | 946 | */ |
| 1275 | 947 | private static function populate_taxonomies( &$post, $item ) { |
| 1276 | 948 | foreach ( $item->category as $c ) { |
| 1277 | 949 | $att = $c->attributes(); |
| @@ -1296,13 +968,13 @@ | ||
| 1296 | 968 | } |
| 1297 | 969 | |
| 1298 | 970 | $post['tax_input'][ $taxonomy ][] = $name; |
| 1299 | 971 | unset( $name ); |
| 1300 | - }//end foreach | |
| 972 | + } | |
| 1301 | 973 | } |
| 1302 | 974 | |
| 1303 | 975 | /** |
| 1304 | - * Edit post if the key and created time match. | |
| 976 | + * Edit post if the key and created time match | |
| 1305 | 977 | */ |
| 1306 | 978 | private static function maybe_editing_post( &$post ) { |
| 1307 | 979 | $match_by = array( |
| 1308 | 980 | 'post_type' => $post['post_type'], |
| @@ -1310,9 +982,9 @@ | ||
| 1310 | 982 | 'post_status' => $post['post_status'], |
| 1311 | 983 | 'posts_per_page' => 1, |
| 1312 | 984 | ); |
| 1313 | 985 | |
| 1314 | - if ( in_array( $post['post_status'], array( 'trash', 'draft' ), true ) ) { | |
| 986 | + if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) { | |
| 1315 | 987 | $match_by['include'] = $post['post_id']; |
| 1316 | 988 | unset( $match_by['name'] ); |
| 1317 | 989 | } |
| 1318 | 990 | |
| @@ -1323,42 +995,26 @@ | ||
| 1323 | 995 | $post['ID'] = current( $editing )->ID; |
| 1324 | 996 | } |
| 1325 | 997 | } |
| 1326 | 998 | |
| 1327 | - /** | |
| 1328 | - * @param array $post | |
| 1329 | - * @param int $post_id | |
| 1330 | - * @return void | |
| 1331 | - */ | |
| 1332 | 999 | private static function update_postmeta( &$post, $post_id ) { |
| 1333 | 1000 | foreach ( $post['postmeta'] as $k => $v ) { |
| 1334 | - switch ( $k ) { | |
| 1335 | - case '_edit_last': | |
| 1336 | - $v = FrmAppHelper::get_user_id_param( $v ); | |
| 1337 | - 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 | + } | |
| 1338 | 1008 | |
| 1339 | - case '_thumbnail_id': | |
| 1340 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 1341 | - // Change the attachment ID. | |
| 1342 | - $field_obj = FrmFieldFactory::get_field_type( 'file' ); | |
| 1343 | - $v = $field_obj->get_file_id( $v ); | |
| 1344 | - } | |
| 1345 | - break; | |
| 1009 | + if ( 'frm_dyncontent' === $k && is_array( $v ) ) { | |
| 1010 | + $v = json_encode( $v ); | |
| 1011 | + } | |
| 1346 | 1012 | |
| 1347 | - case 'frm_dyncontent': | |
| 1348 | - if ( is_array( $v ) ) { | |
| 1349 | - $v = json_encode( $v ); | |
| 1350 | - } | |
| 1351 | - break; | |
| 1013 | + update_post_meta( $post_id, $k, $v ); | |
| 1352 | 1014 | |
| 1353 | - case 'frm_param': | |
| 1354 | - add_rewrite_endpoint( $v, EP_PERMALINK | EP_PAGES ); | |
| 1355 | - break; | |
| 1356 | - }//end switch | |
| 1357 | - | |
| 1358 | - update_post_meta( $post_id, $k, $v ); | |
| 1359 | 1015 | unset( $k, $v ); |
| 1360 | - }//end foreach | |
| 1016 | + } | |
| 1361 | 1017 | } |
| 1362 | 1018 | |
| 1363 | 1019 | /** |
| 1364 | 1020 | * @param array $post |
| @@ -1373,11 +1029,28 @@ | ||
| 1373 | 1029 | } |
| 1374 | 1030 | } |
| 1375 | 1031 | } |
| 1376 | 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 | + | |
| 1377 | 1050 | private static function maybe_update_stylesheet( $imported ) { |
| 1378 | - $new_styles = ! empty( $imported['imported']['styles'] ); | |
| 1379 | - $updated_styles = ! empty( $imported['updated']['styles'] ); | |
| 1051 | + $new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); | |
| 1052 | + $updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); | |
| 1380 | 1053 | if ( $new_styles || $updated_styles ) { |
| 1381 | 1054 | if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { |
| 1382 | 1055 | $frm_style = new FrmStyle(); |
| 1383 | 1056 | $frm_style->update( 'default' ); |
| @@ -1388,44 +1061,20 @@ | ||
| 1388 | 1061 | } |
| 1389 | 1062 | } |
| 1390 | 1063 | |
| 1391 | 1064 | /** |
| 1392 | - * @param mixed $result | |
| 1393 | 1065 | * @param string $message |
| 1394 | - * @param array $errors | |
| 1395 | 1066 | */ |
| 1396 | 1067 | public static function parse_message( $result, &$message, &$errors ) { |
| 1397 | 1068 | if ( is_wp_error( $result ) ) { |
| 1398 | 1069 | $errors[] = $result->get_error_message(); |
| 1399 | - | |
| 1400 | - // Remove the SimpleXML_parse_error from the WP_Error object to avoid | |
| 1401 | - // displaying duplicate error messages from $result->get_error_message() | |
| 1402 | - $error_codes = $result->get_error_codes(); | |
| 1403 | - $error_details = array(); | |
| 1404 | - foreach ( $error_codes as $error_code ) { | |
| 1405 | - // Clone WP_Error data because WP_Error removes all error messages and data | |
| 1406 | - // associated with the specified error code when an item is removed. | |
| 1407 | - // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source | |
| 1408 | - $error_details = $result->get_error_data( $error_code ); | |
| 1409 | - if ( $error_code === 'SimpleXML_parse_error' ) { | |
| 1410 | - $result->remove( $error_code ); | |
| 1411 | - break; | |
| 1412 | - } | |
| 1413 | - } | |
| 1414 | - | |
| 1415 | - if ( ! empty( $error_details ) ) { | |
| 1416 | - $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) ); | |
| 1417 | - } | |
| 1418 | - | |
| 1070 | + } elseif ( ! $result ) { | |
| 1419 | 1071 | return; |
| 1420 | - }//end if | |
| 1421 | - | |
| 1422 | - if ( ! $result ) { | |
| 1423 | - return; | |
| 1424 | 1072 | } |
| 1425 | 1073 | |
| 1426 | 1074 | if ( ! is_array( $result ) ) { |
| 1427 | 1075 | $message = is_string( $result ) ? $result : htmlentities( print_r( $result, 1 ) ); |
| 1076 | + | |
| 1428 | 1077 | return; |
| 1429 | 1078 | } |
| 1430 | 1079 | |
| 1431 | 1080 | $t_strings = array( |
| @@ -1452,30 +1101,18 @@ | ||
| 1452 | 1101 | $message .= '</li>'; |
| 1453 | 1102 | } |
| 1454 | 1103 | } |
| 1455 | 1104 | |
| 1456 | - if ( $message === '<ul>' ) { | |
| 1105 | + if ( $message == '<ul>' ) { | |
| 1457 | 1106 | $message = ''; |
| 1458 | 1107 | $errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
| 1459 | 1108 | } else { |
| 1460 | 1109 | self::add_form_link_to_message( $result, $message ); |
| 1461 | 1110 | |
| 1462 | - /** | |
| 1463 | - * @since 5.3 | |
| 1464 | - * | |
| 1465 | - * @param string $message | |
| 1466 | - * @param array $result | |
| 1467 | - */ | |
| 1468 | - $message = apply_filters( 'frm_xml_parsed_message', $message, $result ); | |
| 1469 | 1111 | $message .= '</ul>'; |
| 1470 | 1112 | } |
| 1471 | 1113 | } |
| 1472 | 1114 | |
| 1473 | - /** | |
| 1474 | - * @param int $m | |
| 1475 | - * @param string $type | |
| 1476 | - * @param array<string> $s_message | |
| 1477 | - */ | |
| 1478 | 1115 | public static function item_count_message( $m, $type, &$s_message ) { |
| 1479 | 1116 | if ( ! $m ) { |
| 1480 | 1117 | return; |
| 1481 | 1118 | } |
| @@ -1489,9 +1126,9 @@ | ||
| 1489 | 1126 | 'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ), |
| 1490 | 1127 | /* translators: %1$s: Number of items */ |
| 1491 | 1128 | 'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ), |
| 1492 | 1129 | /* translators: %1$s: Number of items */ |
| 1493 | - '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 ), | |
| 1494 | 1131 | /* translators: %1$s: Number of items */ |
| 1495 | 1132 | 'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ), |
| 1496 | 1133 | /* translators: %1$s: Number of items */ |
| 1497 | 1134 | 'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ), |
| @@ -1498,23 +1135,9 @@ | ||
| 1498 | 1135 | /* translators: %1$s: Number of items */ |
| 1499 | 1136 | 'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ), |
| 1500 | 1137 | ); |
| 1501 | 1138 | |
| 1502 | - if ( isset( $strings[ $type ] ) ) { | |
| 1503 | - $s_message[] = $strings[ $type ]; | |
| 1504 | - } else { | |
| 1505 | - $string = ' ' . $m . ' ' . ucfirst( $type ); | |
| 1506 | - | |
| 1507 | - /** | |
| 1508 | - * @since 5.3 | |
| 1509 | - * | |
| 1510 | - * @param string $string Message string for imported item. | |
| 1511 | - * @param int $m Number of item that was imported. | |
| 1512 | - * } | |
| 1513 | - */ | |
| 1514 | - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m ); | |
| 1515 | - $s_message[] = $string; | |
| 1516 | - } | |
| 1139 | + $s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type ); | |
| 1517 | 1140 | } |
| 1518 | 1141 | |
| 1519 | 1142 | /** |
| 1520 | 1143 | * If a single form was imported, include a link in the success message. |
| @@ -1591,9 +1214,8 @@ | ||
| 1591 | 1214 | * @since 3.06 |
| 1592 | 1215 | */ |
| 1593 | 1216 | public static function prepare_field_for_export( &$field ) { |
| 1594 | 1217 | self::remove_default_field_options( $field ); |
| 1595 | - self::add_image_src_to_image_options( $field ); | |
| 1596 | 1218 | } |
| 1597 | 1219 | |
| 1598 | 1220 | /** |
| 1599 | 1221 | * Remove defaults from field options too |
| @@ -1625,41 +1247,8 @@ | ||
| 1625 | 1247 | $field->field_options = serialize( $options ); |
| 1626 | 1248 | } |
| 1627 | 1249 | |
| 1628 | 1250 | /** |
| 1629 | - * Add image "src" key to each image option so the image can be imported to another website. | |
| 1630 | - * | |
| 1631 | - * @since 5.5.1 | |
| 1632 | - * | |
| 1633 | - * @param stdClass $field | |
| 1634 | - * @return void | |
| 1635 | - */ | |
| 1636 | - private static function add_image_src_to_image_options( $field ) { | |
| 1637 | - if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) { | |
| 1638 | - return; | |
| 1639 | - } | |
| 1640 | - | |
| 1641 | - $updated = false; | |
| 1642 | - $options = $field->options; | |
| 1643 | - FrmAppHelper::unserialize_or_decode( $options ); | |
| 1644 | - | |
| 1645 | - if ( ! $options || ! is_array( $options ) ) { | |
| 1646 | - return; | |
| 1647 | - } | |
| 1648 | - | |
| 1649 | - foreach ( $options as $key => $option ) { | |
| 1650 | - if ( is_array( $option ) && ! empty( $option['image'] ) ) { | |
| 1651 | - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] ); | |
| 1652 | - $updated = true; | |
| 1653 | - } | |
| 1654 | - } | |
| 1655 | - | |
| 1656 | - if ( $updated ) { | |
| 1657 | - $field->options = maybe_serialize( $options ); | |
| 1658 | - } | |
| 1659 | - } | |
| 1660 | - | |
| 1661 | - /** | |
| 1662 | 1251 | * @since 3.06.03 |
| 1663 | 1252 | */ |
| 1664 | 1253 | private static function default_field_options( $type ) { |
| 1665 | 1254 | $defaults = FrmFieldsHelper::get_default_field_options( $type ); |
| @@ -1711,10 +1300,10 @@ | ||
| 1711 | 1300 | public static function cdata( $str ) { |
| 1712 | 1301 | FrmAppHelper::unserialize_or_decode( $str ); |
| 1713 | 1302 | if ( is_array( $str ) ) { |
| 1714 | 1303 | $str = json_encode( $str ); |
| 1715 | - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) { | |
| 1716 | - $str = FrmAppHelper::maybe_utf8_encode( $str ); | |
| 1304 | + } elseif ( seems_utf8( $str ) === false ) { | |
| 1305 | + $str = utf8_encode( $str ); | |
| 1717 | 1306 | } |
| 1718 | 1307 | |
| 1719 | 1308 | if ( is_numeric( $str ) ) { |
| 1720 | 1309 | return $str; |
| @@ -1758,13 +1347,9 @@ | ||
| 1758 | 1347 | |
| 1759 | 1348 | /** |
| 1760 | 1349 | * Migrate post settings to form action |
| 1761 | 1350 | * |
| 1762 | - * @param array $form_options | |
| 1763 | - * @param int $form_id | |
| 1764 | 1351 | * @param string $post_type |
| 1765 | - * @param array $imported | |
| 1766 | - * @param bool $switch | |
| 1767 | 1352 | */ |
| 1768 | 1353 | private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
| 1769 | 1354 | if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) { |
| 1770 | 1355 | return; |
| @@ -1833,9 +1418,9 @@ | ||
| 1833 | 1418 | |
| 1834 | 1419 | if ( ! $exists ) { |
| 1835 | 1420 | // this isn't an email, but we need to use a class that will always be included |
| 1836 | 1421 | FrmDb::save_json_post( $new_action ); |
| 1837 | - ++$imported['imported']['actions']; | |
| 1422 | + $imported['imported']['actions'] ++; | |
| 1838 | 1423 | } |
| 1839 | 1424 | } |
| 1840 | 1425 | |
| 1841 | 1426 | /** |
| @@ -1842,11 +1427,11 @@ | ||
| 1842 | 1427 | * Switch old field IDs for new field IDs in emails and post |
| 1843 | 1428 | * |
| 1844 | 1429 | * @since 2.0 |
| 1845 | 1430 | * |
| 1846 | - * @param array $post_content Check for old field IDs. | |
| 1847 | - * @param array $basic_fields Fields with string or int saved. | |
| 1848 | - * @param array $array_fields Fields with arrays saved. | |
| 1431 | + * @param array $post_content - check for old field IDs | |
| 1432 | + * @param array $basic_fields - fields with string or int saved | |
| 1433 | + * @param array $array_fields - fields with arrays saved | |
| 1849 | 1434 | * |
| 1850 | 1435 | * @return string $post_content - new field IDs |
| 1851 | 1436 | */ |
| 1852 | 1437 | private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) { |
| @@ -1927,12 +1512,12 @@ | ||
| 1927 | 1512 | ); |
| 1928 | 1513 | |
| 1929 | 1514 | if ( empty( $exists ) ) { |
| 1930 | 1515 | FrmDb::save_json_post( $new_notification ); |
| 1931 | - ++$imported['imported']['actions']; | |
| 1516 | + $imported['imported']['actions'] ++; | |
| 1932 | 1517 | } |
| 1933 | 1518 | unset( $new_notification ); |
| 1934 | - }//end foreach | |
| 1519 | + } | |
| 1935 | 1520 | |
| 1936 | 1521 | self::remove_deprecated_notification_settings( $form_id, $form_options ); |
| 1937 | 1522 | } |
| 1938 | 1523 | |
| @@ -1941,9 +1526,9 @@ | ||
| 1941 | 1526 | * |
| 1942 | 1527 | * @since 2.05 |
| 1943 | 1528 | * |
| 1944 | 1529 | * @param int|string $form_id |
| 1945 | - * @param array $form_options | |
| 1530 | + * @param array $form_options | |
| 1946 | 1531 | */ |
| 1947 | 1532 | private static function remove_deprecated_notification_settings( $form_id, $form_options ) { |
| 1948 | 1533 | $delete_settings = array( 'notification', 'autoresponder', 'email_to' ); |
| 1949 | 1534 | foreach ( $delete_settings as $index ) { |
| @@ -1954,9 +1539,9 @@ | ||
| 1954 | 1539 | FrmForm::update( $form_id, array( 'options' => $form_options ) ); |
| 1955 | 1540 | } |
| 1956 | 1541 | |
| 1957 | 1542 | private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) { |
| 1958 | - if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) { | |
| 1543 | + if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) { | |
| 1959 | 1544 | // add old settings into notification array |
| 1960 | 1545 | $form_options['notification'] = array( 0 => $form_options ); |
| 1961 | 1546 | } elseif ( isset( $form_options['notification']['email_to'] ) ) { |
| 1962 | 1547 | // make sure it's in the correct format |
| @@ -1986,10 +1571,10 @@ | ||
| 1986 | 1571 | $new_notification = array(); |
| 1987 | 1572 | self::setup_new_notification( $new_notification, $notification, $atts ); |
| 1988 | 1573 | |
| 1989 | 1574 | $notifications[] = $new_notification; |
| 1990 | - }//end foreach | |
| 1991 | - }//end if | |
| 1575 | + } | |
| 1576 | + } | |
| 1992 | 1577 | } |
| 1993 | 1578 | |
| 1994 | 1579 | private static function format_email_data( &$atts, $notification ) { |
| 1995 | 1580 | // Format email_to |
| @@ -2085,9 +1670,9 @@ | ||
| 2085 | 1670 | |
| 2086 | 1671 | /** |
| 2087 | 1672 | * Switch field IDs in pre-2.0 email conditional logic |
| 2088 | 1673 | * |
| 2089 | - * @param array $post_content Pass by reference. | |
| 1674 | + * @param $post_content array, pass by reference | |
| 2090 | 1675 | */ |
| 2091 | 1676 | private static function switch_email_condition_field_ids( &$post_content ) { |
| 2092 | 1677 | // Switch field IDs in conditional logic |
| 2093 | 1678 | if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { |
| @@ -2103,9 +1688,9 @@ | ||
| 2103 | 1688 | private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) { |
| 2104 | 1689 | if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) { |
| 2105 | 1690 | // migrate autoresponder |
| 2106 | 1691 | |
| 2107 | - $email_field = $form_options['ar_email_to'] ?? 0; | |
| 1692 | + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0; | |
| 2108 | 1693 | if ( strpos( $email_field, '|' ) ) { |
| 2109 | 1694 | // data from entries field |
| 2110 | 1695 | $email_field = explode( '|', $email_field ); |
| 2111 | 1696 | if ( isset( $email_field[1] ) ) { |
| @@ -2119,18 +1704,18 @@ | ||
| 2119 | 1704 | $notification = $form_options; |
| 2120 | 1705 | $new_notification2 = array( |
| 2121 | 1706 | 'post_content' => array( |
| 2122 | 1707 | 'email_message' => $notification['ar_email_message'], |
| 2123 | - 'email_subject' => $notification['ar_email_subject'] ?? '', | |
| 1708 | + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '', | |
| 2124 | 1709 | 'email_to' => $email_field, |
| 2125 | - 'plain_text' => $notification['ar_plain_text'] ?? 0, | |
| 1710 | + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0, | |
| 2126 | 1711 | 'inc_user_info' => 0, |
| 2127 | 1712 | ), |
| 2128 | 1713 | 'post_name' => $form_id . '_email_' . count( $notifications ), |
| 2129 | 1714 | ); |
| 2130 | 1715 | |
| 2131 | - $reply_to = $notification['ar_reply_to'] ?? ''; | |
| 2132 | - $reply_to_name = $notification['ar_reply_to_name'] ?? ''; | |
| 1716 | + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : ''; | |
| 1717 | + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : ''; | |
| 2133 | 1718 | |
| 2134 | 1719 | if ( ! empty( $reply_to ) ) { |
| 2135 | 1720 | $new_notification2['post_content']['reply_to'] = $reply_to; |
| 2136 | 1721 | } |
| @@ -2140,17 +1725,17 @@ | ||
| 2140 | 1725 | } |
| 2141 | 1726 | |
| 2142 | 1727 | $notifications[] = $new_notification2; |
| 2143 | 1728 | unset( $new_notification2 ); |
| 2144 | - }//end if | |
| 1729 | + } | |
| 2145 | 1730 | } |
| 2146 | 1731 | |
| 2147 | 1732 | /** |
| 2148 | 1733 | * PHP 8 backward compatibility for the libxml_disable_entity_loader function |
| 2149 | 1734 | * |
| 2150 | - * @param bool $loader | |
| 1735 | + * @param boolean $disable | |
| 2151 | 1736 | * |
| 2152 | - * @return bool | |
| 1737 | + * @return boolean | |
| 2153 | 1738 | */ |
| 2154 | 1739 | public static function maybe_libxml_disable_entity_loader( $loader ) { |
| 2155 | 1740 | if ( version_compare( phpversion(), '8.0', '<' ) && function_exists( 'libxml_disable_entity_loader' ) ) { |
| 2156 | 1741 | $loader = libxml_disable_entity_loader( $loader ); // phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated |
| @@ -2161,22 +1746,6 @@ | ||
| 2161 | 1746 | |
| 2162 | 1747 | public static function check_if_libxml_disable_entity_loader_exists() { |
| 2163 | 1748 | return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' ); |
| 2164 | 1749 | } |
| 1750 | +} | |
| 2165 | 1751 | |
| 2166 | - /** | |
| 2167 | - * Get the file types allowed for importing. | |
| 2168 | - * This is used in the "accept" attribute for the file upload input on the XML import page. | |
| 2169 | - * | |
| 2170 | - * @since 6.8.3 | |
| 2171 | - * | |
| 2172 | - * @return array | |
| 2173 | - */ | |
| 2174 | - public static function get_supported_upload_file_types() { | |
| 2175 | - $file_types = array( '.xml' ); | |
| 2176 | - if ( FrmAppHelper::pro_is_installed() ) { | |
| 2177 | - // CSV Importing is only available in Pro. | |
| 2178 | - $file_types[] = '.csv'; | |
| 2179 | - } | |
| 2180 | - return $file_types; | |
| 2181 | - } | |
| 2182 | -} | |