PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.16.1.2
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.16.1.2
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 +286 -652 6.35 → 6.16.1.2 View file →
@@ -9,14 +9,8 @@
9 9 * @var bool 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 - /**
14 - * @param array|string $opt
15 - * @param string $padding
16 - *
17 - * @return void
18 - */
19 13 public static function get_xml_values( $opt, $padding ) {
20 14 if ( is_array( $opt ) ) {
21 15 foreach ( $opt as $ok => $ov ) {
22 16 echo "\n" . esc_html( $padding );
@@ -22,9 +16,8 @@
22 16 echo "\n" . esc_html( $padding );
23 17 $tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok;
24 18 echo '<' . esc_html( $tag ) . '>';
25 19 self::get_xml_values( $ov, $padding . ' ' );
26 -
27 20 if ( is_array( $ov ) ) {
28 21 echo "\n" . esc_html( $padding );
29 22 }
30 23 echo '</' . esc_html( $tag ) . '>';
@@ -35,9 +28,8 @@
35 28 }
36 29
37 30 /**
38 31 * @param string $file
39 - *
40 32 * @return array|WP_Error The items imported
41 33 */
42 34 public static function import_xml( $file ) {
43 35 if ( ! defined( 'WP_IMPORTING' ) ) {
@@ -46,10 +38,10 @@
46 38
47 39 if ( ! class_exists( 'DOMDocument' ) ) {
48 40 $error_message = sprintf(
49 41 /* translators: 1: Documentation link */
50 - __( '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' ), // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
51 - '<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>' // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
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>'
52 44 );
53 45
54 46 return new WP_Error( 'SimpleXML_parse_error', $error_message, libxml_get_errors() );
55 47 }
@@ -61,9 +53,8 @@
61 53
62 54 // LIBXML_COMPACT activates small nodes allocation optimization.
63 55 // Use LIBXML_PARSEHUGE to avoid "parser error : internal error: Huge input lookup" for large (300MB) files.
64 56 $success = $dom->loadXML( $xml_string, LIBXML_COMPACT | LIBXML_PARSEHUGE );
65 -
66 57 if ( ! $success ) {
67 58 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
68 59 }
69 60
@@ -73,9 +64,9 @@
73 64
74 65 $xml = simplexml_import_dom( $dom );
75 66 unset( $dom );
76 67
77 - // Halt if loading produces an error
68 + // halt if loading produces an error
78 69 if ( ! $xml ) {
79 70 return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
80 71 }
81 72
@@ -85,13 +76,12 @@
85 76 /**
86 77 * @since 6.2.3
87 78 *
88 79 * @param string $xml_string
89 - *
90 80 * @return void
91 81 */
92 82 private static function maybe_fix_xml( &$xml_string ) {
93 - if ( ! str_starts_with( $xml_string, '<?xml' ) ) {
83 + if ( '<?xml' !== substr( $xml_string, 0, 5 ) ) {
94 84 // Some XML files have may have unexpected characters at the start.
95 85 $xml_string = substr( $xml_string, strpos( $xml_string, '<?xml' ) );
96 86 }
97 87
@@ -98,20 +88,17 @@
98 88 // The Equity theme adds a <meta name="generator" content="Equity 1.7.13" /> tag using the "the_generator" filter.
99 89 // Strip that out as it breaks the XML import.
100 90 $channel_start_position = strpos( $xml_string, '<channel>' );
101 91 $content_before_channel_tag = substr( $xml_string, 0, $channel_start_position );
102 -
103 - if ( str_starts_with( $content_before_channel_tag, '<meta name="generator" ' ) ) {
104 - return;
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 );
105 100 }
106 -
107 - $content_before_channel_tag = preg_replace(
108 - '/<meta\s+[^>]*name="generator"[^>]*\/>/i',
109 - '',
110 - $content_before_channel_tag,
111 - 1
112 - );
113 - $xml_string = $content_before_channel_tag . substr( $xml_string, $channel_start_position );
114 101 }
115 102
116 103 /**
117 104 * Add terms, forms (form and field ids), posts (post ids), and entries to db, in that order
@@ -119,9 +106,8 @@
119 106 * @since 3.06
120 107 *
121 108 * @param object $xml
122 109 * @param bool $installing_template
123 - *
124 110 * @return array The number of items imported
125 111 */
126 112 public static function import_xml_now( $xml, $installing_template = false ) {
127 113 if ( ! defined( 'WP_IMPORTING' ) ) {
@@ -132,15 +118,13 @@
132 118 $imported = self::pre_import_data();
133 119
134 120 foreach ( array( 'term', 'form', 'view' ) as $item_type ) {
135 121 // Grab cats, tags, and terms, or forms or posts.
136 - if ( ! isset( $xml->{$item_type} ) ) {
137 - continue;
122 + if ( isset( $xml->{$item_type} ) ) {
123 + $function_name = 'import_xml_' . $item_type . 's';
124 + $imported = self::$function_name( $xml->{$item_type}, $imported );
125 + unset( $function_name, $xml->{$item_type} );
138 126 }
139 -
140 - $function_name = 'import_xml_' . $item_type . 's';
141 - $imported = self::$function_name( $xml->{$item_type}, $imported );
142 - unset( $function_name, $xml->{$item_type} );
143 127 }
144 128
145 129 $imported = apply_filters( 'frm_importing_xml', $imported, $xml );
146 130
@@ -155,9 +139,8 @@
155 139 }
156 140
157 141 /**
158 142 * @since 3.06
159 - *
160 143 * @return array
161 144 */
162 145 private static function pre_import_data() {
163 146 $defaults = array(
@@ -180,9 +163,8 @@
180 163
181 164 /**
182 165 * @param SimpleXMLElement $terms
183 166 * @param array $imported
184 - *
185 167 * @return array
186 168 */
187 169 public static function import_xml_terms( $terms, $imported ) {
188 170 foreach ( $terms as $t ) {
@@ -197,9 +179,9 @@
197 179 (string) $t->term_taxonomy,
198 180 array(
199 181 'slug' => (string) $t->term_slug,
200 182 'description' => (string) $t->term_description,
201 - 'parent' => $parent ? $parent : 0,
183 + 'parent' => empty( $parent ) ? 0 : $parent,
202 184 )
203 185 );
204 186
205 187 if ( $term && is_array( $term ) ) {
@@ -216,17 +198,19 @@
216 198 /**
217 199 * @since 2.0.8
218 200 *
219 201 * @param object $t
220 - *
221 202 * @return int|string
222 203 */
223 204 private static function get_term_parent_id( $t ) {
224 205 $parent = (string) $t->term_parent;
225 -
226 - if ( $parent ) {
206 + if ( ! empty( $parent ) ) {
227 207 $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy );
228 - $parent = $parent ? $parent['term_id'] : 0;
208 + if ( $parent ) {
209 + $parent = $parent['term_id'];
210 + } else {
211 + $parent = 0;
212 + }
229 213 }
230 214
231 215 return $parent;
232 216 }
@@ -233,9 +217,8 @@
233 217
234 218 /**
235 219 * @param SimpleXMLElement $forms
236 220 * @param array $imported
237 - *
238 221 * @return array
239 222 */
240 223 public static function import_xml_forms( $forms, $imported ) {
241 224 $child_forms = array();
@@ -247,13 +230,13 @@
247 230 $form = self::fill_form( $item );
248 231
249 232 self::update_custom_style_setting_on_import( $form );
250 233
251 - $this_form = self::maybe_get_form( $form );
234 + $this_form = self::maybe_get_form( $form );
235 +
252 236 $old_id = false;
253 237 $form_fields = false;
254 -
255 - if ( $this_form ) {
238 + if ( ! empty( $this_form ) ) {
256 239 $form_id = $this_form->id;
257 240 $old_id = $this_form->id;
258 241 self::update_form( $this_form, $form, $imported );
259 242
@@ -259,9 +242,8 @@
259 242
260 243 $form_fields = self::get_form_fields( $form_id );
261 244 } else {
262 245 $form_id = FrmForm::create( $form );
263 -
264 246 if ( $form_id ) {
265 247 if ( empty( $form['parent_form_id'] ) ) {
266 248 // Don't include the repeater form in the imported count.
267 249 ++$imported['imported']['forms'];
@@ -294,31 +276,11 @@
294 276 }//end foreach
295 277
296 278 self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms );
297 279
298 - /**
299 - * Fires after all forms in the XML have been imported.
300 - *
301 - * Child forms are imported before their parent form, so per-form fix-ups
302 - * cannot resolve conditional logic that references fields in a form
303 - * imported later. Use this action for cross-form fix-ups that require
304 - * the fully populated `$frm_duplicate_ids` map.
305 - *
306 - * @since 6.31
307 - *
308 - * @param array $imported Summary of imported items, including
309 - * 'forms' => array( old_form_id => new_form_id ).
310 - */
311 - do_action( 'frm_after_import_forms', $imported );
312 -
313 280 return $imported;
314 281 }
315 282
316 - /**
317 - * @param object $item
318 - *
319 - * @return array
320 - */
321 283 private static function fill_form( $item ) {
322 284 $form = array(
323 285 'id' => (int) $item->id,
324 286 'form_key' => (string) $item->form_key,
@@ -346,27 +308,21 @@
346 308
347 309 return $form;
348 310 }
349 311
350 - /**
351 - * @param array $form
352 - *
353 - * @return false|object
354 - */
355 312 private static function maybe_get_form( $form ) {
356 - // If template, allow to edit if form keys match, otherwise, creation date must also match
313 + // if template, allow to edit if form keys match, otherwise, creation date must also match
357 314 $edit_query = array(
358 315 'form_key' => $form['form_key'],
359 316 'is_template' => $form['is_template'],
360 317 );
361 -
362 318 if ( ! $form['is_template'] ) {
363 319 $edit_query['created_at'] = $form['created_at'];
364 320 }
365 321
366 322 $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form );
367 - $form = FrmForm::getAll( $edit_query, '', 1 );
368 323
324 + $form = FrmForm::getAll( $edit_query, '', 1 );
369 325 if ( is_object( $form ) && $form->status === 'trash' ) {
370 326 FrmForm::destroy( $form->id );
371 327 return false;
372 328 }
@@ -373,19 +329,11 @@
373 329
374 330 return $form;
375 331 }
376 332
377 - /**
378 - * @param object $this_form
379 - * @param array $form
380 - * @param array $imported
381 - *
382 - * @return void
383 - */
384 333 private static function update_form( $this_form, $form, &$imported ) {
385 334 $form_id = $this_form->id;
386 335 FrmForm::update( $form_id, $form );
387 -
388 336 if ( empty( $form['parent_form_id'] ) ) {
389 337 // Don't include the repeater form in the updated count.
390 338 ++$imported['updated']['forms'];
391 339 }
@@ -393,43 +341,32 @@
393 341 // Keep track of whether this specific form was updated or not
394 342 $imported['form_status'][ $form_id ] = 'updated';
395 343 }
396 344
397 - /**
398 - * @param int|string $form_id
399 - *
400 - * @return array
401 - */
402 345 private static function get_form_fields( $form_id ) {
403 346 $form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' );
404 347 $old_fields = array();
405 -
406 348 foreach ( $form_fields as $f ) {
407 349 $old_fields[ $f->id ] = $f;
408 350 $old_fields[ $f->field_key ] = $f->id;
409 351 unset( $f );
410 352 }
353 + $form_fields = $old_fields;
411 354
412 - return $old_fields;
355 + return $form_fields;
413 356 }
414 357
415 358 /**
416 359 * Delete any fields attached to this form that were not included in the template
417 - *
418 - * @param array $form_fields
419 - *
420 - * @return void
421 360 */
422 361 private static function delete_removed_fields( $form_fields ) {
423 - if ( ! $form_fields ) {
424 - return;
425 - }
426 -
427 - foreach ( $form_fields as $field ) {
428 - if ( is_object( $field ) ) {
429 - FrmField::destroy( $field->id );
362 + if ( ! empty( $form_fields ) ) {
363 + foreach ( $form_fields as $field ) {
364 + if ( is_object( $field ) ) {
365 + FrmField::destroy( $field->id );
366 + }
367 + unset( $field );
430 368 }
431 - unset( $field );
432 369 }
433 370 }
434 371
435 372 /**
@@ -481,16 +418,14 @@
481 418 * @param array $child_forms
482 419 */
483 420 private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) {
484 421 foreach ( $child_forms as $child_form_id => $old_parent_form_id ) {
485 - if ( ! isset( $imported_forms[ $old_parent_form_id ] ) || (int) $imported_forms[ $old_parent_form_id ] === (int) $old_parent_form_id ) {
486 - continue;
422 + if ( isset( $imported_forms[ $old_parent_form_id ] ) && (int) $imported_forms[ $old_parent_form_id ] !== (int) $old_parent_form_id ) {
423 + // Update all children with this old parent_form_id
424 + $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
425 + FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
426 + do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id );
487 427 }
488 -
489 - // Update all children with this old parent_form_id
490 - $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
491 - FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
492 - do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id );
493 428 }
494 429 }
495 430
496 431 /**
@@ -498,14 +433,8 @@
498 433 *
499 434 * @since 2.0.13
500 435 *
501 436 * TODO: Cut down on params
502 - *
503 - * @param SimpleXMLElement $xml_fields
504 - * @param int|string $form_id
505 - * @param object $this_form
506 - * @param array $form_fields
507 - * @param array $imported
508 437 */
509 438 private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) {
510 439 $in_section = 0;
511 440 $keys_by_original_field_id = array();
@@ -519,10 +448,10 @@
519 448 self::maybe_update_form_select( $f, $imported );
520 449 self::maybe_update_get_values_form_setting( $imported, $f );
521 450 self::migrate_placeholders( $f );
522 451
523 - if ( $this_form ) {
524 - // Check for field to edit by field id
452 + if ( ! empty( $this_form ) ) {
453 + // check for field to edit by field id
525 454 if ( isset( $form_fields[ $f['id'] ] ) ) {
526 455 FrmField::update( $f['id'], $f );
527 456 ++$imported['updated']['fields'];
528 457
@@ -533,11 +462,12 @@
533 462 unset( $form_fields[ $f['field_key'] ] );
534 463 }
535 464 } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) {
536 465 $keys_by_original_field_id[ $f['id'] ] = $f['field_key'];
537 - $old_field_id = $f['id'];
538 466
539 - // Check for field to edit by field key
467 + $old_field_id = $f['id'];
468 +
469 + // check for field to edit by field key
540 470 unset( $f['id'] );
541 471
542 472 FrmField::update( $form_fields[ $f['field_key'] ], $f );
543 473 ++$imported['updated']['fields'];
@@ -553,8 +483,9 @@
553 483 // If no matching field id or key in this form, create the field.
554 484 self::create_imported_field( $f, $imported );
555 485 }//end if
556 486 } else {
487 +
557 488 self::create_imported_field( $f, $imported );
558 489 }//end if
559 490 }//end foreach
560 491
@@ -566,9 +497,8 @@
566 497 /**
567 498 * @since 6.8.4
568 499 *
569 500 * @param array $field_array
570 - *
571 501 * @return array
572 502 */
573 503 private static function update_field_options_with_defaults( $field_array ) {
574 504 $defaults = self::default_field_options( $field_array['type'] );
@@ -601,14 +531,8 @@
601 531 */
602 532 do_action( 'frm_after_existing_field_is_imported', $field_array, $form_fields[ $field_array['field_key'] ], $old_field_id );
603 533 }
604 534
605 - /**
606 - * @param object $field
607 - * @param int|string $form_id
608 - *
609 - * @return array
610 - */
611 535 private static function fill_field( $field, $form_id ) {
612 536 return array(
613 537 'id' => (int) $field->id,
614 538 'field_key' => (string) $field->field_key,
@@ -619,45 +543,14 @@
619 543 'field_order' => (int) $field->field_order,
620 544 'form_id' => (int) $form_id,
621 545 'required' => (int) $field->required,
622 546 'options' => FrmAppHelper::maybe_json_decode( (string) $field->options ),
623 - 'field_options' => self::fill_field_options( $field ),
547 + 'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options ),
624 548 );
625 549 }
626 550
627 551 /**
628 - * Reads a field's options out of the file as an array.
629 - *
630 - * Field options are a settings map, and every step of the import reads them
631 - * as one. A file whose value there cannot be read, because it is empty or
632 - * was written by something other than an export, used to end the whole
633 - * import with a fatal error on the first field it reached. An empty set of
634 - * options is recoverable, since the defaults for the field type fill the
635 - * gaps, so prefer that over stopping.
636 - *
637 - * Unserialize is tried as well as JSON so options written by an older
638 - * version are still read rather than thrown away.
639 - *
640 - * @since 6.35
641 - *
642 - * @param object $field Field element from the file.
643 - *
644 - * @return array
645 - */
646 - private static function fill_field_options( $field ) {
647 - $options = (string) $field->field_options;
648 -
649 - FrmAppHelper::unserialize_or_decode( $options );
650 -
651 - return is_array( $options ) ? $options : array();
652 - }
653 -
654 - /**
655 552 * @since 4.06
656 - *
657 - * @param array $f
658 - *
659 - * @return void
660 553 */
661 554 private static function set_default_value( &$f ) {
662 555 $has_default = array(
663 556 'text',
@@ -671,17 +564,15 @@
671 564 'password',
672 565 'tag',
673 566 );
674 567
675 - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) {
676 - return;
568 + if ( is_array( $f['default_value'] ) && in_array( $f['type'], $has_default, true ) ) {
569 + if ( count( $f['default_value'] ) === 1 ) {
570 + $f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
571 + } else {
572 + $f['default_value'] = reset( $f['default_value'] );
573 + }
677 574 }
678 -
679 - if ( count( $f['default_value'] ) === 1 ) {
680 - $f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
681 - } else {
682 - $f['default_value'] = reset( $f['default_value'] );
683 - }
684 575 }
685 576
686 577 /**
687 578 * Make sure the required indicator is set.
@@ -686,12 +577,8 @@
686 577 /**
687 578 * Make sure the required indicator is set.
688 579 *
689 580 * @since 4.05
690 - *
691 - * @param array $f
692 - *
693 - * @return void
694 581 */
695 582 private static function maybe_add_required( &$f ) {
696 583 if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) {
697 584 $f['field_options']['required_indicator'] = '*';
@@ -701,17 +588,14 @@
701 588 /**
702 589 * Update the current in_section value at the beginning of the field loop
703 590 *
704 591 * @since 2.0.25
705 - *
706 592 * @param int $in_section
707 593 * @param array $f
708 - *
709 - * @return void
710 594 */
711 595 private static function maybe_update_in_section_variable( &$in_section, &$f ) {
712 596 // If we're at the end of a section, switch $in_section is 0
713 - if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ), true ) ) {
597 + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) {
714 598 $in_section = 0;
715 599 }
716 600
717 601 // Update the current field's in_section value
@@ -737,21 +621,16 @@
737 621 if ( ! isset( $imported['forms'] ) ) {
738 622 return;
739 623 }
740 624
741 - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
742 - return;
625 + if ( $f['type'] === 'form' || ( $f['type'] === 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
626 + if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
627 + $form_select = (int) $f['field_options']['form_select'];
628 + if ( isset( $imported['forms'][ $form_select ] ) ) {
629 + $f['field_options']['form_select'] = $imported['forms'][ $form_select ];
630 + }
631 + }
743 632 }
744 -
745 - if ( ! FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
746 - return;
747 - }
748 -
749 - $form_select = (int) $f['field_options']['form_select'];
750 -
751 - if ( isset( $imported['forms'][ $form_select ] ) ) {
752 - $f['field_options']['form_select'] = $imported['forms'][ $form_select ];
753 - }
754 633 }
755 634
756 635 /**
757 636 * Update the get_values_form setting if the form was imported
@@ -759,10 +638,8 @@
759 638 * @since 2.01.0
760 639 *
761 640 * @param array $imported
762 641 * @param array $f
763 - *
764 - * @return void
765 642 */
766 643 private static function maybe_update_get_values_form_setting( $imported, &$f ) {
767 644 if ( ! isset( $imported['forms'] ) ) {
768 645 return;
@@ -767,35 +644,36 @@
767 644 if ( ! isset( $imported['forms'] ) ) {
768 645 return;
769 646 }
770 647
771 - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
772 - return;
648 + if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
649 + $old_form = $f['field_options']['get_values_form'];
650 + if ( isset( $imported['forms'][ $old_form ] ) ) {
651 + $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
652 + }
773 653 }
654 + }
774 655
775 - $old_form = $f['field_options']['get_values_form'];
776 -
777 - if ( isset( $imported['forms'][ $old_form ] ) ) {
778 - $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
779 - }
656 + /**
657 + * If field settings have been migrated, update the values during import.
658 + *
659 + * @since 4.0
660 + */
661 + private static function run_field_migrations( &$f ) {
662 + self::migrate_placeholders( $f );
663 + $f = apply_filters( 'frm_import_xml_field', $f );
780 664 }
781 665
782 666 /**
783 667 * @since 4.0
784 - *
785 - * @param array $f
786 - *
787 - * @return void
788 668 */
789 669 private static function migrate_placeholders( &$f ) {
790 670 $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' );
791 -
792 671 foreach ( $update_values as $k => $v ) {
793 672 $f[ $k ] = $v;
794 673 }
795 674
796 675 $update_values = self::migrate_field_placeholder( $f, 'default_blank' );
797 -
798 676 foreach ( $update_values as $k => $v ) {
799 677 $f[ $k ] = $v;
800 678 }
801 679 }
@@ -804,18 +682,13 @@
804 682 * Move clear_on_focus or default_blank to placeholder.
805 683 * Also called during database migration in FrmMigrate.
806 684 *
807 685 * @since 4.0
808 - *
809 - * @param array|object $field
810 - * @param string $type
811 - *
812 686 * @return array
813 687 */
814 688 public static function migrate_field_placeholder( $field, $type ) {
815 689 $field = (array) $field;
816 690 $field_options = $field['field_options'];
817 -
818 691 if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) {
819 692 return array();
820 693 }
821 694
@@ -828,34 +701,27 @@
828 701 );
829 702
830 703 // If a dropdown placeholder was used, remove the option so it won't be included twice.
831 704 $options = $field['options'];
705 + if ( $type === 'default_blank' && is_array( $options ) ) {
706 + $default_value = $field['default_value'];
707 + if ( is_array( $default_value ) ) {
708 + $default_value = reset( $default_value );
709 + }
832 710
833 - if ( $type !== 'default_blank' || ! is_array( $options ) ) {
834 - return $changes;
835 - }
711 + foreach ( $options as $opt_key => $opt ) {
712 + if ( is_array( $opt ) ) {
713 + $opt = isset( $opt['value'] ) ? $opt['value'] : ( isset( $opt['label'] ) ? $opt['label'] : reset( $opt ) );
714 + }
836 715
837 - $default_value = $field['default_value'];
838 -
839 - if ( is_array( $default_value ) ) {
840 - $default_value = reset( $default_value );
841 - }
842 -
843 - foreach ( $options as $opt_key => $opt ) {
844 - if ( is_array( $opt ) ) {
845 - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
716 + if ( $opt == $default_value ) {
717 + unset( $options[ $opt_key ] );
718 + break;
719 + }
846 720 }
847 -
848 - // phpcs:ignore Universal.Operators.StrictComparisons
849 - if ( $opt == $default_value ) {
850 - unset( $options[ $opt_key ] );
851 - break;
852 - }
721 + $changes['options'] = $options;
853 722 }
854 723
855 - $changes['options'] = $options;
856 - // end if
857 -
858 724 return $changes;
859 725 }
860 726
861 727 /**
@@ -864,10 +730,8 @@
864 730 * @since 2.0.25
865 731 *
866 732 * @param array $f
867 733 * @param array $imported
868 - *
869 - * @return void
870 734 */
871 735 private static function create_imported_field( $f, &$imported ) {
872 736 $f = self::update_field_options_with_defaults( $f );
873 737
@@ -875,15 +739,12 @@
875 739 $f = self::maybe_import_images_for_options( $f );
876 740 }
877 741
878 742 $new_id = FrmField::create( $f );
879 -
880 - if ( ! $new_id ) {
881 - return;
743 + if ( $new_id != false ) {
744 + ++$imported['imported']['fields'];
745 + do_action( 'frm_after_field_is_imported', $f, $new_id );
882 746 }
883 -
884 - ++$imported['imported']['fields'];
885 - do_action( 'frm_after_field_is_imported', $f, $new_id );
886 747 }
887 748
888 749 /**
889 750 * Import images for radio buttons and checkboxes from image src if available.
@@ -890,9 +751,8 @@
890 751 *
891 752 * @since 5.5.1
892 753 *
893 754 * @param array $field
894 - *
895 755 * @return array
896 756 */
897 757 private static function maybe_import_images_for_options( $field ) {
898 758 if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) {
@@ -923,13 +783,10 @@
923 783 /**
924 784 * Fix field ids for fields that already exist prior to import.
925 785 *
926 786 * @since 4.07
927 - *
928 787 * @param int $form_id
929 788 * @param array $keys_by_original_field_id
930 - *
931 - * @return void
932 789 */
933 790 protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) {
934 791 global $frm_duplicate_ids;
935 792
@@ -949,17 +806,15 @@
949 806 $field = (array) $field;
950 807 $frm_duplicate_ids = $keys_by_original_field_id;
951 808 $after = FrmFieldsHelper::switch_field_ids( $field );
952 809
953 - if ( $before['field_options'] === $after['field_options'] ) {
954 - continue;
955 - }
810 + if ( $before['field_options'] !== $after['field_options'] ) {
811 + $frm_duplicate_ids = $field_id_by_key;
812 + $after = FrmFieldsHelper::switch_field_ids( $after );
956 813
957 - $frm_duplicate_ids = $field_id_by_key;
958 - $after = FrmFieldsHelper::switch_field_ids( $after );
959 -
960 - if ( $before['field_options'] !== $after['field_options'] ) {
961 - FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
814 + if ( $before['field_options'] !== $after['field_options'] ) {
815 + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
816 + }
962 817 }
963 818 }
964 819
965 820 $frm_duplicate_ids = $former_duplicate_ids;
@@ -971,10 +826,8 @@
971 826 *
972 827 * @since 2.0.19
973 828 *
974 829 * @param array $form
975 - *
976 - * @return void
977 830 */
978 831 private static function update_custom_style_setting_on_import( &$form ) {
979 832 if ( ! isset( $form['options']['custom_style'] ) ) {
980 833 return;
@@ -982,32 +835,29 @@
982 835
983 836 if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
984 837 // Set to default
985 838 $form['options']['custom_style'] = 1;
839 + } else {
840 + // Replace the style name with the style ID on import
841 + global $wpdb;
842 + $table = $wpdb->prefix . 'posts';
843 + $where = array(
844 + 'post_name' => $form['options']['custom_style'],
845 + 'post_type' => 'frm_styles',
846 + );
847 + $select = 'ID';
848 + $style_id = FrmDb::get_var( $table, $where, $select );
986 849
987 - return;
988 - }
850 + if ( $style_id ) {
851 + $form['options']['custom_style'] = $style_id;
852 + } else {
853 + // save the old style to maybe update after styles import
854 + $form['options']['old_style'] = $form['options']['custom_style'];
989 855
990 - // Replace the style name with the style ID on import
991 - global $wpdb;
992 - $table = $wpdb->prefix . 'posts';
993 - $where = array(
994 - 'post_name' => $form['options']['custom_style'],
995 - 'post_type' => 'frm_styles',
996 - );
997 - $select = 'ID';
998 - $style_id = FrmDb::get_var( $table, $where, $select );
999 -
1000 - if ( $style_id ) {
1001 - $form['options']['custom_style'] = $style_id;
1002 - return;
1003 - }
1004 -
1005 - // Save the old style to maybe update after styles import
1006 - $form['options']['old_style'] = $form['options']['custom_style'];
1007 -
1008 - // Set to default
1009 - $form['options']['custom_style'] = 1;
856 + // Set to default
857 + $form['options']['custom_style'] = 1;
858 + }
859 + }//end if
1010 860 }
1011 861
1012 862 /**
1013 863 * After styles are imported, check for any forms that were linked
@@ -1013,29 +863,22 @@
1013 863 * After styles are imported, check for any forms that were linked
1014 864 * and link them back up.
1015 865 *
1016 866 * @since 2.2.7
1017 - *
1018 - * @param int|string $form_id
1019 - *
1020 - * @return void
1021 867 */
1022 868 private static function update_custom_style_setting_after_import( $form_id ) {
1023 869 $form = FrmForm::getOne( $form_id );
1024 870
1025 - if ( ! $form || ! isset( $form->options['old_style'] ) ) {
1026 - return;
871 + if ( $form && isset( $form->options['old_style'] ) ) {
872 + $form = (array) $form;
873 + $saved_style = $form['options']['custom_style'];
874 + $form['options']['custom_style'] = $form['options']['old_style'];
875 + self::update_custom_style_setting_on_import( $form );
876 + $has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] );
877 + if ( $has_changed ) {
878 + FrmForm::update( $form['id'], $form );
879 + }
1027 880 }
1028 -
1029 - $form = (array) $form;
1030 - $saved_style = $form['options']['custom_style'];
1031 - $form['options']['custom_style'] = $form['options']['old_style'];
1032 - self::update_custom_style_setting_on_import( $form );
1033 - $has_changed = $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style']; // phpcs:ignore Universal.Operators.StrictComparisons, SlevomatCodingStandard.Files.LineLength.LineTooLong
1034 -
1035 - if ( $has_changed ) {
1036 - FrmForm::update( $form['id'], $form );
1037 - }
1038 881 }
1039 882
1040 883 /**
1041 884 * Handle import for all posts. This includes views, styles, pages, and form actions.
@@ -1041,12 +884,11 @@
1041 884 * Handle import for all posts. This includes views, styles, pages, and form actions.
1042 885 *
1043 886 * @param SimpleXMLElement $views
1044 887 * @param array $imported
1045 - *
1046 888 * @return array
1047 889 */
1048 - public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
890 + public static function import_xml_views( $views, $imported ) {
1049 891 $imported['posts'] = array();
1050 892 $form_action_type = FrmFormActionsController::$action_post_type;
1051 893
1052 894 $post_types = array(
@@ -1083,13 +925,8 @@
1083 925 );
1084 926
1085 927 $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] );
1086 928
1087 - // Fix issue with line breaks appearing in descriptions as "rn".
1088 - if ( $post['post_type'] === $form_action_type ) {
1089 - $post['post_content'] = str_replace( '\\\\r\\\\n', '\\r\\n', $post['post_content'] );
1090 - }
1091 -
1092 929 $old_id = $post['post_id'];
1093 930 self::populate_post( $post, $item, $imported );
1094 931
1095 932 unset( $item );
@@ -1094,13 +931,11 @@
1094 931
1095 932 unset( $item );
1096 933
1097 934 $post_id = false;
1098 -
1099 935 if ( $post['post_type'] === $form_action_type ) {
1100 936 $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] );
1101 -
1102 - if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) {
937 + if ( $action_control && is_object( $action_control ) ) {
1103 938 $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] );
1104 939 }
1105 940 unset( $action_control );
1106 941 } elseif ( $post['post_type'] === 'frm_styles' ) {
@@ -1125,9 +960,9 @@
1125 960 if ( ! is_numeric( $post_id ) ) {
1126 961 continue;
1127 962 }
1128 963
1129 - if ( str_contains( $post['post_content'], '[display-frm-data' ) || str_contains( $post['post_content'], '[formidable' ) ) {
964 + if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
1130 965 $posts_with_shortcodes[ $post_id ] = $post;
1131 966 }
1132 967
1133 968 self::update_postmeta( $post, $post_id );
@@ -1133,14 +968,13 @@
1133 968 self::update_postmeta( $post, $post_id );
1134 969 self::update_layout( $post, $post_id );
1135 970
1136 971 $this_type = 'posts';
1137 -
1138 972 if ( isset( $post_types[ $post['post_type'] ] ) ) {
1139 973 $this_type = $post_types[ $post['post_type'] ];
1140 974 }
1141 975
1142 - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) {
976 + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
1143 977 ++$imported['updated'][ $this_type ];
1144 978 } else {
1145 979 ++$imported['imported'][ $this_type ];
1146 980 }
@@ -1161,9 +995,9 @@
1161 995 }
1162 996 unset( $posts_with_shortcodes, $view_ids );
1163 997
1164 998 if ( ! empty( $imported['forms'] ) ) {
1165 - // Clear imported forms style cache to make sure the new styles are applied to the forms
999 + // clear imported forms style cache to make sure the new styles are applied to the forms
1166 1000 self::clear_forms_style_caches( $imported['forms'] );
1167 1001 }
1168 1002
1169 1003 self::maybe_update_stylesheet( $imported );
@@ -1176,10 +1010,8 @@
1176 1010 /**
1177 1011 * Clears styles from cache for imported forms
1178 1012 *
1179 1013 * @param array $imported_forms
1180 - *
1181 - * @return void
1182 1014 */
1183 1015 private static function clear_forms_style_caches( $imported_forms ) {
1184 1016 $where = array(
1185 1017 'id' => $imported_forms,
@@ -1188,19 +1020,18 @@
1188 1020 $forms = FrmDb::get_results( 'frm_forms', $where );
1189 1021
1190 1022 foreach ( $forms as $form ) {
1191 1023 FrmAppHelper::unserialize_or_decode( $form->options );
1192 -
1193 1024 if ( ! $form->options ) {
1194 1025 continue;
1195 1026 }
1196 -
1197 1027 $where = array(
1198 1028 'post_name' => $form->options['old_style'],
1199 1029 'post_type' => FrmStylesController::$post_type,
1200 1030 );
1201 1031
1202 - $select = 'ID';
1032 + $select = 'ID';
1033 +
1203 1034 $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
1204 1035 FrmDb::delete_cache_and_transient( $cache_key, 'post' );
1205 1036 }
1206 1037 }
@@ -1209,13 +1040,12 @@
1209 1040 * Replace old form ids with new ones in a string.
1210 1041 *
1211 1042 * @param string $string
1212 1043 * @param array<int> $form_ids new form ids indexed by old form id.
1213 - *
1214 1044 * @return string
1215 1045 */
1216 1046 private static function switch_form_ids( $string, $form_ids ) {
1217 - if ( ! str_contains( $string, '[formidable' ) ) {
1047 + if ( false === strpos( $string, '[formidable' ) ) {
1218 1048 // Skip string replacing if there are no form shortcodes in string.
1219 1049 return $string;
1220 1050 }
1221 1051
@@ -1242,15 +1072,13 @@
1242 1072
1243 1073 /**
1244 1074 * @param array<array> $posts_with_shortcodes indexed by current post id.
1245 1075 * @param array<int> $view_ids new view ids indexed by old view id.
1246 - *
1247 1076 * @return void
1248 1077 */
1249 1078 private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) {
1250 1079 foreach ( $posts_with_shortcodes as $imported_post_id => $post ) {
1251 1080 $post_content = self::switch_view_ids( $post['post_content'], $view_ids );
1252 -
1253 1081 if ( $post_content === $post['post_content'] ) {
1254 1082 continue;
1255 1083 }
1256 1084
@@ -1267,13 +1095,12 @@
1267 1095 * Replace old view ids with new ones in a string.
1268 1096 *
1269 1097 * @param string $string
1270 1098 * @param array<int> $view_ids new view ids indexed by old view id.
1271 - *
1272 1099 * @return string
1273 1100 */
1274 1101 private static function switch_view_ids( $string, $view_ids ) {
1275 - if ( ! str_contains( $string, '[display-frm-data' ) ) {
1102 + if ( false === strpos( $string, '[display-frm-data' ) ) {
1276 1103 // Skip string replacing if there are no view shortcodes in string.
1277 1104 return $string;
1278 1105 }
1279 1106
@@ -1300,40 +1127,30 @@
1300 1127 }
1301 1128
1302 1129 /**
1303 1130 * @param string $content
1304 - *
1305 1131 * @return string
1306 1132 */
1307 1133 private static function maybe_prepare_json_view_content( $content ) {
1308 1134 $maybe_decoded = FrmAppHelper::maybe_json_decode( $content );
1309 -
1310 1135 if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) {
1311 1136 return FrmAppHelper::prepare_and_encode( $maybe_decoded );
1312 1137 }
1313 -
1314 1138 return $content;
1315 1139 }
1316 1140
1317 - /**
1318 - * @param array $post
1319 - * @param object $item
1320 - * @param array $imported
1321 - *
1322 - * @return void
1323 - */
1324 1141 private static function populate_post( &$post, $item, $imported ) {
1325 1142 if ( isset( $item->attachment_url ) ) {
1326 1143 $post['attachment_url'] = (string) $item->attachment_url;
1327 1144 }
1328 1145
1329 - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1330 - // Update to new form id
1146 + if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1147 + // update to new form id
1331 1148 $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
1332 1149 }
1333 1150
1334 1151 // Don't allow default styles to take over a site's default style
1335 - if ( 'frm_styles' === $post['post_type'] ) {
1152 + if ( 'frm_styles' == $post['post_type'] ) {
1336 1153 $post['menu_order'] = 0;
1337 1154 }
1338 1155
1339 1156 foreach ( $item->postmeta as $meta ) {
@@ -1355,9 +1172,9 @@
1355 1172 * @param array $post
1356 1173 * @param stdClass $meta
1357 1174 * @param array $imported
1358 1175 */
1359 - private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong
1176 + private static function populate_postmeta( &$post, $meta, $imported ) {
1360 1177 global $frm_duplicate_ids;
1361 1178
1362 1179 $m = array(
1363 1180 'key' => (string) $meta->meta_key,
@@ -1369,13 +1186,14 @@
1369 1186 $m['value'] = $imported['forms'][ (int) $m['value'] ];
1370 1187 } else {
1371 1188 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1372 1189
1373 - if ( $frm_duplicate_ids ) {
1190 + if ( ! empty( $frm_duplicate_ids ) ) {
1374 1191 if ( 'frm_dyncontent' === $m['key'] ) {
1375 1192 $m['value'] = self::maybe_prepare_json_view_content( $m['value'] );
1376 1193 $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
1377 1194 } elseif ( 'frm_options' === $m['key'] ) {
1195 +
1378 1196 foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
1379 1197 if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
1380 1198 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1381 1199 }
@@ -1396,21 +1214,9 @@
1396 1214 }
1397 1215 }
1398 1216 }
1399 1217
1400 - if ( ! empty( $m['value']['timeline_options'] ) ) {
1401 - foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) {
1402 - foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) {
1403 - // @mago-expect lint:excessive-nesting
1404 - if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) {
1405 - $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ];
1406 - }
1407 - }
1408 - }
1409 - }
1410 -
1411 1218 $check_dup_array = array();
1412 -
1413 1219 if ( ! empty( $m['value']['order_by'] ) ) {
1414 1220 if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) {
1415 1221 $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ];
1416 1222 } elseif ( is_array( $m['value']['order_by'] ) ) {
@@ -1440,14 +1246,8 @@
1440 1246
1441 1247 $post['postmeta'][ (string) $meta->meta_key ] = $m['value'];
1442 1248 }
1443 1249
1444 - /**
1445 - * @param array $post
1446 - * @param object $layout
1447 - *
1448 - * @return void
1449 - */
1450 1250 private static function populate_layout( &$post, $layout ) {
1451 1251 $post['layout'][ (string) $layout->type ] = (string) $layout->data;
1452 1252 }
1453 1253
@@ -1459,19 +1259,16 @@
1459 1259 */
1460 1260 private static function populate_taxonomies( &$post, $item ) {
1461 1261 foreach ( $item->category as $c ) {
1462 1262 $att = $c->attributes();
1463 -
1464 1263 if ( ! isset( $att['nicename'] ) ) {
1465 1264 continue;
1466 1265 }
1467 1266
1468 1267 $taxonomy = (string) $att['domain'];
1469 -
1470 1268 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1471 1269 $name = (string) $att['nicename'];
1472 1270 $h_term = get_term_by( 'slug', $name, $taxonomy );
1473 -
1474 1271 if ( $h_term ) {
1475 1272 $name = $h_term->term_id;
1476 1273 }
1477 1274 unset( $h_term );
@@ -1489,12 +1286,8 @@
1489 1286 }
1490 1287
1491 1288 /**
1492 1289 * Edit post if the key and created time match.
1493 - *
1494 - * @param array $post By reference.
1495 - *
1496 - * @return void
1497 1290 */
1498 1291 private static function maybe_editing_post( &$post ) {
1499 1292 $match_by = array(
1500 1293 'post_type' => $post['post_type'],
@@ -1509,11 +1302,10 @@
1509 1302 }
1510 1303
1511 1304 $editing = get_posts( $match_by );
1512 1305
1513 - // phpcs:ignore Universal.Operators.StrictComparisons
1514 - if ( $editing && current( $editing )->post_date == $post['post_date'] ) {
1515 - // Set the id of the post to edit
1306 + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
1307 + // set the id of the post to edit
1516 1308 $post['ID'] = current( $editing )->ID;
1517 1309 }
1518 1310 }
1519 1311
@@ -1519,9 +1311,8 @@
1519 1311
1520 1312 /**
1521 1313 * @param array $post
1522 1314 * @param int $post_id
1523 - *
1524 1315 * @return void
1525 1316 */
1526 1317 private static function update_postmeta( &$post, $post_id ) {
1527 1318 foreach ( $post['postmeta'] as $k => $v ) {
@@ -1558,41 +1349,29 @@
1558 1349 * @param array $post
1559 1350 * @param int $post_id
1560 1351 */
1561 1352 private static function update_layout( &$post, $post_id ) {
1562 - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1563 - return;
1353 + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1354 + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1355 + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1356 + if ( $listing_layout || $detail_layout ) {
1357 + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1358 + }
1564 1359 }
1565 -
1566 - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1567 - $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1568 -
1569 - if ( $listing_layout || $detail_layout ) {
1570 - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1571 - }
1572 1360 }
1573 1361
1574 - /**
1575 - * @param array $imported
1576 - *
1577 - * @return void
1578 - */
1579 1362 private static function maybe_update_stylesheet( $imported ) {
1580 1363 $new_styles = ! empty( $imported['imported']['styles'] );
1581 1364 $updated_styles = ! empty( $imported['updated']['styles'] );
1582 -
1583 - if ( ! $new_styles && ! $updated_styles ) {
1584 - return;
1365 + if ( $new_styles || $updated_styles ) {
1366 + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1367 + $frm_style = new FrmStyle();
1368 + $frm_style->update( 'default' );
1369 + }
1370 + foreach ( $imported['forms'] as $form_id ) {
1371 + self::update_custom_style_setting_after_import( $form_id );
1372 + }
1585 1373 }
1586 -
1587 - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1588 - $frm_style = new FrmStyle();
1589 - $frm_style->update( 'default' );
1590 - }
1591 -
1592 - foreach ( $imported['forms'] as $form_id ) {
1593 - self::update_custom_style_setting_after_import( $form_id );
1594 - }
1595 1374 }
1596 1375
1597 1376 /**
1598 1377 * @param mixed $result
@@ -1597,10 +1376,8 @@
1597 1376 /**
1598 1377 * @param mixed $result
1599 1378 * @param string $message
1600 1379 * @param array $errors
1601 - *
1602 - * @return void
1603 1380 */
1604 1381 public static function parse_message( $result, &$message, &$errors ) {
1605 1382 if ( is_wp_error( $result ) ) {
1606 1383 $errors[] = $result->get_error_message();
@@ -1606,16 +1383,15 @@
1606 1383 $errors[] = $result->get_error_message();
1607 1384
1608 1385 // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1609 1386 // displaying duplicate error messages from $result->get_error_message()
1387 + $error_codes = $result->get_error_codes();
1610 1388 $error_details = array();
1611 -
1612 - foreach ( $result->get_error_codes() as $error_code ) {
1389 + foreach ( $error_codes as $error_code ) {
1613 1390 // Clone WP_Error data because WP_Error removes all error messages and data
1614 - // Associated with the specified error code when an item is removed.
1391 + // associated with the specified error code when an item is removed.
1615 1392 // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1616 1393 $error_details = $result->get_error_data( $error_code );
1617 -
1618 1394 if ( $error_code === 'SimpleXML_parse_error' ) {
1619 1395 $result->remove( $error_code );
1620 1396 break;
1621 1397 }
@@ -1620,9 +1396,9 @@
1620 1396 break;
1621 1397 }
1622 1398 }
1623 1399
1624 - if ( $error_details ) {
1400 + if ( ! empty( $error_details ) ) {
1625 1401 $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1626 1402 }
1627 1403
1628 1404 return;
@@ -1642,48 +1418,42 @@
1642 1418 'updated' => __( 'Updated', 'formidable' ),
1643 1419 );
1644 1420
1645 1421 $message = '<ul>';
1646 -
1647 1422 foreach ( $result as $type => $results ) {
1648 1423 if ( ! isset( $t_strings[ $type ] ) ) {
1649 - // Only print imported and updated
1424 + // only print imported and updated
1650 1425 continue;
1651 1426 }
1652 1427
1653 1428 $s_message = array();
1654 -
1655 1429 foreach ( $results as $k => $m ) {
1656 1430 self::item_count_message( $m, $k, $s_message );
1657 1431 unset( $k, $m );
1658 1432 }
1659 1433
1660 - if ( ! $s_message ) {
1661 - continue;
1434 + if ( ! empty( $s_message ) ) {
1435 + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1436 + $message .= implode( ', ', $s_message );
1437 + $message .= '</li>';
1662 1438 }
1439 + }
1663 1440
1664 - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1665 - $message .= implode( ', ', $s_message );
1666 - $message .= '</li>';
1667 - }//end foreach
1668 -
1669 1441 if ( $message === '<ul>' ) {
1670 1442 $message = '';
1671 1443 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1444 + } else {
1445 + self::add_form_link_to_message( $result, $message );
1672 1446
1673 - return;
1447 + /**
1448 + * @since 5.3
1449 + *
1450 + * @param string $message
1451 + * @param array $result
1452 + */
1453 + $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1454 + $message .= '</ul>';
1674 1455 }
1675 -
1676 - self::add_form_link_to_message( $result, $message );
1677 -
1678 - /**
1679 - * @since 5.3
1680 - *
1681 - * @param string $message
1682 - * @param array $result
1683 - */
1684 - $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1685 - $message .= '</ul>';
1686 1456 }
1687 1457
1688 1458 /**
1689 1459 * @param int $m
@@ -1688,10 +1458,8 @@
1688 1458 /**
1689 1459 * @param int $m
1690 1460 * @param string $type
1691 1461 * @param array<string> $s_message
1692 - *
1693 - * @return void
1694 1462 */
1695 1463 public static function item_count_message( $m, $type, &$s_message ) {
1696 1464 if ( ! $m ) {
1697 1465 return;
@@ -1717,22 +1485,21 @@
1717 1485 );
1718 1486
1719 1487 if ( isset( $strings[ $type ] ) ) {
1720 1488 $s_message[] = $strings[ $type ];
1721 - return;
1489 + } else {
1490 + $string = ' ' . $m . ' ' . ucfirst( $type );
1491 +
1492 + /**
1493 + * @since 5.3
1494 + *
1495 + * @param string $string Message string for imported item.
1496 + * @param int $m Number of item that was imported.
1497 + * }
1498 + */
1499 + $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1500 + $s_message[] = $string;
1722 1501 }
1723 -
1724 - $string = ' ' . $m . ' ' . ucfirst( $type );
1725 -
1726 - /**
1727 - * @since 5.3
1728 - *
1729 - * @param string $string Message string for imported item.
1730 - * @param int $m Number of item that was imported.
1731 - * }
1732 - */
1733 - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1734 - $s_message[] = $string;
1735 1502 }
1736 1503
1737 1504 /**
1738 1505 * If a single form was imported, include a link in the success message.
@@ -1737,28 +1504,24 @@
1737 1504 /**
1738 1505 * If a single form was imported, include a link in the success message.
1739 1506 *
1740 1507 * @since 4.0
1741 - *
1742 1508 * @param array $result The response from the XML import.
1743 1509 * @param string $message The response shown on the page after import.
1744 1510 */
1745 1511 private static function add_form_link_to_message( $result, &$message ) {
1746 1512 $total_forms = $result['imported']['forms'] + $result['updated']['forms'];
1747 -
1748 1513 if ( $total_forms > 1 ) {
1749 1514 return;
1750 1515 }
1751 1516
1752 1517 $primary_form = reset( $result['forms'] );
1518 + if ( ! empty( $primary_form ) ) {
1519 + $primary_form = FrmForm::getOne( $primary_form );
1520 + $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id;
1753 1521
1754 - if ( ! $primary_form ) {
1755 - return;
1522 + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1756 1523 }
1757 -
1758 - $primary_form = FrmForm::getOne( $primary_form );
1759 - $form_id = ! empty( $primary_form->parent_form_id ) ? $primary_form->parent_form_id : $primary_form->id;
1760 - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1761 1524 }
1762 1525
1763 1526 /**
1764 1527 * Prepare the form options for export
@@ -1771,17 +1534,22 @@
1771 1534 */
1772 1535 public static function prepare_form_options_for_export( $options ) {
1773 1536 FrmAppHelper::unserialize_or_decode( $options );
1774 1537 // Change custom_style to the post_name instead of ID (1 may be a string)
1775 - $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; // phpcs:ignore Universal.Operators.StrictComparisons
1776 -
1538 + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style'];
1777 1539 if ( $not_default ) {
1778 1540 global $wpdb;
1779 - $table = $wpdb->prefix . 'posts';
1780 - $where = array( 'ID' => $options['custom_style'] );
1781 - $select = 'post_name';
1782 - $style_name = FrmDb::get_var( $table, $where, $select );
1783 - $options['custom_style'] = $style_name ? $style_name : 1;
1541 + $table = $wpdb->prefix . 'posts';
1542 + $where = array( 'ID' => $options['custom_style'] );
1543 + $select = 'post_name';
1544 +
1545 + $style_name = FrmDb::get_var( $table, $where, $select );
1546 +
1547 + if ( $style_name ) {
1548 + $options['custom_style'] = $style_name;
1549 + } else {
1550 + $options['custom_style'] = 1;
1551 + }
1784 1552 }
1785 1553 self::remove_default_form_options( $options );
1786 1554 $options = serialize( $options );
1787 1555
@@ -1792,16 +1560,11 @@
1792 1560 * If the saved value is the same as the default, remove it from the export
1793 1561 * This keeps file size down and prevents overriding global settings after import
1794 1562 *
1795 1563 * @since 3.06
1796 - *
1797 - * @param array $options By reference.
1798 - *
1799 - * @return void
1800 1564 */
1801 1565 private static function remove_default_form_options( &$options ) {
1802 1566 $defaults = FrmFormsHelper::get_default_opts();
1803 -
1804 1567 if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) {
1805 1568 $defaults += FrmProFormsHelper::get_default_opts();
1806 1569 }
1807 1570 self::remove_defaults( $defaults, $options );
@@ -1810,12 +1573,8 @@
1810 1573 /**
1811 1574 * Remove extra settings from field to keep file size down
1812 1575 *
1813 1576 * @since 3.06
1814 - *
1815 - * @param object $field
1816 - *
1817 - * @return void
1818 1577 */
1819 1578 public static function prepare_field_for_export( &$field ) {
1820 1579 self::remove_default_field_options( $field );
1821 1580 self::add_image_src_to_image_options( $field );
@@ -1824,16 +1583,11 @@
1824 1583 /**
1825 1584 * Remove defaults from field options too
1826 1585 *
1827 1586 * @since 3.06
1828 - *
1829 - * @param object $field
1830 - *
1831 - * @return void
1832 1587 */
1833 1588 private static function remove_default_field_options( &$field ) {
1834 1589 $defaults = self::default_field_options( $field->type );
1835 -
1836 1590 if ( empty( $defaults['blank'] ) ) {
1837 1591 $global_settings = new FrmSettings();
1838 1592 $global_defaults = $global_settings->default_options();
1839 1593 $defaults['blank'] = $global_defaults['blank_msg'];
@@ -1861,13 +1615,12 @@
1861 1615 *
1862 1616 * @since 5.5.1
1863 1617 *
1864 1618 * @param stdClass $field
1865 - *
1866 1619 * @return void
1867 1620 */
1868 1621 private static function add_image_src_to_image_options( $field ) {
1869 - if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) {
1622 + if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1870 1623 return;
1871 1624 }
1872 1625
1873 1626 $updated = false;
@@ -1878,14 +1631,12 @@
1878 1631 return;
1879 1632 }
1880 1633
1881 1634 foreach ( $options as $key => $option ) {
1882 - if ( ! is_array( $option ) || empty( $option['image'] ) ) {
1883 - continue;
1635 + if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1636 + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1637 + $updated = true;
1884 1638 }
1885 -
1886 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1887 - $updated = true;
1888 1639 }
1889 1640
1890 1641 if ( $updated ) {
1891 1642 $field->options = maybe_serialize( $options );
@@ -1893,20 +1644,14 @@
1893 1644 }
1894 1645
1895 1646 /**
1896 1647 * @since 3.06.03
1897 - *
1898 - * @param string $type
1899 - *
1900 - * @return array
1901 1648 */
1902 1649 private static function default_field_options( $type ) {
1903 1650 $defaults = FrmFieldsHelper::get_default_field_options( $type );
1904 -
1905 1651 if ( empty( $defaults['custom_html'] ) ) {
1906 1652 $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type );
1907 1653 }
1908 -
1909 1654 return $defaults;
1910 1655 }
1911 1656
1912 1657 /**
@@ -1913,13 +1658,8 @@
1913 1658 * Compare the default array to the saved values and
1914 1659 * remove if they are the same
1915 1660 *
1916 1661 * @since 3.06
1917 - *
1918 - * @param array $defaults
1919 - * @param array $saved
1920 - *
1921 - * @return void
1922 1662 */
1923 1663 private static function remove_defaults( $defaults, &$saved ) {
1924 1664 foreach ( $saved as $key => $value ) {
1925 1665 if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) {
@@ -1931,14 +1671,8 @@
1931 1671 /**
1932 1672 * The line endings may prevent html from being equal when it should
1933 1673 *
1934 1674 * @since 3.06
1935 - *
1936 - * @param string $html_name
1937 - * @param array $defaults
1938 - * @param array $options
1939 - *
1940 - * @return void
1941 1675 */
1942 1676 private static function remove_default_html( $html_name, $defaults, &$options ) {
1943 1677 if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) {
1944 1678 return;
@@ -1945,34 +1679,26 @@
1945 1679 }
1946 1680
1947 1681 $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
1948 1682 $default_html = $defaults[ $html_name ];
1949 -
1950 - // phpcs:ignore Universal.Operators.StrictComparisons
1951 1683 if ( $old_html == $default_html ) {
1952 1684 unset( $options[ $html_name ] );
1685 +
1953 1686 return;
1954 1687 }
1955 1688
1956 1689 // Account for some of the older field default HTML.
1957 1690 $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html );
1958 -
1959 - if ( $old_html === $default_html ) {
1691 + if ( $old_html == $default_html ) {
1960 1692 unset( $options[ $html_name ] );
1961 1693 }
1962 1694 }
1963 1695
1964 - /**
1965 - * @param string $str
1966 - *
1967 - * @return string
1968 - */
1969 1696 public static function cdata( $str ) {
1970 1697 FrmAppHelper::unserialize_or_decode( $str );
1971 -
1972 1698 if ( is_array( $str ) ) {
1973 1699 $str = json_encode( $str );
1974 - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) {
1700 + } elseif ( seems_utf8( $str ) === false ) {
1975 1701 $str = FrmAppHelper::maybe_utf8_encode( $str );
1976 1702 }
1977 1703
1978 1704 if ( is_numeric( $str ) ) {
@@ -1981,9 +1707,11 @@
1981 1707
1982 1708 self::remove_invalid_characters_from_xml( $str );
1983 1709
1984 1710 // $str = ent2ncr(esc_html( $str));
1985 - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1711 + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1712 +
1713 + return $str;
1986 1714 }
1987 1715
1988 1716 /**
1989 1717 * Remove <US> character (unit separator) from exported strings
@@ -1990,10 +1718,8 @@
1990 1718 *
1991 1719 * @since 2.0.22
1992 1720 *
1993 1721 * @param string $str
1994 - *
1995 - * @return void
1996 1722 */
1997 1723 private static function remove_invalid_characters_from_xml( &$str ) {
1998 1724 // Remove <US> character
1999 1725 $str = str_replace( '\x1F', '', $str );
@@ -1998,18 +1724,8 @@
1998 1724 // Remove <US> character
1999 1725 $str = str_replace( '\x1F', '', $str );
2000 1726 }
2001 1727
2002 - /**
2003 - * Migrate form settings to actions
2004 - *
2005 - * @param array $form_options
2006 - * @param int $form_id
2007 - * @param array $imported
2008 - * @param bool $switch
2009 - *
2010 - * @return void
2011 - */
2012 1728 public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) {
2013 1729 // Get post type
2014 1730 $post_type = FrmFormActionsController::$action_post_type;
2015 1731
@@ -2034,9 +1750,9 @@
2034 1750 * @param array $imported
2035 1751 * @param bool $switch
2036 1752 */
2037 1753 private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2038 - if ( empty( $form_options['create_post'] ) ) {
1754 + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
2039 1755 return;
2040 1756 }
2041 1757
2042 1758 $new_action = array(
@@ -2088,9 +1804,8 @@
2088 1804 $array_fields = array( 'post_category', 'post_custom_fields' );
2089 1805
2090 1806 $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
2091 1807 }
2092 -
2093 1808 $new_action['post_content'] = json_encode( $new_action['post_content'] );
2094 1809
2095 1810 $exists = get_posts(
2096 1811 array(
@@ -2100,15 +1815,13 @@
2100 1815 'numberposts' => 1,
2101 1816 )
2102 1817 );
2103 1818
2104 - if ( $exists ) {
2105 - return;
1819 + if ( ! $exists ) {
1820 + // this isn't an email, but we need to use a class that will always be included
1821 + FrmDb::save_json_post( $new_action );
1822 + ++$imported['imported']['actions'];
2106 1823 }
2107 -
2108 - // This isn't an email, but we need to use a class that will always be included
2109 - FrmDb::save_json_post( $new_action );
2110 - ++$imported['imported']['actions'];
2111 1824 }
2112 1825
2113 1826 /**
2114 1827 * Switch old field IDs for new field IDs in emails and post
@@ -2125,9 +1838,9 @@
2125 1838 global $frm_duplicate_ids;
2126 1839
2127 1840 // If there aren't IDs that were switched, end now
2128 1841 if ( ! $frm_duplicate_ids ) {
2129 - return null;
1842 + return;
2130 1843 }
2131 1844
2132 1845 // Get old IDs
2133 1846 $old = array_keys( $frm_duplicate_ids );
@@ -2136,13 +1849,11 @@
2136 1849 $new = array_values( $frm_duplicate_ids );
2137 1850
2138 1851 // Do a str_replace with each item to set the new IDs
2139 1852 foreach ( $post_content as $key => $setting ) {
2140 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2141 1853 if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
2142 1854 // Replace old IDs with new IDs
2143 1855 $post_content[ $key ] = str_replace( $old, $new, $setting );
2144 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2145 1856 } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
2146 1857 foreach ( $setting as $k => $val ) {
2147 1858 // Replace old IDs with new IDs
2148 1859 $post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
@@ -2153,19 +1864,8 @@
2153 1864
2154 1865 return $post_content;
2155 1866 }
2156 1867
2157 - /**
2158 - * Migrate email settings to action
2159 - *
2160 - * @param array $form_options
2161 - * @param int $form_id
2162 - * @param string $post_type
2163 - * @param array $imported
2164 - * @param bool $switch
2165 - *
2166 - * @return void
2167 - */
2168 1868 private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2169 1869 // No old notifications or autoresponders to carry over
2170 1870 if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) {
2171 1871 return;
@@ -2179,9 +1879,9 @@
2179 1879
2180 1880 // Migrate autoresponders
2181 1881 self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
2182 1882
2183 - if ( ! $notifications ) {
1883 + if ( empty( $notifications ) ) {
2184 1884 return;
2185 1885 }
2186 1886
2187 1887 foreach ( $notifications as $new_notification ) {
@@ -2192,8 +1892,9 @@
2192 1892 $new_notification['post_status'] = 'publish';
2193 1893
2194 1894 // Switch field IDs and keys, if needed
2195 1895 if ( $switch ) {
1896 +
2196 1897 // Switch field IDs in email conditional logic
2197 1898 self::switch_email_condition_field_ids( $new_notification['post_content'] );
2198 1899
2199 1900 // Switch all other field IDs in email
@@ -2198,9 +1899,8 @@
2198 1899
2199 1900 // Switch all other field IDs in email
2200 1901 $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
2201 1902 }
2202 -
2203 1903 $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
2204 1904
2205 1905 $exists = get_posts(
2206 1906 array(
@@ -2210,9 +1910,9 @@
2210 1910 'numberposts' => 1,
2211 1911 )
2212 1912 );
2213 1913
2214 - if ( ! $exists ) {
1914 + if ( empty( $exists ) ) {
2215 1915 FrmDb::save_json_post( $new_notification );
2216 1916 ++$imported['imported']['actions'];
2217 1917 }
2218 1918 unset( $new_notification );
@@ -2227,43 +1927,31 @@
2227 1927 * @since 2.05
2228 1928 *
2229 1929 * @param int|string $form_id
2230 1930 * @param array $form_options
2231 - *
2232 - * @return void
2233 1931 */
2234 1932 private static function remove_deprecated_notification_settings( $form_id, $form_options ) {
2235 1933 $delete_settings = array( 'notification', 'autoresponder', 'email_to' );
2236 -
2237 1934 foreach ( $delete_settings as $index ) {
2238 1935 if ( isset( $form_options[ $index ] ) ) {
2239 1936 unset( $form_options[ $index ] );
2240 1937 }
2241 1938 }
2242 -
2243 1939 FrmForm::update( $form_id, array( 'options' => $form_options ) );
2244 1940 }
2245 1941
2246 - /**
2247 - * Migrate notifications to action
2248 - *
2249 - * @param array $form_options
2250 - * @param int $form_id
2251 - * @param array $notifications
2252 - *
2253 - * @return void
2254 - */
2255 1942 private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) {
2256 1943 if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) {
2257 - // Add old settings into notification array
1944 + // add old settings into notification array
2258 1945 $form_options['notification'] = array( 0 => $form_options );
2259 1946 } elseif ( isset( $form_options['notification']['email_to'] ) ) {
2260 - // Make sure it's in the correct format
1947 + // make sure it's in the correct format
2261 1948 $form_options['notification'] = array( 0 => $form_options['notification'] );
2262 1949 }
2263 1950
2264 1951 if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
2265 1952 foreach ( $form_options['notification'] as $email_key => $notification ) {
1953 +
2266 1954 $atts = array(
2267 1955 'email_to' => '',
2268 1956 'reply_to' => '',
2269 1957 'reply_to_name' => '',
@@ -2274,9 +1962,9 @@
2274 1962
2275 1963 // Format the email data
2276 1964 self::format_email_data( $atts, $notification );
2277 1965
2278 - if ( ! empty( $notification['twilio'] ) ) {
1966 + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
2279 1967 do_action( 'frm_create_twilio_action', $atts, $notification );
2280 1968 }
2281 1969
2282 1970 // Setup the new notification
@@ -2287,16 +1975,8 @@
2287 1975 }//end foreach
2288 1976 }//end if
2289 1977 }
2290 1978
2291 - /**
2292 - * Format email data
2293 - *
2294 - * @param array $atts
2295 - * @param array $notification
2296 - *
2297 - * @return void
2298 - */
2299 1979 private static function format_email_data( &$atts, $notification ) {
2300 1980 // Format email_to
2301 1981 self::format_email_to_data( $atts, $notification );
2302 1982
@@ -2304,16 +1984,14 @@
2304 1984 $reply_fields = array(
2305 1985 'reply_to' => '',
2306 1986 'reply_to_name' => '',
2307 1987 );
2308 -
2309 1988 foreach ( $reply_fields as $f => $val ) {
2310 1989 if ( isset( $notification[ $f ] ) ) {
2311 1990 $atts[ $f ] = $notification[ $f ];
2312 -
2313 - if ( 'custom' === $notification[ $f ] ) {
1991 + if ( 'custom' == $notification[ $f ] ) {
2314 1992 $atts[ $f ] = $notification[ 'cust_' . $f ];
2315 - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) {
1993 + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
2316 1994 $atts[ $f ] = '[' . $atts[ $f ] . ']';
2317 1995 }
2318 1996 }
2319 1997 unset( $f, $val );
@@ -2320,27 +1998,21 @@
2320 1998 }
2321 1999
2322 2000 // Format event
2323 2001 $atts['event'] = array( 'create' );
2324 -
2325 - // phpcs:ignore Universal.Operators.StrictComparisons
2326 2002 if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
2327 2003 $atts['event'][] = 'update';
2328 - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons
2004 + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
2329 2005 $atts['event'] = array( 'update' );
2330 2006 }
2331 2007 }
2332 2008
2333 - /**
2334 - * Format email_to data
2335 - *
2336 - * @param array $atts
2337 - * @param array $notification
2338 - *
2339 - * @return void
2340 - */
2341 2009 private static function format_email_to_data( &$atts, $notification ) {
2342 - $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array();
2010 + if ( isset( $notification['email_to'] ) ) {
2011 + $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] );
2012 + } else {
2013 + $atts['email_to'] = array();
2014 + }
2343 2015
2344 2016 if ( isset( $notification['also_email_to'] ) ) {
2345 2017 $email_fields = (array) $notification['also_email_to'];
2346 2018 $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
@@ -2347,36 +2019,24 @@
2347 2019 unset( $email_fields );
2348 2020 }
2349 2021
2350 2022 foreach ( $atts['email_to'] as $key => $email_field ) {
2023 +
2351 2024 if ( is_numeric( $email_field ) ) {
2352 2025 $atts['email_to'][ $key ] = '[' . $email_field . ']';
2353 2026 }
2354 2027
2355 - if ( ! str_contains( $email_field, '|' ) ) {
2356 - continue;
2028 + if ( strpos( $email_field, '|' ) ) {
2029 + $email_opt = explode( '|', $email_field );
2030 + if ( isset( $email_opt[0] ) ) {
2031 + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2032 + }
2033 + unset( $email_opt );
2357 2034 }
2358 -
2359 - $email_opt = explode( '|', $email_field );
2360 -
2361 - if ( isset( $email_opt[0] ) ) {
2362 - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2363 - }
2364 - unset( $email_opt );
2365 2035 }
2366 -
2367 2036 $atts['email_to'] = implode( ', ', $atts['email_to'] );
2368 2037 }
2369 2038
2370 - /**
2371 - * Setup new notification
2372 - *
2373 - * @param array $new_notification
2374 - * @param array $notification
2375 - * @param array $atts
2376 - *
2377 - * @return void
2378 - */
2379 2039 private static function setup_new_notification( &$new_notification, $notification, $atts ) {
2380 2040 // Set up new notification
2381 2041 $new_notification = array(
2382 2042 'post_content' => array(
@@ -2387,13 +2047,12 @@
2387 2047 );
2388 2048
2389 2049 // Add more fields to the new notification
2390 2050 $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' );
2391 -
2392 2051 foreach ( $add_fields as $add_field ) {
2393 2052 if ( isset( $notification[ $add_field ] ) ) {
2394 2053 $new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
2395 - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) {
2054 + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
2396 2055 $new_notification['post_content'][ $add_field ] = 0;
2397 2056 } else {
2398 2057 $new_notification['post_content'][ $add_field ] = '';
2399 2058 }
@@ -2404,9 +2063,9 @@
2404 2063 $new_notification['post_content']['reply_to'] = $atts['reply_to'];
2405 2064
2406 2065 // Set from
2407 2066 if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) {
2408 - $new_notification['post_content']['from'] = ( ! empty( $atts['reply_to_name'] ) ? $atts['reply_to_name'] : '[sitename]' ) . ' <' . ( ! empty( $atts['reply_to'] ) ? $atts['reply_to'] : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
2067 + $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>';
2409 2068 }
2410 2069 }
2411 2070
2412 2071 /**
@@ -2412,81 +2071,63 @@
2412 2071 /**
2413 2072 * Switch field IDs in pre-2.0 email conditional logic
2414 2073 *
2415 2074 * @param array $post_content Pass by reference.
2416 - *
2417 - * @return void
2418 2075 */
2419 2076 private static function switch_email_condition_field_ids( &$post_content ) {
2420 2077 // Switch field IDs in conditional logic
2421 - if ( ! isset( $post_content['conditions'] ) || ! is_array( $post_content['conditions'] ) ) {
2422 - return;
2423 - }
2424 -
2425 - foreach ( $post_content['conditions'] as $email_key => $val ) {
2426 - if ( is_numeric( $email_key ) ) {
2427 - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2078 + if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) {
2079 + foreach ( $post_content['conditions'] as $email_key => $val ) {
2080 + if ( is_numeric( $email_key ) ) {
2081 + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2082 + }
2083 + unset( $email_key, $val );
2428 2084 }
2429 - unset( $email_key, $val );
2430 2085 }
2431 2086 }
2432 2087
2433 - /**
2434 - * Migrate autoresponder to action
2435 - *
2436 - * @param array $form_options
2437 - * @param int $form_id
2438 - * @param array $notifications
2439 - *
2440 - * @return void
2441 - */
2442 2088 private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) {
2443 - if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) {
2444 - return;
2445 - }
2089 + if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) {
2090 + // migrate autoresponder
2446 2091
2447 - // Migrate autoresponder
2092 + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0;
2093 + if ( strpos( $email_field, '|' ) ) {
2094 + // data from entries field
2095 + $email_field = explode( '|', $email_field );
2096 + if ( isset( $email_field[1] ) ) {
2097 + $email_field = $email_field[1];
2098 + }
2099 + }
2100 + if ( is_numeric( $email_field ) && ! empty( $email_field ) ) {
2101 + $email_field = '[' . $email_field . ']';
2102 + }
2448 2103
2449 - $email_field = $form_options['ar_email_to'] ?? 0;
2104 + $notification = $form_options;
2105 + $new_notification2 = array(
2106 + 'post_content' => array(
2107 + 'email_message' => $notification['ar_email_message'],
2108 + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '',
2109 + 'email_to' => $email_field,
2110 + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0,
2111 + 'inc_user_info' => 0,
2112 + ),
2113 + 'post_name' => $form_id . '_email_' . count( $notifications ),
2114 + );
2450 2115
2451 - if ( str_contains( $email_field, '|' ) ) {
2452 - // Data from entries field
2453 - $email_field = explode( '|', $email_field );
2116 + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : '';
2117 + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : '';
2454 2118
2455 - if ( isset( $email_field[1] ) ) {
2456 - $email_field = $email_field[1];
2119 + if ( ! empty( $reply_to ) ) {
2120 + $new_notification2['post_content']['reply_to'] = $reply_to;
2457 2121 }
2458 - }
2459 2122
2460 - if ( is_numeric( $email_field ) && $email_field ) {
2461 - $email_field = '[' . $email_field . ']';
2462 - }
2123 + if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) {
2124 + $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>';
2125 + }
2463 2126
2464 - $notification = $form_options;
2465 - $new_notification2 = array(
2466 - 'post_content' => array(
2467 - 'email_message' => $notification['ar_email_message'],
2468 - 'email_subject' => $notification['ar_email_subject'] ?? '',
2469 - 'email_to' => $email_field,
2470 - 'plain_text' => $notification['ar_plain_text'] ?? 0,
2471 - 'inc_user_info' => 0,
2472 - ),
2473 - 'post_name' => $form_id . '_email_' . count( $notifications ),
2474 - );
2475 -
2476 - $reply_to = $notification['ar_reply_to'] ?? '';
2477 - $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2478 -
2479 - if ( $reply_to ) {
2480 - $new_notification2['post_content']['reply_to'] = $reply_to;
2481 - }
2482 -
2483 - if ( $reply_to || $reply_to_name ) {
2484 - $new_notification2['post_content']['from'] = ( $reply_to_name ? $reply_to_name : '[sitename]' ) . ' <' . ( $reply_to ? $reply_to : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
2485 - }
2486 -
2487 - $notifications[] = $new_notification2;
2488 - unset( $new_notification2 );
2127 + $notifications[] = $new_notification2;
2128 + unset( $new_notification2 );
2129 + }//end if
2489 2130 }
2490 2131
2491 2132 /**
2492 2133 * PHP 8 backward compatibility for the libxml_disable_entity_loader function
@@ -2502,13 +2143,8 @@
2502 2143
2503 2144 return $loader;
2504 2145 }
2505 2146
2506 - /**
2507 - * PHP 8 backward compatibility for the libxml_disable_entity_loader function
2508 - *
2509 - * @return bool
2510 - */
2511 2147 public static function check_if_libxml_disable_entity_loader_exists() {
2512 2148 return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' );
2513 2149 }
2514 2150
@@ -2521,13 +2157,11 @@
2521 2157 * @return array
2522 2158 */
2523 2159 public static function get_supported_upload_file_types() {
2524 2160 $file_types = array( '.xml' );
2525 -
2526 2161 if ( FrmAppHelper::pro_is_installed() ) {
2527 2162 // CSV Importing is only available in Pro.
2528 2163 $file_types[] = '.csv';
2529 2164 }
2530 -
2531 2165 return $file_types;
2532 2166 }
2533 2167 }