Completed
Push — master ( 75b6e7...962a0e )
by Stephanie
02:52
created

FrmXMLHelper::create_imported_field()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 2
dl 0
loc 7
rs 10
c 0
b 0
f 0
1
<?php
2
if ( ! defined( 'ABSPATH' ) ) {
3
	die( 'You are not allowed to call this page directly.' );
4
}
5
6
class FrmXMLHelper {
7
8
	public static function get_xml_values( $opt, $padding ) {
9
		if ( is_array( $opt ) ) {
10
			foreach ( $opt as $ok => $ov ) {
11
				echo "\n" . esc_html( $padding );
12
				$tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok;
13
				echo '<' . esc_html( $tag ) . '>';
14
				self::get_xml_values( $ov, $padding . '    ' );
15
				if ( is_array( $ov ) ) {
16
					echo "\n" . esc_html( $padding );
17
				}
18
				echo '</' . esc_html( $tag ) . '>';
19
			}
20
		} else {
21
			echo self::cdata( $opt ); // WPCS: XSS ok.
22
		}
23
	}
24
25
	public static function import_xml( $file ) {
26
		$defaults = array(
27
			'forms'   => 0,
28
			'fields'  => 0,
29
			'terms'   => 0,
30
			'posts'   => 0,
31
			'views'   => 0,
32
			'actions' => 0,
33
			'styles'  => 0,
34
		);
35
36
        $imported = array(
37
            'imported' => $defaults,
38
			'updated'  => $defaults,
39
			'forms'    => array(),
40
			'terms'    => array(),
41
        );
42
43
		unset( $defaults );
44
45
		if ( ! defined( 'WP_IMPORTING' ) ) {
46
			define( 'WP_IMPORTING', true );
47
        }
48
49
		if ( ! class_exists( 'DOMDocument' ) ) {
50
            return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() );
51
        }
52
53
		$dom = new DOMDocument();
54
		$success = $dom->loadXML( file_get_contents( $file ) );
0 ignored issues
show
introduced by
file_get_contents is highly discouraged, please use wpcom_vip_file_get_contents() instead.
Loading history...
55
		if ( ! $success ) {
56
			return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
57
		}
58
59
		if ( ! function_exists( 'simplexml_import_dom' ) ) {
60
			return new WP_Error( 'SimpleXML_parse_error', __( 'Your server is missing the simplexml_import_dom function', 'formidable' ), libxml_get_errors() );
61
		}
62
63
		$xml = simplexml_import_dom( $dom );
64
		unset( $dom );
65
66
		// halt if loading produces an error
67
		if ( ! $xml ) {
68
			return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() );
69
		}
70
71
        // add terms, forms (form and field ids), posts (post ids), and entries to db, in that order
72
		foreach ( array( 'term', 'form', 'view' ) as $item_type ) {
73
            // grab cats, tags, and terms, or forms or posts
74
			if ( isset( $xml->{$item_type} ) ) {
75
				$function_name = 'import_xml_' . $item_type . 's';
76
				$imported = self::$function_name( $xml->{$item_type}, $imported );
77
				unset( $function_name, $xml->{$item_type} );
78
            }
79
        }
80
81
		$return = apply_filters( 'frm_importing_xml', $imported, $xml );
82
83
	    return $return;
84
    }
85
86
	public static function import_xml_terms( $terms, $imported ) {
87
        foreach ( $terms as $t ) {
88
			if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) {
89
			    continue;
90
			}
91
92
			$parent = self::get_term_parent_id( $t );
93
94
			$term = wp_insert_term(
95
				(string) $t->term_name,
96
				(string) $t->term_taxonomy,
97
				array(
98
					'slug'          => (string) $t->term_slug,
99
					'description'   => (string) $t->term_description,
100
					'parent'        => empty( $parent ) ? 0 : $parent,
101
				)
102
			);
103
104
			if ( $term && is_array( $term ) ) {
105
                $imported['imported']['terms']++;
106
				$imported['terms'][ (int) $t->term_id ] = $term['term_id'];
107
            }
108
109
			unset( $term, $t );
110
		}
111
112
		return $imported;
113
    }
114
115
	/**
116
	 * @since 2.0.8
117
	 */
118
	private static function get_term_parent_id( $t ) {
119
		$parent = (string) $t->term_parent;
120
		if ( ! empty( $parent ) ) {
121
			$parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy );
122
			if ( $parent ) {
123
				$parent = $parent['term_id'];
124
			} else {
125
				$parent = 0;
126
			}
127
		}
128
		return $parent;
129
	}
130
131
	public static function import_xml_forms( $forms, $imported ) {
132
		$child_forms = array();
133
134
		// Import child forms first
135
		self::put_child_forms_first( $forms );
136
137
		foreach ( $forms as $item ) {
138
            $form = self::fill_form( $item );
139
140
			self::update_custom_style_setting_on_import( $form );
141
142
	        $this_form = self::maybe_get_form( $form );
143
144
			$old_id = false;
145
			$form_fields = false;
146
			if ( ! empty( $this_form ) ) {
147
				$form_id = $this_form->id;
148
				$old_id = $this_form->id;
149
				self::update_form( $this_form, $form, $imported );
150
151
				$form_fields = self::get_form_fields( $form_id );
152
			} else {
153
				$form_id = FrmForm::create( $form );
154
		        if ( $form_id ) {
155
		            $imported['imported']['forms']++;
156
		            // Keep track of whether this specific form was updated or not
157
					$imported['form_status'][ $form_id ] = 'imported';
158
					self::track_imported_child_forms( (int) $form_id, $form['parent_form_id'], $child_forms );
159
		        }
160
			}
161
162
			self::import_xml_fields( $item->field, $form_id, $this_form, $form_fields, $imported );
163
164
			self::delete_removed_fields( $form_fields );
165
166
		    // Update field ids/keys to new ones
167
			do_action( 'frm_after_duplicate_form', $form_id, $form, array( 'old_id' => $old_id ) );
168
169
			$imported['forms'][ (int) $item->id ] = $form_id;
170
171
            // Send pre 2.0 form options through function that creates actions
172
            self::migrate_form_settings_to_actions( $form['options'], $form_id, $imported, true );
173
174
			do_action( 'frm_after_import_form', $form_id, $form );
175
176
			unset( $form, $item );
177
		}
178
179
		self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms );
