Advertisement
Guest User

musicfront

a guest
Jun 7th, 2013
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. <?php
  2. // ------- IMPORTER CONFIG (EDIT) ------------
  3.  
  4. define('Z_CATEGORY','Demo'); // category names
  5. define('Z_DELAY',1); //delay (in second)
  6.  
  7. // ------- IMPORTER CONFIG (DONE EDIT) ------------
  8.  
  9.  
  10. // ------- IMPORTER CONFIG (NO EDIT) ------------
  11. require('../wp-blog-header.php');
  12. set_time_limit(0);
  13.  
  14. global $cat_array;
  15. $cat_array=array();
  16.  
  17. $files = array();
  18. if ($handle = opendir('.')) {
  19. while (false !== ($entry = readdir($handle))) {
  20. $ext = pathinfo($entry, PATHINFO_EXTENSION);
  21. if ($entry != "." && $entry != ".." && $ext == 'html') {
  22. $files[] = $entry;
  23. }
  24. }
  25. closedir($handle);
  26. }
  27.  
  28. $cat_name=Z_CATEGORY;
  29. $categories=get_categories(array ('hide_empty'=> 0)); //get the categories
  30.  
  31. //print_r($categories);
  32. foreach($categories as $category){ //loop through categories
  33. if($category->name==$cat_name or $category->slug==$cat_name){ //if category slug or name = $cat_name
  34. $cat_array[]=$category->cat_ID;
  35. break; //end loop
  36. }
  37. }
  38.  
  39. if (count($cat_array)==0)
  40. {
  41. echo 'Category ' . $cat_name .' not found';
  42. exit;
  43. }
  44.  
  45. if (count($files) > 0)
  46. {
  47. $c = count($files);
  48. for ($i=0;$i<$c;$i++)
  49. {
  50. echo 'Import: '.$files[$i].' - ' . ($i+1). ' of ' . $c . '<br/>';
  51. import($files[$i]);
  52. ob_flush();
  53. if (Z_DELAY > 0) sleep(Z_DELAY);
  54. }
  55. echo 'Done.';
  56. }
  57.  
  58. function import($f)
  59. {
  60. global $cat_array;
  61. $doc = new DOMDocument();
  62. $doc->strictErrorChecking = false; // ignore invalid HTML, we hope
  63. $doc->preserveWhiteSpace = false;
  64. $doc->formatOutput = false; // speed this up
  65.  
  66. @$doc->loadHTMLFile($f);
  67.  
  68. $xml = @simplexml_import_dom($doc);
  69. if (!$xml) {
  70. echo 'Error while parsing the document';
  71. exit;
  72. }
  73. // avoid asXML errors when it encounters character range issues
  74. libxml_clear_errors();
  75. libxml_use_internal_errors(false);
  76.  
  77. // start building the WP post object to insert
  78. $my_post = array();
  79.  
  80. $title = $xml->xpath('//title');
  81. $my_post['post_title'] = trim(strip_tags($title[0]));
  82. $my_post['post_type'] = 'post';
  83.  
  84. $my_post['post_date'] = date("Y-m-d H:i:s");
  85. $my_post['post_date_gmt'] = date("Y-m-d H:i:s");
  86.  
  87. $desc = $xml->xpath('//div[@id="desc"]');
  88. if (isset($desc[0]))
  89. {
  90. $my_post['post_content'] = $desc[0]->asXML();
  91. $my_post['post_excerpt'] = trim(clean_html($desc[0]->asXML()));
  92. }
  93. $my_post['post_status'] = 'publish';
  94. $my_post['post_category'] = (array)$cat_array;
  95.  
  96. $post_id = wp_insert_post($my_post);
  97.  
  98. // handle errors
  99. if ( is_wp_error( $post_id ) )
  100. echo 'Error: '.$post_id.'<br/>';
  101. if (!$post_id)
  102. echo "Could not import $f. You should copy its contents manually.<br/>";
  103.  
  104. // if no errors, handle custom fields
  105. $rating = $xml->xpath('//div[@id="rating"]');
  106. if (isset($rating[0]))
  107. add_post_meta($post_id, 'rating', (string)$rating[0], true);
  108.  
  109. $review = $xml->xpath('//div[@id="review"]');
  110. if (isset($review[0]))
  111. add_post_meta($post_id, 'review', (string)$review[0], true);
  112.  
  113. $listprice = $xml->xpath('//div[@id="listprice"]');
  114. if (isset($listprice[0]))
  115. add_post_meta($post_id, 'listprice', mb_convert_encoding($listprice[0],"ISO-8859-1","UTF-8"), true);
  116.  
  117. $price = $xml->xpath('//div[@id="price"]');
  118. if (isset($price[0]))
  119. add_post_meta($post_id, 'price', mb_convert_encoding($price[0],"ISO-8859-1","UTF-8"), true);
  120.  
  121. $disc = $xml->xpath('//div[@id="disc"]');
  122. if (isset($disc[0]))
  123. add_post_meta($post_id, 'disc', (string)$disc[0], true);
  124.  
  125. $available = $xml->xpath('//div[@id="available"]');
  126. if (isset($available[0]))
  127. add_post_meta($post_id, 'available', (string)$available[0], true);
  128.  
  129. $thumb = $xml->xpath('//div[@id="thumb"]');
  130. if (isset($thumb[0]))
  131. add_post_meta($post_id, 'thumb', (string)$thumb[0], true);
  132.  
  133. $afflink = $xml->xpath('//div[@id="afflink"]');
  134. if (isset($afflink[0]))
  135. add_post_meta($post_id, 'afflink', (string)$afflink[0], true);
  136.  
  137. $images = $xml->xpath('//div[@class="images"]');
  138. if (isset($images[0]))
  139. foreach ($images as $image)
  140. add_post_meta($post_id, 'images', (string)$image, false);
  141.  
  142. $productfeatures = $xml->xpath('//div[@class="productfeatures"]');
  143. if (isset($productfeatures[0]))
  144. foreach ($productfeatures as $productfeature)
  145. add_post_meta($post_id, 'productfeatures', (string)$productfeature, false);
  146.  
  147. $customer_review = $xml->xpath('//div[@class="customer_review"]');
  148. if (isset($customer_review[0]))
  149. foreach ($customer_review as $rev)
  150. add_post_meta($post_id, 'customer_review', (string)$rev->asXML(), false);
  151.  
  152. }
  153.  
  154. function clean_html( $string, $allowtags = NULL, $allowattributes = NULL ) {
  155. // from: http://us3.php.net/manual/en/function.strip-tags.php#91498
  156. $string = strip_tags($string,$allowtags);
  157. if (!is_null($allowattributes)) {
  158. if(!is_array($allowattributes))
  159. $allowattributes = explode(",",$allowattributes);
  160. if(is_array($allowattributes))
  161. $allowattributes = implode(")(?<!",$allowattributes);
  162. if (strlen($allowattributes) > 0)
  163. $allowattributes = "(?<!".$allowattributes.")";
  164. $string = preg_replace_callback("/<[^>]*>/i",create_function(
  165. '$matches',
  166. 'return preg_replace("/ [^ =]*'.$allowattributes.'=(\"[^\"]*\"|\'[^\']*\')/i", "", $matches[0]);'
  167. ),$string);
  168. }
  169. // reduce line breaks and remove empty tags
  170. $string = str_replace( '\n', ' ', $string );
  171. $string = preg_replace( "/<[^\/>]*>([\s]?)*<\/[^>]*>/", ' ', $string );
  172. // get rid of remaining newlines; basic HTML cleanup
  173. $string = str_replace('&#13;', ' ', $string);
  174. $string = ereg_replace("[\n\r]", " ", $string);
  175. $string = preg_replace_callback('|<(/?[A-Z]+)|', create_function('$match', 'return "<" . strtolower($match[1]);'), $string);
  176. $string = str_replace('<br>', '<br />', $string);
  177. $string = str_replace('<hr>', '<hr />', $string);
  178. return $string;
  179. }
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement