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" . $padding; |
|
|
|
|
12
|
|
|
$tag = ( is_numeric( $ok ) ? 'key:' : '' ) . $ok; |
13
|
|
|
echo '<' . $tag . '>'; |
|
|
|
|
14
|
|
|
self::get_xml_values( $ov, $padding . ' ' ); |
15
|
|
|
if ( is_array( $ov ) ) { |
16
|
|
|
echo "\n" . $padding; |
|
|
|
|
17
|
|
|
} |
18
|
|
|
echo '</' . $tag . '>'; |
|
|
|
|
19
|
|
|
} |
20
|
|
|
} else { |
21
|
|
|
echo self::cdata( $opt ); |
|
|
|
|
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
public static function import_xml( $file ) { |
26
|
|
|
$defaults = array( |
27
|
|
|
'forms' => 0, 'fields' => 0, 'terms' => 0, |
28
|
|
|
'posts' => 0, 'views' => 0, 'actions' => 0, |
29
|
|
|
'styles' => 0, |
30
|
|
|
); |
31
|
|
|
|
32
|
|
|
$imported = array( |
33
|
|
|
'imported' => $defaults, |
34
|
|
|
'updated' => $defaults, |
35
|
|
|
'forms' => array(), |
36
|
|
|
'terms' => array(), |
37
|
|
|
); |
38
|
|
|
|
39
|
|
|
unset($defaults); |
40
|
|
|
|
41
|
|
|
if ( ! defined( 'WP_IMPORTING' ) ) { |
42
|
|
|
define('WP_IMPORTING', true); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if ( ! class_exists( 'DOMDocument' ) ) { |
46
|
|
|
return new WP_Error( 'SimpleXML_parse_error', __( 'Your server does not have XML enabled', 'formidable' ), libxml_get_errors() ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
$dom = new DOMDocument; |
50
|
|
|
$success = $dom->loadXML( file_get_contents( $file ) ); |
|
|
|
|
51
|
|
|
if ( ! $success ) { |
52
|
|
|
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if ( ! function_exists('simplexml_import_dom') ) { |
56
|
|
|
return new WP_Error( 'SimpleXML_parse_error', __( 'Your server is missing the simplexml_import_dom function', 'formidable' ), libxml_get_errors() ); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
$xml = simplexml_import_dom( $dom ); |
60
|
|
|
unset( $dom ); |
61
|
|
|
|
62
|
|
|
// halt if loading produces an error |
63
|
|
|
if ( ! $xml ) { |
64
|
|
|
return new WP_Error( 'SimpleXML_parse_error', __( 'There was an error when reading this XML file', 'formidable' ), libxml_get_errors() ); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// add terms, forms (form and field ids), posts (post ids), and entries to db, in that order |
68
|
|
|
foreach ( array( 'term', 'form', 'view' ) as $item_type ) { |
69
|
|
|
// grab cats, tags, and terms, or forms or posts |
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} ); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$return = apply_filters('frm_importing_xml', $imported, $xml ); |
78
|
|
|
|
79
|
|
|
return $return; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public static function import_xml_terms( $terms, $imported ) { |
83
|
|
|
foreach ( $terms as $t ) { |
84
|
|
|
if ( term_exists( (string) $t->term_slug, (string) $t->term_taxonomy ) ) { |
85
|
|
|
continue; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
$parent = self::get_term_parent_id( $t ); |
89
|
|
|
|
90
|
|
|
$term = wp_insert_term( (string) $t->term_name, (string) $t->term_taxonomy, array( |
91
|
|
|
'slug' => (string) $t->term_slug, |
92
|
|
|
'description' => (string) $t->term_description, |
93
|
|
|
'parent' => empty( $parent ) ? 0 : $parent, |
94
|
|
|
)); |
95
|
|
|
|
96
|
|
|
if ( $term && is_array( $term ) ) { |
97
|
|
|
$imported['imported']['terms']++; |
98
|
|
|
$imported['terms'][ (int) $t->term_id ] = $term['term_id']; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
unset( $term, $t ); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $imported; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @since 2.0.8 |
109
|
|
|
*/ |
110
|
|
|
private static function get_term_parent_id( $t ) { |
111
|
|
|
$parent = (string) $t->term_parent; |
112
|
|
|
if ( ! empty( $parent ) ) { |
113
|
|
|
$parent = term_exists( (string) $t->term_parent, (string) $t->term_taxonomy ); |
114
|
|
|
if ( $parent ) { |
115
|
|
|
$parent = $parent['term_id']; |
116
|
|
|
} else { |
117
|
|
|
$parent = 0; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
return $parent; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
public static function import_xml_forms( $forms, $imported ) { |
124
|
|
|
$child_forms = array(); |
125
|
|
|
|
126
|
|
|
// Import child forms first |
127
|
|
|
self::put_child_forms_first( $forms ); |
128
|
|
|
|
129
|
|
|
foreach ( $forms as $item ) { |
130
|
|
|
$form = self::fill_form( $item ); |
131
|
|
|
|
132
|
|
|
self::update_custom_style_setting_on_import( $form ); |
133
|
|
|
|
134
|
|
|
$this_form = self::maybe_get_form( $form ); |
135
|
|
|
|
136
|
|
|
$old_id = $form_fields = false; |
137
|
|
|
if ( ! empty( $this_form ) ) { |
138
|
|
|
$form_id = $old_id = $this_form->id; |
139
|
|
|
self::update_form( $this_form, $form, $imported ); |
140
|
|
|
|
141
|
|
|
$form_fields = self::get_form_fields( $form_id ); |
142
|
|
|
} else { |
143
|
|
|
$form_id = FrmForm::create( $form ); |
144
|
|
|
if ( $form_id ) { |
145
|
|
|
$imported['imported']['forms']++; |
146
|
|
|
// Keep track of whether this specific form was updated or not |
147
|
|
|
$imported['form_status'][ $form_id ] = 'imported'; |
148
|
|
|
self::track_imported_child_forms( (int) $form_id, $form['parent_form_id'], $child_forms ); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
self::import_xml_fields( $item->field, $form_id, $this_form, $form_fields, $imported ); |
153
|
|
|
|
154
|
|
|
self::delete_removed_fields( $form_fields ); |
155
|
|
|
|
156
|
|
|
// Update field ids/keys to new ones |
157
|
|
|
do_action( 'frm_after_duplicate_form', $form_id, $form, array( 'old_id' => $old_id ) ); |
158
|
|
|
|
159
|
|
|
$imported['forms'][ (int) $item->id ] = $form_id; |
160
|
|
|
|
161
|
|
|
// Send pre 2.0 form options through function that creates actions |
162
|
|
|
self::migrate_form_settings_to_actions( $form['options'], $form_id, $imported, $switch = true ); |
163
|
|
|
|
164
|
|
|
do_action( 'frm_after_import_form', $form_id, $form ); |
165
|
|
|
|
166
|
|
|
unset($form, $item); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
self::maybe_update_child_form_parent_id( $imported['forms'], $child_forms ); |
170
|
|
|
|
171
|
|
|
return $imported; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
private static function fill_form( $item ) { |
175
|
|
|
$form = array( |
176
|
|
|
'id' => (int) $item->id, |
177
|
|
|
'form_key' => (string) $item->form_key, |
178
|
|
|
'name' => (string) $item->name, |
179
|
|
|
'description' => (string) $item->description, |
180
|
|
|
'options' => (string) $item->options, |
181
|
|
|
'logged_in' => (int) $item->logged_in, |
182
|
|
|
'is_template' => (int) $item->is_template, |
183
|
|
|
'default_template' => (int) $item->default_template, |
184
|
|
|
'editable' => (int) $item->editable, |
185
|
|
|
'status' => (string) $item->status, |
186
|
|
|
'parent_form_id' => isset( $item->parent_form_id ) ? (int) $item->parent_form_id : 0, |
187
|
|
|
'created_at' => date( 'Y-m-d H:i:s', strtotime( (string) $item->created_at ) ), |
188
|
|
|
); |
189
|
|
|
$form['options'] = FrmAppHelper::maybe_json_decode( $form['options'] ); |
190
|
|
|
return $form; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private static function maybe_get_form( $form ) { |
194
|
|
|
// if template, allow to edit if form keys match, otherwise, creation date must also match |
195
|
|
|
$edit_query = array( 'form_key' => $form['form_key'], 'is_template' => $form['is_template'] ); |
196
|
|
|
if ( ! $form['is_template'] ) { |
197
|
|
|
$edit_query['created_at'] = $form['created_at']; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
$edit_query = apply_filters( 'frm_match_xml_form', $edit_query, $form ); |
201
|
|
|
|
202
|
|
|
return FrmForm::getAll( $edit_query, '', 1 ); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
private static function update_form( $this_form, $form, &$imported ) { |
206
|
|
|
$form_id = $this_form->id; |
207
|
|
|
FrmForm::update( $form_id, $form ); |
208
|
|
|
$imported['updated']['forms']++; |
209
|
|
|
// Keep track of whether this specific form was updated or not |
210
|
|
|
$imported['form_status'][ $form_id ] = 'updated'; |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
private static function get_form_fields( $form_id ) { |
214
|
|
|
$form_fields = FrmField::get_all_for_form( $form_id, '', 'exclude', 'exclude' ); |
215
|
|
|
$old_fields = array(); |
216
|
|
|
foreach ( $form_fields as $f ) { |
217
|
|
|
$old_fields[ $f->id ] = $f; |
218
|
|
|
$old_fields[ $f->field_key ] = $f->id; |
219
|
|
|
unset($f); |
220
|
|
|
} |
221
|
|
|
$form_fields = $old_fields; |
222
|
|
|
return $form_fields; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* Delete any fields attached to this form that were not included in the template |
227
|
|
|
*/ |
228
|
|
|
private static function delete_removed_fields( $form_fields ) { |
229
|
|
|
if ( ! empty( $form_fields ) ) { |
230
|
|
|
foreach ( $form_fields as $field ) { |
231
|
|
|
if ( is_object( $field ) ) { |
232
|
|
|
FrmField::destroy( $field->id ); |
233
|
|
|
} |
234
|
|
|
unset( $field ); |
235
|
|
|
} |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Put child forms first so they will be imported before parents |
241
|
|
|
* |
242
|
|
|
* @since 2.0.16 |
243
|
|
|
* @param array $forms |
244
|
|
|
*/ |
245
|
|
|
private static function put_child_forms_first( &$forms ) { |
246
|
|
|
$child_forms = array(); |
247
|
|
|
$regular_forms = array(); |
248
|
|
|
|
249
|
|
|
foreach ( $forms as $form ) { |
250
|
|
|
$parent_form_id = isset( $form->parent_form_id) ? (int) $form->parent_form_id : 0; |
251
|
|
|
|
252
|
|
|
if ( $parent_form_id ) { |
253
|
|
|
$child_forms[] = $form; |
254
|
|
|
} else { |
255
|
|
|
$regular_forms[] = $form; |
256
|
|
|
} |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
$forms = array_merge( $child_forms, $regular_forms ); |
260
|
|
|
} |
261
|
|
|
|
262
|
|
|
/** |
263
|
|
|
* Keep track of all imported child forms |
264
|
|
|
* |
265
|
|
|
* @since 2.0.16 |
266
|
|
|
* @param int $form_id |
267
|
|
|
* @param int $parent_form_id |
268
|
|
|
* @param array $child_forms |
269
|
|
|
*/ |
270
|
|
|
private static function track_imported_child_forms( $form_id, $parent_form_id, &$child_forms ) { |
271
|
|
|
if ( $parent_form_id ) { |
272
|
|
|
$child_forms[ $form_id ] = $parent_form_id; |
273
|
|
|
} |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Update the parent_form_id on imported child forms |
278
|
|
|
* Child forms are imported first so their parent_form_id will need to be updated after the parent is imported |
279
|
|
|
* |
280
|
|
|
* @since 2.0.6 |
281
|
|
|
* @param array $imported_forms |
282
|
|
|
* @param array $child_forms |
283
|
|
|
*/ |
284
|
|
|
private static function maybe_update_child_form_parent_id( $imported_forms, $child_forms ) { |
285
|
|
|
foreach ( $child_forms as $child_form_id => $old_parent_form_id ) { |
286
|
|
|
|
287
|
|
|
if ( isset( $imported_forms[ $old_parent_form_id ] ) && $imported_forms[ $old_parent_form_id ] != $old_parent_form_id ) { |
288
|
|
|
// Update all children with this old parent_form_id |
289
|
|
|
$new_parent_form_id = (int) $imported_forms[ $old_parent_form_id ]; |
290
|
|
|
|
291
|
|
|
FrmForm::update( $child_form_id, array( 'parent_form_id' => $new_parent_form_id ) ); |
292
|
|
|
} |
293
|
|
|
} |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
/** |
297
|
|
|
* Import all fields for a form |
298
|
|
|
* @since 2.0.13 |
299
|
|
|
* |
300
|
|
|
* TODO: Cut down on params |
301
|
|
|
*/ |
302
|
|
|
private static function import_xml_fields( $xml_fields, $form_id, $this_form, &$form_fields, &$imported ) { |
303
|
|
|
$in_section = 0; |
304
|
|
|
|
305
|
|
|
foreach ( $xml_fields as $field ) { |
306
|
|
|
$f = self::fill_field( $field, $form_id ); |
307
|
|
|
|
308
|
|
|
if ( is_array($f['default_value']) && in_array($f['type'], array( |
309
|
|
|
'text', 'email', 'url', 'textarea', |
310
|
|
|
'number','phone', 'date', 'time', |
311
|
|
|
'hidden', 'password', 'tag', 'image', |
312
|
|
|
)) ) { |
313
|
|
|
if ( count($f['default_value']) === 1 ) { |
314
|
|
|
$f['default_value'] = '[' . reset( $f['default_value'] ) . ']'; |
315
|
|
|
} else { |
316
|
|
|
$f['default_value'] = reset($f['default_value']); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
self::maybe_update_in_section_variable( $in_section, $f ); |
321
|
|
|
self::maybe_update_form_select( $f, $imported ); |
322
|
|
|
self::maybe_update_get_values_form_setting( $imported, $f ); |
323
|
|
|
|
324
|
|
|
if ( ! empty($this_form) ) { |
325
|
|
|
// check for field to edit by field id |
326
|
|
|
if ( isset( $form_fields[ $f['id'] ] ) ) { |
327
|
|
|
FrmField::update( $f['id'], $f ); |
328
|
|
|
$imported['updated']['fields']++; |
329
|
|
|
|
330
|
|
|
unset( $form_fields[ $f['id'] ] ); |
331
|
|
|
|
332
|
|
|
//unset old field key |
333
|
|
|
if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
334
|
|
|
unset( $form_fields[ $f['field_key'] ] ); |
335
|
|
|
} |
336
|
|
|
} else if ( isset( $form_fields[ $f['field_key'] ] ) ) { |
337
|
|
|
// check for field to edit by field key |
338
|
|
|
unset($f['id']); |
339
|
|
|
|
340
|
|
|
FrmField::update( $form_fields[ $f['field_key'] ], $f ); |
341
|
|
|
$imported['updated']['fields']++; |
342
|
|
|
|
343
|
|
|
unset( $form_fields[ $form_fields[ $f['field_key'] ] ] ); //unset old field id |
344
|
|
|
unset( $form_fields[ $f['field_key'] ] ); //unset old field key |
345
|
|
|
} else { |
346
|
|
|
// if no matching field id or key in this form, create the field |
347
|
|
|
self::create_imported_field( $f, $imported ); |
348
|
|
|
} |
349
|
|
|
} else { |
350
|
|
|
|
351
|
|
|
self::create_imported_field( $f, $imported ); |
352
|
|
|
} |
353
|
|
|
} |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
private static function fill_field( $field, $form_id ) { |
357
|
|
|
return array( |
358
|
|
|
'id' => (int) $field->id, |
359
|
|
|
'field_key' => (string) $field->field_key, |
360
|
|
|
'name' => (string) $field->name, |
361
|
|
|
'description' => (string) $field->description, |
362
|
|
|
'type' => (string) $field->type, |
363
|
|
|
'default_value' => FrmAppHelper::maybe_json_decode( (string) $field->default_value), |
364
|
|
|
'field_order' => (int) $field->field_order, |
365
|
|
|
'form_id' => (int) $form_id, |
366
|
|
|
'required' => (int) $field->required, |
367
|
|
|
'options' => FrmAppHelper::maybe_json_decode( (string) $field->options), |
368
|
|
|
'field_options' => FrmAppHelper::maybe_json_decode( (string) $field->field_options ), |
369
|
|
|
); |
370
|
|
|
} |
371
|
|
|
|
372
|
|
|
/** |
373
|
|
|
* Update the current in_section value |
374
|
|
|
* |
375
|
|
|
* @since 2.0.25 |
376
|
|
|
* @param int $in_section |
377
|
|
|
* @param array $f |
378
|
|
|
*/ |
379
|
|
|
private static function maybe_update_in_section_variable( &$in_section, &$f ) { |
380
|
|
|
// If we're at the end of a section, switch $in_section is 0 |
381
|
|
|
if ( in_array( $f['type'], array( 'end_divider', 'break', 'form' ) ) ) { |
382
|
|
|
$in_section = 0; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
// Update the current field's in_section value |
386
|
|
|
if ( ! isset( $f['field_options']['in_section'] ) ) { |
387
|
|
|
$f['field_options']['in_section'] = $in_section; |
388
|
|
|
} |
389
|
|
|
|
390
|
|
|
// If we're starting a new section, switch $in_section to ID of divider |
391
|
|
|
if ( $f['type'] == 'divider' ) { |
392
|
|
|
$in_section = $f['id']; |
393
|
|
|
} |
394
|
|
|
} |
395
|
|
|
|
396
|
|
|
/** |
397
|
|
|
* Switch the form_select on a repeating field or embedded form if it needs to be switched |
398
|
|
|
* |
399
|
|
|
* @since 2.0.16 |
400
|
|
|
* @param array $f |
401
|
|
|
* @param array $imported |
402
|
|
|
*/ |
403
|
|
|
private static function maybe_update_form_select( &$f, $imported ) { |
404
|
|
|
if ( ! isset( $imported['forms'] ) ) { |
405
|
|
|
return; |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
if ( $f['type'] == 'form' || ( $f['type'] == 'divider' && FrmField::is_option_true( $f['field_options'], 'repeat' ) ) ) { |
409
|
|
View Code Duplication |
if ( FrmField::is_option_true( $f['field_options'], 'form_select' ) ) { |
|
|
|
|
410
|
|
|
$form_select = $f['field_options']['form_select']; |
411
|
|
|
if ( isset( $imported['forms'][ $form_select ] ) ) { |
412
|
|
|
$f['field_options']['form_select'] = $imported['forms'][ $form_select ]; |
413
|
|
|
} |
414
|
|
|
} |
415
|
|
|
} |
416
|
|
|
} |
417
|
|
|
|
418
|
|
|
/** |
419
|
|
|
* Update the get_values_form setting if the form was imported |
420
|
|
|
* |
421
|
|
|
* @since 2.01.0 |
422
|
|
|
* @param array $imported |
423
|
|
|
* @param array $f |
424
|
|
|
*/ |
425
|
|
|
private static function maybe_update_get_values_form_setting( $imported, &$f ) { |
426
|
|
|
if ( ! isset( $imported['forms'] ) ) { |
427
|
|
|
return; |
428
|
|
|
} |
429
|
|
|
|
430
|
|
View Code Duplication |
if ( FrmField::is_option_true_in_array( $f['field_options'], 'get_values_form' ) ) { |
|
|
|
|
431
|
|
|
$old_form = $f['field_options']['get_values_form']; |
432
|
|
|
if ( isset( $imported['forms'][ $old_form ] ) ) { |
433
|
|
|
$f['field_options']['get_values_form'] = $imported['forms'][ $old_form ]; |
434
|
|
|
} |
435
|
|
|
} |
436
|
|
|
} |
437
|
|
|
|
438
|
|
|
/** |
439
|
|
|
* Create an imported field |
440
|
|
|
* |
441
|
|
|
* @since 2.0.25 |
442
|
|
|
* @param array $f |
443
|
|
|
* @param array $imported |
444
|
|
|
*/ |
445
|
|
|
private static function create_imported_field( $f, &$imported ) { |
446
|
|
|
$new_id = FrmField::create( $f ); |
447
|
|
|
if ( $new_id != false ) { |
448
|
|
|
$imported['imported']['fields']++; |
449
|
|
|
do_action( 'frm_after_field_is_imported', $f, $new_id ); |
450
|
|
|
} |
451
|
|
|
} |
452
|
|
|
|
453
|
|
|
/** |
454
|
|
|
* Updates the custom style setting on import |
455
|
|
|
* Convert the post slug to an ID |
456
|
|
|
* |
457
|
|
|
* @since 2.0.19 |
458
|
|
|
* @param array $form |
459
|
|
|
* |
460
|
|
|
*/ |
461
|
|
|
private static function update_custom_style_setting_on_import( &$form ) { |
462
|
|
|
if ( ! isset( $form['options']['custom_style'] ) ) { |
463
|
|
|
return; |
464
|
|
|
} |
465
|
|
|
|
466
|
|
|
if ( is_numeric( $form['options']['custom_style'] ) ) { |
467
|
|
|
// Set to default |
468
|
|
|
$form['options']['custom_style'] = 1; |
469
|
|
|
} else { |
470
|
|
|
// Replace the style name with the style ID on import |
471
|
|
|
global $wpdb; |
472
|
|
|
$table = $wpdb->prefix . 'posts'; |
473
|
|
|
$where = array( |
474
|
|
|
'post_name' => $form['options']['custom_style'], |
475
|
|
|
'post_type' => 'frm_styles', |
476
|
|
|
); |
477
|
|
|
$select = 'ID'; |
478
|
|
|
$style_id = FrmDb::get_var( $table, $where, $select ); |
479
|
|
|
|
480
|
|
|
if ( $style_id ) { |
481
|
|
|
$form['options']['custom_style'] = $style_id; |
482
|
|
|
} else { |
483
|
|
|
// save the old style to maybe update after styles import |
484
|
|
|
$form['options']['old_style'] = $form['options']['custom_style']; |
485
|
|
|
|
486
|
|
|
// Set to default |
487
|
|
|
$form['options']['custom_style'] = 1; |
488
|
|
|
} |
489
|
|
|
} |
490
|
|
|
} |
491
|
|
|
|
492
|
|
|
/** |
493
|
|
|
* After styles are imported, check for any forms that were linked |
494
|
|
|
* and link them back up. |
495
|
|
|
* |
496
|
|
|
* @since 2.2.7 |
497
|
|
|
*/ |
498
|
|
|
private static function update_custom_style_setting_after_import( $form_id ) { |
499
|
|
|
$form = FrmForm::getOne( $form_id ); |
500
|
|
|
|
501
|
|
|
if ( $form && isset( $form->options['old_style'] ) ) { |
502
|
|
|
$form = (array) $form; |
503
|
|
|
$saved_style = $form['options']['custom_style']; |
504
|
|
|
$form['options']['custom_style'] = $form['options']['old_style']; |
505
|
|
|
self::update_custom_style_setting_on_import( $form ); |
506
|
|
|
$has_changed = ( $form['options']['custom_style'] != $saved_style && $form['options']['custom_style'] != $form['options']['old_style'] ); |
507
|
|
|
if ( $has_changed ) { |
508
|
|
|
FrmForm::update( $form['id'], $form ); |
509
|
|
|
} |
510
|
|
|
} |
511
|
|
|
} |
512
|
|
|
|
513
|
|
|
public static function import_xml_views( $views, $imported ) { |
514
|
|
|
$imported['posts'] = array(); |
515
|
|
|
$form_action_type = FrmFormActionsController::$action_post_type; |
516
|
|
|
|
517
|
|
|
$post_types = array( |
518
|
|
|
'frm_display' => 'views', |
519
|
|
|
$form_action_type => 'actions', |
520
|
|
|
'frm_styles' => 'styles', |
521
|
|
|
); |
522
|
|
|
|
523
|
|
|
foreach ( $views as $item ) { |
524
|
|
|
$post = array( |
525
|
|
|
'post_title' => (string) $item->title, |
526
|
|
|
'post_name' => (string) $item->post_name, |
527
|
|
|
'post_type' => (string) $item->post_type, |
528
|
|
|
'post_password' => (string) $item->post_password, |
529
|
|
|
'guid' => (string) $item->guid, |
530
|
|
|
'post_status' => (string) $item->status, |
531
|
|
|
'post_author' => FrmAppHelper::get_user_id_param( (string) $item->post_author ), |
532
|
|
|
'post_id' => (int) $item->post_id, |
533
|
|
|
'post_parent' => (int) $item->post_parent, |
534
|
|
|
'menu_order' => (int) $item->menu_order, |
535
|
|
|
'post_content' => FrmFieldsHelper::switch_field_ids( (string) $item->content ), |
536
|
|
|
'post_excerpt' => FrmFieldsHelper::switch_field_ids( (string) $item->excerpt ), |
537
|
|
|
'is_sticky' => (string) $item->is_sticky, |
538
|
|
|
'comment_status' => (string) $item->comment_status, |
539
|
|
|
'post_date' => (string) $item->post_date, |
540
|
|
|
'post_date_gmt' => (string) $item->post_date_gmt, |
541
|
|
|
'ping_status' => (string) $item->ping_status, |
542
|
|
|
'postmeta' => array(), |
543
|
|
|
'tax_input' => array(), |
544
|
|
|
); |
545
|
|
|
|
546
|
|
|
$old_id = $post['post_id']; |
547
|
|
|
self::populate_post($post, $item, $imported); |
548
|
|
|
|
549
|
|
|
unset($item); |
550
|
|
|
|
551
|
|
|
$post_id = false; |
552
|
|
|
if ( $post['post_type'] == $form_action_type ) { |
553
|
|
|
$action_control = FrmFormActionsController::get_form_actions( $post['post_excerpt'] ); |
554
|
|
|
if ( $action_control && is_object( $action_control ) ) { |
555
|
|
|
$post_id = $action_control->maybe_create_action( $post, $imported['form_status'] ); |
556
|
|
|
} |
557
|
|
|
unset($action_control); |
558
|
|
|
} else if ( $post['post_type'] == 'frm_styles' ) { |
559
|
|
|
// Properly encode post content before inserting the post |
560
|
|
|
$post['post_content'] = FrmAppHelper::maybe_json_decode( $post['post_content'] ); |
561
|
|
|
$post['post_content'] = FrmAppHelper::prepare_and_encode( $post['post_content'] ); |
562
|
|
|
|
563
|
|
|
// Create/update post now |
564
|
|
|
$post_id = wp_insert_post( $post ); |
565
|
|
|
} else { |
566
|
|
|
// Create/update post now |
567
|
|
|
$post_id = wp_insert_post( $post ); |
568
|
|
|
} |
569
|
|
|
|
570
|
|
|
if ( ! is_numeric($post_id) ) { |
571
|
|
|
continue; |
572
|
|
|
} |
573
|
|
|
|
574
|
|
|
self::update_postmeta($post, $post_id); |
575
|
|
|
|
576
|
|
|
$this_type = 'posts'; |
577
|
|
|
if ( isset( $post_types[ $post['post_type'] ] ) ) { |
578
|
|
|
$this_type = $post_types[ $post['post_type'] ]; |
579
|
|
|
} |
580
|
|
|
|
581
|
|
|
if ( isset($post['ID']) && $post_id == $post['ID'] ) { |
582
|
|
|
$imported['updated'][ $this_type ]++; |
583
|
|
|
} else { |
584
|
|
|
$imported['imported'][ $this_type ]++; |
585
|
|
|
} |
586
|
|
|
|
587
|
|
|
unset($post); |
588
|
|
|
|
589
|
|
|
$imported['posts'][ (int) $old_id ] = $post_id; |
590
|
|
|
} |
591
|
|
|
|
592
|
|
|
self::maybe_update_stylesheet( $imported ); |
593
|
|
|
|
594
|
|
|
return $imported; |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
private static function populate_post( &$post, $item, $imported ) { |
598
|
|
|
if ( isset($item->attachment_url) ) { |
599
|
|
|
$post['attachment_url'] = (string) $item->attachment_url; |
600
|
|
|
} |
601
|
|
|
|
602
|
|
|
if ( $post['post_type'] == FrmFormActionsController::$action_post_type && isset( $imported['forms'][ (int) $post['menu_order'] ] ) ) { |
603
|
|
|
// update to new form id |
604
|
|
|
$post['menu_order'] = $imported['forms'][ (int) $post['menu_order'] ]; |
605
|
|
|
} |
606
|
|
|
|
607
|
|
|
// Don't allow default styles to take over a site's default style |
608
|
|
|
if ( 'frm_styles' == $post['post_type'] ) { |
609
|
|
|
$post['menu_order'] = 0; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
foreach ( $item->postmeta as $meta ) { |
613
|
|
|
self::populate_postmeta($post, $meta, $imported); |
614
|
|
|
unset($meta); |
615
|
|
|
} |
616
|
|
|
|
617
|
|
|
self::populate_taxonomies($post, $item); |
618
|
|
|
|
619
|
|
|
self::maybe_editing_post($post); |
620
|
|
|
} |
621
|
|
|
|
622
|
|
|
private static function populate_postmeta( &$post, $meta, $imported ) { |
623
|
|
|
global $frm_duplicate_ids; |
624
|
|
|
|
625
|
|
|
$m = array( |
626
|
|
|
'key' => (string) $meta->meta_key, |
627
|
|
|
'value' => (string) $meta->meta_value, |
628
|
|
|
); |
629
|
|
|
|
630
|
|
|
//switch old form and field ids to new ones |
631
|
|
|
if ( $m['key'] == 'frm_form_id' && isset($imported['forms'][ (int) $m['value'] ]) ) { |
632
|
|
|
$m['value'] = $imported['forms'][ (int) $m['value'] ]; |
633
|
|
|
} else { |
634
|
|
|
$m['value'] = FrmAppHelper::maybe_json_decode($m['value']); |
635
|
|
|
|
636
|
|
|
if ( ! empty($frm_duplicate_ids) ) { |
637
|
|
|
|
638
|
|
|
if ( $m['key'] == 'frm_dyncontent' ) { |
639
|
|
|
$m['value'] = FrmFieldsHelper::switch_field_ids($m['value']); |
640
|
|
|
} else if ( $m['key'] == 'frm_options' ) { |
641
|
|
|
|
642
|
|
|
foreach ( array( 'date_field_id', 'edate_field_id' ) as $setting_name ) { |
643
|
|
|
if ( isset( $m['value'][ $setting_name ] ) && is_numeric( $m['value'][ $setting_name ] ) && isset( $frm_duplicate_ids[ $m['value'][ $setting_name ] ] ) ) { |
644
|
|
|
$m['value'][ $setting_name ] = $frm_duplicate_ids[ $m['value'][ $setting_name ] ]; |
645
|
|
|
} |
646
|
|
|
} |
647
|
|
|
|
648
|
|
|
$check_dup_array = array(); |
649
|
|
|
if ( isset( $m['value']['order_by'] ) && ! empty( $m['value']['order_by'] ) ) { |
650
|
|
|
if ( is_numeric( $m['value']['order_by'] ) && isset( $frm_duplicate_ids[ $m['value']['order_by'] ] ) ) { |
651
|
|
|
$m['value']['order_by'] = $frm_duplicate_ids[ $m['value']['order_by'] ]; |
652
|
|
|
} else if ( is_array( $m['value']['order_by'] ) ) { |
653
|
|
|
$check_dup_array[] = 'order_by'; |
654
|
|
|
} |
655
|
|
|
} |
656
|
|
|
|
657
|
|
|
if ( isset( $m['value']['where'] ) && ! empty( $m['value']['where'] ) ) { |
658
|
|
|
$check_dup_array[] = 'where'; |
659
|
|
|
} |
660
|
|
|
|
661
|
|
|
foreach ( $check_dup_array as $check_k ) { |
662
|
|
|
foreach ( (array) $m['value'][ $check_k ] as $mk => $mv ) { |
663
|
|
|
if ( isset( $frm_duplicate_ids[ $mv ] ) ) { |
664
|
|
|
$m['value'][ $check_k ][ $mk ] = $frm_duplicate_ids[ $mv ]; |
665
|
|
|
} |
666
|
|
|
unset($mk, $mv); |
667
|
|
|
} |
668
|
|
|
} |
669
|
|
|
} |
670
|
|
|
} |
671
|
|
|
} |
672
|
|
|
|
673
|
|
|
if ( ! is_array($m['value']) ) { |
674
|
|
|
$m['value'] = FrmAppHelper::maybe_json_decode($m['value']); |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
$post['postmeta'][ (string) $meta->meta_key ] = $m['value']; |
|
|
|
|
678
|
|
|
} |
679
|
|
|
|
680
|
|
|
/** |
681
|
|
|
* Add terms to post |
682
|
|
|
* @param array $post by reference |
683
|
|
|
* @param object $item The XML object data |
684
|
|
|
*/ |
685
|
|
|
private static function populate_taxonomies( &$post, $item ) { |
686
|
|
|
foreach ( $item->category as $c ) { |
687
|
|
|
$att = $c->attributes(); |
688
|
|
|
if ( ! isset( $att['nicename'] ) ) { |
689
|
|
|
continue; |
690
|
|
|
} |
691
|
|
|
|
692
|
|
|
$taxonomy = (string) $att['domain']; |
693
|
|
|
if ( is_taxonomy_hierarchical($taxonomy) ) { |
694
|
|
|
$name = (string) $att['nicename']; |
695
|
|
|
$h_term = get_term_by('slug', $name, $taxonomy); |
696
|
|
|
if ( $h_term ) { |
697
|
|
|
$name = $h_term->term_id; |
698
|
|
|
} |
699
|
|
|
unset($h_term); |
700
|
|
|
} else { |
701
|
|
|
$name = (string) $c; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
if ( ! isset( $post['tax_input'][ $taxonomy ] ) ) { |
705
|
|
|
$post['tax_input'][ $taxonomy ] = array(); |
706
|
|
|
} |
707
|
|
|
|
708
|
|
|
$post['tax_input'][ $taxonomy ][] = $name; |
709
|
|
|
unset($name); |
710
|
|
|
} |
711
|
|
|
} |
712
|
|
|
|
713
|
|
|
/** |
714
|
|
|
* Edit post if the key and created time match |
715
|
|
|
*/ |
716
|
|
|
private static function maybe_editing_post( &$post ) { |
717
|
|
|
$match_by = array( |
718
|
|
|
'post_type' => $post['post_type'], |
719
|
|
|
'name' => $post['post_name'], |
720
|
|
|
'post_status' => $post['post_status'], |
721
|
|
|
'posts_per_page' => 1, |
722
|
|
|
); |
723
|
|
|
|
724
|
|
|
if ( in_array( $post['post_status'], array( 'trash', 'draft' ) ) ) { |
725
|
|
|
$match_by['include'] = $post['post_id']; |
726
|
|
|
unset($match_by['name']); |
727
|
|
|
} |
728
|
|
|
|
729
|
|
|
$editing = get_posts($match_by); |
730
|
|
|
|
731
|
|
|
if ( ! empty($editing) && current($editing)->post_date == $post['post_date'] ) { |
732
|
|
|
// set the id of the post to edit |
733
|
|
|
$post['ID'] = current($editing)->ID; |
734
|
|
|
} |
735
|
|
|
} |
736
|
|
|
|
737
|
|
|
private static function update_postmeta( &$post, $post_id ) { |
738
|
|
|
foreach ( $post['postmeta'] as $k => $v ) { |
739
|
|
|
if ( '_edit_last' == $k ) { |
740
|
|
|
$v = FrmAppHelper::get_user_id_param($v); |
741
|
|
|
} else if ( '_thumbnail_id' == $k && FrmAppHelper::pro_is_installed() ) { |
742
|
|
|
//change the attachment ID |
743
|
|
|
$v = FrmProXMLHelper::get_file_id($v); |
744
|
|
|
} |
745
|
|
|
|
746
|
|
|
update_post_meta($post_id, $k, $v); |
747
|
|
|
|
748
|
|
|
unset($k, $v); |
749
|
|
|
} |
750
|
|
|
} |
751
|
|
|
|
752
|
|
|
private static function maybe_update_stylesheet( $imported ) { |
753
|
|
|
$new_styles = isset( $imported['imported']['styles'] ) && ! empty( $imported['imported']['styles'] ); |
754
|
|
|
$updated_styles = isset( $imported['updated']['styles'] ) && ! empty( $imported['updated']['styles'] ); |
755
|
|
|
if ( $new_styles || $updated_styles ) { |
756
|
|
|
if ( is_admin() && function_exists( 'get_filesystem_method' ) ) { |
757
|
|
|
$frm_style = new FrmStyle(); |
758
|
|
|
$frm_style->update( 'default' ); |
759
|
|
|
} |
760
|
|
|
foreach ( $imported['forms'] as $form_id ) { |
761
|
|
|
self::update_custom_style_setting_after_import( $form_id ); |
762
|
|
|
} |
763
|
|
|
} |
764
|
|
|
} |
765
|
|
|
|
766
|
|
|
/** |
767
|
|
|
* @param string $message |
768
|
|
|
*/ |
769
|
|
|
public static function parse_message( $result, &$message, &$errors ) { |
770
|
|
|
if ( is_wp_error($result) ) { |
771
|
|
|
$errors[] = $result->get_error_message(); |
772
|
|
|
} else if ( ! $result ) { |
773
|
|
|
return; |
774
|
|
|
} |
775
|
|
|
|
776
|
|
|
if ( ! is_array($result) ) { |
777
|
|
|
$message = is_string( $result ) ? $result : print_r( $result, 1 ); |
|
|
|
|
778
|
|
|
return; |
779
|
|
|
} |
780
|
|
|
|
781
|
|
|
$t_strings = array( |
782
|
|
|
'imported' => __( 'Imported', 'formidable' ), |
783
|
|
|
'updated' => __( 'Updated', 'formidable' ), |
784
|
|
|
); |
785
|
|
|
|
786
|
|
|
$message = '<ul>'; |
787
|
|
|
foreach ( $result as $type => $results ) { |
788
|
|
|
if ( ! isset( $t_strings[ $type ] ) ) { |
789
|
|
|
// only print imported and updated |
790
|
|
|
continue; |
791
|
|
|
} |
792
|
|
|
|
793
|
|
|
$s_message = array(); |
794
|
|
|
foreach ( $results as $k => $m ) { |
795
|
|
|
self::item_count_message($m, $k, $s_message); |
796
|
|
|
unset($k, $m); |
797
|
|
|
} |
798
|
|
|
|
799
|
|
|
if ( ! empty($s_message) ) { |
800
|
|
|
$message .= '<li><strong>' . $t_strings[ $type ] . ':</strong> '; |
801
|
|
|
$message .= implode(', ', $s_message); |
802
|
|
|
$message .= '</li>'; |
803
|
|
|
} |
804
|
|
|
} |
805
|
|
|
|
806
|
|
|
if ( $message == '<ul>' ) { |
807
|
|
|
$message = ''; |
808
|
|
|
$errors[] = __( 'Nothing was imported or updated', 'formidable' ); |
809
|
|
|
} else { |
810
|
|
|
$message .= '</ul>'; |
811
|
|
|
} |
812
|
|
|
} |
813
|
|
|
|
814
|
|
|
public static function item_count_message( $m, $type, &$s_message ) { |
815
|
|
|
if ( ! $m ) { |
816
|
|
|
return; |
817
|
|
|
} |
818
|
|
|
|
819
|
|
|
$strings = array( |
820
|
|
|
'forms' => sprintf( _n( '%1$s Form', '%1$s Forms', $m, 'formidable' ), $m ), |
821
|
|
|
'fields' => sprintf( _n( '%1$s Field', '%1$s Fields', $m, 'formidable' ), $m ), |
822
|
|
|
'items' => sprintf( _n( '%1$s Entry', '%1$s Entries', $m, 'formidable' ), $m ), |
823
|
|
|
'views' => sprintf( _n( '%1$s View', '%1$s Views', $m, 'formidable' ), $m ), |
824
|
|
|
'posts' => sprintf( _n( '%1$s Post', '%1$s Posts', $m, 'formidable' ), $m ), |
825
|
|
|
'styles' => sprintf( _n( '%1$s Style', '%1$s Styles', $m, 'formidable' ), $m ), |
826
|
|
|
'terms' => sprintf( _n( '%1$s Term', '%1$s Terms', $m, 'formidable' ), $m ), |
827
|
|
|
'actions' => sprintf( _n( '%1$s Form Action', '%1$s Form Actions', $m, 'formidable' ), $m ), |
828
|
|
|
); |
829
|
|
|
|
830
|
|
|
$s_message[] = isset( $strings[ $type ] ) ? $strings[ $type ] : ' ' . $m . ' ' . ucfirst( $type ); |
831
|
|
|
} |
832
|
|
|
|
833
|
|
|
/** |
834
|
|
|
* Prepare the form options for export |
835
|
|
|
* |
836
|
|
|
* @since 2.0.19 |
837
|
|
|
* @param string $options |
838
|
|
|
* @return string |
839
|
|
|
*/ |
840
|
|
|
public static function prepare_form_options_for_export( $options ) { |
841
|
|
|
$options = maybe_unserialize( $options ); |
842
|
|
|
// Change custom_style to the post_name instead of ID |
843
|
|
|
if ( isset( $options['custom_style'] ) && 1 !== $options['custom_style'] ) { |
844
|
|
|
global $wpdb; |
845
|
|
|
$table = $wpdb->prefix . 'posts'; |
846
|
|
|
$where = array( 'ID' => $options['custom_style'] ); |
847
|
|
|
$select = 'post_name'; |
848
|
|
|
|
849
|
|
|
$style_name = FrmDb::get_var( $table, $where, $select ); |
850
|
|
|
|
851
|
|
|
if ( $style_name ) { |
852
|
|
|
$options['custom_style'] = $style_name; |
853
|
|
|
} else { |
854
|
|
|
$options['custom_style'] = 1; |
855
|
|
|
} |
856
|
|
|
} |
857
|
|
|
$options = serialize( $options ); |
858
|
|
|
return self::cdata( $options ); |
859
|
|
|
} |
860
|
|
|
|
861
|
|
|
public static function cdata( $str ) { |
862
|
|
|
$str = maybe_unserialize($str); |
863
|
|
|
if ( is_array($str) ) { |
864
|
|
|
$str = json_encode($str); |
865
|
|
|
} else if ( seems_utf8( $str ) == false ) { |
866
|
|
|
$str = utf8_encode( $str ); |
867
|
|
|
} |
868
|
|
|
|
869
|
|
|
if ( is_numeric($str) ) { |
870
|
|
|
return $str; |
871
|
|
|
} |
872
|
|
|
|
873
|
|
|
self::remove_invalid_characters_from_xml( $str ); |
874
|
|
|
|
875
|
|
|
// $str = ent2ncr(esc_html($str)); |
876
|
|
|
$str = '<![CDATA[' . str_replace( ']]>', ']]]]><![CDATA[>', $str ) . ']]>'; |
877
|
|
|
|
878
|
|
|
return $str; |
879
|
|
|
} |
880
|
|
|
|
881
|
|
|
/** |
882
|
|
|
* Remove <US> character (unit separator) from exported strings |
883
|
|
|
* |
884
|
|
|
* @since 2.0.22 |
885
|
|
|
* @param string $str |
886
|
|
|
*/ |
887
|
|
|
private static function remove_invalid_characters_from_xml( &$str ) { |
888
|
|
|
// Remove <US> character |
889
|
|
|
$str = str_replace( '\x1F', '', $str ); |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
public static function migrate_form_settings_to_actions( $form_options, $form_id, &$imported = array(), $switch = false ) { |
893
|
|
|
// Get post type |
894
|
|
|
$post_type = FrmFormActionsController::$action_post_type; |
895
|
|
|
|
896
|
|
|
// Set up imported index, if not set up yet |
897
|
|
|
if ( ! isset( $imported['imported']['actions'] ) ) { |
898
|
|
|
$imported['imported']['actions'] = 0; |
899
|
|
|
} |
900
|
|
|
|
901
|
|
|
// Migrate post settings to action |
902
|
|
|
self::migrate_post_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch ); |
903
|
|
|
|
904
|
|
|
// Migrate email settings to action |
905
|
|
|
self::migrate_email_settings_to_action( $form_options, $form_id, $post_type, $imported, $switch ); |
906
|
|
|
} |
907
|
|
|
|
908
|
|
|
/** |
909
|
|
|
* Migrate post settings to form action |
910
|
|
|
* |
911
|
|
|
* @param string $post_type |
912
|
|
|
*/ |
913
|
|
|
private static function migrate_post_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
914
|
|
|
if ( ! isset($form_options['create_post']) || ! $form_options['create_post'] ) { |
915
|
|
|
return; |
916
|
|
|
} |
917
|
|
|
|
918
|
|
|
$new_action = array( |
919
|
|
|
'post_type' => $post_type, |
920
|
|
|
'post_excerpt' => 'wppost', |
921
|
|
|
'post_title' => __( 'Create Posts', 'formidable' ), |
922
|
|
|
'menu_order' => $form_id, |
923
|
|
|
'post_status' => 'publish', |
924
|
|
|
'post_content' => array(), |
925
|
|
|
'post_name' => $form_id . '_wppost_1', |
926
|
|
|
); |
927
|
|
|
|
928
|
|
|
$post_settings = array( |
929
|
|
|
'post_type', 'post_category', 'post_content', |
930
|
|
|
'post_excerpt', 'post_title', 'post_name', 'post_date', |
931
|
|
|
'post_status', 'post_custom_fields', 'post_password', |
932
|
|
|
); |
933
|
|
|
|
934
|
|
|
foreach ( $post_settings as $post_setting ) { |
935
|
|
|
if ( isset( $form_options[ $post_setting ] ) ) { |
936
|
|
|
$new_action['post_content'][ $post_setting ] = $form_options[ $post_setting ]; |
937
|
|
|
} |
938
|
|
|
unset($post_setting); |
939
|
|
|
} |
940
|
|
|
|
941
|
|
|
$new_action['event'] = array( 'create', 'update' ); |
942
|
|
|
|
943
|
|
|
if ( $switch ) { |
944
|
|
|
// Fields with string or int saved |
945
|
|
|
$basic_fields = array( 'post_title', 'post_content', 'post_excerpt', 'post_password', 'post_date', 'post_status' ); |
946
|
|
|
|
947
|
|
|
// Fields with arrays saved |
948
|
|
|
$array_fields = array( 'post_category', 'post_custom_fields' ); |
949
|
|
|
|
950
|
|
|
$new_action['post_content'] = self::switch_action_field_ids( $new_action['post_content'], $basic_fields, $array_fields ); |
951
|
|
|
} |
952
|
|
|
$new_action['post_content'] = json_encode($new_action['post_content']); |
953
|
|
|
|
954
|
|
|
$exists = get_posts( array( |
955
|
|
|
'name' => $new_action['post_name'], |
956
|
|
|
'post_type' => $new_action['post_type'], |
957
|
|
|
'post_status' => $new_action['post_status'], |
958
|
|
|
'numberposts' => 1, |
959
|
|
|
) ); |
960
|
|
|
|
961
|
|
|
if ( ! $exists ) { |
962
|
|
|
// this isn't an email, but we need to use a class that will always be included |
963
|
|
|
FrmAppHelper::save_json_post( $new_action ); |
964
|
|
|
$imported['imported']['actions']++; |
965
|
|
|
} |
966
|
|
|
} |
967
|
|
|
|
968
|
|
|
/** |
969
|
|
|
* Switch old field IDs for new field IDs in emails and post |
970
|
|
|
* |
971
|
|
|
* @since 2.0 |
972
|
|
|
* @param array $post_content - check for old field IDs |
973
|
|
|
* @param array $basic_fields - fields with string or int saved |
974
|
|
|
* @param array $array_fields - fields with arrays saved |
975
|
|
|
* |
976
|
|
|
* @return string $post_content - new field IDs |
977
|
|
|
*/ |
978
|
|
|
private static function switch_action_field_ids( $post_content, $basic_fields, $array_fields = array() ) { |
979
|
|
|
global $frm_duplicate_ids; |
980
|
|
|
|
981
|
|
|
// If there aren't IDs that were switched, end now |
982
|
|
|
if ( ! $frm_duplicate_ids ) { |
983
|
|
|
return; |
984
|
|
|
} |
985
|
|
|
|
986
|
|
|
// Get old IDs |
987
|
|
|
$old = array_keys( $frm_duplicate_ids ); |
988
|
|
|
|
989
|
|
|
// Get new IDs |
990
|
|
|
$new = array_values( $frm_duplicate_ids ); |
991
|
|
|
|
992
|
|
|
// Do a str_replace with each item to set the new IDs |
993
|
|
|
foreach ( $post_content as $key => $setting ) { |
994
|
|
|
if ( ! is_array( $setting ) && in_array( $key, $basic_fields ) ) { |
995
|
|
|
// Replace old IDs with new IDs |
996
|
|
|
$post_content[ $key ] = str_replace( $old, $new, $setting ); |
997
|
|
|
} else if ( is_array( $setting ) && in_array( $key, $array_fields ) ) { |
998
|
|
|
foreach ( $setting as $k => $val ) { |
999
|
|
|
// Replace old IDs with new IDs |
1000
|
|
|
$post_content[ $key ][ $k ] = str_replace( $old, $new, $val ); |
1001
|
|
|
} |
1002
|
|
|
} |
1003
|
|
|
unset( $key, $setting ); |
1004
|
|
|
} |
1005
|
|
|
return $post_content; |
1006
|
|
|
} |
1007
|
|
|
|
1008
|
|
|
private static function migrate_email_settings_to_action( $form_options, $form_id, $post_type, &$imported, $switch ) { |
1009
|
|
|
// No old notifications or autoresponders to carry over |
1010
|
|
|
if ( ! isset( $form_options['auto_responder'] ) && ! isset( $form_options['notification'] ) && ! isset( $form_options['email_to'] ) ) { |
1011
|
|
|
return; |
1012
|
|
|
} |
1013
|
|
|
|
1014
|
|
|
// Initialize notifications array |
1015
|
|
|
$notifications = array(); |
1016
|
|
|
|
1017
|
|
|
// Migrate regular notifications |
1018
|
|
|
self::migrate_notifications_to_action( $form_options, $form_id, $notifications ); |
1019
|
|
|
|
1020
|
|
|
// Migrate autoresponders |
1021
|
|
|
self::migrate_autoresponder_to_action( $form_options, $form_id, $notifications ); |
1022
|
|
|
|
1023
|
|
|
if ( empty( $notifications ) ) { |
1024
|
|
|
return; |
1025
|
|
|
} |
1026
|
|
|
|
1027
|
|
|
foreach ( $notifications as $new_notification ) { |
1028
|
|
|
$new_notification['post_type'] = $post_type; |
1029
|
|
|
$new_notification['post_excerpt'] = 'email'; |
1030
|
|
|
$new_notification['post_title'] = __( 'Email Notification', 'formidable' ); |
1031
|
|
|
$new_notification['menu_order'] = $form_id; |
1032
|
|
|
$new_notification['post_status'] = 'publish'; |
1033
|
|
|
|
1034
|
|
|
// Switch field IDs and keys, if needed |
1035
|
|
|
if ( $switch ) { |
1036
|
|
|
|
1037
|
|
|
// Switch field IDs in email conditional logic |
1038
|
|
|
self::switch_email_contition_field_ids( $new_notification['post_content'] ); |
1039
|
|
|
|
1040
|
|
|
// Switch all other field IDs in email |
1041
|
|
|
$new_notification['post_content'] = FrmFieldsHelper::switch_field_ids( $new_notification['post_content'] ); |
1042
|
|
|
} |
1043
|
|
|
$new_notification['post_content'] = FrmAppHelper::prepare_and_encode( $new_notification['post_content'] ); |
1044
|
|
|
|
1045
|
|
|
$exists = get_posts( array( |
1046
|
|
|
'name' => $new_notification['post_name'], |
1047
|
|
|
'post_type' => $new_notification['post_type'], |
1048
|
|
|
'post_status' => $new_notification['post_status'], |
1049
|
|
|
'numberposts' => 1, |
1050
|
|
|
) ); |
1051
|
|
|
|
1052
|
|
|
if ( empty($exists) ) { |
1053
|
|
|
FrmAppHelper::save_json_post( $new_notification ); |
1054
|
|
|
$imported['imported']['actions']++; |
1055
|
|
|
} |
1056
|
|
|
unset($new_notification); |
1057
|
|
|
} |
1058
|
|
|
} |
1059
|
|
|
|
1060
|
|
|
private static function migrate_notifications_to_action( $form_options, $form_id, &$notifications ) { |
1061
|
|
|
if ( ! isset( $form_options['notification'] ) && isset( $form_options['email_to'] ) && ! empty( $form_options['email_to'] ) ) { |
1062
|
|
|
// add old settings into notification array |
1063
|
|
|
$form_options['notification'] = array( 0 => $form_options ); |
1064
|
|
|
} else if ( isset( $form_options['notification']['email_to'] ) ) { |
1065
|
|
|
// make sure it's in the correct format |
1066
|
|
|
$form_options['notification'] = array( 0 => $form_options['notification'] ); |
1067
|
|
|
} |
1068
|
|
|
|
1069
|
|
|
if ( isset( $form_options['notification'] ) && is_array($form_options['notification']) ) { |
1070
|
|
|
foreach ( $form_options['notification'] as $email_key => $notification ) { |
1071
|
|
|
|
1072
|
|
|
$atts = array( 'email_to' => '', 'reply_to' => '', 'reply_to_name' => '', 'event' => '', 'form_id' => $form_id, 'email_key' => $email_key ); |
1073
|
|
|
|
1074
|
|
|
// Format the email data |
1075
|
|
|
self::format_email_data( $atts, $notification ); |
1076
|
|
|
|
1077
|
|
|
if ( isset( $notification['twilio'] ) && $notification['twilio'] ) { |
1078
|
|
|
do_action( 'frm_create_twilio_action', $atts, $notification ); |
1079
|
|
|
} |
1080
|
|
|
|
1081
|
|
|
// Setup the new notification |
1082
|
|
|
$new_notification = array(); |
1083
|
|
|
self::setup_new_notification( $new_notification, $notification, $atts ); |
1084
|
|
|
|
1085
|
|
|
$notifications[] = $new_notification; |
1086
|
|
|
} |
1087
|
|
|
} |
1088
|
|
|
} |
1089
|
|
|
|
1090
|
|
|
private static function format_email_data( &$atts, $notification ) { |
1091
|
|
|
// Format email_to |
1092
|
|
|
self::format_email_to_data( $atts, $notification ); |
1093
|
|
|
|
1094
|
|
|
// Format the reply to email and name |
1095
|
|
|
$reply_fields = array( 'reply_to' => '', 'reply_to_name' => '' ); |
1096
|
|
|
foreach ( $reply_fields as $f => $val ) { |
1097
|
|
|
if ( isset( $notification[ $f ] ) ) { |
1098
|
|
|
$atts[ $f ] = $notification[ $f ]; |
1099
|
|
|
if ( 'custom' == $notification[ $f ] ) { |
1100
|
|
|
$atts[ $f ] = $notification[ 'cust_' . $f ]; |
1101
|
|
|
} else if ( is_numeric( $atts[ $f ] ) && ! empty( $atts[ $f ] ) ) { |
1102
|
|
|
$atts[ $f ] = '[' . $atts[ $f ] . ']'; |
1103
|
|
|
} |
1104
|
|
|
} |
1105
|
|
|
unset( $f, $val ); |
1106
|
|
|
} |
1107
|
|
|
|
1108
|
|
|
// Format event |
1109
|
|
|
$atts['event'] = array( 'create' ); |
1110
|
|
|
if ( isset( $notification['update_email'] ) && 1 == $notification['update_email'] ) { |
1111
|
|
|
$atts['event'][] = 'update'; |
1112
|
|
|
} else if ( isset($notification['update_email']) && 2 == $notification['update_email'] ) { |
1113
|
|
|
$atts['event'] = array( 'update' ); |
1114
|
|
|
} |
1115
|
|
|
} |
1116
|
|
|
|
1117
|
|
|
private static function format_email_to_data( &$atts, $notification ) { |
1118
|
|
|
if ( isset( $notification['email_to'] ) ) { |
1119
|
|
|
$atts['email_to'] = preg_split( '/ (,|;) /', $notification['email_to'] ); |
1120
|
|
|
} else { |
1121
|
|
|
$atts['email_to'] = array(); |
1122
|
|
|
} |
1123
|
|
|
|
1124
|
|
|
if ( isset( $notification['also_email_to'] ) ) { |
1125
|
|
|
$email_fields = (array) $notification['also_email_to']; |
1126
|
|
|
$atts['email_to'] = array_merge( $email_fields, $atts['email_to'] ); |
1127
|
|
|
unset( $email_fields ); |
1128
|
|
|
} |
1129
|
|
|
|
1130
|
|
|
foreach ( $atts['email_to'] as $key => $email_field ) { |
1131
|
|
|
|
1132
|
|
|
if ( is_numeric( $email_field ) ) { |
1133
|
|
|
$atts['email_to'][ $key ] = '[' . $email_field . ']'; |
1134
|
|
|
} |
1135
|
|
|
|
1136
|
|
|
if ( strpos( $email_field, '|') ) { |
1137
|
|
|
$email_opt = explode( '|', $email_field ); |
1138
|
|
|
if ( isset( $email_opt[0] ) ) { |
1139
|
|
|
$atts['email_to'][ $key ] = '[' . $email_opt[0] . ' show=' . $email_opt[1] . ']'; |
1140
|
|
|
} |
1141
|
|
|
unset( $email_opt ); |
1142
|
|
|
} |
1143
|
|
|
} |
1144
|
|
|
$atts['email_to'] = implode(', ', $atts['email_to']); |
1145
|
|
|
} |
1146
|
|
|
|
1147
|
|
|
private static function setup_new_notification( &$new_notification, $notification, $atts ) { |
1148
|
|
|
// Set up new notification |
1149
|
|
|
$new_notification = array( |
1150
|
|
|
'post_content' => array( |
1151
|
|
|
'email_to' => $atts['email_to'], |
1152
|
|
|
'event' => $atts['event'], |
1153
|
|
|
), |
1154
|
|
|
'post_name' => $atts['form_id'] . '_email_' . $atts['email_key'], |
1155
|
|
|
); |
1156
|
|
|
|
1157
|
|
|
// Add more fields to the new notification |
1158
|
|
|
$add_fields = array( 'email_message', 'email_subject', 'plain_text', 'inc_user_info', 'conditions' ); |
1159
|
|
|
foreach ( $add_fields as $add_field ) { |
1160
|
|
|
if ( isset( $notification[ $add_field ] ) ) { |
1161
|
|
|
$new_notification['post_content'][ $add_field ] = $notification[ $add_field ]; |
1162
|
|
|
} else if ( in_array( $add_field, array( 'plain_text', 'inc_user_info' ) ) ) { |
1163
|
|
|
$new_notification['post_content'][ $add_field ] = 0; |
1164
|
|
|
} else { |
1165
|
|
|
$new_notification['post_content'][ $add_field ] = ''; |
1166
|
|
|
} |
1167
|
|
|
unset( $add_field ); |
1168
|
|
|
} |
1169
|
|
|
|
1170
|
|
|
// Set reply to |
1171
|
|
|
$new_notification['post_content']['reply_to'] = $atts['reply_to']; |
1172
|
|
|
|
1173
|
|
|
// Set from |
1174
|
|
|
if ( ! empty( $atts['reply_to'] ) || ! empty( $atts['reply_to_name'] ) ) { |
1175
|
|
|
$new_notification['post_content']['from'] = ( empty( $atts['reply_to_name'] ) ? '[sitename]' : $atts['reply_to_name'] ) . ' <' . ( empty( $atts['reply_to'] ) ? '[admin_email]' : $atts['reply_to'] ) . '>'; |
1176
|
|
|
} |
1177
|
|
|
} |
1178
|
|
|
|
1179
|
|
|
/** |
1180
|
|
|
* Switch field IDs in pre-2.0 email conditional logic |
1181
|
|
|
* |
1182
|
|
|
* @param $post_content array, pass by reference |
1183
|
|
|
*/ |
1184
|
|
|
private static function switch_email_contition_field_ids( &$post_content ) { |
1185
|
|
|
// Switch field IDs in conditional logic |
1186
|
|
|
if ( isset( $post_content['conditions'] ) && is_array( $post_content['conditions'] ) ) { |
1187
|
|
|
foreach ( $post_content['conditions'] as $email_key => $val ) { |
1188
|
|
|
if ( is_numeric( $email_key ) ) { |
1189
|
|
|
$post_content['conditions'][ $email_key ] = self::switch_action_field_ids( $val, array( 'hide_field' ) ); |
1190
|
|
|
} |
1191
|
|
|
unset( $email_key, $val); |
1192
|
|
|
} |
1193
|
|
|
} |
1194
|
|
|
} |
1195
|
|
|
|
1196
|
|
|
private static function migrate_autoresponder_to_action( $form_options, $form_id, &$notifications ) { |
1197
|
|
|
if ( isset($form_options['auto_responder']) && $form_options['auto_responder'] && isset($form_options['ar_email_message']) && $form_options['ar_email_message'] ) { |
1198
|
|
|
// migrate autoresponder |
1199
|
|
|
|
1200
|
|
|
$email_field = isset($form_options['ar_email_to']) ? $form_options['ar_email_to'] : 0; |
1201
|
|
|
if ( strpos($email_field, '|') ) { |
1202
|
|
|
// data from entries field |
1203
|
|
|
$email_field = explode('|', $email_field); |
1204
|
|
|
if ( isset($email_field[1]) ) { |
1205
|
|
|
$email_field = $email_field[1]; |
1206
|
|
|
} |
1207
|
|
|
} |
1208
|
|
|
if ( is_numeric($email_field) && ! empty($email_field) ) { |
1209
|
|
|
$email_field = '[' . $email_field . ']'; |
1210
|
|
|
} |
1211
|
|
|
|
1212
|
|
|
$notification = $form_options; |
1213
|
|
|
$new_notification2 = array( |
1214
|
|
|
'post_content' => array( |
1215
|
|
|
'email_message' => $notification['ar_email_message'], |
1216
|
|
|
'email_subject' => isset($notification['ar_email_subject']) ? $notification['ar_email_subject'] : '', |
1217
|
|
|
'email_to' => $email_field, |
1218
|
|
|
'plain_text' => isset($notification['ar_plain_text']) ? $notification['ar_plain_text'] : 0, |
1219
|
|
|
'inc_user_info' => 0, |
1220
|
|
|
), |
1221
|
|
|
'post_name' => $form_id . '_email_' . count( $notifications ), |
1222
|
|
|
); |
1223
|
|
|
|
1224
|
|
|
$reply_to = isset($notification['ar_reply_to']) ? $notification['ar_reply_to'] : ''; |
1225
|
|
|
$reply_to_name = isset($notification['ar_reply_to_name']) ? $notification['ar_reply_to_name'] : ''; |
1226
|
|
|
|
1227
|
|
|
if ( ! empty( $reply_to ) ) { |
1228
|
|
|
$new_notification2['post_content']['reply_to'] = $reply_to; |
1229
|
|
|
} |
1230
|
|
|
|
1231
|
|
|
if ( ! empty( $reply_to ) || ! empty( $reply_to_name ) ) { |
1232
|
|
|
$new_notification2['post_content']['from'] = ( empty( $reply_to_name ) ? '[sitename]' : $reply_to_name ) . ' <' . ( empty( $reply_to ) ? '[admin_email]' : $reply_to ) . '>'; |
1233
|
|
|
} |
1234
|
|
|
|
1235
|
|
|
$notifications[] = $new_notification2; |
1236
|
|
|
unset( $new_notification2 ); |
1237
|
|
|
} |
1238
|
|
|
} |
1239
|
|
|
} |
1240
|
|
|
|
1241
|
|
|
|