function wpv_admin_import_data_from_xmlstring($xmlstring, $items = array(), $import_type = null) { global $WP_Views; if (!empty($xmlstring)) { if (!function_exists('simplexml_load_string')) { return new WP_Error('xml_missing', __('The Simple XML library is missing.', 'wpv-views')); } $xml = simplexml_load_string($xmlstring); if (!$xml) { return new WP_Error('not_xml_file', sprintf(__('The XML could not be read.', 'wpv-views'))); } $import_data = wpv_admin_import_export_simplexml2array($xml); if (isset($import_type)) { // import view templates if ('view-templates' == $import_type) { $result = wpv_admin_import_view_templates($import_data, $items); if ($result) { return $result; } } elseif ('views' == $import_type) { $result = wpv_admin_import_views($import_data, $items); if ($result) { return $result; } } else { $results = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array()); return $results; } } } else { // empty xml string $results = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array()); return $results; } }
function wpv_admin_import_data() { global $WP_Views; if (isset($_FILES['import-file'])) { $file = $_FILES['import-file']; } else { $file = null; } if ($file == null) { // check for import from settings.xml in theme if (isset($_POST['import-file'])) { $file = array(); $file['name'] = $_POST['import-file']; $file['tmp_name'] = $_POST['import-file']; $file['size'] = filesize($file['tmp_name']); } } $data = array(); $info = pathinfo($file['name']); $is_zip = $info['extension'] == 'zip' ? true : false; if ($is_zip) { $zip = zip_open(urldecode($file['tmp_name'])); 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 { return new WP_Error('could_not_open_file', __('Unable to open zip file', 'wpv-views')); } } else { $fh = fopen($file['tmp_name'], 'r'); if ($fh) { $data = fread($fh, $file['size']); fclose($fh); } } if (!empty($data)) { if (!function_exists('simplexml_load_string')) { return new WP_Error('xml_missing', __('The Simple XML library is missing.', 'wpv-views')); } $xml = simplexml_load_string($data); if (!$xml) { return new WP_Error('not_xml_file', sprintf(__('The XML file (%s) could not be read.', 'wpv-views'), $file['name'])); } $import_data = wpv_admin_import_export_simplexml2array($xml); // import view templates first. $error = wpv_admin_import_view_templates($import_data); if ($error) { return $error; } // import views next. $error = wpv_admin_import_views($import_data); if ($error) { return $error; } // import views next. $error = wpv_admin_import_settings($import_data); if ($error) { return $error; } } else { return new WP_Error('could_not_open_file', __('Could not read the Views import file.', 'wpv-views')); } }