180
181
		return $imported;
182
    }
183
184
	private static function fill_form( $item ) {
185
		$form = array(
186
			'id'            => (int) $item->id,
187
			'form_key'      => (string) $item->form_key,
188
			'name'          => (string) $item->name,
189
			'description'   => (string) $item->description,
190
			'options'       => (string) $item->options,
191
			'logged_in'     => (int) $item->logged_in,
192
			'is_template'   => (int) $item->is_template,
193
			'default_template' => (int) $item->default_template,
194
			'editable'      => (int) $item->editable,
195
			'status'        => (string) $item->status,
196
			'parent_form_id' => isset( $item->parent_form_id ) ? (int) $item->parent_form_id : 0,
197
			'created_at'    => date( 'Y-m-d H:i:s', strtotime( (string) $item->created_at ) ),
198
		);
199
		$form['options'] = FrmAppHelper::maybe_json_decode( $form['options'] );
200
		return $form;
201
	}
202
203
	private static function maybe_get_form( $form ) {
204
		// if template, allow to edit if form keys match, otherwise, creation date must also match
205
		$edit_query = array(
206
			'form_key'    => $form['form_key'],
207
			'is_template' => $form['is_template'],
208
		);
209
		if ( ! $form['is_template'] ) {
210
			$edit_query['created_at'] = $form['created_at'];
211
		}
212
213
		$edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form );
214
215
		return FrmForm::getAll( $edit_query, '', 1 );
216
	}
217
218
	private static function update_form( $this_form, $form, &$imported ) {
219
		$form_id = $this_form->id;
220
		FrmForm::update( $form_id, $form );
221
		$imported['updated']['forms']++;
222
		// Keep track of whether this specific form was updated or not
223
		$imported['form_status'][ $form_id ] = 'updated';
224
	}
225
226
	private static function get_form_fields( $form_id ) {
227
		$form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' );
228
		$old_fields = array();
229
		foreach ( $form_fields as $f ) {
230
			$old_fields[ $f->id ] = $f;
231
			$old_fields[ $f->field_key ] = $f->id;
232
			unset( $f );
233
		}
234
		$form_fields = $old_fields;
235
		return $form_fields;
236
	}
237
238
	/**
239
	 * Delete any fields attached to this form that were not included in the template
240
	 */
241
	private static function delete_removed_fields( $form_fields ) {
242
		if ( ! empty( $form_fields ) ) {
243
			foreach ( $form_fields as $field ) {
244
				if ( is_object( $field ) ) {
245
					FrmField::destroy( $field->id );
246
				}
247
				unset( $field );
248
			}
249
		}
250
	}
251
252
	/**
253
	* Put child forms first so they will be imported before parents
254
	*
255
	* @since 2.0.16
256
	* @param array $forms
257
	*/
258
	private static function put_child_forms_first( &$forms ) {
259
		$child_forms = array();
260
		$regular_forms = array();
261
262
		foreach ( $forms as $form ) {
263
			$parent_form_id = isset( $form->parent_form_id ) ? (int) $form->parent_form_id : 0;
264
265
			if ( $parent_form_id ) {
266
				$child_forms[] = $form;
267
			} else {
268
				$regular_forms[] = $form;
269
			}
270
		}
271
272
		$forms = array_merge( $child_forms, $regular_forms );
273
	}
274
275
	/**
276
	* Keep track of all imported child forms
277
	*
278
	* @since 2.0.16
279
	* @param int $form_id
280
	* @param int $parent_form_id
281
	* @param array $child_forms
282
	*/
283
	private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) {
284
		if ( $parent_form_id ) {
285
			$child_forms[ $form_id ] = $parent_form_id;
286
		}
287
	}
288
289
	/**
290
	* Update the parent_form_id on imported child forms
291
	* Child forms are imported first so their parent_form_id will need to be updated after the parent is imported
292
	*
293
	* @since 2.0.6
294
	* @param array $imported_forms
295
	* @param array $child_forms
296
	*/
297
	private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) {
298
		foreach ( $child_forms as $child_form_id => $old_parent_form_id ) {
299
300
			if ( isset( $imported_forms[ $old_parent_form_id ] ) && $imported_forms[ $old_parent_form_id ] != $old_parent_form_id ) {
301
				// Update all children with this old parent_form_id
302
				$new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ];
303
304
				FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) );
305
			}
306
		}
307
	}
308
309
	/**
310
	* Import all fields for a form
311
	 *
312
	* @since 2.0.13
313
	*
314
	* TODO: Cut down on params
315
	*/
316
	private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) {
317
		$in_section = 0;
318
319
		foreach ( $xml_fields as $field ) {
320
			$f = self::fill_field( $field, $form_id );
321
322
			$has_default = array( 'text', 'email', 'url', 'textarea', 'number', 'phone', 'date', 'hidden', 'password', 'tag' );
323
			if ( is_array( $f['default_value'] ) && in_array( $f['type'], $has_default, true ) ) {
324
				if ( count( $f['default_value'] ) === 1 ) {
325
					$f['default_value'] = '[' . reset( $f['default_value'] ) . ']';
326
				} else {
327
					$f['default_value'] = reset( $f['default_value'] );
328
				}
329
			}
330
331
			self::maybe_update_in_section_variable( $in_section, $f );
332
			self::maybe_update_form_select( $f, $imported );
333
			self::maybe_update_get_values_form_setting( $imported, $f );
334
335
			if ( ! empty( $this_form ) ) {
336
				// check for field to edit by field id
337
				if ( isset( $form_fields[ $f['id'] ] ) ) {
338
					FrmField::update( $f['id'], $f );
339
					$imported['updated']['fields']++;
340
341
					unset( $form_fields[ $f['id'] ] );
342
343
					//unset old field key
344
					if ( isset( $form_fields[ $f['field_key'] ] ) ) {
345
						unset( $form_fields[ $f['field_key'] ] );
346
					}
347
				} else if ( isset( $form_fields[ $f['field_key'] ] ) ) {
348
					// check for field to edit by field key
349
					unset( $f['id'] );
350
351
					FrmField::update( $form_fields[ $f['field_key'] ], $f );
352
					$imported['updated']['fields']++;
353
354
					unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); //unset old field id
355
					unset( $form_fields[ $f['field_key'] ] ); //unset old field key
356
				} else {
357
					// if no matching field id or key in this form, create the field
358
					self::create_imported_field( $f, $imported );
359
				}
360
			} else {
361
362
				self::create_imported_field( $f, $imported );
363
			}
364
		}
