/**
 * Loops over elements and convert to array or empty string.
 * 
 * @param type $element
 * @return string 
 */
function wpv_admin_import_export_simplexml2array($element)
{
    $element = is_string($element) ? trim($element) : $element;
    if (!empty($element) && is_object($element)) {
        $element = (array) $element;
    }
    // SRDJAN - slider settings that have 0 values are imported as empty string https://icanlocalize.basecamphq.com/projects/7393061-wp-views/todo_items/142382765/comments
    if (!is_array($element) && strval($element) == '0') {
        $element = 0;
    } else {
        if (empty($element)) {
            $element = '';
        } else {
            if (is_array($element)) {
                foreach ($element as $k => $v) {
                    $v = is_string($v) ? trim($v) : $v;
                    if (!is_array($v) && strval($v) == '0') {
                        $element[$k] = 0;
                    } else {
                        if (empty($v)) {
                            $element[$k] = '';
                            continue;
                        }
                    }
                    $add = wpv_admin_import_export_simplexml2array($v);
                    if (!is_array($add) && strval($add) == '0') {
                        $element[$k] = 0;
                    } else {
                        if (!empty($add)) {
                            $element[$k] = $add;
                        } else {
                            $element[$k] = '';
                        }
                    }
                }
            }
        }
    }
    if (!is_array($element) && strval($element) == '0') {
        $element = 0;
    } else {
        if (empty($element)) {
            $element = '';
        }
    }
    return $element;
}
 /** Otherwise FALSE*/
 public function wcviews_import_settings($xml)
 {
     if ($xml) {
         //$xml is sensible
         //Require $wpdb
         global $wpdb;
         if (function_exists('wpv_admin_import_export_simplexml2array')) {
             //public function exists get import data
             $import_data = wpv_admin_import_export_simplexml2array($xml);
             //Loop through the settings and update WooCommerce options
             $updated_settings = array();
             foreach ($import_data as $key => $value) {
                 if ('woocommerce_views_theme_template_file' == $key) {
                     //Assign compatible templates at import site
                     $updated_value = $this->wcviews_fix_theme_templates_after_import($value);
                 } elseif ('woocommerce_views_theme_archivetemplate_file' == $key) {
                     //Assign compatible templates at import site
                     $updated_value = $this->wcviews_fix_theme_archivetemplates_after_import($value);
                 } else {
                     update_option($key, $value);
                 }
             }
         }
     }
 }
	/**
	* import_data
	*
	* Main import method for Views
	*
	* @param $args (array) Import modifiers
	*	'import-file' 				path to the file to import, can be overriden by $_FILES['import-file'] and $_POST['import-file']
	*	'views-overwrite' 			will force overwriting existing Views and WPA
	* 	'views-delete' 				will delete any existing Views and WPA that are not on the import file
	* 	'view-templates-overwrite'	will force overwriting existing CT
	* 	'view-templates-delete' 	will delete any existing CT that is not on the import file
	* 	'view-settings-overwrite' 	will overwrite existing settings
	*	'force_import_id,			will only import items with those XML IDs
	*	'force_import_post_name',	will only import items with those XML post_name
	*	'force_skip_id',			will not import items with those XML IDs
	*	'force_skip_post_name',		will not import items with those XML post_name
	*	'force_duplicate_id',		will duplicate, if they already exist, items with those XML IDs
	*	'force_duplicate_post_name'	will duplicate, if they already exist, items with those XML post_name
	*
	* @return (mixed) true on success, WP_Error otherwise
	*
	* @since unknown
	*/

	function import_data( $args = array() ) {
		if ( ! current_user_can( 'manage_options' ) ) {
			return new WP_Error( 'wrong_capability', __( 'Your user can not perform that action.', 'wpv-views' ) );
		}
		global $WP_Views;
		$file = false;
		if ( isset( $_FILES['import-file'] ) ) {
			// If import is happening from the import form, there should be a $_FILES['import-file'] entry
			$file = $_FILES['import-file'];
		} else {
			$candidate_file = false;
			if ( isset( $_POST['import-file'] ) ) {
				// Check for import file from settings.xml in theme
				$candidate_file = $_POST['import-file'];
			} else if ( isset( $args['import-file'] ) ) {
				// Check for import file from $args
				$candidate_file = $args['import-file'];
			}
			if ( 
				$candidate_file
				&& file_exists( $candidate_file )
			) {
				$file = array();
				$file['name'] = $candidate_file;
				$file['tmp_name'] = $candidate_file;
				$file['size'] = filesize( $candidate_file );
			}
		}

		if ( 
			! $file 
			|| ! isset( $file['name'] )
			|| empty( $file['name'] )
		) {
			return new WP_Error(' could_not_open_file', __( 'Could not read the Views import file.', 'wpv-views' ) );
		}

		$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 Content Templates
			$result_content_templates = $this->import_content_templates( $import_data, $args );
			if ( is_wp_error( $result_content_templates ) ) {
				return $result_content_templates;
			}
			// Import Views
			$result_views = $this->import_views( $import_data, $args );
			if ( is_wp_error( $result_views ) ) {
				return $result_views;
			}
			// Import Settings
			$result_view_settings = $this->import_settings( $import_data, $args );
			if ( is_wp_error( $result_view_settings ) ) {
				return $result_view_settings;
			}
			
			return true;
			
		} else {
			return new WP_Error( 'could_not_open_file', __( 'Could not read the Views import file.', 'wpv-views' ) );
		}
		
		return true;
	}
