apgmxs_generate_sitemap

The Google Monthly XML Sitemap apgmxs generate sitemap function.

Description

apgmxs_generate_sitemap(); 

Usage

  1. if ( !function_exists( 'apgmxs_generate_sitemap' ) ) { 
  2. require_once ABSPATH . PLUGINDIR . 'google-monthly-xml-sitemap/monthly-xml-sitemap.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = apgmxs_generate_sitemap(); 
  7.  

Defined (1)

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

/monthly-xml-sitemap.php  
  1. function apgmxs_generate_sitemap() { 
  2. global $apgmxs_sitemap_version, $table_prefix; 
  3. global $wpdb; 
  4.  
  5. $t = $table_prefix; 
  6.  
  7. $apgmxs_news_active = get_option('apgmxs_news_active'); 
  8. $apgmxs_path = get_option('apgmxs_path'); 
  9. $apgmxs_urls = get_option('apgmxs_urls'); 
  10. $apgmxs_excludecatlist = get_option('apgmxs_excludecatlist'); 
  11. $apgmxs_excludepostlist = get_option('apgmxs_excludepostlist'); 
  12.  
  13. $includeMe = ''; 
  14. $includeNoCat = ''; 
  15. $includeNoPost = ''; 
  16. if ( apgmxs_excludecatlist <> NULL ) { 
  17. $exPosts = get_objects_in_term($apgmxs_excludecatlist, "category"); 
  18. $includeNoCat = ' AND `ID` NOT IN ('.implode(", ", $exPosts).')'; 
  19. $ceck = implode(", ", $exPosts); 
  20. if ($ceck == '' || $ceck == ' ') $includeNoCat = ''; 
  21. if ($apgmxs_excludepostlist != '') { 
  22. $includeNoPost = ' AND `ID` NOT IN ('.$apgmxs_excludepostlist.')'; 
  23. $ceck = implode(", ", $exPosts); 
  24. if ($apgmxs_excludepostlist == '' || $apgmxs_excludepostlist == ' ') $includeNoPost = ''; 
  25.  
  26. $apgmxs_permission = apgmxs_permissions(); 
  27. if ($apgmxs_permission > 2 || (!$apgmxs_active && !$apgmxs_news_active)) return; 
  28.  
  29. $home = get_option('home') . "/"; 
  30.  
  31. $xml_sitemap_google_monthly = '<?xml version="1.0" encoding="UTF-8"?>'; 
  32. $xml_sitemap_google_monthly .= ' 
  33. <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> 
  34. <!-- Generated by Google Monthly XML Sitemap '.$apgmxs_sitemap_version.' --> 
  35. <!-- plugin by Andrea Pernici --> 
  36. <!-- http://andreapernici.com/wordpress/google-monthly-xml-sitemap/ --> 
  37. <!-- Created '.date("F d, Y, H:i").' -->'; 
  38.  
  39. $posts = $wpdb->get_results("SELECT * FROM ".$wpdb->posts." WHERE `post_status`='publish'  
  40. AND (`post_type`='page' OR `post_type`='post') ". $includeNoCat . ' ' . $includeNoPost." GROUP BY `ID` ORDER BY `post_date` DESC");  
  41.  
  42. $now = date("Y-m-d"); 
  43.  
  44. $actualmonth = date("m"); 
  45. $actualyear = date("Y"); 
  46. $toprependurl = ''; 
  47. // To generate only the last month sitemap 
  48. $apgmxs_flag=0; 
  49. // This loop assumes that the posts are ordered 
  50. foreach ($posts as $post) { 
  51. if ($apgmxs_news_active && $apgmxs_permission != 2) { 
  52. $postDate = strtotime($post->post_date); 
  53. $postmonth = mysql2date('m', $post->post_date); 
  54. $postyear = mysql2date('Y', $post->post_date); 
  55. //echo 'postlmonth'.$postmonth.'<br/>'; 
  56. //echo 'actualmonth'.$actualmonth.'<br/>'; 
  57. if ($postmonth == $actualmonth && $actualyear == $postyear ) { 
  58. $xml_sitemap_urls .= $toprependurl." 
  59. <url> 
  60. <loc>".apgmxs_escapexml(get_permalink($post->ID))."</loc> 
  61. <lastmod>".mysql2date('Y-m-d', $post->post_date)."</lastmod> 
  62. </url>"; 
  63. $toprependurl = ''; 
  64. } else { 
  65. $toprependurl = " 
  66. <url> 
  67. <loc>".apgmxs_escapexml(get_permalink($post->ID))."</loc> 
  68. <lastmod>".mysql2date('Y-m-d', $post->post_date)."</lastmod> 
  69. </url>"; 
  70. $filename = $apgmxs_path . "/sitemap-ap-".$actualyear."-".$actualmonth.".xml"; 
  71. if (!file_exists($filename) || ( $apgmxs_flag==0 )) { 
  72. $fp = fopen($filename, 'w'); 
  73. $steppedsitemap = $xml_sitemap_google_monthly . $xml_sitemap_urls . " 
  74. </urlset>"; 
  75. fwrite($fp, $steppedsitemap); 
  76. fclose($fp); 
  77. } else {echo 'Execution Stopped at this point: '.$filename.'<br />You Have probably some issue with your post_date that is set to 0000-00-00 - Please Check'; break;} 
  78. $actualmonth = $postmonth; 
  79. $actualyear = $postyear; 
  80. $xml_sitemap_urls = ''; 
  81. $steppedsitemap = ''; 
  82. $apgmxs_flag=1;  
  83.  
  84.  
  85. // Get all the generated monthly Sitemaps and generate the sitemap Index. 
  86. $apgmxs_file_list = apgmxs_getDirectoryList($apgmxs_path); 
  87. $apgmxs_j=0; 
  88. while ($apgmxs_file_list[$apgmxs_j]) {  
  89. if ($apgmxs_file_list[$apgmxs_j] == 'sitemap-ap-monthly-index.xml') {  
  90. $apgmxs_j++; 
  91. } else { 
  92. $apgmxs_sitemap_index_content .= '<sitemap> 
  93. <loc>'.$apgmxs_urls.'/'.$apgmxs_file_list[$apgmxs_j].'</loc> 
  94. <lastmod>'.$now.'</lastmod> 
  95. </sitemap>'; 
  96. $apgmxs_j++; 
  97.  
  98.  
  99. $apgmxs_sitemap_index_start = '<?xml version="1.0" encoding="UTF-8"?> 
  100. <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'; 
  101. $apgmxs_sitemap_index_end = ' 
  102. </sitemapindex>'; 
  103.  
  104. if ($apgmxs_news_active && $apgmxs_permission != 2) { 
  105. $fp = fopen($apgmxs_path . "/sitemap-ap-monthly-index.xml", 'w'); 
  106. fwrite($fp, $apgmxs_sitemap_index_start.$apgmxs_sitemap_index_content.$apgmxs_sitemap_index_end); 
  107. fclose($fp); 
  108.  
  109. // Ping the sitemap index to search engine 
  110. $apgmxs_last_ping = get_option('apgmxs_last_ping'); 
  111. if ((time() - $apgmxs_last_ping) > 60 * 60) { 
  112. //get_headers("http://www.google.com/webmasters/tools/ping?sitemap=" . urlencode($home . $apgmxs_path . "sitemap.xml")); //PHP5+ 
  113. $fp = @fopen("http://www.google.com/webmasters/tools/ping?sitemap=" . urlencode($apgmxs_urls. "/sitemap-ap-monthly-index.xml"), 80); 
  114. @fclose($fp); 
  115. $fp = @fopen("http://www.bing.com/webmaster/ping.aspx?siteMap=". urlencode($apgmxs_urls. "/sitemap-ap-monthly-index.xml"), 80); 
  116. @fclose($fp); 
  117. update_option('apgmxs_last_ping', time());