forked from weDevsOfficial/wp-user-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclass-tools.php
390 lines (310 loc) · 15.7 KB
/
class-tools.php
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
<?php
/**
* Manage Import Export
*
* @since 2.2
*
* @package WP User Frontend
*/
class WPUF_Admin_Tools {
/**
* List of All the post forms
*
* @return void
*/
function list_forms() {
if ( isset( $_POST['export'] ) ) {
$this->export_data( $_POST['export_content'], $_POST['formlist'] );
}
$args = array(
'post_type' => 'wpuf_forms',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$forms = get_posts( $args );
if ( $forms ) {
?>
<div class="postbox" style="margin-top: 15px;">
<h3 style="padding:10px 15px"><?php _e( 'Form Export', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<div class="main">
<form action="" method="post" style="margin-top: 20px;">
<p>
<input class="export_type" type="radio" name="export_content" value="all" id="wpuf-all_export" checked>
<label for="wpuf-all_export"><?php _e( 'All', 'wp-user-frontend' ); ?></label>
</p>
<p>
<input class="export_type" type="radio" name="export_content" value="selected" id="wpuf-selected_export">
<label for="wpuf-selected_export"><?php _e( 'Select individual', 'wp-user-frontend' ); ?></label></p>
<p>
<select class="formlist" name="formlist[]" multiple="multiple">
<?php foreach ( $forms as $form ) { ?>
<option value="<?php echo esc_attr( $form->ID ) ?>"><?php echo esc_attr( $form->post_title ); ?></option>
<?php } ?>
</select>
</p>
<?php wp_nonce_field( 'wpuf-export-form' ); ?>
<input type="submit" class="button button-primary" name="export" value="<?php _e( 'Export', 'wp-user-frontend' ) ?>">
</form>
</div>
</div>
</div>
<?php
} else {
sprintf( '<p>%s</p>', __( 'Sorry you have no form to export', 'wp-user-frontend' ) );
}
}
/**
* List of All Registration forms
*
* @return void
*/
function list_regis_forms() {
if ( isset( $_POST['export_regis_form'] ) ) {
$this->export_regis_data( $_POST['export_regis_content'], $_POST['formlist'] );
}
$args = array(
'post_type' => 'wpuf_profile',
'posts_per_page' => -1,
'post_status' => 'publish'
);
$forms = get_posts( $args );
if ( $forms ) {
?>
<div class="postbox">
<h3 style="padding:10px 15px"><?php _e( 'Registration Form Export', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<div class="main">
<form action="" method="post" style="margin-top: 20px;">
<p>
<input class="export_type" type="radio" name="export_regis_content" value="all" id="wpuf-all_regis_export" checked>
<label for="wpuf-all_regis_export"><?php _e( 'All', 'wp-user-frontend' ); ?></label>
</p>
<p>
<input class="export_type" type="radio" name="export_regis_content" value="selected" id="wpuf-selected_regis_export">
<label for="wpuf-selected_regis_export"><?php _e( 'Select individual', 'wp-user-frontend' ); ?></label>
</p>
<p>
<select class="formlist" name="formlist[]" multiple="multiple">
<?php foreach ( $forms as $form ) { ?>
<option value="<?php echo esc_attr( $form->ID ); ?>"><?php echo esc_attr( $form->post_title ); ?></option>";
<?php } ?>
</select>
</p>
<?php wp_nonce_field( 'wpuf-export-regs-form' ); ?>
<input type="submit" class="button button-primary" name="export_regis_form" value="<?php _e( 'Export', 'wp-user-frontend' ) ?>">
</form>
</div>
</div>
</div>
<?php
} else {
sprintf( '<p>%s</p>', __( 'Sorry you have no form to export', 'wp-user-frontend' ) );
}
}
/**
* Import functionality
*/
function import_data() {
if ( isset( $_FILES['import'] ) && check_admin_referer( 'wpuf-import' ) ) {
if ( $_FILES['import']['error'] > 0 ) {
printf( '<div class="error"><p>%s</p></div>', __( 'Somthing went wrong. Please choose a file again', 'wp-user-frontend' ) );
} else {
$file_name = $_FILES['import']['name'];
$file_ext = pathinfo( $file_name, PATHINFO_EXTENSION );
$file_size = $_FILES['import']['size'];
if ( ($file_ext == "json") && ($file_size < 500000) ) {
$data = static::import_json_file( $_FILES['import']['tmp_name'] );
if ( $data ) {
printf( '<div class="updated"><p>%s</p></div>', __( 'Import successful. Have fun!', 'wp-user-frontend' ) );
}
} else {
printf( '<div class="error"><p>%s</p></div>', __( 'Invalid file or file size too big.', 'wp-user-frontend' ) );
}
}
}
?>
<h3><?php _e( 'Import forms', 'wp-user-frontend' ); ?></h3>
<p><?php _e( 'Click Browse button and choose a json file that you backup before.', 'wp-user-frontend' ); ?></p>
<p><?php _e( 'Press <strong>Import</strong> button, we will do the rest for you.', 'wp-user-frontend' ); ?></p>
<form action="" method="post" enctype='multipart/form-data' style="margin-top: 20px;">
<?php wp_nonce_field( 'wpuf-import' ); ?>
<input type='file' name='import' />
<input type="submit" class="button button-primary" name="import_data" value="<?php _e( 'Import', 'wp-user-frontend' ); ?>">
</form>
<?php
}
/**
* Import json file into database
* @param array $file
* @return boolean
*/
public static function import_json_file( $file ) {
$encode_data = file_get_contents( $file );
$options = json_decode( $encode_data, true );
foreach ( $options as $key => $value ) {
$generate_post = array(
'post_title' => $value['post_data']['post_title'],
'post_status' => $value['post_data']['post_status'],
'post_type' => $value['post_data']['post_type'],
'ping_status' => $value['post_data']['ping_status'],
'comment_status' => $value['post_data']['comment_status']
);
$post_id = wp_insert_post( $generate_post, true );
if ( $post_id && !is_wp_error( $post_id ) ) {
foreach ( $value['meta_data']['fields'] as $order => $field ) {
wpuf_insert_form_field( $post_id, $field, false, $order );
}
update_post_meta( $post_id, 'wpuf_form_settings', $value['meta_data']['settings'] );
update_post_meta( $post_id, 'notifications', $value['meta_data']['notifications'] );
}
}
return true;
}
/**
* Export Registration form
* @param string $export_type
* @param integer $post_ids
*/
function export_regis_data( $export_type, $post_ids ) {
if ( $export_type == 'all' && check_admin_referer( 'wpuf-export-regs-form' ) ) {
static::export_to_json( 'wpuf_profile' );
} elseif ( $export_type == 'selected' && check_admin_referer( 'wpuf-export-regs-form' ) ) {
if ( $_POST['formlist'] == NULL ) {
printf( '<div class="error"><p>%s</p></div>', __( 'Please select some form for exporting', 'wp-user-frontend' ) );
} else {
static::export_to_json( 'wpuf_profile', $post_ids );
}
}
}
/**
* Export normal form data
* @param string $export_type
* @param integer $post_ids
*/
function export_data( $export_type, $post_ids ) {
if ( $export_type == 'all' && check_admin_referer( 'wpuf-export-form' ) ) {
static::export_to_json( 'wpuf_forms' );
} elseif ( $export_type == 'selected' && check_admin_referer( 'wpuf-export-form' ) ) {
if ( $_POST['formlist'] == NULL ) {
printf( '<div class="error"><p>%s</p></div>', __( 'Please select some form for exporting', 'wp-user-frontend' ) );
} else {
static::export_to_json( 'wpuf_forms', $post_ids );
}
}
}
/**
* Export into json file
*
* @param string $post_type
* @param array $post_ids
*/
public static function export_to_json( $post_type, $post_ids = array( ) ) {
$formatted_data = array();
$ids = array();
$blogname = strtolower( str_replace( " ", "-", get_option( 'blogname' ) ) );
$date = date( "Y-m-d" );
$json_name = $blogname . "-wpuf-" . $post_type . '-' . $date; // Namming the filename will be generated.
if ( ! empty( $post_ids ) ) {
foreach ( $post_ids as $key => $value ) {
array_push( $ids, $value );
}
}
$args = array(
'post_status' => 'publish',
'post_type' => $post_type,
'post__in' => (!empty( $ids ) ) ? $ids : ''
);
$query = new WP_Query( $args );
foreach ( $query->posts as $post ) {
$postdata = get_object_vars( $post );
unset( $postdata['ID'] );
$data = array(
'post_data' => $postdata,
'meta_data' => array(
'fields' => wpuf_get_form_fields( $post->ID ),
'settings' => wpuf_get_form_settings( $post->ID ),
'notifications' => wpuf_get_form_notifications( $post->ID )
)
);
array_push( $formatted_data, $data );
}
$json_file = json_encode( $formatted_data ); // Encode data into json data
ob_clean();
echo $json_file;
header( "Content-Type: text/json; charset=" . get_option( 'blog_charset' ) );
header( "Content-Disposition: attachment; filename=$json_name.json" );
exit();
}
/**
* Formetted meta key value
*
* @param array $array
* @return array
*/
function formetted_meta_key_value( $array ) {
$result = array( );
foreach ( $array as $key => $val ) {
$result[$key] = $val[0];
}
return $result;
}
function tool_page() {
$msg = isset( $_GET['msg'] ) ? $_GET['msg'] : '';
$text = '';
$confirmation_message = __( 'Are you Sure?', 'wp-user-frontend' );
switch ($msg) {
case 'del_forms':
$text = __( 'All forms has been deleted', 'wp-user-frontend' );
break;
case 'settings_cleared':
$text = __( 'Settings has been cleared!', 'wp-user-frontend' );
break;
case 'del_trans':
$text = __( 'All transactions has been deleted!', 'wp-user-frontend' );
break;
}
if ( $text ) {
?>
<div class="updated">
<p>
<?php echo $text; ?>
</p>
</div>
<?php } ?>
<div class="metabox-holder">
<div class="postbox">
<h3><?php _e( 'Page Installation', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<p><?php _e( 'Clicking this button will create required pages for the plugin. Note: It\'ll not delete/replace existing pages.', 'wp-user-frontend' ); ?></p>
<a class="button button-primary" href="<?php echo add_query_arg( array( 'install_wpuf_pages' => true ) ); ?>"><?php _e( 'Install WPUF Pages', 'wp-user-frontend' ); ?></a>
</div>
</div>
<div class="postbox">
<h3><?php _e( 'Reset Settings', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<p><?php _e( '<strong>Caution:</strong> This tool will delete all the plugin settings of WP User Frontend Pro', 'wp-user-frontend' ); ?></p>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'clear_settings' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('Are you sure?');"><?php _e( 'Reset Settings', 'wp-user-frontend' ); ?></a>
</div>
</div>
<div class="postbox">
<h3><?php _e( 'Delete Forms', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<p><?php _e( '<strong>Caution:</strong> This tool will delete all the post and registration/profile forms.', 'wp-user-frontend' ); ?></p>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'del_post_forms' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('<?php echo $confirmation_message ?>');"><?php _e( 'Delete Post Forms', 'wp-user-frontend' ); ?></a>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'del_pro_forms' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('<?php echo $confirmation_message ?>');"><?php _e( 'Delete Registration Forms', 'wp-user-frontend' ); ?></a>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'del_subs' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('<?php echo $confirmation_message ?>');"><?php _e( 'Delete Subscriptions', 'wp-user-frontend' ); ?></a>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'del_coupon' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('<?php echo $confirmation_message ?>');"><?php _e( 'Delete Coupons', 'wp-user-frontend' ); ?></a>
</div>
</div>
<div class="postbox">
<h3><?php _e( 'Transactions', 'wp-user-frontend' ); ?></h3>
<div class="inside">
<p><?php _e( 'This tool will delete all the transactions from the transaction table.', 'wp-user-frontend' ); ?></p>
<a class="button button-primary" href="<?php echo wp_nonce_url( add_query_arg( array( 'wpuf_action' => 'clear_transaction' ), 'admin.php?page=wpuf_tools&action=tools' ), 'wpuf-tools-action' ); ?>" onclick="return confirm('<?php echo $confirmation_message ?>');"><?php _e( 'Delete Transactions', 'wp-user-frontend' ); ?></a>
</div>
</div>
</div>
<?php
}
}