365
	}
366
367
	private static function fill_field( $field, $form_id ) {
368
		return array(
369
			'id'            => (int) $field->id,
370
			'field_key'     => (string) $field->field_key,
371
			'name'          => (string) $field->name,
372
			'description'   => (string) $field->description,
373
			'type'          => (string) $field->type,
374
			'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value ),
375
			'field_order'   => (int) $field->field_order,
376
			'form_id'       => (int) $form_id,
377
			'required'      => (int) $field->required,
378
			'options'       => FrmAppHelper::maybe_json_decode( (string) $field->options ),
379
			'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options ),
380
		);
381
	}
382
383
	/**
384
	 * Update the current in_section value
385
	 *
386
	 * @since 2.0.25
387
	 * @param int $in_section
388
	 * @param array $f
389
	 */
390
	private static function maybe_update_in_section_variable( &$in_section, &$f ) {
391
		// If we're at the end of a section, switch $in_section is 0
392
		if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) {
393
			$in_section = 0;
394
		}
395
396
		// Update the current field's in_section value
397
		if ( ! isset( $f['field_options']['in_section'] ) ) {
398
			$f['field_options']['in_section'] = $in_section;
399
		}
400
401
		// If we're starting a new section, switch $in_section to ID of divider
402
		if ( $f['type'] == 'divider' ) {
403
			$in_section = $f['id'];
404
		}
405
	}
406
407
	/**
408
	* Switch the form_select on a repeating field or embedded form if it needs to be switched
409
	*
410
	* @since 2.0.16
411
	* @param array $f
412
	* @param array $imported
413
	*/
414
	private static function maybe_update_form_select( &$f, $imported ) {
415
		if ( ! isset( $imported['forms'] ) ) {
416
			return;
417
		}
418
419
		if ( $f['type'] == 'form' || ( $f['type'] == 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) {
420 View Code Duplication
			if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
421
				$form_select = (int) $f['field_options']['form_select'];
422
				if ( isset( $imported['forms'][ $form_select ] ) ) {
423
					$f['field_options']['form_select'] = $imported['forms'][ $form_select ];
424
				}
425
			}
426
		}
427
	}
428
429
	/**
430
	 * Update the get_values_form setting if the form was imported
431
	 *
432
	 * @since 2.01.0
433
	 * @param array $imported
434
	 * @param array $f
435
	 */
436
	private static function maybe_update_get_values_form_setting( $imported, &$f ) {
437
		if ( ! isset( $imported['forms'] ) ) {
438
			return;
439
		}
440
441 View Code Duplication
		if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
442
			$old_form = $f['field_options']['get_values_form'];
443
			if ( isset( $imported['forms'][ $old_form ] ) ) {
444
				$f['field_options']['get_values_form'] = $imported['forms'][ $old_form ];
445
			}
446
		}
447
	}
448
449
	/**
450
	 * Create an imported field
451
	 *
452
	 * @since 2.0.25
453
	 * @param array $f
454
	 * @param array $imported
455
	 */
456
	private static function create_imported_field( $f, &$imported ) {
457
		$new_id = FrmField::create( $f );
458
		if ( $new_id != false ) {
459
			$imported['imported']['fields']++;
460
			do_action( 'frm_after_field_is_imported', $f, $new_id );
461
		}
462
	}
463
464
	/**
465
	* Updates the custom style setting on import
466
	* Convert the post slug to an ID
467
	*
468
	* @since 2.0.19
469
	* @param array $form
470
	*/
471
	private static function update_custom_style_setting_on_import( &$form ) {
472
		if ( ! isset( $form['options']['custom_style'] ) ) {
473
			return;
474
		}
475
476
		if ( is_numeric( $form['options']['custom_style'] ) ) {
477
			// Set to default
478
			$form['options']['custom_style'] = 1;
479
		} else {
480
			// Replace the style name with the style ID on import
481
			global $wpdb;
482
			$table = $wpdb->prefix . 'posts';
483
			$where = array(
484
				'post_name' => $form['options']['custom_style'],
485
				'post_type' => 'frm_styles',
486
			);
487
			$select = 'ID';
488
			$style_id = FrmDb::get_var( $table, $where, $select );
489
490
			if ( $style_id ) {
491
				$form['options']['custom_style'] = $style_id;
492
			} else {
493
				// save the old style to maybe update after styles import
494
				$form['options']['old_style'] = $form['options']['custom_style'];
495
496
				// Set to default
497
				$form['options']['custom_style'] = 1;
498
			}
499
		}
500
	}
501
502
	/**
503
	 * After styles are imported, check for any forms that were linked
504
	 * and link them back up.
505
	 *
506
	 * @since 2.2.7
507
	 */
508
	private static function update_custom_style_setting_after_import( $form_id ) {
509
		$form = FrmForm::getOne( $form_id );
510
511
		if ( $form && isset( $form->options['old_style'] ) ) {
512
			$form = (array) $form;
513
			$saved_style = $form['options']['custom_style'];
514
			$form['options']['custom_style'] = $form['options']['old_style'];
515
			self::update_custom_style_setting_on_import( $form );
516
			$has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] );
517
			if ( $has_changed ) {
518
				FrmForm::update( $form['id'], $form );
519
			}
520
		}
521
	}
