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