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