builder
6 years ago
plugin-updates
8 years ago
settings
6 years ago
views
6 years ago
class-evf-admin-addons.php
6 years ago
class-evf-admin-assets.php
6 years ago
class-evf-admin-builder.php
7 years ago
class-evf-admin-editor.php
6 years ago
class-evf-admin-entries-table-list.php
6 years ago
class-evf-admin-entries.php
6 years ago
class-evf-admin-forms-table-list.php
6 years ago
class-evf-admin-forms.php
6 years ago
class-evf-admin-import-export.php
6 years ago
class-evf-admin-menus.php
6 years ago
class-evf-admin-notices.php
6 years ago
class-evf-admin-settings.php
6 years ago
class-evf-admin-tools.php
6 years ago
class-evf-admin-welcome.php
6 years ago
class-evf-admin.php
6 years ago
evf-admin-functions.php
6 years ago
class-evf-admin-import-export.php
161 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( wp_unslash( $_POST['everest-forms-export-nonce'] ), 'everest_forms_export_nonce' ) ) { // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 34 | wp_die( esc_html__( 'Action failed. Please refresh the page and retry.', 'everest-forms' ) ); |
| 35 | } |
| 36 | |
| 37 | $form_id = isset( $_POST['form_id'] ) ? absint( wp_unslash( $_POST['form_id'] ) ) : 0; |
| 38 | |
| 39 | // Return if form id is not set and current user doesnot have export capability. |
| 40 | if ( empty( $form_id ) || ! current_user_can( 'export' ) ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $form_post = get_post( $form_id ); |
| 45 | $export_data = array( |
| 46 | 'form_post' => array( |
| 47 | 'post_content' => $form_post->post_content, |
| 48 | 'post_title' => $form_post->post_title, |
| 49 | 'post_name' => $form_post->post_name, |
| 50 | 'post_type' => $form_post->post_type, |
| 51 | 'post_status' => $form_post->post_status, |
| 52 | ), |
| 53 | ); |
| 54 | $form_name = strtolower( str_replace( ' ', '-', get_the_title( $form_id ) ) ); |
| 55 | $file_name = html_entity_decode( $form_name, ENT_QUOTES, 'UTF-8' ) . '-' . current_time( 'Y-m-d_H:i:s' ) . '.json'; |
| 56 | |
| 57 | if ( ob_get_contents() ) { |
| 58 | ob_clean(); |
| 59 | } |
| 60 | |
| 61 | $export_json = wp_json_encode( $export_data ); |
| 62 | // Force download. |
| 63 | header( 'Content-Type: application/force-download' ); |
| 64 | // Disposition / Encoding on response body. |
| 65 | header( "Content-Disposition: attachment;filename={$file_name}; charset=utf-8" ); |
| 66 | header( 'Content-type: application/json' ); |
| 67 | echo $export_json; // phpcs:ignore WordPress.Security.EscapeOutput |
| 68 | exit(); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Import Form from backend. |
| 73 | */ |
| 74 | public static function import_form() { |
| 75 | // Check for $_FILES set or not. |
| 76 | if ( isset( $_FILES['jsonfile']['name'], $_FILES['jsonfile']['tmp_name'] ) ) { |
| 77 | $filename = esc_html( sanitize_text_field( wp_unslash( $_FILES['jsonfile']['name'] ) ) ); |
| 78 | $extension = pathinfo( $filename, PATHINFO_EXTENSION ); |
| 79 | |
| 80 | // Check for file format. |
| 81 | if ( 'json' === $extension ) { |
| 82 | $form_data = json_decode( file_get_contents( $_FILES['jsonfile']['tmp_name'] ) ); // @codingStandardsIgnoreLine |
| 83 | |
| 84 | // Check for non-empty JSON file. |
| 85 | if ( ! empty( $form_data ) ) { |
| 86 | // Check for non-empty post data array. |
| 87 | if ( ! empty( $form_data->form_post ) ) { |
| 88 | $args = array( 'post_type' => 'everest_form' ); |
| 89 | $forms = get_posts( $args ); |
| 90 | |
| 91 | foreach ( $forms as $key => $form_obj ) { |
| 92 | if ( $form_data->form_post->post_title === $form_obj->post_title ) { |
| 93 | $form_data->form_post->post_title = $form_data->form_post->post_title . ' (Imported)'; |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Get the form data. |
| 99 | $new_form_data = evf_decode( $form_data->form_post->post_content ); |
| 100 | $new_form = array( |
| 101 | 'post_content' => evf_encode( $new_form_data ), |
| 102 | 'post_status' => $form_data->form_post->post_status, |
| 103 | 'post_title' => $form_data->form_post->post_title, |
| 104 | 'post_type' => $form_data->form_post->post_type, |
| 105 | ); |
| 106 | $post_id = wp_insert_post( $new_form ); |
| 107 | |
| 108 | // Set new form ID. |
| 109 | $new_form_data['id'] = absint( $post_id ); |
| 110 | $form = array( |
| 111 | 'ID' => $post_id, |
| 112 | 'post_content' => evf_encode( $new_form_data ), |
| 113 | ); |
| 114 | |
| 115 | wp_update_post( $form ); |
| 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 | wp_send_json_success( |
| 124 | array( |
| 125 | 'message' => esc_html__( 'Imported Successfully.', 'everest-forms' ), |
| 126 | ) |
| 127 | ); |
| 128 | } |
| 129 | } else { |
| 130 | wp_send_json_error( |
| 131 | array( |
| 132 | 'message' => esc_html__( 'Invalid file content. Please export file from Everest Forms plugin.', 'everest-forms' ), |
| 133 | ) |
| 134 | ); |
| 135 | } |
| 136 | } else { |
| 137 | wp_send_json_error( |
| 138 | array( |
| 139 | 'message' => esc_html__( 'Invalid file content. Please export file from Everest Forms plugin.', 'everest-forms' ), |
| 140 | ) |
| 141 | ); |
| 142 | } |
| 143 | } else { |
| 144 | wp_send_json_error( |
| 145 | array( |
| 146 | 'message' => esc_html__( 'Invalid file format. Only JSON File Allowed.', 'everest-forms' ), |
| 147 | ) |
| 148 | ); |
| 149 | } |
| 150 | } else { |
| 151 | wp_send_json_error( |
| 152 | array( |
| 153 | 'message' => esc_html__( 'Please select json file to import form data.', 'everest-forms' ), |
| 154 | ) |
| 155 | ); |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | new EVF_Admin_Import_Export(); |
| 161 |