wccs_generate_sitemap_function

The Woocommerce Category based sitemaps wccs generate sitemap function function.

Description

wccs_generate_sitemap_function(); 

Usage

  1. if ( !function_exists( 'wccs_generate_sitemap_function' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'woocommerce-category-based-sitemaps/includes/wccs-functions.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wccs_generate_sitemap_function(); 
  7.  

Defined (1)

The function is defined in the following location(s).

/includes/wccs-functions.php  
  1. function wccs_generate_sitemap_function() { 
  2.  
  3.  
  4. global $product; 
  5.  
  6.  
  7.  
  8. $args = array(  
  9. 'orderby' => 'name',  
  10. 'order' => 'ASC',  
  11. 'hide_empty' => true,  
  12. 'number' => '',  
  13. 'fields' => 'all',  
  14. 'slug' => '',  
  15. 'parent' => '',  
  16. 'hierarchical' => true,  
  17. 'child_of' => 0,  
  18. 'get' => '',  
  19. 'name__like' => '',  
  20. 'pad_counts' => false,  
  21. 'offset' => '',  
  22. 'search' => '',  
  23. 'cache_domain' => 'core' ); 
  24.  
  25. $product_categories = get_terms( 'product_cat', $args ); 
  26. global $wccs_options; 
  27. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) { 
  28. $url=''.site_url().'/'.$wccs_options['foldername'].''; 
  29. } else { 
  30. $url=site_url(); 
  31.  
  32. $categorysitemapindex = '<?xml version="1.0" encoding="UTF-8"?>' . "\n";  
  33.  
  34. $categorysitemapindex .= '<?xml-stylesheet type="text/xsl" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/woocommerce-catsitemap/sitemapindex.xsl"?>' . "\n"; 
  35.  
  36. $categorysitemapindex .= '<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";  
  37.  
  38.  
  39.  
  40. foreach ($product_categories as $cat ) {  
  41.  
  42.  
  43.  
  44. $args = array( 'post_type' => 'product', 'product_cat' => $cat->name ); 
  45.  
  46. $products=get_posts( $args );  
  47. $eachcategorysitemap ='<?xml version="1.0" encoding="UTF-8"?>' . "\n";  
  48. $eachcategorysitemap .= '<?xml-stylesheet type="text/xsl" href="' . get_bloginfo('wpurl') . '/wp-content/plugins/woocommerce-catsitemap/urlset.xsl"?>' . "\n"; 
  49. $eachcategorysitemap .='<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; 
  50.  
  51. foreach ($products as $product) { 
  52. $productid=$product->ID; 
  53.  
  54. $sitemap_tab_options = array( 
  55. 'priority' => get_post_meta($productid, '_sitemappriority', true),  
  56. 'change' => get_post_meta($productid, '_sitemapchange', true),  
  57. 'exclude' => get_post_meta($productid, '_sitemapexclude', true) 
  58. ); 
  59.  
  60. if ($sitemap_tab_options['priority'] != '') { 
  61. $changefrequency=$sitemap_tab_options['change']; 
  62. } else {  
  63. $t_id = $cat->term_id; 
  64. $cat_meta = get_option( "category_$t_id"); 
  65.  
  66.  
  67. if ($cat_meta['change'] != '') { 
  68. $changefrequency=$cat_meta['change']; 
  69. } else { 
  70. global $wccs_options; 
  71. if ($wccs_options['productchange'] != '') { 
  72. $changefrequency=$wccs_options['productchange']; 
  73. } else { 
  74. $changefrequency='monthly'; 
  75.  
  76. if ($sitemap_tab_options['priority'] != '') { 
  77. $priority=$sitemap_tab_options['priority']; 
  78. } else {  
  79. $t_id = $cat->term_id; 
  80. $cat_meta = get_option( "category_$t_id"); 
  81.  
  82.  
  83. if ($cat_meta['priority'] != '') { 
  84. $priority=$cat_meta['priority']; 
  85. } else { 
  86. global $wccs_options; 
  87. if ($wccs_options['productpriority'] != '') { 
  88. $priority=$wccs_options['productpriority']; 
  89. } else { 
  90. $priority='0.5'; 
  91.  
  92. if ($sitemap_tab_options['exclude'] != 'yes') { 
  93.  
  94. $eachcategorysitemap .= "<url>\n"; 
  95. $eachcategorysitemap .= '<loc>'.get_permalink( $productid ).'</loc>\n';  
  96. $eachcategorysitemap .= "<lastmod>$product->post_modified_gmt</lastmod>\n";  
  97. $eachcategorysitemap .= "<changefreq>$changefrequency</changefreq>\n";  
  98. $eachcategorysitemap .= "<priority>$priority</priority>\n";  
  99. $eachcategorysitemap .= "</url>\n"; 
  100.  
  101.  
  102. $eachcategorysitemap.='</urlset>';  
  103. global $wccs_options; 
  104.  
  105. if ((isset($wccs_options['gzip'])) && (!isset($wccs_options['both']))) { 
  106.  
  107. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) { 
  108. $eachcatesitemapurl=''.ABSPATH.''.$wccs_options['foldername'].'/sitemap_'.$cat->name.'.xml'; 
  109. } else { 
  110. $eachcatesitemapurl=''.ABSPATH.'sitemap_'.$cat->name.'.xml'; 
  111.  
  112. $fp2 = gzopen(''.$eachcatesitemapurl.'.gz', 'w9'); 
  113. gzwrite ($fp2, $eachcategorysitemap); 
  114. gzclose($fp2); 
  115. $sitemapurl="$url/sitemap_$cat->name.xml.gz"; 
  116.  
  117. } elseif (isset($wccs_options['both'])) { 
  118.  
  119. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) { 
  120. $eachcatesitemapurl=''.ABSPATH.''.$wccs_options['foldername'].'/sitemap_'.$cat->name.'.xml'; 
  121. } else { 
  122. $eachcatesitemapurl=''.ABSPATH.'sitemap_'.$cat->name.'.xml'; 
  123.  
  124.  
  125. $fp1= fopen($eachcatesitemapurl, 'w');  
  126. fwrite($fp1, $eachcategorysitemap); 
  127. fclose($fp1); 
  128.  
  129. $fp2 = gzopen(''.$eachcatesitemapurl.'.gz', 'w9'); 
  130. gzwrite ($fp2, $eachcategorysitemap); 
  131. gzclose($fp2); 
  132.  
  133. switch($wccs_options['type']) { 
  134. case 01: 
  135. $sitemapurl="$url/sitemap_$cat->name.xml"; 
  136. break; 
  137.  
  138. case 02: 
  139. $sitemapurl="$url/sitemap_$cat->name.xml.gz"; 
  140. break; 
  141.  
  142. case 03: 
  143. $t_id = $cat->term_id; 
  144. $cat_meta = get_option( "category_$t_id"); 
  145. $catsitemaptype=$cat_meta['sitemaptype']; 
  146. switch($catsitemaptype) { 
  147. case 01: 
  148. $sitemapurl="$url/sitemap_$cat->name.xml"; 
  149. break; 
  150.  
  151. case 02: 
  152. $sitemapurl="$url/sitemap_$cat->name.xml.gz"; 
  153. break; 
  154.  
  155. default: 
  156. $sitemapurl="$url/sitemap_$cat->name.xml"; 
  157. break; 
  158.  
  159. default: 
  160. $sitemapurl="$url/sitemap_$cat->name.xml"; 
  161.  
  162. } else {  
  163.  
  164. global $wccs_options; 
  165. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) { 
  166. $eachcatesitemapurl=''.ABSPATH.''.$wccs_options['foldername'].'/sitemap_'.$cat->name.'.xml'; 
  167. } else { 
  168. $eachcatesitemapurl=''.ABSPATH.'sitemap_'.$cat->name.'.xml'; 
  169.  
  170. $fp1= fopen($eachcatesitemapurl, 'w');  
  171. fwrite($fp1, $eachcategorysitemap); 
  172. fclose($fp1); 
  173. $sitemapurl="$url/sitemap_$cat->name.xml"; 
  174.  
  175.  
  176.  
  177.  
  178.  
  179.  
  180.  
  181.  
  182. $args = array( 'post_type' => 'product', 'product_cat' => $cat->name , 'orderby' => 'modified' ); 
  183.  
  184. $query = new WP_Query( $args ); 
  185. $date = ''; 
  186. if ( $query->have_posts() ) 
  187. $date = $query->posts[0]->post_modified_gmt; 
  188. $date = date( 'c', strtotime( $date ) ); 
  189. $t_id = $cat->term_id; 
  190. $cat_meta = get_option( "category_$t_id"); 
  191. $catsitemaptype=$cat_meta['exclude']; 
  192.  
  193. if ($catsitemaptype != "no") { 
  194. $categorysitemapindex .= "<sitemap>\n"; 
  195. $categorysitemapindex .= "<loc>$sitemapurl</loc>\n";  
  196. $categorysitemapindex .= "<lastmod>" . htmlspecialchars( $date ) . "</lastmod>\n";  
  197. $categorysitemapindex .= "</sitemap>\n"; 
  198. }  
  199.  
  200.  
  201. $categorysitemapindex .= '</sitemapindex>'; 
  202. global $wccs_options; 
  203. if ( isset($wccs_options['xmltitle']) && $wccs_options['xmltitlename'] !='' ) 
  204. {  
  205. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) 
  206. {  
  207.  
  208.  
  209. $indexsitemapname=''.$wccs_options['foldername'].'/'.$wccs_options['xmltitlename'].'.xml';  
  210. } else { 
  211. $indexsitemapname=''.$wccs_options['xmltitlename'].'.xml';  
  212.  
  213. } else { 
  214. if ( isset($wccs_options['folder']) && $wccs_options['foldername'] !='' ) 
  215. {  
  216.  
  217. $indexsitemapname =''.$wccs_options['foldername'].'/product_category_index.xml'; 
  218. } else { 
  219. $indexsitemapname ='product_category_index.xml'; 
  220.  
  221.  
  222.  
  223.  
  224. $fp2 = fopen(ABSPATH . $indexsitemapname, 'w'); 
  225.  
  226.  
  227. fwrite($fp2, $categorysitemapindex);  
  228. fclose($fp2 );  
  229. $sitemapindexurl=''.site_url().'/'.$indexsitemapname.''; 
  230. $currenttime = current_time( 'mysql' ); 
  231. update_option('wccs_sitemap_url', $sitemapindexurl); 
  232. update_option('wccs_sitemap_generation_time', $currenttime); 
  233.  
  234.  
  235.  
  236.