ImportExport.php
18.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
<?php if ( ! defined( 'ABSPATH' ) ) exit;
class NF_Admin_Menus_ImportExport extends NF_Abstracts_Submenu
{
public $parent_slug = 'ninja-forms';
public $menu_slug = 'nf-import-export';
/**
* @var int
*/
public $position = 4;
final public function __construct()
{
add_action( 'init', array( $this, 'import_form_listener' ), 0 );
add_action( 'init', array( $this, 'export_form_listener' ), 0 );
add_action( 'init', array( $this, 'import_fields_listener' ), 0 );
add_action( 'init', array( $this, 'export_fields_listener' ), 0 );
add_filter( 'ninja_forms_before_import_fields', array( $this, 'import_fields_backwards_compatibility' ) );
parent::__construct();
add_action( 'admin_init', array( $this, 'nf_upgrade_redirect' ) );
}
/**
* If we have required updates, redirect to the main Ninja Forms page
*/
final public function nf_upgrade_redirect() {
global $pagenow;
if( "1" == get_option( 'ninja_forms_needs_updates' ) ) {
remove_submenu_page( $this->parent_slug, $this->menu_slug );
if( 'admin.php' == $pagenow && 'nf-import-export' == $_GET[ 'page' ] ) {
wp_safe_redirect( admin_url( 'admin.php?page=ninja-forms' ), 301 );
exit;
}
}
}
final public function get_page_title()
{
return esc_html__( 'Import / Export', 'ninja-forms' );
}
final public function import_form_listener()
{
$capability = apply_filters( 'ninja_forms_admin_import_export_capabilities', 'manage_options' );
$capability = apply_filters( 'ninja_forms_admin_import_form_capabilities', $capability );
if( ! current_user_can( $capability ) ) return;
if( ! isset( $_REQUEST['nf_import_security'] )
|| ! wp_verify_nonce( $_REQUEST[ 'nf_import_security' ], 'ninja_forms_import_form_nonce' ) ) return;
if( ! isset( $_FILES[ 'nf_import_form' ] ) || ! $_FILES[ 'nf_import_form' ] ) return;
$this->upload_error_check( $_FILES[ 'nf_import_form' ] );
$data = file_get_contents( $_FILES[ 'nf_import_form' ][ 'tmp_name' ] );
// Check to see if the user turned off UTF-8 encoding
$decode_utf8 = TRUE;
if( isset( $_REQUEST[ 'nf_import_form_turn_off_encoding' ] ) &&
$_REQUEST[ 'nf_import_form_turn_off_encoding' ] ) {
$decode_utf8 = FALSE;
}
$import = Ninja_Forms()->form()->import_form( $data, $decode_utf8 );
if( ! $import ){
$err_msg = '';
if ( function_exists( 'json_last_error_msg' ) ) {
$err_msg = json_last_error_msg();
}
wp_die(
esc_html__( 'There uploaded file is not a valid format.', 'ninja-forms' ) . ' ' . $err_msg,
esc_html__( 'Invalid Form Upload.', 'ninja-forms' )
);
}
}
final public function export_form_listener()
{
$capability = apply_filters( 'ninja_forms_admin_import_export_capabilities', 'manage_options' );
$capability = apply_filters( 'ninja_forms_admin_export_form_capabilities', $capability );
if( ! current_user_can( $capability ) ) return;
if( isset( $_REQUEST[ 'nf_export_form' ] ) && $_REQUEST[ 'nf_export_form' ] ){
$form_id = absint($_REQUEST[ 'nf_export_form' ]);
Ninja_Forms()->form( $form_id )->export_form();
}
}
final public function import_fields_listener()
{
if( ! current_user_can( apply_filters( 'ninja_forms_admin_import_fields_capabilities', 'manage_options' ) ) ) return;
if( ! isset( $_FILES[ 'nf_import_fields' ] ) || ! $_FILES[ 'nf_import_fields' ] ) return;
if( ! isset( $_POST['nf_import_security'] ) ||
! wp_verify_nonce( $_REQUEST[ 'nf_import_security' ], 'ninja_forms_batch_nonce' ) ) return;
$this->upload_error_check( $_FILES[ 'nf_import_fields' ] );
$import = file_get_contents( $_FILES[ 'nf_import_fields' ][ 'tmp_name' ] );
$fields = $this->sanitizeUnserialize($import);
foreach( $fields as $settings ){
Ninja_Forms()->form()->import_field( $settings );
}
}
/**
* Ensure unserialized value is sanitized against malicious objects
*
* @param string $serializedValue
* @return array Expected return is indexed array
*/
protected function sanitizeUnserialize( $serializedValue)
{
$return = unserialize($serializedValue,['allowed_classes'=>false]);
return $return;
}
final public function export_fields_listener()
{
if( ! current_user_can( apply_filters( 'ninja_forms_admin_export_fields_capabilities', 'manage_options' ) ) ) return;
if( isset( $_REQUEST[ 'nf_export_fields' ] ) && $_REQUEST[ 'nf_export_fields' ] ){
$field_ids = (array) $_REQUEST[ 'nf_export_fields' ];
$field_ids = array_map('esc_attr', $field_ids);
$fields = array();
foreach( $field_ids as $field_id ){
$field = Ninja_Forms()->form()->field( $field_id )->get();
$fields[] = $field->get_settings();
}
header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=favorites-" . time() . ".nff");
header("Pragma: no-cache");
header("Expires: 0");
echo serialize( $fields );
die();
}
}
final public function display()
{
$tabs = apply_filters( 'ninja_forms_import_export_tabs', array(
'forms' => esc_html__( 'Form', 'ninja-forms' ),
'favorite_fields' => esc_html__( 'Favorite Fields', 'ninja-forms' )
)
);
$tab_keys = array_keys( $tabs );
$active_tab = ( isset( $_GET[ 'tab' ] ) ) ? WPN_Helper::sanitize_text_field($_GET[ 'tab' ]) : reset( $tab_keys );
$this->add_meta_boxes();
wp_enqueue_script('postbox');
wp_enqueue_script('jquery-ui-draggable');
wp_enqueue_style( 'nf-admin-settings', Ninja_Forms::$url . 'assets/css/admin-settings.css' );
wp_nonce_field( 'closedpostboxes', 'closedpostboxesnonce', false );
wp_nonce_field( 'meta-box-order', 'meta-box-order-nonce', false );
wp_register_script( 'ninja_forms_admin_import_export',
Ninja_Forms::$url . 'assets/js/admin-import-export.js', array( 'jquery' ), FALSE, TRUE );
wp_enqueue_script( 'ninja_forms_admin_import_export' );
wp_localize_script( 'ninja_forms_admin_import_export', 'nfAdmin', array(
'ajax_url' => admin_url( 'admin-ajax.php' ),
'batchNonce' => wp_create_nonce( 'ninja_forms_batch_nonce' ),
'i18n' => array(
'trashExpiredSubsMessage' => esc_html__( 'Are you sure you want to trash all expired submissions?', 'ninja-forms' ),
'trashExpiredSubsButtonPrimary' => esc_html__( 'Trash', 'ninja-forms' ),
'trashExpiredSubsButtonSecondary' => esc_html__( 'Cancel', 'ninja-forms' ),
),
'builderURL' => admin_url( 'admin.php?page=ninja-forms&form_id=' ),
));
wp_enqueue_script( 'jBox', Ninja_Forms::$url . 'assets/js/lib/jBox.min.js', array( 'jquery' ) );
wp_enqueue_style( 'jBox', Ninja_Forms::$url . 'assets/css/jBox.css' );
wp_enqueue_script( 'nf-ninja-modal', Ninja_Forms::$url . 'assets/js/lib/ninjaModal.js', array( 'jquery' ) );
wp_enqueue_script( 'nf-batch-processor', Ninja_Forms::$url . 'assets/js/lib/batch-processor.js', array( 'jquery' ) );
wp_enqueue_style( 'nf-font-awesome', Ninja_Forms::$url . 'assets/css/font-awesome.min.css' );
Ninja_Forms::template( 'admin-menu-import-export.html.php', compact( 'tabs', 'active_tab' ) );
}
final public function add_meta_boxes()
{
/*
* Forms
*/
add_meta_box(
'nf_import_export_forms_import',
esc_html__( 'Import Forms', 'ninja-forms' ),
array( $this, 'template_import_forms' ),
'nf_import_export_forms'
);
add_meta_box(
'nf_import_export_forms_export',
esc_html__( 'Export Forms', 'ninja-forms' ),
array( $this, 'template_export_forms' ),
'nf_import_export_forms'
);
/*
* FAVORITE FIELDS
*/
add_meta_box(
'nf_import_export_favorite_fields_import',
esc_html__( 'Import Favorite Fields', 'ninja-forms' ),
array( $this, 'template_import_favorite_fields' ),
'nf_import_export_favorite_fields'
);
add_meta_box(
'nf_import_export_favorite_fields_export',
esc_html__( 'Export Favorite Fields', 'ninja-forms' ),
array( $this, 'template_export_favorite_fields' ),
'nf_import_export_favorite_fields'
);
}
final public function template_import_forms()
{
Ninja_Forms::template( 'admin-metabox-import-export-forms-import.html.php' );
}
final public function template_export_forms()
{
/**
* we're gonna create a new array so that we can select a form in the
* export drop down based on a url parameter
**/
$formObjs = Ninja_Forms()->form()->get_forms();
$forms = array();
foreach( $formObjs as $form ) {
$selected = '';
if( isset( $_REQUEST[ 'exportFormId' ] )
&& $form->get_id() == absint($_REQUEST[ 'exportFormId' ]) ) {
$selected = 'selected';
}
$forms[] = array(
'id' => $form->get_id(),
'title' => $form->get_setting( 'title' ),
'selected' => $selected,
);
}
Ninja_Forms::template( 'admin-metabox-import-export-forms-export.html.php', compact( 'forms' ) );
}
final public function template_import_favorite_fields()
{
Ninja_Forms::template( 'admin-metabox-import-export-favorite-fields-import.html.php' );
}
final public function template_export_favorite_fields()
{
$fields = Ninja_Forms()->form()->get_fields( array( 'saved' => 1) );
Ninja_Forms::template( 'admin-metabox-import-export-favorite-fields-export.html.php', compact( 'fields' ) );
}
/*
|--------------------------------------------------------------------------
| Backwards Compatibility
|--------------------------------------------------------------------------
*/
final public function import_fields_backwards_compatibility( $field )
{
//TODO: This was copied over. Instead need to abstract backwards compatibility for re-use.
// Flatten field settings array
if( isset( $field[ 'data' ] ) ){
$field = array_merge( $field, $field[ 'data' ] );
unset( $field[ 'data' ] );
}
// Drop form_id in favor of parent_id, which is set by the form.
if( isset( $field[ 'form_id' ] ) ){
unset( $field[ 'form_id' ] );
}
// Remove `_` prefix from type setting
$field[ 'type' ] = ltrim( $field[ 'type' ], '_' );
// Type: `text` -> `textbox`
if( 'text' == $field[ 'type' ] ){
$field[ 'type' ] = 'textbox';
}
if( 'submit' == $field[ 'type' ] ){
$field[ 'processing_label' ] = 'Processing';
}
if( 'calc' == $field[ 'type' ] ){
$field[ 'type' ] = 'note';
if( isset( $field[ 'calc_method' ] ) ) {
switch( $field[ 'calc_method' ] ){
case 'eq':
$method = esc_html__( 'Equation (Advanced)', 'ninja-forms' );
break;
case 'fields':
$method = esc_html__( 'Operations and Fields (Advanced)', 'ninja-forms' );
break;
case 'auto':
$method = esc_html__( 'Auto-Total Fields', 'ninja-forms' );
break;
default:
$method = '';
}
$field['default'] = $method . "\r\n";
if ('eq' == $field['calc_method'] && isset( $field['calc_eq'] ) ) {
$field['default'] .= $field['calc_eq'];
}
if ('fields' == $field['calc_method'] && isset( $field['calc'] ) ) {
// TODO: Support 'operations and fields (advanced)' calculations.
}
if ('auto' == $field['calc_method'] && isset( $field['calc'] ) ) {
// TODO: Support 'auto-totaling' calculations.
}
}
unset( $field[ 'calc' ] );
unset( $field[ 'calc_eq' ] );
unset( $field[ 'calc_method' ] );
}
if( isset( $field[ 'email' ] ) ){
if( 'textbox' == $field[ 'type' ] && $field[ 'email' ] ) {
$field['type'] = 'email';
}
unset( $field[ 'email' ] );
}
if( isset( $field[ 'class' ] ) ){
$field[ 'element_class' ] = $field[ 'class' ];
unset( $field[ 'class' ] );
}
if( isset( $field[ 'req' ] ) ){
$field[ 'required' ] = $field[ 'req' ];
unset( $field[ 'req' ] );
}
if( isset( $field[ 'default_value_type' ] ) ){
/* User Data */
if( '_user_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:id}';
if( '_user_email' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:email}';
if( '_user_lastname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:last_name}';
if( '_user_firstname' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:first_name}';
if( '_user_display_name' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{user:display_name}';
/* Post Data */
if( 'post_id' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:id}';
if( 'post_url' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:url}';
if( 'post_title' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{post:title}';
/* System Data */
if( 'today' == $field[ 'default_value_type' ] ) $field[ 'default' ] = '{system:date}';
/* Miscellaneous */
if( '_custom' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ){
$field[ 'default' ] = $field[ 'default_value' ];
}
if( 'querystring' == $field[ 'default_value_type' ] && isset( $field[ 'default_value' ] ) ){
$field[ 'default' ] = '{' . $field[ 'default_value' ] . '}';
}
unset( $field[ 'default_value' ] );
unset( $field[ 'default_value_type' ] );
}
if( 'list' == $field[ 'type' ] ) {
if ( isset( $field[ 'list_type' ] ) ) {
if ('dropdown' == $field['list_type']) {
$field['type'] = 'listselect';
}
if ('radio' == $field['list_type']) {
$field['type'] = 'listradio';
}
if ('checkbox' == $field['list_type']) {
$field['type'] = 'listcheckbox';
}
if ('multi' == $field['list_type']) {
$field['type'] = 'listmultiselect';
}
}
if( isset( $field[ 'list' ][ 'options' ] ) ) {
$field[ 'options' ] = $field[ 'list' ][ 'options' ];
unset( $field[ 'list' ][ 'options' ] );
}
}
// Convert `textbox` to other field types
foreach( array( 'fist_name', 'last_name', 'user_zip', 'user_city', 'user_phone', 'user_email', 'user_address_1', 'user_address_2', 'datepicker' ) as $item ) {
if ( isset( $field[ $item ] ) && $field[ $item ] ) {
$field[ 'type' ] = str_replace( array( '_', 'user', '1', '2', 'picker' ), '', $item );
unset( $field[ $item ] );
}
}
if( 'timed_submit' == $field[ 'type' ] ) {
$field[ 'type' ] = 'submit';
}
return $field;
}
private function upload_error_check( $file )
{
if( ! $file[ 'error' ] ) return;
switch ( $file[ 'error' ] ) {
case UPLOAD_ERR_INI_SIZE:
$error_message = esc_html__( 'The uploaded file exceeds the upload_max_filesize directive in php.ini.', 'ninja-forms' );
break;
case UPLOAD_ERR_FORM_SIZE:
$error_message = esc_html__( 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.', 'ninja-forms' );
break;
case UPLOAD_ERR_PARTIAL:
$error_message = esc_html__( 'The uploaded file was only partially uploaded.', 'ninja-forms' );
break;
case UPLOAD_ERR_NO_FILE:
$error_message = esc_html__( 'No file was uploaded.', 'ninja-forms' );
break;
case UPLOAD_ERR_NO_TMP_DIR:
$error_message = esc_html__( 'Missing a temporary folder.', 'ninja-forms' );
break;
case UPLOAD_ERR_CANT_WRITE:
$error_message = esc_html__( 'Failed to write file to disk.', 'ninja-forms' );
break;
case UPLOAD_ERR_EXTENSION:
$error_message = esc_html__( 'File upload stopped by extension.', 'ninja-forms' );
break;
default:
$error_message = esc_html__( 'Unknown upload error.', 'ninja-forms' );
break;
}
$args = array(
'title' => esc_html__( 'File Upload Error', 'ninja-forms' ),
'message' => $error_message,
'debug' => $file,
);
$message = Ninja_Forms()->template( 'admin-wp-die.html.php', $args );
wp_die( $message, $args[ 'title' ], array( 'back_link' => TRUE ) );
}
final public function get_capability()
{
return apply_filters( 'ninja_forms_admin_import_export_capabilities', $this->capability );
}
}