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 +345 -876 6.31 → 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' ) ) {
@@ -132,20 +75,18 @@
132 75 $imported = self::pre_import_data();
133 76
134 77 foreach ( array( 'term', 'form', 'view' ) as $item_type ) {
135 78 // Grab cats, tags, and terms, or forms or posts.
136 - if ( ! isset( $xml->{$item_type} ) ) {
137 - continue;
79 + if ( isset( $xml->{$item_type} ) ) {
80 + $function_name = 'import_xml_' . $item_type . 's';
81 + $imported = self::$function_name( $xml->{$item_type}, $imported );
82 + unset( $function_name, $xml->{$item_type} );
138 83 }
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 84 }
144 85
145 86 $imported = apply_filters( 'frm_importing_xml', $imported, $xml );
146 87
147 - if ( empty( $imported['form_status'] ) ) {
88 + if ( ! isset( $imported['form_status'] ) || empty( $imported['form_status'] ) ) {
148 89 // Check for an error message in the XML.
149 90 if ( isset( $xml->Code ) && isset( $xml->Message ) ) { // phpcs:ignore WordPress.NamingConventions
150 91 $imported['error'] = (string) $xml->Message; // phpcs:ignore WordPress.NamingConventions
151 92 }
@@ -155,9 +96,8 @@
155 96 }
156 97
157 98 /**
158 99 * @since 3.06
159 - *
160 100 * @return array
161 101 */
162 102 private static function pre_import_data() {
163 103 $defaults = array(
@@ -177,14 +117,8 @@
177 117 'terms' => array(),
178 118 );
179 119 }
180 120
181 - /**
182 - * @param SimpleXMLElement $terms
183 - * @param array $imported
184 - *
185 - * @return array
186 - */
187 121 public static function import_xml_terms( $terms, $imported ) {
188 122 foreach ( $terms as $t ) {
189 123 if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) {
190 124 continue;
@@ -197,19 +131,19 @@
197 131 (string) $t->term_taxonomy,
198 132 array(
199 133 'slug' => (string) $t->term_slug,
200 134 'description' => (string) $t->term_description,
201 - 'parent' => $parent ? $parent : 0,
135 + 'parent' => empty( $parent ) ? 0 : $parent,
202 136 )
203 137 );
204 138
205 139 if ( $term && is_array( $term ) ) {
206 - ++$imported['imported']['terms'];
140 + $imported['imported']['terms'] ++;
207 141 $imported['terms'][ (int) $t->term_id ] = $term['term_id'];
208 142 }
209 143
210 144 unset( $term, $t );
211 - }//end foreach
145 + }
212 146
213 147 return $imported;
214 148 }
215 149
@@ -214,30 +148,23 @@
214 148 }
215 149
216 150 /**
217 151 * @since 2.0.8
218 - *
219 - * @param object $t
220 - *
221 - * @return int|string
222 152 */
223 153 private static function get_term_parent_id( $t ) {
224 154 $parent = (string) $t->term_parent;
225 -
226 - if ( $parent ) {
155 + if ( ! empty( $parent ) ) {
227 156 $parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy );
228 - $parent = $parent ? $parent['term_id'] : 0;
157 + if ( $parent ) {
158 + $parent = $parent['term_id'];
159 + } else {
160 + $parent = 0;
161 + }
229 162 }
230 163
231 164 return $parent;
232 165 }
233 166
234 - /**
235 - * @param SimpleXMLElement $forms
236 - * @param array $imported
237 - *
238 - * @return array
239 - */
240 167 public static function import_xml_forms( $forms, $imported ) {
241 168 $child_forms = array();
242 169
243 170 // Import child forms first
@@ -247,13 +174,13 @@
247 174 $form = self::fill_form( $item );
248 175
249 176 self::update_custom_style_setting_on_import( $form );
250 177
251 - $this_form = self::maybe_get_form( $form );
178 + $this_form = self::maybe_get_form( $form );
179 +
252 180 $old_id = false;
253 181 $form_fields = false;
254 -
255 - if ( $this_form ) {
182 + if ( ! empty( $this_form ) ) {
256 183 $form_id = $this_form->id;
257 184 $old_id = $this_form->id;
258 185 self::update_form( $this_form, $form, $imported );
259 186
@@ -259,13 +186,12 @@
259 186
260 187 $form_fields = self::get_form_fields( $form_id );
261 188 } else {
262 189 $form_id = FrmForm::create( $form );
263 -
264 190 if ( $form_id ) {
265 191 if ( empty( $form['parent_form_id'] ) ) {
266 192 // Don't include the repeater form in the imported count.
267 - ++$imported['imported']['forms'];
193 + $imported['imported']['forms'] ++;
268 194 }
269 195
270 196 // Keep track of whether this specific form was updated or not.
271 197 $imported['form_status'][ $form_id ] = 'imported';
@@ -290,35 +216,15 @@
290 216
291 217 do_action( 'frm_after_import_form', $form_id, $form );
292 218
293 219 unset( $form, $item );
294 - }//end foreach
220 + }
295 221
296 222 self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms );
297 223
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 224 return $imported;
314 225 }
315 226
316 - /**
317 - * @param object $item
318 - *
319 - * @return array
320 - */
321 227 private static function fill_form( $item ) {
322 228 $form = array(
323 229 'id' => (int) $item->id,
324 230 'form_key' => (string) $item->form_key,
@@ -346,49 +252,29 @@
346 252
347 253 return $form;
348 254 }
349 255
350 - /**
351 - * @param array $form
352 - *
353 - * @return false|object
354 - */
355 256 private static function maybe_get_form( $form ) {
356 - // 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
357 258 $edit_query = array(
358 259 'form_key' => $form['form_key'],
359 260 'is_template' => $form['is_template'],
360 261 );
361 -
362 262 if ( ! $form['is_template'] ) {
363 263 $edit_query['created_at'] = $form['created_at'];
364 264 }
365 265
366 266 $edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form );
367 - $form = FrmForm::getAll( $edit_query, '', 1 );
368 267
369 - if ( is_object( $form ) && $form->status === 'trash' ) {
370 - FrmForm::destroy( $form->id );
371 - return false;
372 - }
373 -
374 - return $form;
268 + return FrmForm::getAll( $edit_query, '', 1 );
375 269 }
376 270
377 - /**
378 - * @param object $this_form
379 - * @param array $form
380 - * @param array $imported
381 - *
382 - * @return void
383 - */
384 271 private static function update_form( $this_form, $form, &$imported ) {
385 272 $form_id = $this_form->id;
386 273 FrmForm::update( $form_id, $form );
387 -
388 274 if ( empty( $form['parent_form_id'] ) ) {
389 275 // Don't include the repeater form in the updated count.
390 - ++$imported['updated']['forms'];
276 + $imported['updated']['forms'] ++;
391 277 }
392 278
393 279 // Keep track of whether this specific form was updated or not
394 280 $imported['form_status'][ $form_id ] = 'updated';
@@ -393,43 +279,32 @@
393 279 // Keep track of whether this specific form was updated or not
394 280 $imported['form_status'][ $form_id ] = 'updated';
395 281 }
396 282
397 - /**
398 - * @param int|string $form_id
399 - *
400 - * @return array
401 - */
402 283 private static function get_form_fields( $form_id ) {
403 284 $form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' );
404 285 $old_fields = array();
405 -
406 286 foreach ( $form_fields as $f ) {
407 287 $old_fields[ $f->id ] = $f;
408 288 $old_fields[ $f->field_key ] = $f->id;
409 289 unset( $f );
410 290 }
291 + $form_fields = $old_fields;
411 292
412 - return $old_fields;
293 + return $form_fields;
413 294 }
414 295
415 296 /**
416 297 * 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 298 */
422 299 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 );
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 );
430 306 }
431 - unset( $field );
432 307 }
433 308 }
434 309
435 310 /**
@@ -460,10 +335,10 @@
460 335 * Keep track of all imported child forms
461 336 *
462 337 * @since 2.0.16
463 338 *
464 - * @param int $form_id
465 - * @param int $parent_form_id
339 + * @param int $form_id
340 + * @param int $parent_form_id
466 341 * @param array $child_forms
467 342 */
468 343 private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) {
469 344 if ( $parent_form_id ) {
@@ -481,16 +356,14 @@
481 356 * @param array $child_forms
482 357 */
483 358 private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) {
484 359 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;
360 + if ( isset( $imported_forms[ $old_parent_form_id ] ) && (int) $imported_forms[ $old_parent_form_id ] !== (int) $old_parent_form_id ) {
361 + // Update all children with this old parent_form_id
362 + $new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
363 + FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
364 + do_action( 'frm_update_child_form_parent_id', $child_form_id, $new_parent_form_id );
487 365 }
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 366 }
494 367 }
495 368
496 369 /**
@@ -498,14 +371,8 @@
498 371 *
499 372 * @since 2.0.13
500 373 *
501 374 * 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 375 */
509 376 private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) {
510 377 $in_section = 0;
511 378 $keys_by_original_field_id = array();
@@ -519,45 +386,40 @@
519 386 self::maybe_update_form_select( $f, $imported );
520 387 self::maybe_update_get_values_form_setting( $imported, $f );
521 388 self::migrate_placeholders( $f );
522 389
523 - if ( $this_form ) {
524 - // Check for field to edit by field id
390 + if ( ! empty( $this_form ) ) {
391 + // check for field to edit by field id
525 392 if ( isset( $form_fields[ $f['id'] ] ) ) {
526 393 FrmField::update( $f['id'], $f );
527 - ++$imported['updated']['fields'];
394 + $imported['updated']['fields'] ++;
528 395
529 396 unset( $form_fields[ $f['id'] ] );
530 397
531 - // Unset old field key.
398 + //unset old field key
532 399 if ( isset( $form_fields[ $f['field_key'] ] ) ) {
533 400 unset( $form_fields[ $f['field_key'] ] );
534 401 }
535 402 } elseif ( isset( $form_fields[ $f['field_key'] ] ) ) {
536 403 $keys_by_original_field_id[ $f['id'] ] = $f['field_key'];
537 - $old_field_id = $f['id'];
538 404
539 - // Check for field to edit by field key
405 + // check for field to edit by field key
540 406 unset( $f['id'] );
541 407
542 408 FrmField::update( $form_fields[ $f['field_key'] ], $f );
543 - ++$imported['updated']['fields'];
409 + $imported['updated']['fields'] ++;
544 410
545 - self::do_after_field_imported_action( $f, $form_fields, $old_field_id );
546 -
547 - // Unset old field id.
548 - unset( $form_fields[ $form_fields[ $f['field_key'] ] ] );
549 -
550 - // Unset old field key.
551 - 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
552 413 } else {
553 - // 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
554 415 self::create_imported_field( $f, $imported );
555 - }//end if
416 + }
556 417 } else {
418 +
557 419 self::create_imported_field( $f, $imported );
558 - }//end if
559 - }//end foreach
420 + }
421 + }
560 422
561 423 if ( $keys_by_original_field_id ) {
562 424 self::maybe_update_field_ids( $form_id, $keys_by_original_field_id );
563 425 }
@@ -562,53 +424,8 @@
562 424 self::maybe_update_field_ids( $form_id, $keys_by_original_field_id );
563 425 }
564 426 }
565 427
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 428 private static function fill_field( $field, $form_id ) {
612 429 return array(
613 430 'id' => (int) $field->id,
614 431 'field_key' => (string) $field->field_key,
@@ -625,12 +442,8 @@
625 442 }
626 443
627 444 /**
628 445 * @since 4.06
629 - *
630 - * @param array $f
631 - *
632 - * @return void
633 446 */
634 447 private static function set_default_value( &$f ) {
635 448 $has_default = array(
636 449 'text',
@@ -644,17 +457,15 @@
644 457 'password',
645 458 'tag',
646 459 );
647 460
648 - if ( ! is_array( $f['default_value'] ) || ! in_array( $f['type'], $has_default, true ) ) {
649 - 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 + }
650 467 }
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 468 }
658 469
659 470 /**
660 471 * Make sure the required indicator is set.
@@ -659,12 +470,8 @@
659 470 /**
660 471 * Make sure the required indicator is set.
661 472 *
662 473 * @since 4.05
663 - *
664 - * @param array $f
665 - *
666 - * @return void
667 474 */
668 475 private static function maybe_add_required( &$f ) {
669 476 if ( $f['required'] && ! isset( $f['field_options']['required_indicator'] ) ) {
670 477 $f['field_options']['required_indicator'] = '*';
@@ -674,17 +481,14 @@
674 481 /**
675 482 * Update the current in_section value at the beginning of the field loop
676 483 *
677 484 * @since 2.0.25
678 - *
679 - * @param int $in_section
485 + * @param int $in_section
680 486 * @param array $f
681 - *
682 - * @return void
683 487 */
684 488 private static function maybe_update_in_section_variable( &$in_section, &$f ) {
685 489 // 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 ) ) {
490 + if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) {
687 491 $in_section = 0;
688 492 }
689 493
690 494 // Update the current field's in_section value
@@ -692,9 +496,9 @@
692 496 $f['field_options']['in_section'] = $in_section;
693 497 }
694 498
695 499 // If we're starting a new section, switch $in_section to ID of divider
696 - if ( $f['type'] === 'divider' ) {
500 + if ( $f['type'] == 'divider' ) {
697 501 $in_section = $f['id'];
698 502 }
699 503 }
700 504
@@ -710,21 +514,16 @@
710 514 if ( ! isset( $imported['forms'] ) ) {
711 515 return;
712 516 }
713 517
714 - if ( $f['type'] !== 'form' && ( $f['type'] !== 'divider' || ! FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
715 - 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 + }
716 525 }
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 526 }
728 527
729 528 /**
730 529 * Update the get_values_form setting if the form was imported
@@ -732,10 +531,8 @@
732 531 * @since 2.01.0
733 532 *
734 533 * @param array $imported
735 534 * @param array $f
736 - *
737 - * @return void
738 535 */
739 536 private static function maybe_update_get_values_form_setting( $imported, &$f ) {
740 537 if ( ! isset( $imported['forms'] ) ) {
741 538 return;
@@ -740,35 +537,36 @@
740 537 if ( ! isset( $imported['forms'] ) ) {
741 538 return;
742 539 }
743 540
744 - if ( ! FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
745 - 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 + }
746 546 }
547 + }
747 548
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 - }
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 );
753 557 }
754 558
755 559 /**
756 560 * @since 4.0
757 - *
758 - * @param array $f
759 - *
760 - * @return void
761 561 */
762 562 private static function migrate_placeholders( &$f ) {
763 563 $update_values = self::migrate_field_placeholder( $f, 'clear_on_focus' );
764 -
765 564 foreach ( $update_values as $k => $v ) {
766 565 $f[ $k ] = $v;
767 566 }
768 567
769 568 $update_values = self::migrate_field_placeholder( $f, 'default_blank' );
770 -
771 569 foreach ( $update_values as $k => $v ) {
772 570 $f[ $k ] = $v;
773 571 }
774 572 }
@@ -777,18 +575,13 @@
777 575 * Move clear_on_focus or default_blank to placeholder.
778 576 * Also called during database migration in FrmMigrate.
779 577 *
780 578 * @since 4.0
781 - *
782 - * @param array|object $field
783 - * @param string $type
784 - *
785 579 * @return array
786 580 */
787 581 public static function migrate_field_placeholder( $field, $type ) {
788 - $field = (array) $field;
582 + $field = (array) $field;
789 583 $field_options = $field['field_options'];
790 -
791 584 if ( empty( $field_options[ $type ] ) || empty( $field['default_value'] ) ) {
792 585 return array();
793 586 }
794 587
@@ -801,34 +594,27 @@
801 594 );
802 595
803 596 // If a dropdown placeholder was used, remove the option so it won't be included twice.
804 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 + }
805 603
806 - if ( $type !== 'default_blank' || ! is_array( $options ) ) {
807 - return $changes;
808 - }
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 + }
809 608
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 );
609 + if ( $opt == $default_value ) {
610 + unset( $options[ $opt_key ] );
611 + break;
612 + }
819 613 }
820 -
821 - // phpcs:ignore Universal.Operators.StrictComparisons
822 - if ( $opt == $default_value ) {
823 - unset( $options[ $opt_key ] );
824 - break;
825 - }
614 + $changes['options'] = $options;
826 615 }
827 616
828 - $changes['options'] = $options;
829 - // end if
830 -
831 617 return $changes;
832 618 }
833 619
834 620 /**
@@ -837,13 +623,12 @@
837 623 * @since 2.0.25
838 624 *
839 625 * @param array $f
840 626 * @param array $imported
841 - *
842 - * @return void
843 627 */
844 628 private static function create_imported_field( $f, &$imported ) {
845 - $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'] );
846 631
847 632 if ( is_callable( 'FrmProFileImport::import_attachment' ) ) {
848 633 $f = self::maybe_import_images_for_options( $f );
849 634 }
@@ -848,15 +633,12 @@
848 633 $f = self::maybe_import_images_for_options( $f );
849 634 }
850 635
851 636 $new_id = FrmField::create( $f );
852 -
853 - if ( ! $new_id ) {
854 - return;
637 + if ( $new_id != false ) {
638 + $imported['imported']['fields'] ++;
639 + do_action( 'frm_after_field_is_imported', $f, $new_id );
855 640 }
856 -
857 - ++$imported['imported']['fields'];
858 - do_action( 'frm_after_field_is_imported', $f, $new_id );
859 641 }
860 642
861 643 /**
862 644 * Import images for radio buttons and checkboxes from image src if available.
@@ -863,9 +645,8 @@
863 645 *
864 646 * @since 5.5.1
865 647 *
866 648 * @param array $field
867 - *
868 649 * @return array
869 650 */
870 651 private static function maybe_import_images_for_options( $field ) {
871 652 if ( empty( $field['options'] ) || ! is_array( $field['options'] ) ) {
@@ -876,15 +657,13 @@
876 657 if ( ! is_array( $option ) || empty( $option['src'] ) ) {
877 658 continue;
878 659 }
879 660
880 - $field_object = (object) $field;
881 - // Fake the file type as FrmProImport::import_attachment checks for file type.
882 - $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.
883 663
884 664 $image_id = FrmProFileImport::import_attachment( $option['src'], $field_object );
885 - // Remove the src from options as it isn't required after import.
886 - unset( $field['options'][ $key ]['src'] );
665 + unset( $field['options'][ $key ]['src'] ); // Remove the src from options as it isn't required after import.
887 666
888 667 if ( is_numeric( $image_id ) ) {
889 668 $field['options'][ $key ]['image'] = $image_id;
890 669 }
@@ -896,13 +675,10 @@
896 675 /**
897 676 * Fix field ids for fields that already exist prior to import.
898 677 *
899 678 * @since 4.07
900 - *
901 - * @param int $form_id
679 + * @param int $form_id
902 680 * @param array $keys_by_original_field_id
903 - *
904 - * @return void
905 681 */
906 682 protected static function maybe_update_field_ids( $form_id, $keys_by_original_field_id ) {
907 683 global $frm_duplicate_ids;
908 684
@@ -922,17 +698,15 @@
922 698 $field = (array) $field;
923 699 $frm_duplicate_ids = $keys_by_original_field_id;
924 700 $after = FrmFieldsHelper::switch_field_ids( $field );
925 701
926 - if ( $before['field_options'] === $after['field_options'] ) {
927 - continue;
928 - }
702 + if ( $before['field_options'] !== $after['field_options'] ) {
703 + $frm_duplicate_ids = $field_id_by_key;
704 + $after = FrmFieldsHelper::switch_field_ids( $after );
929 705
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'] ) );
706 + if ( $before['field_options'] !== $after['field_options'] ) {
707 + FrmField::update( $field['id'], array( 'field_options' => $after['field_options'] ) );
708 + }
935 709 }
936 710 }
937 711
938 712 $frm_duplicate_ids = $former_duplicate_ids;
@@ -944,10 +718,8 @@
944 718 *
945 719 * @since 2.0.19
946 720 *
947 721 * @param array $form
948 - *
949 - * @return void
950 722 */
951 723 private static function update_custom_style_setting_on_import( &$form ) {
952 724 if ( ! isset( $form['options']['custom_style'] ) ) {
953 725 return;
@@ -952,35 +724,32 @@
952 724 if ( ! isset( $form['options']['custom_style'] ) ) {
953 725 return;
954 726 }
955 727
956 - if ( is_numeric( $form['options']['custom_style'] ) && 1 === intval( $form['options']['custom_style'] ) ) {
728 + if ( is_numeric( $form['options']['custom_style'] ) ) {
957 729 // Set to default
958 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 );
959 741
960 - return;
961 - }
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'];
962 747
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;
748 + // Set to default
749 + $form['options']['custom_style'] = 1;
750 + }
976 751 }
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;
983 752 }
984 753
985 754 /**
986 755 * After styles are imported, check for any forms that were linked
@@ -986,40 +755,25 @@
986 755 * After styles are imported, check for any forms that were linked
987 756 * and link them back up.
988 757 *
989 758 * @since 2.2.7
990 - *
991 - * @param int|string $form_id
992 - *
993 - * @return void
994 759 */
995 760 private static function update_custom_style_setting_after_import( $form_id ) {
996 761 $form = FrmForm::getOne( $form_id );
997 762
998 - if ( ! $form || ! isset( $form->options['old_style'] ) ) {
999 - 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 + }
1000 772 }
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 773 }
1012 774
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
775 + public static function import_xml_views( $views, $imported ) {
1022 776 $imported['posts'] = array();
1023 777 $form_action_type = FrmFormActionsController::$action_post_type;
1024 778
1025 779 $post_types = array(
@@ -1056,13 +810,8 @@
1056 810 );
1057 811
1058 812 $post['post_content'] = self::switch_form_ids( $post['post_content'], $imported['forms'] );
1059 813
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 814 $old_id = $post['post_id'];
1066 815 self::populate_post( $post, $item, $imported );
1067 816
1068 817 unset( $item );
@@ -1067,13 +816,11 @@
1067 816
1068 817 unset( $item );
1069 818
1070 819 $post_id = false;
1071 -
1072 820 if ( $post['post_type'] === $form_action_type ) {
1073 821 $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] );
1074 -
1075 - if ( $action_control && is_object( $action_control ) && isset( $imported['form_status'] ) ) {
822 + if ( $action_control && is_object( $action_control ) ) {
1076 823 $post_id = $action_control->maybe_create_action( $post, $imported['form_status'] );
1077 824 }
1078 825 unset( $action_control );
1079 826 } elseif ( $post['post_type'] === 'frm_styles' ) {
@@ -1078,14 +825,14 @@
1078 825 unset( $action_control );
1079 826 } elseif ( $post['post_type'] === 'frm_styles' ) {
1080 827 // Properly encode post content before inserting the post
1081 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'] : '';
1082 830 $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 831
1086 832 // Create/update post now
1087 833 $post_id = wp_insert_post( $post );
834 + self::maybe_update_custom_css( $custom_css );
1088 835 } else {
1089 836 if ( $post['post_type'] === 'frm_display' ) {
1090 837 $post['post_content'] = self::maybe_prepare_json_view_content( $post['post_content'] );
1091 838 } elseif ( 'page' === $post['post_type'] && isset( $imported['posts'][ $post['post_parent'] ] ) ) {
@@ -1092,15 +839,15 @@
1092 839 $post['post_parent'] = $imported['posts'][ $post['post_parent'] ];
1093 840 }
1094 841 // Create/update post now
1095 842 $post_id = wp_insert_post( $post );
1096 - }//end if
843 + }
1097 844
1098 845 if ( ! is_numeric( $post_id ) ) {
1099 846 continue;
1100 847 }
1101 848
1102 - 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' ) ) {
1103 850 $posts_with_shortcodes[ $post_id ] = $post;
1104 851 }
1105 852
1106 853 self::update_postmeta( $post, $post_id );
@@ -1106,29 +853,28 @@
1106 853 self::update_postmeta( $post, $post_id );
1107 854 self::update_layout( $post, $post_id );
1108 855
1109 856 $this_type = 'posts';
1110 -
1111 857 if ( isset( $post_types[ $post['post_type'] ] ) ) {
1112 858 $this_type = $post_types[ $post['post_type'] ];
1113 859 }
1114 860
1115 - if ( isset( $post['ID'] ) && (int) $post_id === (int) $post['ID'] ) {
1116 - ++$imported['updated'][ $this_type ];
861 + if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
862 + $imported['updated'][ $this_type ] ++;
1117 863 } else {
1118 - ++$imported['imported'][ $this_type ];
864 + $imported['imported'][ $this_type ] ++;
1119 865 }
1120 866
1121 - $imported['posts'][ $old_id ] = $post_id;
867 + $imported['posts'][ (int) $old_id ] = $post_id;
1122 868
1123 869 if ( $post['post_type'] === 'frm_display' ) {
1124 - $view_ids[ $old_id ] = $post_id;
870 + $view_ids[ (int) $old_id ] = $post_id;
1125 871 }
1126 872
1127 873 do_action( 'frm_after_import_view', $post_id, $post );
1128 874
1129 875 unset( $post );
1130 - }//end foreach
876 + }
1131 877
1132 878 if ( $posts_with_shortcodes && $view_ids ) {
1133 879 self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids );
1134 880 }
@@ -1133,13 +879,8 @@
1133 879 self::maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids );
1134 880 }
1135 881 unset( $posts_with_shortcodes, $view_ids );
1136 882
1137 - if ( ! empty( $imported['forms'] ) ) {
1138 - // Clear imported forms style cache to make sure the new styles are applied to the forms
1139 - self::clear_forms_style_caches( $imported['forms'] );
1140 - }
1141 -
1142 883 self::maybe_update_stylesheet( $imported );
1143 884
1144 885 flush_rewrite_rules();
1145 886
@@ -1146,49 +887,16 @@
1146 887 return $imported;
1147 888 }
1148 889
1149 890 /**
1150 - * Clears styles from cache for imported forms
1151 - *
1152 - * @param array $imported_forms
1153 - *
1154 - * @return void
1155 - */
1156 - private static function clear_forms_style_caches( $imported_forms ) {
1157 - $where = array(
1158 - 'id' => $imported_forms,
1159 - 'options LIKE' => '"old_style"',
1160 - );
1161 - $forms = FrmDb::get_results( 'frm_forms', $where );
1162 -
1163 - foreach ( $forms as $form ) {
1164 - FrmAppHelper::unserialize_or_decode( $form->options );
1165 -
1166 - if ( ! $form->options ) {
1167 - continue;
1168 - }
1169 -
1170 - $where = array(
1171 - 'post_name' => $form->options['old_style'],
1172 - 'post_type' => FrmStylesController::$post_type,
1173 - );
1174 -
1175 - $select = 'ID';
1176 - $cache_key = FrmDb::generate_cache_key( $where, array( 'limit' => 1 ), $select, 'var' );
1177 - FrmDb::delete_cache_and_transient( $cache_key, 'post' );
1178 - }
1179 - }
1180 -
1181 - /**
1182 891 * Replace old form ids with new ones in a string.
1183 892 *
1184 893 * @param string $string
1185 894 * @param array<int> $form_ids new form ids indexed by old form id.
1186 - *
1187 895 * @return string
1188 896 */
1189 897 private static function switch_form_ids( $string, $form_ids ) {
1190 - if ( ! str_contains( $string, '[formidable' ) ) {
898 + if ( false === strpos( $string, '[formidable' ) ) {
1191 899 // Skip string replacing if there are no form shortcodes in string.
1192 900 return $string;
1193 901 }
1194 902
@@ -1215,15 +923,13 @@
1215 923
1216 924 /**
1217 925 * @param array<array> $posts_with_shortcodes indexed by current post id.
1218 926 * @param array<int> $view_ids new view ids indexed by old view id.
1219 - *
1220 927 * @return void
1221 928 */
1222 929 private static function maybe_switch_view_ids_after_importing_posts( $posts_with_shortcodes, $view_ids ) {
1223 930 foreach ( $posts_with_shortcodes as $imported_post_id => $post ) {
1224 931 $post_content = self::switch_view_ids( $post['post_content'], $view_ids );
1225 -
1226 932 if ( $post_content === $post['post_content'] ) {
1227 933 continue;
1228 934 }
1229 935
@@ -1240,13 +946,12 @@
1240 946 * Replace old view ids with new ones in a string.
1241 947 *
1242 948 * @param string $string
1243 949 * @param array<int> $view_ids new view ids indexed by old view id.
1244 - *
1245 950 * @return string
1246 951 */
1247 952 private static function switch_view_ids( $string, $view_ids ) {
1248 - if ( ! str_contains( $string, '[display-frm-data' ) ) {
953 + if ( false === strpos( $string, '[display-frm-data' ) ) {
1249 954 // Skip string replacing if there are no view shortcodes in string.
1250 955 return $string;
1251 956 }
1252 957
@@ -1273,40 +978,30 @@
1273 978 }
1274 979
1275 980 /**
1276 981 * @param string $content
1277 - *
1278 982 * @return string
1279 983 */
1280 984 private static function maybe_prepare_json_view_content( $content ) {
1281 985 $maybe_decoded = FrmAppHelper::maybe_json_decode( $content );
1282 -
1283 986 if ( is_array( $maybe_decoded ) && isset( $maybe_decoded[0] ) && isset( $maybe_decoded[0]['box'] ) ) {
1284 987 return FrmAppHelper::prepare_and_encode( $maybe_decoded );
1285 988 }
1286 -
1287 989 return $content;
1288 990 }
1289 991
1290 - /**
1291 - * @param array $post
1292 - * @param object $item
1293 - * @param array $imported
1294 - *
1295 - * @return void
1296 - */
1297 992 private static function populate_post( &$post, $item, $imported ) {
1298 993 if ( isset( $item->attachment_url ) ) {
1299 994 $post['attachment_url'] = (string) $item->attachment_url;
1300 995 }
1301 996
1302 - if ( $post['post_type'] === FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
1303 - // 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
1304 999 $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
1305 1000 }
1306 1001
1307 1002 // Don't allow default styles to take over a site's default style
1308 - if ( 'frm_styles' === $post['post_type'] ) {
1003 + if ( 'frm_styles' == $post['post_type'] ) {
1309 1004 $post['menu_order'] = 0;
1310 1005 }
1311 1006
1312 1007 foreach ( $item->postmeta as $meta ) {
@@ -1328,9 +1023,9 @@
1328 1023 * @param array $post
1329 1024 * @param stdClass $meta
1330 1025 * @param array $imported
1331 1026 */
1332 - 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 ) {
1333 1028 global $frm_duplicate_ids;
1334 1029
1335 1030 $m = array(
1336 1031 'key' => (string) $meta->meta_key,
@@ -1336,19 +1031,20 @@
1336 1031 'key' => (string) $meta->meta_key,
1337 1032 'value' => (string) $meta->meta_value,
1338 1033 );
1339 1034
1340 - // Switch old form and field ids to new ones.
1035 + //switch old form and field ids to new ones
1341 1036 if ( 'frm_form_id' === $m['key'] && isset( $imported['forms'][ (int) $m['value'] ] ) ) {
1342 1037 $m['value'] = $imported['forms'][ (int) $m['value'] ];
1343 1038 } else {
1344 1039 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1345 1040
1346 - if ( $frm_duplicate_ids ) {
1041 + if ( ! empty( $frm_duplicate_ids ) ) {
1347 1042 if ( 'frm_dyncontent' === $m['key'] ) {
1348 1043 $m['value'] = self::maybe_prepare_json_view_content( $m['value'] );
1349 1044 $m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
1350 1045 } elseif ( 'frm_options' === $m['key'] ) {
1046 +
1351 1047 foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
1352 1048 if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
1353 1049 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1354 1050 }
@@ -1353,38 +1049,10 @@
1353 1049 $m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
1354 1050 }
1355 1051 }
1356 1052
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 1053 $check_dup_array = array();
1385 -
1386 - if ( ! empty( $m['value']['order_by'] ) ) {
1054 + if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) {
1387 1055 if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) {
1388 1056 $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ];
1389 1057 } elseif ( is_array( $m['value']['order_by'] ) ) {
1390 1058 $check_dup_array[] = 'order_by';
@@ -1390,9 +1058,9 @@
1390 1058 $check_dup_array[] = 'order_by';
1391 1059 }
1392 1060 }
1393 1061
1394 - if ( ! empty( $m['value']['where'] ) ) {
1062 + if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) {
1395 1063 $check_dup_array[] = 'where';
1396 1064 }
1397 1065
1398 1066 foreach ( $check_dup_array as $check_k ) {
@@ -1402,11 +1070,11 @@
1402 1070 }
1403 1071 unset( $mk, $mv );
1404 1072 }
1405 1073 }
1406 - }//end if
1407 - }//end if
1408 - }//end if
1074 + }
1075 + }
1076 + }
1409 1077
1410 1078 if ( ! is_array( $m['value'] ) ) {
1411 1079 $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
1412 1080 }
@@ -1413,14 +1081,8 @@
1413 1081
1414 1082 $post['postmeta'][ (string) $meta->meta_key ] = $m['value'];
1415 1083 }
1416 1084
1417 - /**
1418 - * @param array $post
1419 - * @param object $layout
1420 - *
1421 - * @return void
1422 - */
1423 1085 private static function populate_layout( &$post, $layout ) {
1424 1086 $post['layout'][ (string) $layout->type ] = (string) $layout->data;
1425 1087 }
1426 1088
@@ -1426,25 +1088,22 @@
1426 1088
1427 1089 /**
1428 1090 * Add terms to post
1429 1091 *
1430 - * @param array $post By reference.
1431 - * @param object $item The XML object data.
1092 + * @param array $post by reference
1093 + * @param object $item The XML object data
1432 1094 */
1433 1095 private static function populate_taxonomies( &$post, $item ) {
1434 1096 foreach ( $item->category as $c ) {
1435 1097 $att = $c->attributes();
1436 -
1437 1098 if ( ! isset( $att['nicename'] ) ) {
1438 1099 continue;
1439 1100 }
1440 1101
1441 1102 $taxonomy = (string) $att['domain'];
1442 -
1443 1103 if ( is_taxonomy_hierarchical( $taxonomy ) ) {
1444 1104 $name = (string) $att['nicename'];
1445 1105 $h_term = get_term_by( 'slug', $name, $taxonomy );
1446 -
1447 1106 if ( $h_term ) {
1448 1107 $name = $h_term->term_id;
1449 1108 }
1450 1109 unset( $h_term );
@@ -1457,17 +1116,13 @@
1457 1116 }
1458 1117
1459 1118 $post['tax_input'][ $taxonomy ][] = $name;
1460 1119 unset( $name );
1461 - }//end foreach
1120 + }
1462 1121 }
1463 1122
1464 1123 /**
1465 - * Edit post if the key and created time match.
1466 - *
1467 - * @param array $post By reference.
1468 - *
1469 - * @return void
1124 + * Edit post if the key and created time match
1470 1125 */
1471 1126 private static function maybe_editing_post( &$post ) {
1472 1127 $match_by = array(
1473 1128 'post_type' => $post['post_type'],
@@ -1475,9 +1130,9 @@
1475 1130 'post_status' => $post['post_status'],
1476 1131 'posts_per_page' => 1,
1477 1132 );
1478 1133
1479 - if ( in_array( $post['post_status'], array( 'trash', 'draft' ), true ) ) {
1134 + if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) {
1480 1135 $match_by['include'] = $post['post_id'];
1481 1136 unset( $match_by['name'] );
1482 1137 }
1483 1138
@@ -1482,11 +1137,10 @@
1482 1137 }
1483 1138
1484 1139 $editing = get_posts( $match_by );
1485 1140
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
1141 + if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
1142 + // set the id of the post to edit
1489 1143 $post['ID'] = current( $editing )->ID;
1490 1144 }
1491 1145 }
1492 1146
@@ -1492,9 +1146,8 @@
1492 1146
1493 1147 /**
1494 1148 * @param array $post
1495 1149 * @param int $post_id
1496 - *
1497 1150 * @return void
1498 1151 */
1499 1152 private static function update_postmeta( &$post, $post_id ) {
1500 1153 foreach ( $post['postmeta'] as $k => $v ) {
@@ -1519,13 +1172,13 @@
1519 1172
1520 1173 case 'frm_param':
1521 1174 add_rewrite_endpoint( $v, EP_PERMALINK | EP_PAGES );
1522 1175 break;
1523 - }//end switch
1176 + }
1524 1177
1525 1178 update_post_meta( $post_id, $k, $v );
1526 1179 unset( $k, $v );
1527 - }//end foreach
1180 + }
1528 1181 }
1529 1182
1530 1183 /**
1531 1184 * @param array $post
@@ -1531,83 +1184,61 @@
1531 1184 * @param array $post
1532 1185 * @param int $post_id
1533 1186 */
1534 1187 private static function update_layout( &$post, $post_id ) {
1535 - if ( ! is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1536 - return;
1188 + if ( is_callable( 'FrmViewsLayout::maybe_create_layouts_for_view' ) ) {
1189 + $listing_layout = ! empty( $post['layout']['listing'] ) ? json_decode( $post['layout']['listing'], true ) : array();
1190 + $detail_layout = ! empty( $post['layout']['detail'] ) ? json_decode( $post['layout']['detail'], true ) : array();
1191 + if ( $listing_layout || $detail_layout ) {
1192 + FrmViewsLayout::maybe_create_layouts_for_view( $post_id, $listing_layout, $detail_layout );
1193 + }
1537 1194 }
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 1195 }
1546 1196
1547 1197 /**
1548 - * @param array $imported
1198 + * If a template includes custom css, let's include it.
1199 + * The custom css is included on the default style.
1549 1200 *
1550 - * @return void
1201 + * @since 2.03
1551 1202 */
1552 - 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 ) {
1203 + private static function maybe_update_custom_css( $custom_css ) {
1204 + if ( empty( $custom_css ) ) {
1557 1205 return;
1558 1206 }
1559 1207
1560 - if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
1561 - $frm_style = new FrmStyle();
1562 - $frm_style->update( 'default' );
1563 - }
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 + }
1564 1213
1565 - foreach ( $imported['forms'] as $form_id ) {
1566 - 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 + }
1567 1225 }
1568 1226 }
1569 1227
1570 1228 /**
1571 - * @param mixed $result
1572 1229 * @param string $message
1573 - * @param array $errors
1574 - *
1575 - * @return void
1576 1230 */
1577 1231 public static function parse_message( $result, &$message, &$errors ) {
1578 1232 if ( is_wp_error( $result ) ) {
1579 1233 $errors[] = $result->get_error_message();
1580 -
1581 - // Remove the SimpleXML_parse_error from the WP_Error object to avoid
1582 - // displaying duplicate error messages from $result->get_error_message()
1583 - $error_details = array();
1584 -
1585 - foreach ( $result->get_error_codes() as $error_code ) {
1586 - // 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.
1588 - // Source: https://developer.wordpress.org/reference/classes/wp_error/remove/#source
1589 - $error_details = $result->get_error_data( $error_code );
1590 -
1591 - if ( $error_code === 'SimpleXML_parse_error' ) {
1592 - $result->remove( $error_code );
1593 - break;
1594 - }
1595 - }
1596 -
1597 - if ( $error_details ) {
1598 - $errors[] = '<br />' . esc_html_x( 'Error details:', 'import xml message', 'formidable' ) . '<br />' . esc_html( print_r( $error_details, 1 ) );
1599 - }
1600 -
1234 + } elseif ( ! $result ) {
1601 1235 return;
1602 - }//end if
1603 -
1604 - if ( ! $result ) {
1605 - return;
1606 1236 }
1607 1237
1608 1238 if ( ! is_array( $result ) ) {
1609 1239 $message = is_string( $result ) ? $result : htmlentities( print_r( $result, 1 ) );
1240 +
1610 1241 return;
1611 1242 }
1612 1243
1613 1244 $t_strings = array(
@@ -1615,48 +1246,42 @@
1615 1246 'updated' => __( 'Updated', 'formidable' ),
1616 1247 );
1617 1248
1618 1249 $message = '<ul>';
1619 -
1620 1250 foreach ( $result as $type => $results ) {
1621 1251 if ( ! isset( $t_strings[ $type ] ) ) {
1622 - // Only print imported and updated
1252 + // only print imported and updated
1623 1253 continue;
1624 1254 }
1625 1255
1626 1256 $s_message = array();
1627 -
1628 1257 foreach ( $results as $k => $m ) {
1629 1258 self::item_count_message( $m, $k, $s_message );
1630 1259 unset( $k, $m );
1631 1260 }
1632 1261
1633 - if ( ! $s_message ) {
1634 - continue;
1262 + if ( ! empty( $s_message ) ) {
1263 + $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1264 + $message .= implode( ', ', $s_message );
1265 + $message .= '</li>';
1635 1266 }
1267 + }
1636 1268
1637 - $message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
1638 - $message .= implode( ', ', $s_message );
1639 - $message .= '</li>';
1640 - }//end foreach
1641 -
1642 - if ( $message === '<ul>' ) {
1269 + if ( $message == '<ul>' ) {
1643 1270 $message = '';
1644 1271 $errors[] = __( 'Nothing was imported or updated', 'formidable' );
1272 + } else {
1273 + self::add_form_link_to_message( $result, $message );
1645 1274
1646 - 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>';
1647 1283 }
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 1284 }
1660 1285
1661 1286 /**
1662 1287 * @param int $m
@@ -1661,10 +1286,8 @@
1661 1286 /**
1662 1287 * @param int $m
1663 1288 * @param string $type
1664 1289 * @param array<string> $s_message
1665 - *
1666 - * @return void
1667 1290 */
1668 1291 public static function item_count_message( $m, $type, &$s_message ) {
1669 1292 if ( ! $m ) {
1670 1293 return;
@@ -1690,22 +1313,21 @@
1690 1313 );
1691 1314
1692 1315 if ( isset( $strings[ $type ] ) ) {
1693 1316 $s_message[] = $strings[ $type ];
1694 - 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;
1695 1329 }
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 1330 }
1709 1331
1710 1332 /**
1711 1333 * If a single form was imported, include a link in the success message.
@@ -1710,28 +1332,24 @@
1710 1332 /**
1711 1333 * If a single form was imported, include a link in the success message.
1712 1334 *
1713 1335 * @since 4.0
1714 - *
1715 1336 * @param array $result The response from the XML import.
1716 1337 * @param string $message The response shown on the page after import.
1717 1338 */
1718 1339 private static function add_form_link_to_message( $result, &$message ) {
1719 1340 $total_forms = $result['imported']['forms'] + $result['updated']['forms'];
1720 -
1721 1341 if ( $total_forms > 1 ) {
1722 1342 return;
1723 1343 }
1724 1344
1725 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;
1726 1349
1727 - if ( ! $primary_form ) {
1728 - return;
1350 + $message .= '<li><a href="' . esc_url( FrmForm::get_edit_link( $form_id ) ) . '">' . esc_html__( 'Go to imported form', 'formidable' ) . '</a></li>';
1729 1351 }
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 1352 }
1735 1353
1736 1354 /**
1737 1355 * Prepare the form options for export
@@ -1744,17 +1362,22 @@
1744 1362 */
1745 1363 public static function prepare_form_options_for_export( $options ) {
1746 1364 FrmAppHelper::unserialize_or_decode( $options );
1747 1365 // 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 -
1366 + $not_default = isset( $options['custom_style'] ) && 1 != $options['custom_style'];
1750 1367 if ( $not_default ) {
1751 1368 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;
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 + }
1757 1380 }
1758 1381 self::remove_default_form_options( $options );
1759 1382 $options = serialize( $options );
1760 1383
@@ -1765,16 +1388,11 @@
1765 1388 * If the saved value is the same as the default, remove it from the export
1766 1389 * This keeps file size down and prevents overriding global settings after import
1767 1390 *
1768 1391 * @since 3.06
1769 - *
1770 - * @param array $options By reference.
1771 - *
1772 - * @return void
1773 1392 */
1774 1393 private static function remove_default_form_options( &$options ) {
1775 1394 $defaults = FrmFormsHelper::get_default_opts();
1776 -
1777 1395 if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) {
1778 1396 $defaults += FrmProFormsHelper::get_default_opts();
1779 1397 }
1780 1398 self::remove_defaults( $defaults, $options );
@@ -1783,12 +1401,8 @@
1783 1401 /**
1784 1402 * Remove extra settings from field to keep file size down
1785 1403 *
1786 1404 * @since 3.06
1787 - *
1788 - * @param object $field
1789 - *
1790 - * @return void
1791 1405 */
1792 1406 public static function prepare_field_for_export( &$field ) {
1793 1407 self::remove_default_field_options( $field );
1794 1408 self::add_image_src_to_image_options( $field );
@@ -1797,16 +1411,11 @@
1797 1411 /**
1798 1412 * Remove defaults from field options too
1799 1413 *
1800 1414 * @since 3.06
1801 - *
1802 - * @param object $field
1803 - *
1804 - * @return void
1805 1415 */
1806 1416 private static function remove_default_field_options( &$field ) {
1807 1417 $defaults = self::default_field_options( $field->type );
1808 -
1809 1418 if ( empty( $defaults['blank'] ) ) {
1810 1419 $global_settings = new FrmSettings();
1811 1420 $global_defaults = $global_settings->default_options();
1812 1421 $defaults['blank'] = $global_defaults['blank_msg'];
@@ -1834,13 +1443,12 @@
1834 1443 *
1835 1444 * @since 5.5.1
1836 1445 *
1837 1446 * @param stdClass $field
1838 - *
1839 1447 * @return void
1840 1448 */
1841 1449 private static function add_image_src_to_image_options( $field ) {
1842 - if ( empty( $field->options ) || ! str_contains( $field->options, 'image' ) ) {
1450 + if ( empty( $field->options ) || false === strpos( $field->options, 'image' ) ) {
1843 1451 return;
1844 1452 }
1845 1453
1846 1454 $updated = false;
@@ -1851,14 +1459,12 @@
1851 1459 return;
1852 1460 }
1853 1461
1854 1462 foreach ( $options as $key => $option ) {
1855 - if ( ! is_array( $option ) || empty( $option['image'] ) ) {
1856 - continue;
1463 + if ( is_array( $option ) && ! empty( $option['image'] ) ) {
1464 + $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1465 + $updated = true;
1857 1466 }
1858 -
1859 - $options[ $key ]['src'] = wp_get_attachment_url( $option['image'] );
1860 - $updated = true;
1861 1467 }
1862 1468
1863 1469 if ( $updated ) {
1864 1470 $field->options = maybe_serialize( $options );
@@ -1866,20 +1472,14 @@
1866 1472 }
1867 1473
1868 1474 /**
1869 1475 * @since 3.06.03
1870 - *
1871 - * @param string $type
1872 - *
1873 - * @return array
1874 1476 */
1875 1477 private static function default_field_options( $type ) {
1876 1478 $defaults = FrmFieldsHelper::get_default_field_options( $type );
1877 -
1878 1479 if ( empty( $defaults['custom_html'] ) ) {
1879 1480 $defaults['custom_html'] = FrmFieldsHelper::get_default_html( $type );
1880 1481 }
1881 -
1882 1482 return $defaults;
1883 1483 }
1884 1484
1885 1485 /**
@@ -1886,13 +1486,8 @@
1886 1486 * Compare the default array to the saved values and
1887 1487 * remove if they are the same
1888 1488 *
1889 1489 * @since 3.06
1890 - *
1891 - * @param array $defaults
1892 - * @param array $saved
1893 - *
1894 - * @return void
1895 1490 */
1896 1491 private static function remove_defaults( $defaults, &$saved ) {
1897 1492 foreach ( $saved as $key => $value ) {
1898 1493 if ( isset( $defaults[ $key ] ) && $defaults[ $key ] === $value ) {
@@ -1904,14 +1499,8 @@
1904 1499 /**
1905 1500 * The line endings may prevent html from being equal when it should
1906 1501 *
1907 1502 * @since 3.06
1908 - *
1909 - * @param string $html_name
1910 - * @param array $defaults
1911 - * @param array $options
1912 - *
1913 - * @return void
1914 1503 */
1915 1504 private static function remove_default_html( $html_name, $defaults, &$options ) {
1916 1505 if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) {
1917 1506 return;
@@ -1918,35 +1507,27 @@
1918 1507 }
1919 1508
1920 1509 $old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
1921 1510 $default_html = $defaults[ $html_name ];
1922 -
1923 - // phpcs:ignore Universal.Operators.StrictComparisons
1924 1511 if ( $old_html == $default_html ) {
1925 1512 unset( $options[ $html_name ] );
1513 +
1926 1514 return;
1927 1515 }
1928 1516
1929 1517 // Account for some of the older field default HTML.
1930 1518 $default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html );
1931 -
1932 - if ( $old_html === $default_html ) {
1519 + if ( $old_html == $default_html ) {
1933 1520 unset( $options[ $html_name ] );
1934 1521 }
1935 1522 }
1936 1523
1937 - /**
1938 - * @param string $str
1939 - *
1940 - * @return string
1941 - */
1942 1524 public static function cdata( $str ) {
1943 1525 FrmAppHelper::unserialize_or_decode( $str );
1944 -
1945 1526 if ( is_array( $str ) ) {
1946 1527 $str = json_encode( $str );
1947 - } elseif ( FrmAppHelper::is_valid_utf8( $str ) === false ) {
1948 - $str = FrmAppHelper::maybe_utf8_encode( $str );
1528 + } elseif ( seems_utf8( $str ) === false ) {
1529 + $str = utf8_encode( $str );
1949 1530 }
1950 1531
1951 1532 if ( is_numeric( $str ) ) {
1952 1533 return $str;
@@ -1954,9 +1535,11 @@
1954 1535
1955 1536 self::remove_invalid_characters_from_xml( $str );
1956 1537
1957 1538 // $str = ent2ncr(esc_html( $str));
1958 - return '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1539 + $str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
1540 +
1541 + return $str;
1959 1542 }
1960 1543
1961 1544 /**
1962 1545 * Remove <US> character (unit separator) from exported strings
@@ -1963,10 +1546,8 @@
1963 1546 *
1964 1547 * @since 2.0.22
1965 1548 *
1966 1549 * @param string $str
1967 - *
1968 - * @return void
1969 1550 */
1970 1551 private static function remove_invalid_characters_from_xml( &$str ) {
1971 1552 // Remove <US> character
1972 1553 $str = str_replace( '\x1F', '', $str );
@@ -1971,18 +1552,8 @@
1971 1552 // Remove <US> character
1972 1553 $str = str_replace( '\x1F', '', $str );
1973 1554 }
1974 1555
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 1556 public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) {
1986 1557 // Get post type
1987 1558 $post_type = FrmFormActionsController::$action_post_type;
1988 1559
@@ -2000,16 +1571,12 @@
2000 1571
2001 1572 /**
2002 1573 * Migrate post settings to form action
2003 1574 *
2004 - * @param array $form_options
2005 - * @param int $form_id
2006 1575 * @param string $post_type
2007 - * @param array $imported
2008 - * @param bool $switch
2009 1576 */
2010 1577 private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2011 - if ( empty( $form_options['create_post'] ) ) {
1578 + if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
2012 1579 return;
2013 1580 }
2014 1581
2015 1582 $new_action = array(
@@ -2061,9 +1628,8 @@
2061 1628 $array_fields = array( 'post_category', 'post_custom_fields' );
2062 1629
2063 1630 $new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
2064 1631 }
2065 -
2066 1632 $new_action['post_content'] = json_encode( $new_action['post_content'] );
2067 1633
2068 1634 $exists = get_posts(
2069 1635 array(
@@ -2073,15 +1639,13 @@
2073 1639 'numberposts' => 1,
2074 1640 )
2075 1641 );
2076 1642
2077 - if ( $exists ) {
2078 - 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'] ++;
2079 1647 }
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 1648 }
2085 1649
2086 1650 /**
2087 1651 * Switch old field IDs for new field IDs in emails and post
@@ -2087,11 +1651,11 @@
2087 1651 * Switch old field IDs for new field IDs in emails and post
2088 1652 *
2089 1653 * @since 2.0
2090 1654 *
2091 - * @param array $post_content Check for old field IDs.
2092 - * @param array $basic_fields Fields with string or int saved.
2093 - * @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
2094 1658 *
2095 1659 * @return string $post_content - new field IDs
2096 1660 */
2097 1661 private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) {
@@ -2098,9 +1662,9 @@
2098 1662 global $frm_duplicate_ids;
2099 1663
2100 1664 // If there aren't IDs that were switched, end now
2101 1665 if ( ! $frm_duplicate_ids ) {
2102 - return null;
1666 + return;
2103 1667 }
2104 1668
2105 1669 // Get old IDs
2106 1670 $old = array_keys( $frm_duplicate_ids );
@@ -2109,13 +1673,11 @@
2109 1673 $new = array_values( $frm_duplicate_ids );
2110 1674
2111 1675 // Do a str_replace with each item to set the new IDs
2112 1676 foreach ( $post_content as $key => $setting ) {
2113 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2114 1677 if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
2115 1678 // Replace old IDs with new IDs
2116 1679 $post_content[ $key ] = str_replace( $old, $new, $setting );
2117 - // phpcs:ignore WordPress.PHP.StrictInArray.MissingTrueStrict
2118 1680 } elseif ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
2119 1681 foreach ( $setting as $k => $val ) {
2120 1682 // Replace old IDs with new IDs
2121 1683 $post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
@@ -2126,19 +1688,8 @@
2126 1688
2127 1689 return $post_content;
2128 1690 }
2129 1691
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 1692 private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
2142 1693 // No old notifications or autoresponders to carry over
2143 1694 if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) {
2144 1695 return;
@@ -2152,9 +1703,9 @@
2152 1703
2153 1704 // Migrate autoresponders
2154 1705 self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
2155 1706
2156 - if ( ! $notifications ) {
1707 + if ( empty( $notifications ) ) {
2157 1708 return;
2158 1709 }
2159 1710
2160 1711 foreach ( $notifications as $new_notification ) {
@@ -2165,8 +1716,9 @@
2165 1716 $new_notification['post_status'] = 'publish';
2166 1717
2167 1718 // Switch field IDs and keys, if needed
2168 1719 if ( $switch ) {
1720 +
2169 1721 // Switch field IDs in email conditional logic
2170 1722 self::switch_email_condition_field_ids( $new_notification['post_content'] );
2171 1723
2172 1724 // Switch all other field IDs in email
@@ -2171,9 +1723,8 @@
2171 1723
2172 1724 // Switch all other field IDs in email
2173 1725 $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
2174 1726 }
2175 -
2176 1727 $new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
2177 1728
2178 1729 $exists = get_posts(
2179 1730 array(
@@ -2183,14 +1734,14 @@
2183 1734 'numberposts' => 1,
2184 1735 )
2185 1736 );
2186 1737
2187 - if ( ! $exists ) {
1738 + if ( empty( $exists ) ) {
2188 1739 FrmDb::save_json_post( $new_notification );
2189 - ++$imported['imported']['actions'];
1740 + $imported['imported']['actions'] ++;
2190 1741 }
2191 1742 unset( $new_notification );
2192 - }//end foreach
1743 + }
2193 1744
2194 1745 self::remove_deprecated_notification_settings( $form_id, $form_options );
2195 1746 }
2196 1747
@@ -2199,44 +1750,32 @@
2199 1750 *
2200 1751 * @since 2.05
2201 1752 *
2202 1753 * @param int|string $form_id
2203 - * @param array $form_options
2204 - *
2205 - * @return void
1754 + * @param array $form_options
2206 1755 */
2207 1756 private static function remove_deprecated_notification_settings( $form_id, $form_options ) {
2208 1757 $delete_settings = array( 'notification', 'autoresponder', 'email_to' );
2209 -
2210 1758 foreach ( $delete_settings as $index ) {
2211 1759 if ( isset( $form_options[ $index ] ) ) {
2212 1760 unset( $form_options[ $index ] );
2213 1761 }
2214 1762 }
2215 -
2216 1763 FrmForm::update( $form_id, array( 'options' => $form_options ) );
2217 1764 }
2218 1765
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 1766 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
1767 + if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) {
1768 + // add old settings into notification array
2231 1769 $form_options['notification'] = array( 0 => $form_options );
2232 1770 } elseif ( isset( $form_options['notification']['email_to'] ) ) {
2233 - // Make sure it's in the correct format
1771 + // make sure it's in the correct format
2234 1772 $form_options['notification'] = array( 0 => $form_options['notification'] );
2235 1773 }
2236 1774
2237 1775 if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
2238 1776 foreach ( $form_options['notification'] as $email_key => $notification ) {
1777 +
2239 1778 $atts = array(
2240 1779 'email_to' => '',
2241 1780 'reply_to' => '',
2242 1781 'reply_to_name' => '',
@@ -2247,9 +1786,9 @@
2247 1786
2248 1787 // Format the email data
2249 1788 self::format_email_data( $atts, $notification );
2250 1789
2251 - if ( ! empty( $notification['twilio'] ) ) {
1790 + if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
2252 1791 do_action( 'frm_create_twilio_action', $atts, $notification );
2253 1792 }
2254 1793
2255 1794 // Setup the new notification
@@ -2256,20 +1795,12 @@
2256 1795 $new_notification = array();
2257 1796 self::setup_new_notification( $new_notification, $notification, $atts );
2258 1797
2259 1798 $notifications[] = $new_notification;
2260 - }//end foreach
2261 - }//end if
1799 + }
1800 + }
2262 1801 }
2263 1802
2264 - /**
2265 - * Format email data
2266 - *
2267 - * @param array $atts
2268 - * @param array $notification
2269 - *
2270 - * @return void
2271 - */
2272 1803 private static function format_email_data( &$atts, $notification ) {
2273 1804 // Format email_to
2274 1805 self::format_email_to_data( $atts, $notification );
2275 1806
@@ -2277,16 +1808,14 @@
2277 1808 $reply_fields = array(
2278 1809 'reply_to' => '',
2279 1810 'reply_to_name' => '',
2280 1811 );
2281 -
2282 1812 foreach ( $reply_fields as $f => $val ) {
2283 1813 if ( isset( $notification[ $f ] ) ) {
2284 1814 $atts[ $f ] = $notification[ $f ];
2285 -
2286 - if ( 'custom' === $notification[ $f ] ) {
1815 + if ( 'custom' == $notification[ $f ] ) {
2287 1816 $atts[ $f ] = $notification[ 'cust_' . $f ];
2288 - } elseif ( is_numeric( $atts[ $f ] ) && $atts[ $f ] ) {
1817 + } elseif ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
2289 1818 $atts[ $f ] = '[' . $atts[ $f ] . ']';
2290 1819 }
2291 1820 }
2292 1821 unset( $f, $val );
@@ -2293,27 +1822,21 @@
2293 1822 }
2294 1823
2295 1824 // Format event
2296 1825 $atts['event'] = array( 'create' );
2297 -
2298 - // phpcs:ignore Universal.Operators.StrictComparisons
2299 1826 if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
2300 1827 $atts['event'][] = 'update';
2301 - } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) { // phpcs:ignore Universal.Operators.StrictComparisons
1828 + } elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
2302 1829 $atts['event'] = array( 'update' );
2303 1830 }
2304 1831 }
2305 1832
2306 - /**
2307 - * Format email_to data
2308 - *
2309 - * @param array $atts
2310 - * @param array $notification
2311 - *
2312 - * @return void
2313 - */
2314 1833 private static function format_email_to_data( &$atts, $notification ) {
2315 - $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 + }
2316 1839
2317 1840 if ( isset( $notification['also_email_to'] ) ) {
2318 1841 $email_fields = (array) $notification['also_email_to'];
2319 1842 $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
@@ -2320,36 +1843,24 @@
2320 1843 unset( $email_fields );
2321 1844 }
2322 1845
2323 1846 foreach ( $atts['email_to'] as $key => $email_field ) {
1847 +
2324 1848 if ( is_numeric( $email_field ) ) {
2325 1849 $atts['email_to'][ $key ] = '[' . $email_field . ']';
2326 1850 }
2327 1851
2328 - if ( ! str_contains( $email_field, '|' ) ) {
2329 - 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 );
2330 1858 }
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 1859 }
2339 -
2340 1860 $atts['email_to'] = implode( ', ', $atts['email_to'] );
2341 1861 }
2342 1862
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 1863 private static function setup_new_notification( &$new_notification, $notification, $atts ) {
2353 1864 // Set up new notification
2354 1865 $new_notification = array(
2355 1866 'post_content' => array(
@@ -2360,13 +1871,12 @@
2360 1871 );
2361 1872
2362 1873 // Add more fields to the new notification
2363 1874 $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' );
2364 -
2365 1875 foreach ( $add_fields as $add_field ) {
2366 1876 if ( isset( $notification[ $add_field ] ) ) {
2367 1877 $new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
2368 - } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ), true ) ) {
1878 + } elseif ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
2369 1879 $new_notification['post_content'][ $add_field ] = 0;
2370 1880 } else {
2371 1881 $new_notification['post_content'][ $add_field ] = '';
2372 1882 }
@@ -2377,9 +1887,9 @@
2377 1887 $new_notification['post_content']['reply_to'] = $atts['reply_to'];
2378 1888
2379 1889 // Set from
2380 1890 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
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'] ) . '>';
2382 1892 }
2383 1893 }
2384 1894
2385 1895 /**
@@ -2384,90 +1894,72 @@
2384 1894
2385 1895 /**
2386 1896 * Switch field IDs in pre-2.0 email conditional logic
2387 1897 *
2388 - * @param array $post_content Pass by reference.
2389 - *
2390 - * @return void
1898 + * @param $post_content array, pass by reference
2391 1899 */
2392 1900 private static function switch_email_condition_field_ids( &$post_content ) {
2393 1901 // 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' ) );
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 );
2401 1908 }
2402 - unset( $email_key, $val );
2403 1909 }
2404 1910 }
2405 1911
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 1912 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 - }
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
2419 1915
2420 - // 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 + }
2421 1927
2422 - $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 + );
2423 1939
2424 - if ( str_contains( $email_field, '|' ) ) {
2425 - // Data from entries field
2426 - $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'] : '';
2427 1942
2428 - if ( isset( $email_field[1] ) ) {
2429 - $email_field = $email_field[1];
1943 + if ( ! empty( $reply_to ) ) {
1944 + $new_notification2['post_content']['reply_to'] = $reply_to;
2430 1945 }
2431 - }
2432 1946
2433 - if ( is_numeric( $email_field ) && $email_field ) {
2434 - $email_field = '[' . $email_field . ']';
2435 - }
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 + }
2436 1950
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;
1951 + $notifications[] = $new_notification2;
1952 + unset( $new_notification2 );
2454 1953 }
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 );
2462 1954 }
2463 1955
2464 1956 /**
2465 1957 * PHP 8 backward compatibility for the libxml_disable_entity_loader function
2466 1958 *
2467 - * @param bool $loader
1959 + * @param boolean $disable
2468 1960 *
2469 - * @return bool
1961 + * @return boolean
2470 1962 */
2471 1963 public static function maybe_libxml_disable_entity_loader( $loader ) {
2472 1964 if ( version_compare( phpversion(), '8.0', '<' ) && function_exists( 'libxml_disable_entity_loader' ) ) {
2473 1965 $loader = libxml_disable_entity_loader( $loader ); // phpcs:disable Generic.PHP.DeprecatedFunctions.Deprecated
@@ -2475,32 +1967,9 @@
2475 1967
2476 1968 return $loader;
2477 1969 }
2478 1970
2479 - /**
2480 - * PHP 8 backward compatibility for the libxml_disable_entity_loader function
2481 - *
2482 - * @return bool
2483 - */
2484 1971 public static function check_if_libxml_disable_entity_loader_exists() {
2485 1972 return version_compare( phpversion(), '8.0', '<' ) && ! function_exists( 'libxml_disable_entity_loader' );
2486 1973 }
1974 +}
2487 1975
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 -}