function wpv_admin_import_data_from_xmlstring($xmlstring, $items = array(), $import_type = null)
{
    global $WPV_Export_Import;
    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)) {
            if ('view-templates' == $import_type) {
                // Import Content Templates
                $import_items = array();
                foreach ($items as $item) {
                    $import_items[] = str_replace(_VIEW_TEMPLATES_MODULE_MANAGER_KEY_, '', $item);
                }
                $args = array('force_import_id' => $import_items, 'return_to' => 'module_manager');
                $result = $WPV_Export_Import->import_content_templates($import_data, $args);
                /** EMERSON: Import Content Template assignments to post types */
                //Proceed only if settings are set and not empty
                if (isset($import_data['settings']) && !empty($import_data['settings'])) {
                    $error = $WPV_Export_Import->import_settings($import_data);
                }
                return $result;
            } elseif ('views' == $import_type) {
                // Import Views
                $import_items = array();
                foreach ($items as $item) {
                    $import_items[] = str_replace(_VIEWS_MODULE_MANAGER_KEY_, '', $item);
                }
                $args = array('force_import_id' => $import_items, 'return_to' => 'module_manager');
                $result = $WPV_Export_Import->import_views($import_data, $args);
                /** EMERSON: Import WordPress archive assignments */
                //Proceed only if settings are set and not empty
                if (isset($import_data['settings']) && !empty($import_data['settings'])) {
                    foreach ($import_data['settings'] as $k => $v) {
                        if (!(strpos($k, 'view_') === 0)) {
                            unset($import_data['settings'][$k]);
                        }
                    }
                    $error = $WPV_Export_Import->import_settings($import_data);
                }
                return $result;
            } else {
                // Defined but not known $import_type
                $results = array('updated' => 0, 'new' => 0, 'failed' => 0, 'errors' => array());
                return $results;
            }
        } else {
            // Not set $import_type
            $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;
    }
}
/**
 * Loops over elements and convert to array or empty string.
 * 
 * @param type $element
 * @return string 
 */
function wpv_admin_import_export_simplexml2array($element)
{
    $element = is_string($element) ? trim($element) : $element;
    if (!empty($element) && is_object($element)) {
        $element = (array) $element;
    }
    if (empty($element)) {
        $element = '';
    } else {
        if (is_array($element)) {
            foreach ($element as $k => $v) {
                $v = is_string($v) ? trim($v) : $v;
                if (empty($v)) {
                    $element[$k] = '';
                    continue;
                }
                $add = wpv_admin_import_export_simplexml2array($v);
                if (!empty($add)) {
                    $element[$k] = $add;
                } else {
                    $element[$k] = '';
                }
            }
        }
    }
    if (empty($element)) {
        $element = '';
    }
    return $element;
}