522
523
	public static function import_xml_views( $views, $imported ) {
524
        $imported['posts'] = array();
525
        $form_action_type = FrmFormActionsController::$action_post_type;
526
527
        $post_types = array(
528
            'frm_display' => 'views',
529
            $form_action_type => 'actions',
530
            'frm_styles'    => 'styles',
531
        );
532
533
        foreach ( $views as $item ) {
534
			$post = array(
535
				'post_title'    => (string) $item->title,
536
				'post_name'     => (string) $item->post_name,
537
				'post_type'     => (string) $item->post_type,
538
				'post_password' => (string) $item->post_password,
539
				'guid'          => (string) $item->guid,
540
				'post_status'   => (string) $item->status,
541
				'post_author'   => FrmAppHelper::get_user_id_param( (string) $item->post_author ),
542
				'post_id'       => (int) $item->post_id,
543
				'post_parent'   => (int) $item->post_parent,
544
				'menu_order'    => (int) $item->menu_order,
545
				'post_content'  => FrmFieldsHelper::switch_field_ids( (string) $item->content ),
546
				'post_excerpt'  => FrmFieldsHelper::switch_field_ids( (string) $item->excerpt ),
547
				'is_sticky'     => (string) $item->is_sticky,
548
				'comment_status' => (string) $item->comment_status,
549
				'post_date'     => (string) $item->post_date,
550
				'post_date_gmt' => (string) $item->post_date_gmt,
551
				'ping_status'   => (string) $item->ping_status,
552
                'postmeta'      => array(),
553
                'tax_input'     => array(),
554
			);
555
556
            $old_id = $post['post_id'];
557
			self::populate_post( $post, $item, $imported );
558
559
			unset( $item );
560
561
			$post_id = false;
562
            if ( $post['post_type'] == $form_action_type ) {
563
                $action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] );
564
				if ( $action_control && is_object( $action_control ) ) {
565
					$post_id = $action_control->maybe_create_action( $post, $imported['form_status'] );
566
				}
567
				unset( $action_control );
568
            } else if ( $post['post_type'] == 'frm_styles' ) {
569
                // Properly encode post content before inserting the post
570
                $post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] );
571
				$custom_css = isset( $post['post_content']['custom_css'] ) ? $post['post_content']['custom_css'] : '';
572
                $post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] );
573
574
                // Create/update post now
575
                $post_id = wp_insert_post( $post );
576
				self::maybe_update_custom_css( $custom_css );
577
            } else {
578
                // Create/update post now
579
                $post_id = wp_insert_post( $post );
580
            }
581
582
			if ( ! is_numeric( $post_id ) ) {
583
                continue;
584
            }
585
586
			self::update_postmeta( $post, $post_id );
587
588
            $this_type = 'posts';
589
			if ( isset( $post_types[ $post['post_type'] ] ) ) {
590
				$this_type = $post_types[ $post['post_type'] ];
591
            }
592
593
			if ( isset( $post['ID'] ) && $post_id == $post['ID'] ) {
594
                $imported['updated'][ $this_type ]++;
595
            } else {
596
                $imported['imported'][ $this_type ]++;
597
            }
598
599
			$imported['posts'][ (int) $old_id ] = $post_id;
600
601
			do_action( 'frm_after_import_view', $post_id, $post );
602
603
			unset( $post );
604
		}
605
606
		self::maybe_update_stylesheet( $imported );
607
608
		return $imported;
609
    }
610
611
    private static function populate_post( &$post, $item, $imported ) {
612
		if ( isset( $item->attachment_url ) ) {
613
			$post['attachment_url'] = (string) $item->attachment_url;
614
		}
615
616
		if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) {
617
		    // update to new form id
618
		    $post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ];
619
		}
620
621
		// Don't allow default styles to take over a site's default style
622
		if ( 'frm_styles' == $post['post_type'] ) {
623
			$post['menu_order'] = 0;
624
		}
625
626
		foreach ( $item->postmeta as $meta ) {
627
			self::populate_postmeta( $post, $meta, $imported );
628
			unset( $meta );
629
		}
630
631
		self::populate_taxonomies( $post, $item );
632
633
		self::maybe_editing_post( $post );
634
    }
635
636
    private static function populate_postmeta( &$post, $meta, $imported ) {
637
        global $frm_duplicate_ids;
638
639
	    $m = array(
640
			'key'   => (string) $meta->meta_key,
641
			'value' => (string) $meta->meta_value,
642
		);
643
644
		//switch old form and field ids to new ones
645
		if ( $m['key'] == 'frm_form_id' && isset( $imported['forms'][ (int) $m['value'] ] ) ) {
646
		    $m['value'] = $imported['forms'][ (int) $m['value'] ];
647
		} else {
648
		    $m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
649
650
			if ( ! empty( $frm_duplicate_ids ) ) {
651
652
		        if ( $m['key'] == 'frm_dyncontent' ) {
653
					$m['value'] = FrmFieldsHelper::switch_field_ids( $m['value'] );
654
    		    } else if ( $m['key'] == 'frm_options' ) {
655
656
					foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) {
657
						if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) {
658
							$m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ];
659
    		            }
660
    		        }
661
662
                    $check_dup_array = array();
663
    		        if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) {
664
    		            if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) {
665
    		                $m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ];
666
    		            } else if ( is_array( $m['value']['order_by'] ) ) {
667
                            $check_dup_array[] = 'order_by';
668
    		            }
669
    		        }
670
671
    		        if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) {
672
    		            $check_dup_array[] = 'where';
673
    		        }
674
675
                    foreach ( $check_dup_array as $check_k ) {
676
						foreach ( (array) $m['value'][ $check_k ] as $mk => $mv ) {
677
							if ( isset( $frm_duplicate_ids[ $mv ] ) ) {
678
								$m['value'][ $check_k ][ $mk ] = $frm_duplicate_ids[ $mv ];
679
		                    }
680
							unset( $mk, $mv );
681
		                }
682
                    }
683
    		    }
684
		    }
685
		}
686
687
		if ( ! is_array( $m['value'] ) ) {
688
			$m['value'] = FrmAppHelper::maybe_json_decode( $m['value'] );
689
		}
690
691
		$post['postmeta'][ (string) $meta->meta_key ] = $m['value'];
0 ignored issues
show
introduced by
Detected usage of meta_key, possible slow query.
Loading history...
692
    }
693
694
    /**
695
     * Add terms to post
696
	 *
697
     * @param array $post by reference
698
     * @param object $item The XML object data
699
     */
