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