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