700
    private static function populate_taxonomies( &$post, $item ) {
701
		foreach ( $item->category as $c ) {
702
			$att = $c->attributes();
703
			if ( ! isset( $att['nicename'] ) ) {
704
                continue;
705
            }
706
707
		    $taxonomy = (string) $att['domain'];
708
			if ( is_taxonomy_hierarchical( $taxonomy ) ) {
709
		        $name = (string) $att['nicename'];
710
				$h_term = get_term_by( 'slug', $name, $taxonomy );
711
		        if ( $h_term ) {
712
		            $name = $h_term->term_id;
713
		        }
714
				unset( $h_term );
715
		    } else {
716
		        $name = (string) $c;
717
		    }
718
719
			if ( ! isset( $post['tax_input'][ $taxonomy ] ) ) {
720
				$post['tax_input'][ $taxonomy ] = array();
721
			}
722
723
			$post['tax_input'][ $taxonomy ][] = $name;
724
			unset( $name );
725
		}
726
    }
727
728
    /**
729
     * Edit post if the key and created time match
730
     */
731
    private static function maybe_editing_post( &$post ) {
732
		$match_by = array(
733
		    'post_type'     => $post['post_type'],
734
		    'name'          => $post['post_name'],
735
		    'post_status'   => $post['post_status'],
736
		    'posts_per_page' => 1,
737
		);
738
739
		if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) {
740
		    $match_by['include'] = $post['post_id'];
741
			unset( $match_by['name'] );
742
		}
743
744
		$editing = get_posts( $match_by );
745
746
		if ( ! empty( $editing ) && current( $editing )->post_date == $post['post_date'] ) {
747
			// set the id of the post to edit
748
			$post['ID'] = current( $editing )->ID;
749
		}
750
    }
751
752
    private static function update_postmeta( &$post, $post_id ) {
753
        foreach ( $post['postmeta'] as $k => $v ) {
754
            if ( '_edit_last' == $k ) {
755
				$v = FrmAppHelper::get_user_id_param( $v );
756
            } else if ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) {
757
                //change the attachment ID
758
				$field_obj = FrmFieldFactory::get_field_type( 'file' );
759
				$v = $field_obj->get_file_id( $v );
760
            }
761
762
			update_post_meta( $post_id, $k, $v );
763
764
			unset( $k, $v );
765
        }
766
    }
767
768
	/**
769
	 * If a template includes custom css, let's include it.
770
	 * The custom css is included on the default style.
771
	 *
772
	 * @since 2.03
773
	 */
774
	private static function maybe_update_custom_css( $custom_css ) {
775
		if ( empty( $custom_css ) ) {
776
			return;
777
		}
778
779
		$frm_style = new FrmStyle();
780
		$default_style = $frm_style->get_default_style();
781
		$default_style->post_content['custom_css'] .= "\r\n\r\n" . $custom_css;
782
		$frm_style->save( $default_style );
783
	}
784
785
	private static function maybe_update_stylesheet( $imported ) {
786
		$new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] );
787
		$updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] );
788
		if ( $new_styles || $updated_styles ) {
789
			if ( is_admin() && function_exists( 'get_filesystem_method' ) ) {
790
				$frm_style = new FrmStyle();
791
				$frm_style->update( 'default' );
792
			}
793
			foreach ( $imported['forms'] as $form_id ) {
794
				self::update_custom_style_setting_after_import( $form_id );
795
			}
796
		}
797
	}
798
799
    /**
800
     * @param string $message
801
     */
802
	public static function parse_message( $result, &$message, &$errors ) {
803
		if ( is_wp_error( $result ) ) {
804
            $errors[] = $result->get_error_message();
805
        } else if ( ! $result ) {
806
            return;
807
        }
808
809
		if ( ! is_array( $result ) ) {
810
            $message = is_string( $result ) ? $result : htmlentities( print_r( $result, 1 ) );
0 ignored issues
show
introduced by
The use of function print_r() is discouraged
Loading history...
811
            return;
812
        }
813
814
        $t_strings = array(
815
            'imported'  => __( 'Imported', 'formidable' ),
816
            'updated'   => __( 'Updated', 'formidable' ),
817
        );
818
819
        $message = '<ul>';
820
        foreach ( $result as $type => $results ) {
821
			if ( ! isset( $t_strings[ $type ] ) ) {
822
                // only print imported and updated
823
                continue;
824
            }
825
826
			$s_message = array();
827
			foreach ( $results as $k => $m ) {
828
				self::item_count_message( $m, $k, $s_message );
829
				unset( $k, $m );
830
			}
831
832
			if ( ! empty( $s_message ) ) {
833
				$message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> ';
834
				$message .= implode( ', ', $s_message );
835
				$message .= '</li>';
836
			}
837
        }
838
839
        if ( $message == '<ul>' ) {
840
            $message = '';
841
            $errors[] = __( 'Nothing was imported or updated', 'formidable' );
842
        } else {
843
            $message .= '</ul>';
844
        }
845
    }
846
847
	public static function item_count_message( $m, $type, &$s_message ) {
848
        if ( ! $m ) {
849
            return;
850
        }
851
852
        $strings = array(
853
            'forms'     => sprintf( _n( '%1$s Form', '%1$s Forms', $m, 'formidable' ), $m ),
854
            'fields'    => sprintf( _n( '%1$s Field', '%1$s Fields', $m, 'formidable' ), $m ),
855
            'items'     => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ),
856
            'views'     => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ),
857
            'posts'     => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ),
858
            'styles'     => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ),
859
            'terms'     => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ),
860
            'actions'   => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ),
861
        );
862
863
		$s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type );
864
    }
865
866
	/**
867
	 * Prepare the form options for export
868
	 *
869
	 * @since 2.0.19
870
	 * @param string $options
871
	 * @return string
872
	 */
873
	public static function prepare_form_options_for_export( $options ) {
874
		$options = maybe_unserialize( $options );
875
		// Change custom_style to the post_name instead of ID
876
		if ( isset( $options['custom_style'] ) && 1 !== $options['custom_style'] ) {
877
			global $wpdb;
878
			$table = $wpdb->prefix . 'posts';
879
			$where = array( 'ID' => $options['custom_style'] );
880
			$select = 'post_name';
881
882
			$style_name = FrmDb::get_var( $table, $where, $select );
883
884
			if ( $style_name ) {
885
				$options['custom_style'] = $style_name;
886
			} else {
887
				$options['custom_style'] = 1;
888
			}
889
		}
890
		self::remove_default_form_options( $options );
891
		$options = serialize( $options );
892
		return self::cdata( $options );
893
	}
