builder
2 years ago
plugin-updates
8 years ago
settings
2 years ago
views
2 years ago
class-evf-admin-addons.php
4 years ago
class-evf-admin-assets.php
2 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-deactivation-feedback.php
3 years ago
class-evf-admin-editor.php
4 years ago
class-evf-admin-entries-table-list.php
3 years ago
class-evf-admin-entries.php
4 years ago
class-evf-admin-form-templates.php
3 years ago
class-evf-admin-forms-table-list.php
3 years ago
class-evf-admin-forms.php
3 years ago
class-evf-admin-import-export.php
4 years ago
class-evf-admin-menus.php
2 years ago
class-evf-admin-notices.php
3 years ago
class-evf-admin-settings.php
2 years ago
class-evf-admin-tools.php
4 years ago
class-evf-admin-welcome.php
2 years ago
class-evf-admin.php
2 years ago
evf-admin-functions.php
3 years ago
class-evf-admin-import-export.php
245 lines
| 1 | <?php |
| 2 | /** |
| 3 | * EverestForms Import Export Class |
| 4 | * |
| 5 | * @package EverestForms\Admin |
| 6 | * @since 1.6.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | /** |
| 12 | * EVF_Admin_Forms class. |
| 13 | */ |
| 14 | class EVF_Admin_Import_Export { |
| 15 | |
| 16 | /** |
| 17 | * Constructor. |
| 18 | */ |
| 19 | public function __construct() { |
| 20 | add_action( 'admin_init', array( $this, 'export_json' ) ); |
| 21 | } |
| 22 | |
| 23 | /** |
| 24 | * Exports form data along with settings in JSON format. |
| 25 | */ |
| 26 | public function export_json() { |
| 27 | // Check for non empty $_POST. |
| 28 | if ( ! isset( $_POST['everest-forms-export-form'] ) || ! isset( $_POST['everest-forms-export-nonce'] ) ) { |
| 29 | return; |
| 30 | } |
| 31 | |
| 32 | // Nonce check. |
| 33 | if ( ! wp_verify_nonce( sanitize_key( wp_unslash( $_POST['everest-forms-export-nonce'] ) ), 'everest_forms_export_nonce' ) ) { |
| 34 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms' ) ); |
| 35 | } |
| 36 | $form_ids = isset( $_POST['form_ids'] ) ? wp_unslash( $_POST['form_ids'] ) : array(); //phpcs:ignore |
| 37 | |
| 38 | // Return if form id is not set and current user doesnot have export capability. |
| 39 | if ( empty( $form_ids ) || ! current_user_can( 'export' ) ) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | $export_all_forms = array(); |
| 44 | $file_name = 'evf-export-forms-' . current_time( 'Y-m-d_H:i:s' ) . '.json'; |
| 45 | |
| 46 | foreach ( $form_ids as $key => $form_id ) { |
| 47 | $form_id = absint( wp_unslash( $form_id ) ); |
| 48 | $form_post = get_post( $form_id ); |
| 49 | $export_data = array( |
| 50 | 'form_post' => array( |
| 51 | 'post_content' => $form_post->post_content, |
| 52 | 'post_title' => $form_post->post_title, |
| 53 | 'post_name' => $form_post->post_name, |
| 54 | 'post_type' => $form_post->post_type, |
| 55 | 'post_status' => $form_post->post_status, |
| 56 | ), |
| 57 | ); |
| 58 | // Export form styles if found. |
| 59 | $form_styles = get_option( 'everest_forms_styles', array() ); |
| 60 | if ( ! empty( $form_styles[ $form_id ] ) ) { |
| 61 | $export_data['form_styles'] = wp_json_encode( $form_styles[ $form_id ] ); |
| 62 | } |
| 63 | |
| 64 | if ( ob_get_contents() ) { |
| 65 | ob_clean(); |
| 66 | } |
| 67 | |
| 68 | $export_all_forms[] = $export_data; |
| 69 | } |
| 70 | |
| 71 | $forms['forms'] = $export_all_forms; |
| 72 | |
| 73 | // Force download. |
| 74 | header( 'Content-Type: application/force-download' ); |
| 75 | // Disposition / Encoding on response body. |
| 76 | header( "Content-Disposition: attachment;filename={$file_name}; charset=utf-8" ); |
| 77 | header( 'Content-type: application/json' ); |
| 78 | echo wp_json_encode( $forms ); |
| 79 | exit(); |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Import Forms from backend. |
| 84 | */ |
| 85 | public static function import_forms() { |
| 86 | // Check for $_FILES set or not. |
| 87 | if ( isset( $_FILES['jsonfile']['name'], $_FILES['jsonfile']['tmp_name'] ) ) { |
| 88 | $filename = sanitize_file_name( wp_unslash( $_FILES['jsonfile']['name'] ) ); |
| 89 | $extension = pathinfo( $filename, PATHINFO_EXTENSION ); |
| 90 | |
| 91 | // Check for file format. |
| 92 | if ( 'json' === $extension ) { |
| 93 | $all_form_data = json_decode( file_get_contents( $_FILES['jsonfile']['tmp_name'] ) ); // @codingStandardsIgnoreLine |
| 94 | $is_parsable = array(); |
| 95 | $import_success = false; |
| 96 | $post_ids = array(); |
| 97 | |
| 98 | // Check for non-empty JSON file. |
| 99 | if ( isset( $all_form_data->forms ) && ! empty( $all_form_data->forms ) ) { |
| 100 | foreach ( $all_form_data->forms as $key => $form_data ) { |
| 101 | // Check for non-empty post data array. |
| 102 | if ( ! empty( $form_data->form_post ) ) { |
| 103 | $is_parsable[] = true; |
| 104 | } else { |
| 105 | wp_send_json_error( |
| 106 | array( |
| 107 | 'message' => esc_html__( 'Invalid file format. Only JSON File Allowed.', 'everest-forms' ), |
| 108 | ) |
| 109 | ); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if ( count( $is_parsable ) === count( $all_form_data->forms ) ) { |
| 114 | foreach ( $all_form_data->forms as $key => $form_data ) { |
| 115 | $post_id = self::parse_forms( $form_data ); |
| 116 | |
| 117 | // Check for any error while inserting. |
| 118 | if ( is_wp_error( $post_id ) ) { |
| 119 | return $post_id; |
| 120 | } |
| 121 | |
| 122 | if ( $post_id ) { |
| 123 | $import_success = true; |
| 124 | $post_ids[] = $post_id; |
| 125 | } |
| 126 | } |
| 127 | if ( $import_success ) { |
| 128 | if ( 1 === count( $is_parsable ) ) { |
| 129 | wp_send_json_success( |
| 130 | array( |
| 131 | 'message' => esc_html__( 'Imported Successfully. ', 'everest-forms' ) . '<a href="' . esc_url( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . $post_ids[0] ) ) . '">' . esc_html__( 'View Form', 'everest-forms' ) . '</a>', |
| 132 | ) |
| 133 | ); |
| 134 | } else { |
| 135 | wp_send_json_success( |
| 136 | array( |
| 137 | 'message' => esc_html__( 'Imported Successfully. ', 'everest-forms' ), |
| 138 | 'data' => $post_ids, |
| 139 | ) |
| 140 | ); |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | } elseif ( ! isset( $all_form_data->forms ) && ! empty( $all_form_data ) ) { |
| 145 | // Backward Compatibility. |
| 146 | if ( ! empty( $all_form_data->form_post ) ) { |
| 147 | $post_id = self::parse_forms( $all_form_data ); |
| 148 | |
| 149 | // Check for any error while inserting. |
| 150 | if ( is_wp_error( $post_id ) ) { |
| 151 | return $post_id; |
| 152 | } |
| 153 | |
| 154 | if ( $post_id ) { |
| 155 | wp_send_json_success( |
| 156 | array( |
| 157 | 'message' => esc_html__( 'Imported Successfully. ', 'everest-forms' ) . '<a href="' . esc_url( admin_url( 'admin.php?page=evf-builder&tab=fields&form_id=' . absint( $post_id ) ) ) . '">' . esc_html__( 'View Form', 'everest-forms' ) . '</a>', |
| 158 | ) |
| 159 | ); |
| 160 | } |
| 161 | } else { |
| 162 | wp_send_json_error( |
| 163 | array( |
| 164 | 'message' => esc_html__( 'Invalid file format. Only JSON File Allowed.', 'everest-forms' ), |
| 165 | ) |
| 166 | ); |
| 167 | } |
| 168 | } else { |
| 169 | wp_send_json_error( |
| 170 | array( |
| 171 | 'message' => esc_html__( 'Invalid file format. Only JSON File Allowed.', 'everest-forms' ), |
| 172 | ) |
| 173 | ); |
| 174 | } |
| 175 | } else { |
| 176 | wp_send_json_error( |
| 177 | array( |
| 178 | 'message' => esc_html__( 'Invalid file format. Only JSON File Allowed.', 'everest-forms' ), |
| 179 | ) |
| 180 | ); |
| 181 | } |
| 182 | } else { |
| 183 | wp_send_json_error( |
| 184 | array( |
| 185 | 'message' => esc_html__( 'Please select json file to import form data.', 'everest-forms' ), |
| 186 | ) |
| 187 | ); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /** |
| 192 | * Insert form. |
| 193 | * |
| 194 | * @since 1.8.6 |
| 195 | * |
| 196 | * @param object $form_data Form data. |
| 197 | */ |
| 198 | public static function parse_forms( $form_data ) { |
| 199 | $args = array( 'post_type' => 'everest_form' ); |
| 200 | $forms = get_posts( $args ); |
| 201 | |
| 202 | foreach ( $forms as $key => $form_obj ) { |
| 203 | if ( $form_data->form_post->post_title === $form_obj->post_title ) { |
| 204 | $form_data->form_post->post_title = $form_data->form_post->post_title . ' (Imported)'; |
| 205 | break; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | // Get the form data. |
| 210 | $new_form_data = evf_decode( $form_data->form_post->post_content ); |
| 211 | $new_form = array( |
| 212 | 'post_content' => evf_encode( $new_form_data ), |
| 213 | 'post_status' => $form_data->form_post->post_status, |
| 214 | 'post_title' => $form_data->form_post->post_title, |
| 215 | 'post_type' => $form_data->form_post->post_type, |
| 216 | ); |
| 217 | $post_id = wp_insert_post( $new_form ); |
| 218 | |
| 219 | // Set new form ID. |
| 220 | $new_form_data['id'] = absint( $post_id ); |
| 221 | $form = array( |
| 222 | 'ID' => $post_id, |
| 223 | 'post_content' => evf_encode( $new_form_data ), |
| 224 | ); |
| 225 | |
| 226 | wp_update_post( $form ); |
| 227 | |
| 228 | // Import form styles if present. |
| 229 | $style_needed = false; |
| 230 | if ( ! empty( $form_data->form_styles ) ) { |
| 231 | $style_needed = true; |
| 232 | $form_styles = get_option( 'everest_forms_styles', array() ); |
| 233 | $form_styles[ $post_id ] = evf_decode( $form_data->form_styles ); |
| 234 | |
| 235 | // Update forms styles. |
| 236 | update_option( 'everest_forms_styles', $form_styles ); |
| 237 | } |
| 238 | |
| 239 | do_action( 'everest_forms_import_form', $post_id, $form, array(), $style_needed ); |
| 240 | return $post_id; |
| 241 | } |
| 242 | } |
| 243 | |
| 244 | new EVF_Admin_Import_Export(); |
| 245 |