Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // ------- IMPORTER CONFIG (EDIT) ------------
- define('Z_CATEGORY','Demo'); // category names
- define('Z_DELAY',1); //delay (in second)
- // ------- IMPORTER CONFIG (DONE EDIT) ------------
- // ------- IMPORTER CONFIG (NO EDIT) ------------
- require('../wp-blog-header.php');
- set_time_limit(0);
- global $cat_array;
- $cat_array=array();
- $files = array();
- if ($handle = opendir('.')) {
- while (false !== ($entry = readdir($handle))) {
- $ext = pathinfo($entry, PATHINFO_EXTENSION);
- if ($entry != "." && $entry != ".." && $ext == 'html') {
- $files[] = $entry;
- }
- }
- closedir($handle);
- }
- $cat_name=Z_CATEGORY;
- $categories=get_categories(array ('hide_empty'=> 0)); //get the categories
- //print_r($categories);
- foreach($categories as $category){ //loop through categories
- if($category->name==$cat_name or $category->slug==$cat_name){ //if category slug or name = $cat_name
- $cat_array[]=$category->cat_ID;
- break; //end loop
- }
- }
- if (count($cat_array)==0)
- {
- echo 'Category ' . $cat_name .' not found';
- exit;
- }
- if (count($files) > 0)
- {
- $c = count($files);
- for ($i=0;$i<$c;$i++)
- {
- echo 'Import: '.$files[$i].' - ' . ($i+1). ' of ' . $c . '<br/>';
- import($files[$i]);
- ob_flush();
- if (Z_DELAY > 0) sleep(Z_DELAY);
- }
- echo 'Done.';
- }
- function import($f)
- {
- global $cat_array;
- $doc = new DOMDocument();
- $doc->strictErrorChecking = false; // ignore invalid HTML, we hope
- $doc->preserveWhiteSpace = false;
- $doc->formatOutput = false; // speed this up
- @$doc->loadHTMLFile($f);
- $xml = @simplexml_import_dom($doc);
- if (!$xml) {
- echo 'Error while parsing the document';
- exit;
- }
- // avoid asXML errors when it encounters character range issues
- libxml_clear_errors();
- libxml_use_internal_errors(false);
- // start building the WP post object to insert
- $my_post = array();
- $title = $xml->xpath('//title');
- $my_post['post_title'] = trim(strip_tags($title[0]));
- $my_post['post_type'] = 'post';
- $my_post['post_date'] = date("Y-m-d H:i:s");
- $my_post['post_date_gmt'] = date("Y-m-d H:i:s");
- $desc = $xml->xpath('//div[@id="desc"]');
- if (isset($desc[0]))
- {
- $my_post['post_content'] = $desc[0]->asXML();
- $my_post['post_excerpt'] = trim(clean_html($desc[0]->asXML()));
- }
- $my_post['post_status'] = 'publish';
- $my_post['post_category'] = (array)$cat_array;
- $post_id = wp_insert_post($my_post);
- // handle errors
- if ( is_wp_error( $post_id ) )
- echo 'Error: '.$post_id.'<br/>';
- if (!$post_id)
- echo "Could not import $f. You should copy its contents manually.<br/>";
- // if no errors, handle custom fields
- $rating = $xml->xpath('//div[@id="rating"]');
- if (isset($rating[0]))
- add_post_meta($post_id, 'rating', (string)$rating[0], true);
- $review = $xml->xpath('//div[@id="review"]');
- if (isset($review[0]))
- add_post_meta($post_id, 'review', (string)$review[0], true);
- $listprice = $xml->xpath('//div[@id="listprice"]');
- if (isset($listprice[0]))
- add_post_meta($post_id, 'listprice', mb_convert_encoding($listprice[0],"ISO-8859-1","UTF-8"), true);
- $price = $xml->xpath('//div[@id="price"]');
- if (isset($price[0]))
- add_post_meta($post_id, 'price', mb_convert_encoding($price[0],"ISO-8859-1","UTF-8"), true);
- $disc = $xml->xpath('//div[@id="disc"]');
- if (isset($disc[0]))
- add_post_meta($post_id, 'disc', (string)$disc[0], true);
- $available = $xml->xpath('//div[@id="available"]');
- if (isset($available[0]))
- add_post_meta($post_id, 'available', (string)$available[0], true);
- $thumb = $xml->xpath('//div[@id="thumb"]');
- if (isset($thumb[0]))
- add_post_meta($post_id, 'thumb', (string)$thumb[0], true);
- $afflink = $xml->xpath('//div[@id="afflink"]');
- if (isset($afflink[0]))
- add_post_meta($post_id, 'afflink', (string)$afflink[0], true);
- $images = $xml->xpath('//div[@class="images"]');
- if (isset($images[0]))
- foreach ($images as $image)
- add_post_meta($post_id, 'images', (string)$image, false);
- $productfeatures = $xml->xpath('//div[@class="productfeatures"]');
- if (isset($productfeatures[0]))
- foreach ($productfeatures as $productfeature)
- add_post_meta($post_id, 'productfeatures', (string)$productfeature, false);
- $customer_review = $xml->xpath('//div[@class="customer_review"]');
- if (isset($customer_review[0]))
- foreach ($customer_review as $rev)
- add_post_meta($post_id, 'customer_review', (string)$rev->asXML(), false);
- }
- function clean_html( $string, $allowtags = NULL, $allowattributes = NULL ) {
- // from: http://us3.php.net/manual/en/function.strip-tags.php#91498
- $string = strip_tags($string,$allowtags);
- if (!is_null($allowattributes)) {
- if(!is_array($allowattributes))
- $allowattributes = explode(",",$allowattributes);
- if(is_array($allowattributes))
- $allowattributes = implode(")(?<!",$allowattributes);
- if (strlen($allowattributes) > 0)
- $allowattributes = "(?<!".$allowattributes.")";
- $string = preg_replace_callback("/<[^>]*>/i",create_function(
- '$matches',
- 'return preg_replace("/ [^ =]*'.$allowattributes.'=(\"[^\"]*\"|\'[^\']*\')/i", "", $matches[0]);'
- ),$string);
- }
- // reduce line breaks and remove empty tags
- $string = str_replace( '\n', ' ', $string );
- $string = preg_replace( "/<[^\/>]*>([\s]?)*<\/[^>]*>/", ' ', $string );
- // get rid of remaining newlines; basic HTML cleanup
- $string = str_replace(' ', ' ', $string);
- $string = ereg_replace("[\n\r]", " ", $string);
- $string = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $string);
- $string = str_replace('<br>', '<br />', $string);
- $string = str_replace('<hr>', '<hr />', $string);
- return $string;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement