PluginProbe
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More / 6.24.1
Formidable Forms – WordPress Form Builder for Contact Forms, Calculators, Quizzes & More v6.24.1
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 +283 -607 6.31 → 6.24.1 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,
@@ -625,12 +549,8 @@
625 549 }
626 550
627 551 /**
628 552 * @since 4.06
629 - *
630 - * @param array $f
631 - *
632 - * @return void
633 553 */
634 554 private static function set_default_value( &$f ) {
635 555 $has_default = array(
636 556 'text',
@@ -644,17 +564,15 @@
644 564 'password',
645 565 'tag',
646 566 );
647 567
648 - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) {
649 - 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 + }
650 574 }
651 -
652 - if ( count( $f['default_value'] ) === 1 ) {
653 - $f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
654 - } else {
655 - $f['default_value'] = reset( $f['default_value'] );
656 - }
657 575 }
658 576
659 577 /**
660 578 * Make sure the required indicator is set.
@@ -659,12 +577,8 @@
659 577 /**
660 578 * Make sure the required indicator is set.
661 579 *
662 580 * @since 4.05
663 - *
664 - * @param array $f
665 - *
666 - * @return void
667 581 */
668 582 private static function maybe_add_required( &$f ) {
669 583 if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) {
670 584 $f['field_options']['required_indicator'] = '*';
@@ -674,17 +588,14 @@
674 588 /**
675 589 * Update the current in_section value at the beginning of the field loop
676 590 *
677 591 * @since 2.0.25
678 - *
679 592 * @param int $in_section
680 593 * @param array $f
681 - *
682 - * @return void
683 594 */
684 595 private static function maybe_update_in_section_variable( &$in_section, &$f ) {
685 596 // If we're at the end of a section, switch $in_section is 0
686 - if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ), true ) ) {
597 + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) {
687 598 $in_section = 0;
688 599 }
689 600
690 601 // Update the current field's in_section value
@@ -710,21 +621,16 @@
710 621 if ( ! isset( $imported['forms'] ) ) {
711 622 return;
712 623 }
713 624
714 - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
715 - 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 + }
716 632 }
717 -
718 - if ( ! FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
719 - return;
720 - }
721 -
722 - $form_select = (int) $f['field_options']['form_select'];
723 -
724 - if ( isset( $imported['forms'][ $form_select ] ) ) {
725 - $f['field_options']['form_select'] = $imported['forms'][ $form_select ];
726 - }
727 633 }
728 634
729 635 /**
730 636 * Update the get_values_form setting if the form was imported
@@ -732,10 +638,8 @@
732 638 * @since 2.01.0
733 639 *
734 640 * @param array $imported
735 641 * @param array $f
736 - *
737 - * @return void
738 642 */
739 643 private static function maybe_update_get_values_form_setting( $imported, &$f ) {
740 644 if ( ! isset( $imported['forms'] ) ) {
741 645 return;
@@ -740,35 +644,36 @@
740 644 if ( ! isset( $imported['forms'] ) ) {
741 645 return;
742 646 }
743 647
744 - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
745 - 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 + }
746 653 }
654 + }
747 655
748 - $old_form = $f['field_options']['get_values_form'];
749 -
750 - if ( isset( $imported['forms'][ $old_form ] ) ) {
751 - $f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
752 - }
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 );
753 664 }
754 665
755 666 /**
756 667 * @since 4.0
757 - *
758 - * @param array $f
759 - *
760 - * @return void
761 668 */
762 669 private static function migrate_placeholders( &$f ) {
763 670 $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' );
764 -
765 671 foreach ( $update_values as $k => $v ) {
766 672 $f[ $k ] = $v;
767 673 }
768 674
769 675 $update_values = self::migrate_field_placeholder( $f, 'default_blank' );
770 -
771 676 foreach ( $update_values as $k => $v ) {
772 677 $f[ $k ] = $v;
773 678 }
774 679 }
@@ -777,18 +682,13 @@
777 682 * Move clear_on_focus or default_blank to placeholder.
778 683 * Also called during database migration in FrmMigrate.
779 684 *
780 685 * @since 4.0
781 - *
782 - * @param array|object $field
783 - * @param string $type
784 - *
785 686 * @return array
786 687 */
787 688 public static function migrate_field_placeholder( $field, $type ) {
788 689 $field = (array) $field;
789 690 $field_options = $field['field_options'];
790 -
791 691 if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) {
792 692 return array();
793 693 }
794 694
@@ -801,34 +701,27 @@
801 701 );
802 702
803 703 // If a dropdown placeholder was used, remove the option so it won't be included twice.
804 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 + }
805 710
806 - if ( $type !== 'default_blank' || ! is_array( $options ) ) {
807 - return $changes;
808 - }
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 + }
809 715
810 - $default_value = $field['default_value'];
811 -
812 - if ( is_array( $default_value ) ) {
813 - $default_value = reset( $default_value );
814 - }
815 -
816 - foreach ( $options as $opt_key => $opt ) {
817 - if ( is_array( $opt ) ) {
818 - $opt = $opt['value'] ?? $opt['label'] ?? reset( $opt );
716 + if ( $opt == $default_value ) {
717 + unset( $options[ $opt_key ] );
718 + break;
719 + }
819 720 }
820 -
821 - // phpcs:ignore Universal.Operators.StrictComparisons
822 - if ( $opt == $default_value ) {
823 - unset( $options[ $opt_key ] );
824 - break;
825 - }
721 + $changes['options'] = $options;
826 722 }
827 723
828 - $changes['options'] = $options;
829 - // end if
830 -
831 724 return $changes;
832 725 }
833 726
834 727 /**
@@ -837,10 +730,8 @@
837 730 * @since 2.0.25
838 731 *
839 732 * @param array $f
840 733 * @param array $imported
841 - *
842 - * @return void
843 734 */
844 735 private static function create_imported_field( $f, &$imported ) {
845 736 $f = self::update_field_options_with_defaults( $f );
846 737
@@ -848,15 +739,12 @@
848 739 $f = self::maybe_import_images_for_options( $f );
849 740 }
850 741
851 742 $new_id = FrmField::create( $f );
852 -
853 - if ( ! $new_id ) {
854 - return;
743 + if ( $new_id != false ) {
744 + ++$imported['imported']['fields'];
745 + do_action( 'frm_after_field_is_imported', $f, $new_id );
855 746 }
856 -
857 - ++$imported['imported']['fields'];
858 - do_action( 'frm_after_field_is_imported', $f, $new_id );
859 747 }
860 748
861 749 /**
862 750 * Import images for radio buttons and checkboxes from image src if available.
@@ -863,9 +751,8 @@
863 751 *
864 752 * @since 5.5.1
865 753 *
866 754 * @param array $field
867 - *
868 755 * @return array
869 756 */
870 757 private static function maybe_import_images_for_options( $field ) {
871 758 if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) {
@@ -896,13 +783,10 @@
896 783 /**
897 784 * Fix field ids for fields that already exist prior to import.
898 785 *
899 786 * @since 4.07
900 - *
901 787 * @param int $form_id
902 788 * @param array $keys_by_original_field_id
903 - *
904 - * @return void
905 789 */
906 790 protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) {
907 791 global $frm_duplicate_ids;
908 792
@@ -922,17 +806,15 @@
922 806 $field = (array) $field;
923 807 $frm_duplicate_ids = $keys_by_original_field_id;
924 808 $after = FrmFieldsHelper::switch_field_ids( $field );
925 809
926 - if ( $before['field_options'] === $after['field_options'] ) {
927 - continue;
928 - }
810 + if ( $before['field_options'] !== $after['field_options'] ) {
811 + $frm_duplicate_ids = $field_id_by_key;
812 + $after = FrmFieldsHelper::switch_field_ids( $after );
929 813
930 - $frm_duplicate_ids = $field_id_by_key;
931 - $after = FrmFieldsHelper::switch_field_ids( $after );
932 -
933 - if ( $before['field_options'] !== $after['field_options'] ) {
934 - 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 + }
935 817 }
936 818 }
937 819
938 820 $frm_duplicate_ids = $former_duplicate_ids;
@@ -944,10 +826,8 @@
944 826 *
945 827 * @since 2.0.19
946 828 *
947 829 * @param array $form
948 - *
949 - * @return void
950 830 */
951 831 private static function update_custom_style_setting_on_import( &$form ) {
952 832 if ( ! isset( $form['options']['custom_style'] ) ) {
953 833 return;
@@ -955,32 +835,29 @@
955 835
956 836 if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
957 837 // Set to default
958 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 );
959 849
960 - return;
961 - }
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'];
962 855
963 - // Replace the style name with the style ID on import
964 - global $wpdb;
965 - $table = $wpdb->prefix . 'posts';
966 - $where = array(
967 - 'post_name' => $form['options']['custom_style'],
968 - 'post_type' => 'frm_styles',
969 - );
970 - $select = 'ID';
971 - $style_id = FrmDb::get_var( $table, $where, $select );
972 -
973 - if ( $style_id ) {
974 - $form['options']['custom_style'] = $style_id;
975 - return;
976 - }
977 -
978 - // Save the old style to maybe update after styles import
979 - $form['options']['old_style'] = $form['options']['custom_style'];
980 -
981 - // Set to default
982 - $form['options']['custom_style'] = 1;
856 + // Set to default
857 + $form['options']['custom_style'] = 1;
858 + }
859 + }//end if
983 860 }
984 861
985 862 /**
986 863 * After styles are imported, check for any forms that were linked
@@ -986,29 +863,22 @@
986 863 * After styles are imported, check for any forms that were linked
987 864 * and link them back up.
988 865 *
989 866 * @since 2.2.7
990 - *
991 - * @param int|string $form_id
992 - *
993 - * @return void
994 867 */
995 868 private static function update_custom_style_setting_after_import( $form_id ) {
996 869 $form = FrmForm::getOne( $form_id );
997 870
998 - if ( ! $form || ! isset( $form->options['old_style'] ) ) {
999 - 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 + }
1000 880 }
1001 -
1002 - $form = (array) $form;
1003 - $saved_style = $form['options']['custom_style'];
1004 - $form['options']['custom_style'] = $form['options']['old_style'];
1005 - self::update_custom_style_setting_on_import( $form );
1006 - $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
1007 -
1008 - if ( $has_changed ) {
1009 - FrmForm::update( $form['id'], $form );
1010 - }
1011 881 }
1012 882
1013 883 /**
1014 884 * Handle import for all posts. This includes views, styles, pages, and form actions.
@@ -1014,12 +884,11 @@
1014 884 * Handle import for all posts. This includes views, styles, pages, and form actions.
1015 885 *
1016 886 * @param SimpleXMLElement $views
1017 887 * @param array $imported
1018 - *
1019 888 * @return array
1020 889 */
1021 - public static function import_xml_views( $views, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh
890 + public static function import_xml_views( $views, $imported ) {
1022 891 $imported['posts'] = array();
1023 892 $form_action_type = FrmFormActionsController::$action_post_type;
1024 893
1025 894 $post_types = array(
@@ -1067,12 +936,10 @@
1067 936
1068 937 unset( $item );
1069 938
1070 939 $post_id = false;
1071 -
1072 940 if ( $post['post_type'] === $form_action_type ) {
1073 941 $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] );
1074 -
1075 942 if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) {
1076 943 $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] );
1077 944 }
1078 945 unset( $action_control );
@@ -1098,9 +965,9 @@
1098 965 if ( ! is_numeric( $post_id ) ) {
1099 966 continue;
1100 967 }
1101 968
1102 - if ( str_contains( $post['post_content'], '[display-frm-data' ) || str_contains( $post['post_content'], '[formidable' ) ) {
969 + if ( false !== strpos( $post['post_content'], '[display-frm-data' ) || false !== strpos( $post['post_content'], '[formidable' ) ) {
1103 970 $posts_with_shortcodes[ $post_id ] = $post;
1104 971 }
1105 972
1106 973 self::update_postmeta( $post, $post_id );
@@ -1106,14 +973,13 @@
1106 973 self::update_postmeta( $post, $post_id );
1107 974 self::update_layout( $post, $post_id );
1108 975
1109 976 $this_type = 'posts';
1110 -
1111 977 if ( isset( $post_types[ $post['post_type'] ] ) ) {
1112 978 $this_type = $post_types[ $post['post_type'] ];
1113 979 }
1114 980
1115 - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) {
981 + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
1116 982 ++$imported['updated'][ $this_type ];
1117 983 } else {
1118 984 ++$imported['imported'][ $this_type ];
1119 985 }
@@ -1134,9 +1000,9 @@
1134 1000 }
1135 1001 unset( $posts_with_shortcodes, $view_ids );
1136 1002
1137 1003 if ( ! empty( $imported['forms'] ) ) {
1138 - // Clear imported forms style cache to make sure the new styles are applied to the forms
1004 + // clear imported forms style cache to make sure the new styles are applied to the forms
1139 1005 self::clear_forms_style_caches( $imported['forms'] );
1140 1006 }
1141 1007
1142 1008 self::maybe_update_stylesheet( $imported );
@@ -1149,10 +1015,8 @@
1149 1015 /**
1150 1016 * Clears styles from cache for imported forms
1151 1017 *
1152 1018 * @param array $imported_forms
1153 - *
1154 - * @return void
1155 1019 */
1156 1020 private static function clear_forms_style_caches( $imported_forms ) {
1157 1021 $where = array(
1158 1022 'id' => $imported_forms,
@@ -1161,19 +1025,18 @@
1161 1025 $forms = FrmDb::get_results( 'frm_forms', $where );
1162 1026
1163 1027 foreach ( $forms as $form ) {
1164 1028 FrmAppHelper::unserialize_or_decode( $form->options );
1165 -
1166 1029 if ( ! $form->options ) {
1167 1030 continue;
1168 1031 }
1169 -
1170 1032 $where = array(
1171 1033 'post_name' => $form->options['old_style'],
1172 1034 'post_type' => FrmStylesController::$post_type,
1173 1035 );
1174 1036
1175 - $select = 'ID';
1037 + $select = 'ID';
1038 +
1176 1039 $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
1177 1040 FrmDb::delete_cache_and_transient( $cache_key, 'post' );
1178 1041 }
1179 1042 }
@@ -1182,13 +1045,12 @@
1182 1045 * Replace old form ids with new ones in a string.
1183 1046 *
1184 1047 * @param string $string
1185 1048 * @param array<int> $form_ids new form ids indexed by old form id.
1186 - *
1187 1049 * @return string
1188 1050 */
1189 1051 private static function switch_form_ids( $string, $form_ids ) {
1190 - if ( ! str_contains( $string, '[formidable' ) ) {
1052 + if ( false === strpos( $string, '[formidable' ) ) {
1191 1053 // Skip string replacing if there are no form shortcodes in string.
1192 1054 return $string;
1193 1055 }
1194 1056
@@ -1215,15 +1077,13 @@
1215 1077
1216 1078 /**
1217 1079 * @param array<array> $posts_with_shortcodes indexed by current post id.
1218 1080 * @param array<int> $view_ids new view ids indexed by old view id.
1219 - *
1220 1081 * @return void
1221 1082 */
1222 1083 private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) {
1223 1084 foreach ( $posts_with_shortcodes as $imported_post_id => $post ) {
1224 1085 $post_content = self::switch_view_ids( $post['post_content'], $view_ids );
1225 -
1226 1086 if ( $post_content === $post['post_content'] ) {
1227 1087 continue;
1228 1088 }
1229 1089
@@ -1240,13 +1100,12 @@
1240 1100 * Replace old view ids with new ones in a string.
1241 1101 *
1242 1102 * @param string $string
1243 1103 * @param array<int> $view_ids new view ids indexed by old view id.
1244 - *
1245 1104 * @return string
1246 1105 */
1247 1106 private static function switch_view_ids( $string, $view_ids ) {
1248 - if ( ! str_contains( $string, '[display-frm-data' ) ) {
1107 + if ( false === strpos( $string, '[display-frm-data' ) ) {
1249 1108 // Skip string replacing if there are no view shortcodes in string.
1250 1109 return $string;
1251 1110 }
1252 1111
@@ -1273,40 +1132,30 @@
1273 1132 }
1274 1133
1275 1134 /**
1276 1135 * @param string $content
1277 - *
1278 1136 * @return string
1279 1137 */
1280 1138 private static function maybe_prepare_json_view_content( $content ) {
1281 1139 $maybe_decoded = FrmAppHelper::maybe_json_decode( $content );
1282 -
1283 1140 if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) {
1284 1141 return FrmAppHelper::prepare_and_encode( $maybe_decoded );
1285 1142 }
1286 -
1287 1143 return $content;
1288 1144 }
1289 1145
1290 - /**
1291 - * @param array $post
1292 - * @param object $item
1293 - * @param array $imported
1294 - *
1295 - * @return void
1296 - */
1297 1146 private static function populate_post( &$post, $item, $imported ) {
1298 1147 if ( isset( $item->attachment_url ) ) {
1299 1148 $post['attachment_url'] = (string) $item->attachment_url;
1300 1149 }
1301 1150
1302 - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1303 - // Update to new form id
1151 + if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1152 + // update to new form id
1304 1153 $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
1305 1154 }
1306 1155
1307 1156 // Don't allow default styles to take over a site's default style
1308 - if ( 'frm_styles' === $post['post_type'] ) {
1157 + if ( 'frm_styles' == $post['post_type'] ) {
1309 1158 $post['menu_order'] = 0;
1310 1159 }
1311 1160
1312 1161 foreach ( $item->postmeta as $meta ) {
@@ -1328,9 +1177,9 @@
1328 1177 * @param array $post
1329 1178 * @param stdClass $meta
1330 1179 * @param array $imported
1331 1180 */
1332 - private static function populate_postmeta( &$post, $meta, $imported ) { // phpcs:ignore SlevomatCodingStandard.Complexity.Cognitive.ComplexityTooHigh, Generic.Metrics.CyclomaticComplexity.MaxExceeded, SlevomatCodingStandard.Files.LineLength.LineTooLong
1181 + private static function populate_postmeta( &$post, $meta, $imported ) {
1333 1182 global $frm_duplicate_ids;
1334 1183
1335 1184 $m = array(
1336 1185 'key' => (string) $meta->meta_key,
@@ -1342,13 +1191,14 @@
1342 1191 $m['value'] = $imported['forms'][ (int) $m['value'] ];
1343 1192 } else {
1344 1193 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1345 1194
1346 - if ( $frm_duplicate_ids ) {
1195 + if ( ! empty( $frm_duplicate_ids ) ) {
1347 1196 if ( 'frm_dyncontent' === $m['key'] ) {
1348 1197 $m['value'] = self::maybe_prepare_json_view_content( $m['value'] );
1349 1198 $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
1350 1199 } elseif ( 'frm_options' === $m['key'] ) {
1200 +
1351 1201 foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
1352 1202 if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
1353 1203 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1354 1204 }
@@ -1372,9 +1222,8 @@
1372 1222
1373 1223 if ( ! empty( $m['value']['timeline_options'] ) ) {
1374 1224 foreach ( $m['value']['timeline_options'] as $timeline_option_group_key => $timeline_group_option ) {
1375 1225 foreach ( $timeline_group_option as $timeline_option_key => $timeline_option ) {
1376 - // @mago-expect lint:excessive-nesting
1377 1226 if ( isset( $frm_duplicate_ids[ $timeline_option ] ) ) {
1378 1227 $m['value']['timeline_options'][ $timeline_option_group_key ][ $timeline_option_key ] = $frm_duplicate_ids[ $timeline_option ];
1379 1228 }
1380 1229 }
@@ -1381,9 +1230,8 @@
1381 1230 }
1382 1231 }
1383 1232
1384 1233 $check_dup_array = array();
1385 -
1386 1234 if ( ! empty( $m['value']['order_by'] ) ) {
1387 1235 if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) {
1388 1236 $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ];
1389 1237 } elseif ( is_array( $m['value']['order_by'] ) ) {
@@ -1413,14 +1261,8 @@
1413 1261
1414 1262 $post['postmeta'][ (string) $meta->meta_key ] = $m['value'];
1415 1263 }
1416 1264
1417 - /**
1418 - * @param array $post
1419 - * @param object $layout
1420 - *
1421 - * @return void
1422 - */
1423 1265 private static function populate_layout( &$post, $layout ) {
1424 1266 $post['layout'][ (string) $layout->type ] = (string) $layout->data;
1425 1267 }
1426 1268
@@ -1432,19 +1274,16 @@
1432 1274 */
1433 1275 private static function populate_taxonomies( &$post, $item ) {
1434 1276 foreach ( $item->category as $c ) {
1435 1277 $att = $c->attributes();
1436 -
1437 1278 if ( ! isset( $att['nicename'] ) ) {
1438 1279 continue;
1439 1280 }
1440 1281
1441 1282 $taxonomy = (string) $att['domain'];
1442 -
1443 1283 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1444 1284 $name = (string) $att['nicename'];
1445 1285 $h_term = get_term_by( 'slug', $name, $taxonomy );
1446 -
1447 1286 if ( $h_term ) {
1448 1287 $name = $h_term->term_id;
1449 1288 }
1450 1289 unset( $h_term );
@@ -1462,12 +1301,8 @@
1462 1301 }
1463 1302
1464 1303 /**
1465 1304 * Edit post if the key and created time match.
1466 - *
1467 - * @param array $post By reference.
1468 - *
1469 - * @return void
1470 1305 */
1471 1306 private static function maybe_editing_post( &$post ) {
1472 1307 $match_by = array(
1473 1308 'post_type' => $post['post_type'],
@@ -1482,11 +1317,10 @@
1482 1317 }
1483 1318
1484 1319 $editing = get_posts( $match_by );
1485 1320
1486 - // phpcs:ignore Universal.Operators.StrictComparisons
1487 - if ( $editing && current( $editing )->post_date == $post['post_date'] ) {
1488 - // Set the id of the post to edit
1321 + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
1322 + // set the id of the post to edit
1489 1323 $post['ID'] = current( $editing )->ID;
1490 1324 }
1491 1325 }
1492 1326
@@ -1492,9 +1326,8 @@
1492 1326
1493 1327 /**
1494 1328 * @param array $post
1495 1329 * @param int $post_id
1496 - *
1497 1330 * @return void
1498 1331 */
1499 1332 private static function update_postmeta( &$post, $post_id ) {
1500 1333 foreach ( $post['postmeta'] as $k => $v ) {
@@ -1531,41 +1364,29 @@
1531 1364 * @param array $post
1532 1365 * @param int $post_id
1533 1366 */
1534 1367 private static function update_layout( &$post, $post_id ) {
1535 - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1536 - return;
1368 + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1369 + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1370 + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1371 + if ( $listing_layout || $detail_layout ) {
1372 + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1373 + }
1537 1374 }
1538 -
1539 - $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1540 - $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1541 -
1542 - if ( $listing_layout || $detail_layout ) {
1543 - FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1544 - }
1545 1375 }
1546 1376
1547 - /**
1548 - * @param array $imported
1549 - *
1550 - * @return void
1551 - */
1552 1377 private static function maybe_update_stylesheet( $imported ) {
1553 1378 $new_styles = ! empty( $imported['imported']['styles'] );
1554 1379 $updated_styles = ! empty( $imported['updated']['styles'] );
1555 -
1556 - if ( ! $new_styles && ! $updated_styles ) {
1557 - return;
1380 + if ( $new_styles || $updated_styles ) {
1381 + if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1382 + $frm_style = new FrmStyle();
1383 + $frm_style->update( 'default' );
1384 + }
1385 + foreach ( $imported['forms'] as $form_id ) {
1386 + self::update_custom_style_setting_after_import( $form_id );
1387 + }
1558 1388 }
1559 -
1560 - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1561 - $frm_style = new FrmStyle();
1562 - $frm_style->update( 'default' );
1563 - }
1564 -
1565 - foreach ( $imported['forms'] as $form_id ) {
1566 - self::update_custom_style_setting_after_import( $form_id );
1567 - }
1568 1389 }
1569 1390
1570 1391 /**
1571 1392 * @param mixed $result
@@ -1570,10 +1391,8 @@
1570 1391 /**
1571 1392 * @param mixed $result
1572 1393 * @param string $message
1573 1394 * @param array $errors
1574 - *
1575 - * @return void
1576 1395 */
1577 1396 public static function parse_message( $result, &$message, &$errors ) {
1578 1397 if ( is_wp_error( $result ) ) {
1579 1398 $errors[] = $result->get_error_message();
@@ -1579,16 +1398,15 @@
1579 1398 $errors[] = $result->get_error_message();
1580 1399
1581 1400 // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1582 1401 // displaying duplicate error messages from $result->get_error_message()
1402 + $error_codes = $result->get_error_codes();
1583 1403 $error_details = array();
1584 -
1585 - foreach ( $result->get_error_codes() as $error_code ) {
1404 + foreach ( $error_codes as $error_code ) {
1586 1405 // Clone WP_Error data because WP_Error removes all error messages and data
1587 - // Associated with the specified error code when an item is removed.
1406 + // associated with the specified error code when an item is removed.
1588 1407 // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1589 1408 $error_details = $result->get_error_data( $error_code );
1590 -
1591 1409 if ( $error_code === 'SimpleXML_parse_error' ) {
1592 1410 $result->remove( $error_code );
1593 1411 break;
1594 1412 }
@@ -1593,9 +1411,9 @@
1593 1411 break;
1594 1412 }
1595 1413 }
1596 1414
1597 - if ( $error_details ) {
1415 + if ( ! empty( $error_details ) ) {
1598 1416 $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1599 1417 }
1600 1418
1601 1419 return;
@@ -1615,48 +1433,42 @@
1615 1433 'updated' => __( 'Updated', 'formidable' ),
1616 1434 );
1617 1435
1618 1436 $message = '<ul>';
1619 -
1620 1437 foreach ( $result as $type => $results ) {
1621 1438 if ( ! isset( $t_strings[ $type ] ) ) {
1622 - // Only print imported and updated
1439 + // only print imported and updated
1623 1440 continue;
1624 1441 }
1625 1442
1626 1443 $s_message = array();
1627 -
1628 1444 foreach ( $results as $k => $m ) {
1629 1445 self::item_count_message( $m, $k, $s_message );
1630 1446 unset( $k, $m );
1631 1447 }
1632 1448
1633 - if ( ! $s_message ) {
1634 - continue;
1449 + if ( ! empty( $s_message ) ) {
1450 + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1451 + $message .= implode( ', ', $s_message );
1452 + $message .= '</li>';
1635 1453 }
1454 + }
1636 1455
1637 - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1638 - $message .= implode( ', ', $s_message );
1639 - $message .= '</li>';
1640 - }//end foreach
1641 -
1642 1456 if ( $message === '<ul>' ) {
1643 1457 $message = '';
1644 1458 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1459 + } else {
1460 + self::add_form_link_to_message( $result, $message );
1645 1461
1646 - return;
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 + $message .= '</ul>';
1647 1470 }
1648 -
1649 - self::add_form_link_to_message( $result, $message );
1650 -
1651 - /**
1652 - * @since 5.3
1653 - *
1654 - * @param string $message
1655 - * @param array $result
1656 - */
1657 - $message = apply_filters( 'frm_xml_parsed_message', $message, $result );
1658 - $message .= '</ul>';
1659 1471 }
1660 1472
1661 1473 /**
1662 1474 * @param int $m
@@ -1661,10 +1473,8 @@
1661 1473 /**
1662 1474 * @param int $m
1663 1475 * @param string $type
1664 1476 * @param array<string> $s_message
1665 - *
1666 - * @return void
1667 1477 */
1668 1478 public static function item_count_message( $m, $type, &$s_message ) {
1669 1479 if ( ! $m ) {
1670 1480 return;
@@ -1690,22 +1500,21 @@
1690 1500 );
1691 1501
1692 1502 if ( isset( $strings[ $type ] ) ) {
1693 1503 $s_message[] = $strings[ $type ];
1694 - return;
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;
1695 1516 }
1696 -
1697 - $string = ' ' . $m . ' ' . ucfirst( $type );
1698 -
1699 - /**
1700 - * @since 5.3
1701 - *
1702 - * @param string $string Message string for imported item.
1703 - * @param int $m Number of item that was imported.
1704 - * }
1705 - */
1706 - $string = apply_filters( 'frm_xml_' . $type . '_count_message', $string, $m );
1707 - $s_message[] = $string;
1708 1517 }
1709 1518
1710 1519 /**
1711 1520 * If a single form was imported, include a link in the success message.
@@ -1710,28 +1519,24 @@
1710 1519 /**
1711 1520 * If a single form was imported, include a link in the success message.
1712 1521 *
1713 1522 * @since 4.0
1714 - *
1715 1523 * @param array $result The response from the XML import.
1716 1524 * @param string $message The response shown on the page after import.
1717 1525 */
1718 1526 private static function add_form_link_to_message( $result, &$message ) {
1719 1527 $total_forms = $result['imported']['forms'] + $result['updated']['forms'];
1720 -
1721 1528 if ( $total_forms > 1 ) {
1722 1529 return;
1723 1530 }
1724 1531
1725 1532 $primary_form = reset( $result['forms'] );
1533 + if ( ! empty( $primary_form ) ) {
1534 + $primary_form = FrmForm::getOne( $primary_form );
1535 + $form_id = empty( $primary_form->parent_form_id ) ? $primary_form->id : $primary_form->parent_form_id;
1726 1536
1727 - if ( ! $primary_form ) {
1728 - return;
1537 + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1729 1538 }
1730 -
1731 - $primary_form = FrmForm::getOne( $primary_form );
1732 - $form_id = ! empty( $primary_form->parent_form_id ) ? $primary_form->parent_form_id : $primary_form->id;
1733 - $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1734 1539 }
1735 1540
1736 1541 /**
1737 1542 * Prepare the form options for export
@@ -1744,17 +1549,22 @@
1744 1549 */
1745 1550 public static function prepare_form_options_for_export( $options ) {
1746 1551 FrmAppHelper::unserialize_or_decode( $options );
1747 1552 // Change custom_style to the post_name instead of ID (1 may be a string)
1748 - $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style']; // phpcs:ignore Universal.Operators.StrictComparisons
1749 -
1553 + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style'];
1750 1554 if ( $not_default ) {
1751 1555 global $wpdb;
1752 - $table = $wpdb->prefix . 'posts';
1753 - $where = array( 'ID' => $options['custom_style'] );
1754 - $select = 'post_name';
1755 - $style_name = FrmDb::get_var( $table, $where, $select );
1756 - $options['custom_style'] = $style_name ? $style_name : 1;
1556 + $table = $wpdb->prefix . 'posts';
1557 + $where = array( 'ID' => $options['custom_style'] );
1558 + $select = 'post_name';
1559 +
1560 + $style_name = FrmDb::get_var( $table, $where, $select );
1561 +
1562 + if ( $style_name ) {
1563 + $options['custom_style'] = $style_name;
1564 + } else {
1565 + $options['custom_style'] = 1;
1566 + }
1757 1567 }
1758 1568 self::remove_default_form_options( $options );
1759 1569 $options = serialize( $options );
1760 1570
@@ -1765,16 +1575,11 @@
1765 1575 * If the saved value is the same as the default, remove it from the export
1766 1576 * This keeps file size down and prevents overriding global settings after import
1767 1577 *
1768 1578 * @since 3.06
1769 - *
1770 - * @param array $options By reference.
1771 - *
1772 - * @return void
1773 1579 */
1774 1580 private static function remove_default_form_options( &$options ) {
1775 1581 $defaults = FrmFormsHelper::get_default_opts();
1776 -
1777 1582 if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) {
1778 1583 $defaults += FrmProFormsHelper::get_default_opts();
1779 1584 }
1780 1585 self::remove_defaults( $defaults, $options );
@@ -1783,12 +1588,8 @@
1783 1588 /**
1784 1589 * Remove extra settings from field to keep file size down
1785 1590 *
1786 1591 * @since 3.06
1787 - *
1788 - * @param object $field
1789 - *
1790 - * @return void
1791 1592 */
1792 1593 public static function prepare_field_for_export( &$field ) {
1793 1594 self::remove_default_field_options( $field );
1794 1595 self::add_image_src_to_image_options( $field );
@@ -1797,16 +1598,11 @@
1797 1598 /**
1798 1599 * Remove defaults from field options too
1799 1600 *
1800 1601 * @since 3.06
1801 - *
1802 - * @param object $field
1803 - *
1804 - * @return void
1805 1602 */
1806 1603 private static function remove_default_field_options( &$field ) {
1807 1604 $defaults = self::default_field_options( $field->type );
1808 -
1809 1605 if ( empty( $defaults['blank'] ) ) {
1810 1606 $global_settings = new FrmSettings();
1811 1607 $global_defaults = $global_settings->default_options();
1812 1608 $defaults['blank'] = $global_defaults['blank_msg'];
@@ -1834,13 +1630,12 @@
1834 1630 *
1835 1631 * @since 5.5.1
1836 1632 *
1837 1633 * @param stdClass $field
1838 - *
1839 1634 * @return void
1840 1635 */
1841 1636 private static function add_image_src_to_image_options( $field ) {
1842 - if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) {
1637 + if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1843 1638 return;
1844 1639 }
1845 1640
1846 1641 $updated = false;
@@ -1851,14 +1646,12 @@
1851 1646 return;
1852 1647 }
1853 1648
1854 1649 foreach ( $options as $key => $option ) {
1855 - if ( ! is_array( $option ) || empty( $option['image'] ) ) {
1856 - continue;
1650 + if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1651 + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1652 + $updated = true;
1857 1653 }
1858 -
1859 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1860 - $updated = true;
1861 1654 }
1862 1655
1863 1656 if ( $updated ) {
1864 1657 $field->options = maybe_serialize( $options );
@@ -1866,20 +1659,14 @@
1866 1659 }
1867 1660
1868 1661 /**
1869 1662 * @since 3.06.03
1870 - *
1871 - * @param string $type
1872 - *
1873 - * @return array
1874 1663 */
1875 1664 private static function default_field_options( $type ) {
1876 1665 $defaults = FrmFieldsHelper::get_default_field_options( $type );
1877 -
1878 1666 if ( empty( $defaults['custom_html'] ) ) {
1879 1667 $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type );
1880 1668 }
1881 -
1882 1669 return $defaults;
1883 1670 }
1884 1671
1885 1672 /**
@@ -1886,13 +1673,8 @@
1886 1673 * Compare the default array to the saved values and
1887 1674 * remove if they are the same
1888 1675 *
1889 1676 * @since 3.06
1890 - *
1891 - * @param array $defaults
1892 - * @param array $saved
1893 - *
1894 - * @return void
1895 1677 */
1896 1678 private static function remove_defaults( $defaults, &$saved ) {
1897 1679 foreach ( $saved as $key => $value ) {
1898 1680 if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) {
@@ -1904,14 +1686,8 @@
1904 1686 /**
1905 1687 * The line endings may prevent html from being equal when it should
1906 1688 *
1907 1689 * @since 3.06
1908 - *
1909 - * @param string $html_name
1910 - * @param array $defaults
1911 - * @param array $options
1912 - *
1913 - * @return void
1914 1690 */
1915 1691 private static function remove_default_html( $html_name, $defaults, &$options ) {
1916 1692 if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) {
1917 1693 return;
@@ -1918,31 +1694,23 @@
1918 1694 }
1919 1695
1920 1696 $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
1921 1697 $default_html = $defaults[ $html_name ];
1922 -
1923 - // phpcs:ignore Universal.Operators.StrictComparisons
1924 1698 if ( $old_html == $default_html ) {
1925 1699 unset( $options[ $html_name ] );
1700 +
1926 1701 return;
1927 1702 }
1928 1703
1929 1704 // Account for some of the older field default HTML.
1930 1705 $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html );
1931 -
1932 - if ( $old_html === $default_html ) {
1706 + if ( $old_html == $default_html ) {
1933 1707 unset( $options[ $html_name ] );
1934 1708 }
1935 1709 }
1936 1710
1937 - /**
1938 - * @param string $str
1939 - *
1940 - * @return string
1941 - */
1942 1711 public static function cdata( $str ) {
1943 1712 FrmAppHelper::unserialize_or_decode( $str );
1944 -
1945 1713 if ( is_array( $str ) ) {
1946 1714 $str = json_encode( $str );
1947 1715 } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) {
1948 1716 $str = FrmAppHelper::maybe_utf8_encode( $str );
@@ -1954,9 +1722,11 @@
1954 1722
1955 1723 self::remove_invalid_characters_from_xml( $str );
1956 1724
1957 1725 // $str = ent2ncr(esc_html( $str));
1958 - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1726 + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1727 +
1728 + return $str;
1959 1729 }
1960 1730
1961 1731 /**
1962 1732 * Remove <US> character (unit separator) from exported strings
@@ -1963,10 +1733,8 @@
1963 1733 *
1964 1734 * @since 2.0.22
1965 1735 *
1966 1736 * @param string $str
1967 - *
1968 - * @return void
1969 1737 */
1970 1738 private static function remove_invalid_characters_from_xml( &$str ) {
1971 1739 // Remove <US> character
1972 1740 $str = str_replace( '\x1F', '', $str );
@@ -1971,18 +1739,8 @@
1971 1739 // Remove <US> character
1972 1740 $str = str_replace( '\x1F', '', $str );
1973 1741 }
1974 1742
1975 - /**
1976 - * Migrate form settings to actions
1977 - *
1978 - * @param array $form_options
1979 - * @param int $form_id
1980 - * @param array $imported
1981 - * @param bool $switch
1982 - *
1983 - * @return void
1984 - */
1985 1743 public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) {
1986 1744 // Get post type
1987 1745 $post_type = FrmFormActionsController::$action_post_type;
1988 1746
@@ -2007,9 +1765,9 @@
2007 1765 * @param array $imported
2008 1766 * @param bool $switch
2009 1767 */
2010 1768 private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2011 - if ( empty( $form_options['create_post'] ) ) {
1769 + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
2012 1770 return;
2013 1771 }
2014 1772
2015 1773 $new_action = array(
@@ -2061,9 +1819,8 @@
2061 1819 $array_fields = array( 'post_category', 'post_custom_fields' );
2062 1820
2063 1821 $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
2064 1822 }
2065 -
2066 1823 $new_action['post_content'] = json_encode( $new_action['post_content'] );
2067 1824
2068 1825 $exists = get_posts(
2069 1826 array(
@@ -2073,15 +1830,13 @@
2073 1830 'numberposts' => 1,
2074 1831 )
2075 1832 );
2076 1833
2077 - if ( $exists ) {
2078 - return;
1834 + if ( ! $exists ) {
1835 + // this isn't an email, but we need to use a class that will always be included
1836 + FrmDb::save_json_post( $new_action );
1837 + ++$imported['imported']['actions'];
2079 1838 }
2080 -
2081 - // This isn't an email, but we need to use a class that will always be included
2082 - FrmDb::save_json_post( $new_action );
2083 - ++$imported['imported']['actions'];
2084 1839 }
2085 1840
2086 1841 /**
2087 1842 * Switch old field IDs for new field IDs in emails and post
@@ -2098,9 +1853,9 @@
2098 1853 global $frm_duplicate_ids;
2099 1854
2100 1855 // If there aren't IDs that were switched, end now
2101 1856 if ( ! $frm_duplicate_ids ) {
2102 - return null;
1857 + return;
2103 1858 }
2104 1859
2105 1860 // Get old IDs
2106 1861 $old = array_keys( $frm_duplicate_ids );
@@ -2109,13 +1864,11 @@
2109 1864 $new = array_values( $frm_duplicate_ids );
2110 1865
2111 1866 // Do a str_replace with each item to set the new IDs
2112 1867 foreach ( $post_content as $key => $setting ) {
2113 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2114 1868 if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
2115 1869 // Replace old IDs with new IDs
2116 1870 $post_content[ $key ] = str_replace( $old, $new, $setting );
2117 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2118 1871 } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
2119 1872 foreach ( $setting as $k => $val ) {
2120 1873 // Replace old IDs with new IDs
2121 1874 $post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
@@ -2126,19 +1879,8 @@
2126 1879
2127 1880 return $post_content;
2128 1881 }
2129 1882
2130 - /**
2131 - * Migrate email settings to action
2132 - *
2133 - * @param array $form_options
2134 - * @param int $form_id
2135 - * @param string $post_type
2136 - * @param array $imported
2137 - * @param bool $switch
2138 - *
2139 - * @return void
2140 - */
2141 1883 private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2142 1884 // No old notifications or autoresponders to carry over
2143 1885 if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) {
2144 1886 return;
@@ -2152,9 +1894,9 @@
2152 1894
2153 1895 // Migrate autoresponders
2154 1896 self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
2155 1897
2156 - if ( ! $notifications ) {
1898 + if ( empty( $notifications ) ) {
2157 1899 return;
2158 1900 }
2159 1901
2160 1902 foreach ( $notifications as $new_notification ) {
@@ -2165,8 +1907,9 @@
2165 1907 $new_notification['post_status'] = 'publish';
2166 1908
2167 1909 // Switch field IDs and keys, if needed
2168 1910 if ( $switch ) {
1911 +
2169 1912 // Switch field IDs in email conditional logic
2170 1913 self::switch_email_condition_field_ids( $new_notification['post_content'] );
2171 1914
2172 1915 // Switch all other field IDs in email
@@ -2171,9 +1914,8 @@
2171 1914
2172 1915 // Switch all other field IDs in email
2173 1916 $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
2174 1917 }
2175 -
2176 1918 $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
2177 1919
2178 1920 $exists = get_posts(
2179 1921 array(
@@ -2183,9 +1925,9 @@
2183 1925 'numberposts' => 1,
2184 1926 )
2185 1927 );
2186 1928
2187 - if ( ! $exists ) {
1929 + if ( empty( $exists ) ) {
2188 1930 FrmDb::save_json_post( $new_notification );
2189 1931 ++$imported['imported']['actions'];
2190 1932 }
2191 1933 unset( $new_notification );
@@ -2200,43 +1942,31 @@
2200 1942 * @since 2.05
2201 1943 *
2202 1944 * @param int|string $form_id
2203 1945 * @param array $form_options
2204 - *
2205 - * @return void
2206 1946 */
2207 1947 private static function remove_deprecated_notification_settings( $form_id, $form_options ) {
2208 1948 $delete_settings = array( 'notification', 'autoresponder', 'email_to' );
2209 -
2210 1949 foreach ( $delete_settings as $index ) {
2211 1950 if ( isset( $form_options[ $index ] ) ) {
2212 1951 unset( $form_options[ $index ] );
2213 1952 }
2214 1953 }
2215 -
2216 1954 FrmForm::update( $form_id, array( 'options' => $form_options ) );
2217 1955 }
2218 1956
2219 - /**
2220 - * Migrate notifications to action
2221 - *
2222 - * @param array $form_options
2223 - * @param int $form_id
2224 - * @param array $notifications
2225 - *
2226 - * @return void
2227 - */
2228 1957 private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) {
2229 1958 if ( ! isset( $form_options['notification'] ) && ! empty( $form_options['email_to'] ) ) {
2230 - // Add old settings into notification array
1959 + // add old settings into notification array
2231 1960 $form_options['notification'] = array( 0 => $form_options );
2232 1961 } elseif ( isset( $form_options['notification']['email_to'] ) ) {
2233 - // Make sure it's in the correct format
1962 + // make sure it's in the correct format
2234 1963 $form_options['notification'] = array( 0 => $form_options['notification'] );
2235 1964 }
2236 1965
2237 1966 if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
2238 1967 foreach ( $form_options['notification'] as $email_key => $notification ) {
1968 +
2239 1969 $atts = array(
2240 1970 'email_to' => '',
2241 1971 'reply_to' => '',
2242 1972 'reply_to_name' => '',
@@ -2247,9 +1977,9 @@
2247 1977
2248 1978 // Format the email data
2249 1979 self::format_email_data( $atts, $notification );
2250 1980
2251 - if ( ! empty( $notification['twilio'] ) ) {
1981 + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
2252 1982 do_action( 'frm_create_twilio_action', $atts, $notification );
2253 1983 }
2254 1984
2255 1985 // Setup the new notification
@@ -2260,16 +1990,8 @@
2260 1990 }//end foreach
2261 1991 }//end if
2262 1992 }
2263 1993
2264 - /**
2265 - * Format email data
2266 - *
2267 - * @param array $atts
2268 - * @param array $notification
2269 - *
2270 - * @return void
2271 - */
2272 1994 private static function format_email_data( &$atts, $notification ) {
2273 1995 // Format email_to
2274 1996 self::format_email_to_data( $atts, $notification );
2275 1997
@@ -2277,16 +1999,14 @@
2277 1999 $reply_fields = array(
2278 2000 'reply_to' => '',
2279 2001 'reply_to_name' => '',
2280 2002 );
2281 -
2282 2003 foreach ( $reply_fields as $f => $val ) {
2283 2004 if ( isset( $notification[ $f ] ) ) {
2284 2005 $atts[ $f ] = $notification[ $f ];
2285 -
2286 - if ( 'custom' === $notification[ $f ] ) {
2006 + if ( 'custom' == $notification[ $f ] ) {
2287 2007 $atts[ $f ] = $notification[ 'cust_' . $f ];
2288 - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) {
2008 + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
2289 2009 $atts[ $f ] = '[' . $atts[ $f ] . ']';
2290 2010 }
2291 2011 }
2292 2012 unset( $f, $val );
@@ -2293,27 +2013,21 @@
2293 2013 }
2294 2014
2295 2015 // Format event
2296 2016 $atts['event'] = array( 'create' );
2297 -
2298 - // phpcs:ignore Universal.Operators.StrictComparisons
2299 2017 if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
2300 2018 $atts['event'][] = 'update';
2301 - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons
2019 + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
2302 2020 $atts['event'] = array( 'update' );
2303 2021 }
2304 2022 }
2305 2023
2306 - /**
2307 - * Format email_to data
2308 - *
2309 - * @param array $atts
2310 - * @param array $notification
2311 - *
2312 - * @return void
2313 - */
2314 2024 private static function format_email_to_data( &$atts, $notification ) {
2315 - $atts['email_to'] = isset( $notification['email_to'] ) ? preg_split( '/ (,|;) /', $notification['email_to'] ) : array();
2025 + if ( isset( $notification['email_to'] ) ) {
2026 + $atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] );
2027 + } else {
2028 + $atts['email_to'] = array();
2029 + }
2316 2030
2317 2031 if ( isset( $notification['also_email_to'] ) ) {
2318 2032 $email_fields = (array) $notification['also_email_to'];
2319 2033 $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
@@ -2320,36 +2034,24 @@
2320 2034 unset( $email_fields );
2321 2035 }
2322 2036
2323 2037 foreach ( $atts['email_to'] as $key => $email_field ) {
2038 +
2324 2039 if ( is_numeric( $email_field ) ) {
2325 2040 $atts['email_to'][ $key ] = '[' . $email_field . ']';
2326 2041 }
2327 2042
2328 - if ( ! str_contains( $email_field, '|' ) ) {
2329 - continue;
2043 + if ( strpos( $email_field, '|' ) ) {
2044 + $email_opt = explode( '|', $email_field );
2045 + if ( isset( $email_opt[0] ) ) {
2046 + $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2047 + }
2048 + unset( $email_opt );
2330 2049 }
2331 -
2332 - $email_opt = explode( '|', $email_field );
2333 -
2334 - if ( isset( $email_opt[0] ) ) {
2335 - $atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
2336 - }
2337 - unset( $email_opt );
2338 2050 }
2339 -
2340 2051 $atts['email_to'] = implode( ', ', $atts['email_to'] );
2341 2052 }
2342 2053
2343 - /**
2344 - * Setup new notification
2345 - *
2346 - * @param array $new_notification
2347 - * @param array $notification
2348 - * @param array $atts
2349 - *
2350 - * @return void
2351 - */
2352 2054 private static function setup_new_notification( &$new_notification, $notification, $atts ) {
2353 2055 // Set up new notification
2354 2056 $new_notification = array(
2355 2057 'post_content' => array(
@@ -2360,13 +2062,12 @@
2360 2062 );
2361 2063
2362 2064 // Add more fields to the new notification
2363 2065 $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' );
2364 -
2365 2066 foreach ( $add_fields as $add_field ) {
2366 2067 if ( isset( $notification[ $add_field ] ) ) {
2367 2068 $new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
2368 - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) {
2069 + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
2369 2070 $new_notification['post_content'][ $add_field ] = 0;
2370 2071 } else {
2371 2072 $new_notification['post_content'][ $add_field ] = '';
2372 2073 }
@@ -2377,9 +2078,9 @@
2377 2078 $new_notification['post_content']['reply_to'] = $atts['reply_to'];
2378 2079
2379 2080 // Set from
2380 2081 if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) {
2381 - $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
2082 + $new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>';
2382 2083 }
2383 2084 }
2384 2085
2385 2086 /**
@@ -2385,81 +2086,63 @@
2385 2086 /**
2386 2087 * Switch field IDs in pre-2.0 email conditional logic
2387 2088 *
2388 2089 * @param array $post_content Pass by reference.
2389 - *
2390 - * @return void
2391 2090 */
2392 2091 private static function switch_email_condition_field_ids( &$post_content ) {
2393 2092 // Switch field IDs in conditional logic
2394 - if ( ! isset( $post_content['conditions'] ) || ! is_array( $post_content['conditions'] ) ) {
2395 - return;
2396 - }
2397 -
2398 - foreach ( $post_content['conditions'] as $email_key => $val ) {
2399 - if ( is_numeric( $email_key ) ) {
2400 - $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2093 + if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) {
2094 + foreach ( $post_content['conditions'] as $email_key => $val ) {
2095 + if ( is_numeric( $email_key ) ) {
2096 + $post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
2097 + }
2098 + unset( $email_key, $val );
2401 2099 }
2402 - unset( $email_key, $val );
2403 2100 }
2404 2101 }
2405 2102
2406 - /**
2407 - * Migrate autoresponder to action
2408 - *
2409 - * @param array $form_options
2410 - * @param int $form_id
2411 - * @param array $notifications
2412 - *
2413 - * @return void
2414 - */
2415 2103 private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) {
2416 - if ( empty( $form_options['auto_responder'] ) || empty( $form_options['ar_email_message'] ) ) {
2417 - return;
2418 - }
2104 + if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) {
2105 + // migrate autoresponder
2419 2106
2420 - // Migrate autoresponder
2107 + $email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0;
2108 + if ( strpos( $email_field, '|' ) ) {
2109 + // data from entries field
2110 + $email_field = explode( '|', $email_field );
2111 + if ( isset( $email_field[1] ) ) {
2112 + $email_field = $email_field[1];
2113 + }
2114 + }
2115 + if ( is_numeric( $email_field ) && ! empty( $email_field ) ) {
2116 + $email_field = '[' . $email_field . ']';
2117 + }
2421 2118
2422 - $email_field = $form_options['ar_email_to'] ?? 0;
2119 + $notification = $form_options;
2120 + $new_notification2 = array(
2121 + 'post_content' => array(
2122 + 'email_message' => $notification['ar_email_message'],
2123 + 'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '',
2124 + 'email_to' => $email_field,
2125 + 'plain_text' => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0,
2126 + 'inc_user_info' => 0,
2127 + ),
2128 + 'post_name' => $form_id . '_email_' . count( $notifications ),
2129 + );
2423 2130
2424 - if ( str_contains( $email_field, '|' ) ) {
2425 - // Data from entries field
2426 - $email_field = explode( '|', $email_field );
2131 + $reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : '';
2132 + $reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : '';
2427 2133
2428 - if ( isset( $email_field[1] ) ) {
2429 - $email_field = $email_field[1];
2134 + if ( ! empty( $reply_to ) ) {
2135 + $new_notification2['post_content']['reply_to'] = $reply_to;
2430 2136 }
2431 - }
2432 2137
2433 - if ( is_numeric( $email_field ) && $email_field ) {
2434 - $email_field = '[' . $email_field . ']';
2435 - }
2138 + if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) {
2139 + $new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>';
2140 + }
2436 2141
2437 - $notification = $form_options;
2438 - $new_notification2 = array(
2439 - 'post_content' => array(
2440 - 'email_message' => $notification['ar_email_message'],
2441 - 'email_subject' => $notification['ar_email_subject'] ?? '',
2442 - 'email_to' => $email_field,
2443 - 'plain_text' => $notification['ar_plain_text'] ?? 0,
2444 - 'inc_user_info' => 0,
2445 - ),
2446 - 'post_name' => $form_id . '_email_' . count( $notifications ),
2447 - );
2448 -
2449 - $reply_to = $notification['ar_reply_to'] ?? '';
2450 - $reply_to_name = $notification['ar_reply_to_name'] ?? '';
2451 -
2452 - if ( $reply_to ) {
2453 - $new_notification2['post_content']['reply_to'] = $reply_to;
2454 - }
2455 -
2456 - if ( $reply_to || $reply_to_name ) {
2457 - $new_notification2['post_content']['from'] = ( $reply_to_name ? $reply_to_name : '[sitename]' ) . ' <' . ( $reply_to ? $reply_to : '[admin_email]' ) . '>'; // phpcs:ignore SlevomatCodingStandard.Files.LineLength.LineTooLong
2458 - }
2459 -
2460 - $notifications[] = $new_notification2;
2461 - unset( $new_notification2 );
2142 + $notifications[] = $new_notification2;
2143 + unset( $new_notification2 );
2144 + }//end if
2462 2145 }
2463 2146
2464 2147 /**
2465 2148 * PHP 8 backward compatibility for the libxml_disable_entity_loader function
@@ -2475,13 +2158,8 @@
2475 2158
2476 2159 return $loader;
2477 2160 }
2478 2161
2479 - /**
2480 - * PHP 8 backward compatibility for the libxml_disable_entity_loader function
2481 - *
2482 - * @return bool
2483 - */
2484 2162 public static function check_if_libxml_disable_entity_loader_exists() {
2485 2163 return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' );
2486 2164 }
2487 2165
@@ -2494,13 +2172,11 @@
2494 2172 * @return array
2495 2173 */
2496 2174 public static function get_supported_upload_file_types() {
2497 2175 $file_types = array( '.xml' );
2498 -
2499 2176 if ( FrmAppHelper::pro_is_installed() ) {
2500 2177 // CSV Importing is only available in Pro.
2501 2178 $file_types[] = '.csv';
2502 2179 }
2503 -
2504 2180 return $file_types;
2505 2181 }
2506 2182 }