| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Manage Import Export |
| 5 |
* |
| 6 |
* @since 1.1.0 |
| 7 |
*/ |
| 8 |
class WeForms_Admin_Tools { |
| 9 |
|
| 10 |
/** |
| 11 |
* Import json file into database |
| 12 |
* |
| 13 |
* @param array $file |
| 14 |
* |
| 15 |
* @return bool |
| 16 |
*/ |
| 17 |
public static function import_json_file( $file ) { |
| 18 |
$encode_data = file_get_contents( $file ); |
| 19 |
$jsonData = html_entity_decode( $encode_data ); |
| 20 |
$options = json_decode( $jsonData, true ); |
| 21 |
|
| 22 |
foreach ( $options as $key => $value ) { |
| 23 |
$generate_post = [ |
| 24 |
'post_title' => $value['post_data']['post_title'], |
| 25 |
'post_status' => $value['post_data']['post_status'], |
| 26 |
'post_type' => $value['post_data']['post_type'], |
| 27 |
'ping_status' => $value['post_data']['ping_status'], |
| 28 |
'comment_status' => $value['post_data']['comment_status'], |
| 29 |
]; |
| 30 |
|
| 31 |
$post_id = wp_insert_post( $generate_post, true ); |
| 32 |
|
| 33 |
if ( $post_id && !is_wp_error( $post_id ) ) { |
| 34 |
foreach ( $value['meta_data']['fields'] as $order => $field ) { |
| 35 |
weforms_insert_form_field( $post_id, $field, false, $order ); |
| 36 |
} |
| 37 |
|
| 38 |
update_post_meta( $post_id, 'wpuf_form_settings', $value['meta_data']['settings'] ); |
| 39 |
update_post_meta( $post_id, 'notifications', $value['meta_data']['notifications'] ); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
return true; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Export into json file |
| 48 |
* |
| 49 |
* @param string $post_type |
| 50 |
* @param array $post_ids |
| 51 |
*/ |
| 52 |
public static function export_to_json( $post_ids = [ ] ) { |
| 53 |
$formatted_data = []; |
| 54 |
$ids = []; |
| 55 |
$blogname = strtolower( str_replace( ' ', '-', get_option( 'blogname' ) ) ); |
| 56 |
$json_name = $blogname . '-weforms-' . time(); // Namming the filename will be generated. |
| 57 |
|
| 58 |
if ( !empty( $post_ids ) ) { |
| 59 |
foreach ( $post_ids as $key => $value ) { |
| 60 |
array_push( $ids, $value ); |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
$args = [ |
| 65 |
'post_status' => 'publish', |
| 66 |
'post_type' => 'wpuf_contact_form', |
| 67 |
'post__in' => ( !empty( $ids ) ) ? $ids : '', |
| 68 |
]; |
| 69 |
|
| 70 |
$query = new WP_Query( $args ); |
| 71 |
|
| 72 |
foreach ( $query->posts as $post ) { |
| 73 |
$postdata = get_object_vars( $post ); |
| 74 |
unset( $postdata['ID'] ); |
| 75 |
|
| 76 |
$form = weforms()->form->get( $post ); |
| 77 |
|
| 78 |
$data = [ |
| 79 |
'post_data' => $postdata, |
| 80 |
'meta_data' => [ |
| 81 |
'fields' => $form->get_fields(), |
| 82 |
'settings' => $form->get_settings(), |
| 83 |
'notifications' => $form->get_notifications(), |
| 84 |
], |
| 85 |
]; |
| 86 |
|
| 87 |
array_push( $formatted_data, $data ); |
| 88 |
} |
| 89 |
|
| 90 |
$json_file = json_encode( $formatted_data ); // Encode data into json data |
| 91 |
|
| 92 |
error_reporting( 0 ); |
| 93 |
|
| 94 |
if ( ob_get_contents() ) { |
| 95 |
ob_clean(); |
| 96 |
} |
| 97 |
|
| 98 |
header( 'Content-Type: text/json; charset=' . get_option( 'blog_charset' ) ); |
| 99 |
header( "Content-Disposition: attachment; filename=$json_name.json" ); |
| 100 |
|
| 101 |
echo esc_attr( $json_file ); |
| 102 |
|
| 103 |
exit(); |
| 104 |
} |
| 105 |
|
| 106 |
/** |
| 107 |
* Formetted meta key value |
| 108 |
* |
| 109 |
* @param array $array |
| 110 |
* |
| 111 |
* @return array |
| 112 |
*/ |
| 113 |
public function formetted_meta_key_value( $array ) { |
| 114 |
$result = [ ]; |
| 115 |
|
| 116 |
foreach ( $array as $key => $val ) { |
| 117 |
$result[$key] = $val[0]; |
| 118 |
} |
| 119 |
|
| 120 |
return $result; |
| 121 |
} |
| 122 |
} |
| 123 |
|