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