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