894
895
	/**
896
	 * If the saved value is the same as the default, remove it from the export
897
	 * This keeps file size down and prevents overriding global settings after import
898
	 *
899
	 * @since 3.05.01
900
	 */
901
	private static function remove_default_form_options( &$options ) {
902
		$defaults = FrmFormsHelper::get_default_opts();
903
		if ( is_callable( 'FrmProFormsHelper::get_default_opts' ) ) {
904
			$defaults += FrmProFormsHelper::get_default_opts();
905
		}
906
 		self::remove_defaults( $defaults, $options );
907
	}
908
909
	/**
910
	 * Remove extra settings from field to keep file size down
911
	 *
912
	 * @since 3.05.01
913
	 */
914
	public static function prepare_field_for_export( &$field ) {
915
		self::remove_default_field_options( $field );
916
	}
917
918
	/**
919
	 * Remove defaults from field options too
920
	 *
921
	 * @since 3.05.01
922
	 */
923
	private static function remove_default_field_options( &$field ) {
924
		$defaults = FrmFieldsHelper::get_default_field_options( $field->type );
925
926
		if ( empty( $defaults['custom_html'] ) ) {
927
			$defaults['custom_html'] = FrmFieldsHelper::get_default_html( $field->type );
928
		}
929
		$options = maybe_unserialize( $field->field_options );
930
 		self::remove_defaults( $defaults, $options );
931
		self::remove_default_html( 'custom_html', $defaults, $options );
932
		$field->field_options = serialize( $options );
933
	}
934
935
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
936
	 * Compare the default array to the saved values and
937
	 * remove if they are the same
938
	 *
939
	 * @since 3.05.01
940
	 */
941
	private static function remove_defaults( $defaults, &$saved ) {
942
		$array_defaults = array_filter( $defaults, 'is_array' );
943
		foreach ( $array_defaults as $d => $default ) {
944
			// compare array defaults
945
			if ( $default == $saved[ $d ] ) {
946
				unset( $saved[ $d ] );
947
			}
948
			unset( $defaults[ $d ] );
949
		}
950
 		$saved = array_diff_assoc( (array) $saved, $defaults );
951
	}
952
953
 	/**
0 ignored issues
show
Coding Style introduced by
There is some trailing whitespace on this line which should be avoided as per coding-style.
Loading history...
954
	 * The line endings may prevent html from being equal when it should
955
	 *
956
	 * @since 3.05.01
957
	 */
958
	private static function remove_default_html( $html_name, $defaults, &$options ) {
959
		if ( ! isset( $options[ $html_name ] ) || ! isset( $defaults[ $html_name ] ) ) {
960
			return;
961
		}
962
963
		$old_html = str_replace( "\r\n", "\n", $options[ $html_name ] );
964
		$default_html = $defaults[ $html_name ];
965
		if ( $old_html == $default_html ) {
966
			unset( $options[ $html_name ] );
967
			return;
968
		}
969
970
		// Account for some of the older field default HTML.
971
		$default_html = str_replace( ' id="frm_desc_field_[key]"', '', $default_html );
972
		if ( $old_html == $default_html ) {
973
			unset( $options[ $html_name ] );
974
		}
975
	}
976
977
	public static function cdata( $str ) {
978
		$str = maybe_unserialize( $str );
979
		if ( is_array( $str ) ) {
980
			$str = json_encode( $str );
981
		} else if ( seems_utf8( $str ) == false ) {
982
			$str = utf8_encode( $str );
983
		}
984
985
		if ( is_numeric( $str ) ) {
986
            return $str;
987
        }
988
989
		self::remove_invalid_characters_from_xml( $str );
990
991
		// $str = ent2ncr(esc_html( $str));
992
		$str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>';
993
994
		return $str;
995
	}
996
997
	/**
998
	 * Remove <US> character (unit separator) from exported strings
999
	 *
1000
	 * @since 2.0.22
1001
	 * @param string $str
1002
	 */
1003
	private static function remove_invalid_characters_from_xml( &$str ) {
1004
		// Remove <US> character
1005
		$str = str_replace( '\x1F', '', $str );
1006
	}
1007
1008
    public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) {
1009
        // Get post type
1010
        $post_type = FrmFormActionsController::$action_post_type;
1011
1012
        // Set up imported index, if not set up yet
1013
        if ( ! isset( $imported['imported']['actions'] ) ) {
1014
            $imported['imported']['actions'] = 0;
1015
        }
1016
1017
        // Migrate post settings to action
1018
        self::migrate_post_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch );
1019
1020
        // Migrate email settings to action
1021
        self::migrate_email_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch );
1022
    }
1023
1024
    /**
1025
    * Migrate post settings to form action
1026
    *
1027
    * @param string $post_type
1028
    */
1029
    private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
1030
		if ( ! isset( $form_options['create_post'] ) || ! $form_options['create_post'] ) {
1031
            return;
1032
        }
1033
1034
        $new_action = array(
1035
            'post_type'     => $post_type,
1036
            'post_excerpt'  => 'wppost',
1037
			'post_title'    => __( 'Create Posts', 'formidable' ),
1038
            'menu_order'    => $form_id,
1039
            'post_status'   => 'publish',
1040
            'post_content'  => array(),
1041
			'post_name'     => $form_id . '_wppost_1',
1042
        );
1043
1044
		$post_settings = array( 'post_type', 'post_category', 'post_content', 'post_excerpt', 'post_title', 'post_name', 'post_date', 'post_status', 'post_custom_fields', 'post_password' );
1045
1046
        foreach ( $post_settings as $post_setting ) {
1047
			if ( isset( $form_options[ $post_setting ] ) ) {
1048
				$new_action['post_content'][ $post_setting ] = $form_options[ $post_setting ];
1049
            }
1050
			unset( $post_setting );
1051
        }
1052
1053
		$new_action['event'] = array( 'create', 'update' );
1054
1055
        if ( $switch ) {
1056
			// Fields with string or int saved
1057
			$basic_fields = array( 'post_title', 'post_content', 'post_excerpt', 'post_password', 'post_date', 'post_status' );
1058
1059
			// Fields with arrays saved
1060
			$array_fields = array( 'post_category', 'post_custom_fields' );
1061
1062
			$new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields );
1063
        }
1064
		$new_action['post_content'] = json_encode( $new_action['post_content'] );
1065
1066
		$exists = get_posts(
1067
			array(
1068
				'name'          => $new_action['post_name'],
1069
				'post_type'     => $new_action['post_type'],
1070
				'post_status'   => $new_action['post_status'],
1071
				'numberposts'   => 1,
1072
			)
1073
		);
1074
1075
        if ( ! $exists ) {
1076
			// this isn't an email, but we need to use a class that will always be included
1077
			FrmDb::save_json_post( $new_action );
1078
            $imported['imported']['actions']++;
1079
        }
1080
    }
1081
1082
	/**
1083
	 * Switch old field IDs for new field IDs in emails and post
1084
	 *
1085
	 * @since 2.0
1086
	 * @param array $post_content - check for old field IDs
1087
	 * @param array $basic_fields - fields with string or int saved
1088
	 * @param array $array_fields - fields with arrays saved
1089
	 *
1090
	 * @return string $post_content - new field IDs
1091
	 */
1092
	private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) {
1093
        global $frm_duplicate_ids;
1094
1095
        // If there aren't IDs that were switched, end now
1096
        if ( ! $frm_duplicate_ids ) {
1097
            return;
1098
        }
1099
1100
        // Get old IDs
1101
        $old = array_keys( $frm_duplicate_ids );
1102
1103
        // Get new IDs
1104
        $new = array_values( $frm_duplicate_ids );
1105
1106
        // Do a str_replace with each item to set the new IDs
1107
        foreach ( $post_content as $key => $setting ) {
1108
            if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) {
1109
                // Replace old IDs with new IDs
1110
				$post_content[ $key ] = str_replace( $old, $new, $setting );
1111
            } else if ( is_array( $setting ) && in_array( $key, $array_fields ) ) {
1112
                foreach ( $setting as $k => $val ) {
1113
                    // Replace old IDs with new IDs
1114
					$post_content[ $key ][ $k ] = str_replace( $old, $new, $val );
1115
                }
1116
            }
1117
            unset( $key, $setting );
1118
        }
1119
        return $post_content;
1120
    }
1121
1122
    private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) {
1123
        // No old notifications or autoresponders to carry over
1124
		if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) {
1125
            return;
1126
        }
1127
1128
        // Initialize notifications array
1129
        $notifications = array();
1130
1131
        // Migrate regular notifications
1132
        self::migrate_notifications_to_action( $form_options, $form_id, $notifications );
1133
1134
        // Migrate autoresponders
1135
        self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications );
1136
1137
        if ( empty( $notifications ) ) {
1138
            return;
1139
        }
1140
1141
        foreach ( $notifications as $new_notification ) {
1142
            $new_notification['post_type']      = $post_type;
1143
            $new_notification['post_excerpt']   = 'email';
1144
			$new_notification['post_title']     = __( 'Email Notification', 'formidable' );
1145
            $new_notification['menu_order']     = $form_id;
1146
            $new_notification['post_status']    = 'publish';
1147
1148
            // Switch field IDs and keys, if needed
1149
            if ( $switch ) {
1150
1151
				// Switch field IDs in email conditional logic
1152
				self::switch_email_contition_field_ids( $new_notification['post_content'] );
1153
1154
				// Switch all other field IDs in email
1155
                $new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] );
1156
            }
1157
            $new_notification['post_content']   = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] );
1158
1159
			$exists = get_posts(
1160
				array(
1161
					'name'          => $new_notification['post_name'],
1162
					'post_type'     => $new_notification['post_type'],
1163
					'post_status'   => $new_notification['post_status'],
1164
					'numberposts'   => 1,
1165
				)
1166
			);
1167
1168
			if ( empty( $exists ) ) {
1169
				FrmDb::save_json_post( $new_notification );
1170
                $imported['imported']['actions']++;
1171
            }
1172
			unset( $new_notification );
1173
        }
1174
1175
		self::remove_deprecated_notification_settings( $form_id, $form_options );
1176
    }
1177
1178
	/**
1179
	 * Remove deprecated notification settings after migration
1180
	 *
1181
	 * @since 2.05
1182
	 *
1183
	 * @param int|string $form_id
1184
	 * @param array $form_options
1185
	 */
1186
	private static function remove_deprecated_notification_settings( $form_id, $form_options ) {
1187
		$delete_settings = array( 'notification', 'autoresponder', 'email_to' );
1188
		foreach ( $delete_settings as $index ) {
1189
			if ( isset( $form_options[ $index ] ) ) {
1190
				unset( $form_options[ $index ] );
1191
			}
1192
		}
1193
		FrmForm::update( $form_id, array( 'options' => $form_options ) );
1194
	}
1195
1196
    private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) {
1197
        if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) {
1198
            // add old settings into notification array
1199
			$form_options['notification'] = array( 0 => $form_options );
1200
        } else if ( isset( $form_options['notification']['email_to'] ) ) {
1201
            // make sure it's in the correct format
1202
			$form_options['notification'] = array( 0 => $form_options['notification'] );
1203
        }
1204
1205
		if ( isset( $form_options['notification'] ) && is_array( $form_options['notification'] ) ) {
1206
            foreach ( $form_options['notification'] as $email_key => $notification ) {
1207
1208
				$atts = array(
1209
					'email_to'  => '',
1210
					'reply_to'  => '',
1211
					'reply_to_name' => '',
1212
					'event'     => '',
1213
					'form_id'   => $form_id,
1214
					'email_key' => $email_key,
1215
				);
1216
1217
                // Format the email data
1218
                self::format_email_data( $atts, $notification );
1219
1220
				if ( isset( $notification['twilio'] ) && $notification['twilio'] ) {
1221
					do_action( 'frm_create_twilio_action', $atts, $notification );
1222
				}
1223
1224
                // Setup the new notification
1225
                $new_notification = array();
1226
                self::setup_new_notification( $new_notification, $notification, $atts );
1227
1228
                $notifications[] = $new_notification;
1229
            }
1230
        }
