| @@ -1,122 +1,125 @@ | ||
| 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 | -} | |
| 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 | +} | |