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