1231
    }
1232
1233
    private static function format_email_data( &$atts, $notification ) {
1234
        // Format email_to
1235
        self::format_email_to_data( $atts, $notification );
1236
1237
        // Format the reply to email and name
1238
		$reply_fields = array(
1239
			'reply_to'      => '',
1240
			'reply_to_name' => '',
1241
		);
1242
        foreach ( $reply_fields as $f => $val ) {
1243
			if ( isset( $notification[ $f ] ) ) {
1244
				$atts[ $f ] = $notification[ $f ];
1245
				if ( 'custom' == $notification[ $f ] ) {
1246
					$atts[ $f ] = $notification[ 'cust_' . $f ];
1247
				} else if ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) {
1248
					$atts[ $f ] = '[' . $atts[ $f ] . ']';
1249
                }
1250
            }
1251
            unset( $f, $val );
1252
        }
1253
1254
        // Format event
1255
		$atts['event'] = array( 'create' );
1256
        if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) {
1257
            $atts['event'][] = 'update';
1258
		} elseif ( isset( $notification['update_email'] ) && 2 == $notification['update_email'] ) {
1259
			$atts['event'] = array( 'update' );
1260
        }
1261
    }
1262
1263
    private static function format_email_to_data( &$atts, $notification ) {
1264
        if ( isset( $notification['email_to'] ) ) {
1265
			$atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] );
1266
        } else {
1267
            $atts['email_to'] = array();
1268
        }
1269
1270
        if ( isset( $notification['also_email_to'] ) ) {
1271
            $email_fields = (array) $notification['also_email_to'];
1272
            $atts['email_to'] = array_merge( $email_fields, $atts['email_to'] );
1273
            unset( $email_fields );
1274
        }
1275
1276
        foreach ( $atts['email_to'] as $key => $email_field ) {
1277
1278
            if ( is_numeric( $email_field ) ) {
1279
				$atts['email_to'][ $key ] = '[' . $email_field . ']';
1280
            }
1281
1282
			if ( strpos( $email_field, '|' ) ) {
1283
                $email_opt = explode( '|', $email_field );
1284
                if ( isset( $email_opt[0] ) ) {
1285
					$atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']';
1286
                }
1287
                unset( $email_opt );
1288
            }
1289
        }
1290
		$atts['email_to'] = implode( ', ', $atts['email_to'] );
1291
    }
1292
1293
    private static function setup_new_notification( &$new_notification, $notification, $atts ) {
1294
        // Set up new notification
1295
        $new_notification = array(
1296
            'post_content'  => array(
1297
                'email_to'      => $atts['email_to'],
1298
                'event'         => $atts['event'],
1299
            ),
1300
			'post_name'         => $atts['form_id'] . '_email_' . $atts['email_key'],
1301
        );
1302
1303
        // Add more fields to the new notification
1304
        $add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' );
1305
        foreach ( $add_fields as $add_field ) {
1306
			if ( isset( $notification[ $add_field ] ) ) {
1307
				$new_notification['post_content'][ $add_field ] = $notification[ $add_field ];
1308
            } else if ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) {
1309
				$new_notification['post_content'][ $add_field ] = 0;
1310
            } else {
1311
				$new_notification['post_content'][ $add_field ] = '';
1312
            }
1313
            unset( $add_field );
1314
        }
1315
1316
		// Set reply to
1317
		$new_notification['post_content']['reply_to'] = $atts['reply_to'];
1318
1319
        // Set from
1320
		if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) {
1321
			$new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>';
1322
        }
1323
    }
1324
1325
	/**
1326
	* Switch field IDs in pre-2.0 email conditional logic
1327
	*
1328
	* @param $post_content array, pass by reference
1329
	*/
1330
	private static function switch_email_contition_field_ids( &$post_content ) {
1331
		// Switch field IDs in conditional logic
1332
		if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) {
1333
			foreach ( $post_content['conditions'] as $email_key => $val ) {
1334
				if ( is_numeric( $email_key ) ) {
1335
					$post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) );
1336
				}
1337
				unset( $email_key, $val );
1338
			}
1339
		}
1340
	}
1341
1342
    private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) {
1343
		if ( isset( $form_options['auto_responder'] ) && $form_options['auto_responder'] && isset( $form_options['ar_email_message'] ) && $form_options['ar_email_message'] ) {
1344
			// migrate autoresponder
1345
1346
			$email_field = isset( $form_options['ar_email_to'] ) ? $form_options['ar_email_to'] : 0;
1347
			if ( strpos( $email_field, '|' ) ) {
1348
				// data from entries field
1349
				$email_field = explode( '|', $email_field );
1350
				if ( isset( $email_field[1] ) ) {
1351
					$email_field = $email_field[1];
1352
				}
1353
			}
1354
			if ( is_numeric( $email_field ) && ! empty( $email_field ) ) {
1355
				$email_field = '[' . $email_field . ']';
1356
			}
1357
1358
            $notification = $form_options;
1359
            $new_notification2 = array(
1360
				'post_content'  => array(
1361
					'email_message' => $notification['ar_email_message'],
1362
					'email_subject' => isset( $notification['ar_email_subject'] ) ? $notification['ar_email_subject'] : '',
1363
					'email_to'      => $email_field,
1364
					'plain_text'    => isset( $notification['ar_plain_text'] ) ? $notification['ar_plain_text'] : 0,
1365
					'inc_user_info' => 0,
1366
				),
1367
				'post_name'     => $form_id . '_email_' . count( $notifications ),
1368
            );
1369
1370
			$reply_to = isset( $notification['ar_reply_to'] ) ? $notification['ar_reply_to'] : '';
1371
			$reply_to_name = isset( $notification['ar_reply_to_name'] ) ? $notification['ar_reply_to_name'] : '';
1372
1373
			if ( ! empty( $reply_to ) ) {
1374
				$new_notification2['post_content']['reply_to'] = $reply_to;
1375
			}
1376
1377
			if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) {
1378
				$new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>';
1379
			}
1380
1381
            $notifications[] = $new_notification2;
1382
            unset( $new_notification2 );
1383
        }
1384
    }
1385
}
1386
1387