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