function wpcf_admin_import_confirmation_form() { $form = array(); if (isset($_POST['types-import-step'])) { $mode = 'none'; $data = ''; if (!empty($_POST['types-import-submit']) && (!empty($_POST['types-import-method']) && $_POST['types-import-method'] == 'file') && !empty($_FILES['types-import-file']['tmp_name'])) { if ($_FILES['types-import-file']['type'] == 'text/xml') { $_FILES['types-import-file']['name'] .= '.txt'; } /* * * We need to move uploaded file manually */ if (!empty($_FILES['types-import-file']['error'])) { echo '<div class="message error"><p>' . __('Error uploading file', 'wpcf') . '</p></div>'; echo wpcf_admin_import_form(); return; } $wp_upload_dir = wp_upload_dir(); $new_file = $wp_upload_dir['basedir'] . '/' . sanitize_file_name($_FILES['types-import-file']['name']); $move = move_uploaded_file($_FILES['types-import-file']['tmp_name'], $new_file); if (!$move) { echo '<div class="message error"><p>' . __('Error moving uploaded file', 'wpcf') . '</p></div>'; echo wpcf_admin_import_form(); return; } $uploaded_file = array('file' => $new_file); $info = pathinfo($uploaded_file['file']); $is_zip = $info['extension'] == 'zip' ? true : false; if ($is_zip) { $zip = zip_open($uploaded_file['file']); if (is_resource($zip)) { while (($zip_entry = zip_read($zip)) !== false) { if (zip_entry_name($zip_entry) == 'settings.xml') { $data = @zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); } } } else { echo '<div class="message error"><p>' . __('Unable to open zip file', 'wpcf') . '</p></div>'; echo wpcf_admin_import_form(); return; } } else { $data = @file_get_contents($uploaded_file['file']); } /** * use Transients API to store file fullpath */ $current_user = wp_get_current_user(); $cache_key = md5($current_user->user_email . $uploaded_file['file']); set_transient($cache_key, $uploaded_file['file'], 60 * 60); $form['types-import-file'] = array('#type' => 'hidden', '#name' => 'types-import-file', '#value' => $cache_key); $mode = 'file'; } elseif (!empty($_POST['types-import-submit']) && (!empty($_POST['types-import-method']) && $_POST['types-import-method'] == 'text') && !empty($_POST['types-import-text'])) { $data = stripslashes($_POST['types-import-text']); if (preg_match('/encoding=("[^"]*"|\'[^\']*\')/s', $data, $match)) { $charset = trim($match[1], '"'); } else { $charset = !empty($_POST['types-import-text-encoding']) ? sanitize_text_field($_POST['types-import-text-encoding']) : get_option('blog_charset'); } $form['types-import-text'] = array('#type' => 'hidden', '#name' => 'types-import-text', '#value' => htmlentities(stripslashes($_POST['types-import-text']), ENT_QUOTES, $charset)); $form['types-import-text-encoding'] = array('#type' => 'hidden', '#name' => 'types-import-text-encoding', '#value' => $charset); $mode = 'text'; } if (empty($data)) { echo '<div class="message error"><p>' . __('Data to import not set or not valid', 'wpcf') . '</p></div>'; echo wpcf_admin_import_form(); return; } else { $data = wpcf_admin_import_export_settings($data); if (empty($data)) { echo '<div class="message error"><p>' . __('Data not valid', 'wpcf') . '</p></div>'; echo wpcf_admin_import_form(); return; } else { $form = array_merge($form, $data); $form['types-import-mode'] = array('#type' => 'hidden', '#name' => 'types-import-mode', '#value' => $mode); $form['types-import-final'] = array('#type' => 'hidden', '#name' => 'types-import-final', '#value' => 1); $form['types-import-final-submit'] = array('#type' => 'submit', '#name' => 'types-import-final-submit', '#value' => __('Import', 'wpcf'), '#attributes' => array('class' => 'button-primary'), '#before' => '<p class="toolset-update-button-wrap">', '#after' => '</p>'); $form['types_import_wpnonce'] = array('#type' => 'hidden', '#name' => 'types_import_wpnonce', '#value' => wp_create_nonce('wpcf_import')); } } } return wpcf_form_simple($form); }
function wpcf_render_import_form() { require_once WPCF_INC_ABSPATH . '/fields.php'; require_once WPCF_INC_ABSPATH . '/import-export.php'; echo '<form method="post" action="' . admin_url('admin.php') . '?page=toolset-export-import&tab=types" class="wpcf-import-export-form ' . 'wpcf-form-validate" enctype="multipart/form-data">'; if (isset($_POST['types_import_wpnonce']) && wp_verify_nonce($_POST['types_import_wpnonce'], 'wpcf_import') && isset($_POST['types-import-step'])) { echo wpcf_admin_import_confirmation_form(); } else { echo wpcf_admin_import_form(); } echo '</form>'; }