apgnsm_generate_sitemap

The Google News Sitemap apgnsm generate sitemap function.

Description

apgnsm_generate_sitemap(); 

Usage

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

Defined (1)

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

/apgnsm.php  
  1. function apgnsm_generate_sitemap() { 
  2. global $apgnsm_sitemap_version, $table_prefix; 
  3. global $wpdb; 
  4.  
  5. $t = $table_prefix; 
  6.  
  7. $apgnsm_news_active = get_option('apgnsm_news_active'); 
  8. $apgnsm_path = get_option('apgnsm_path'); 
  9. //add_option('apgnsm_publication_name', '<publication_name>'); 
  10. $apgnsm_n_name = get_option('apgnsm_n_name'); 
  11. $apgnsm_n_lang = get_option('apgnsm_n_lang'); 
  12. // Genere dei contenuti 
  13. $apgnsm_n_genres = get_option('apgnsm_n_genres'); 
  14. $apgnsm_n_genres_type = get_option('apgnsm_n_genres_type'); 
  15. // Tipo di accesso dell'articolo - Facoltativo 
  16. $apgnsm_n_access = get_option('apgnsm_n_access'); 
  17. $apgnsm_n_access_type = get_option('apgnsm_n_access_type'); 
  18. //add_option('apgnsm_n_access_type', 'Registration'); 
  19. //$apgnsm_n_excludecat = get_option('apgnsm_n_excludecat'); 
  20. $apgnsm_n_excludecatlist = get_option('apgnsm_n_excludecatlist'); 
  21. $apgnsm_n_excludepostlist = get_option('apgnsm_n_excludepostlist'); 
  22.  
  23. $includeMe = ''; 
  24. $includeNoCat = ''; 
  25. $includeNoPost = ''; 
  26. if ( $apgnsm_n_excludecatlist <> NULL ) { 
  27. $exPosts = get_objects_in_term($apgnsm_n_excludecatlist, "category"); 
  28. $includeNoCat = ' AND `ID` NOT IN ('.implode(", ", $exPosts).')'; 
  29. $ceck = implode(", ", $exPosts); 
  30. if ($ceck == '' || $ceck == ' ') $includeNoCat = ''; 
  31. if ($apgnsm_n_excludepostlist != '') { 
  32. $includeNoPost = ' AND `ID` NOT IN ('.$apgnsm_n_excludepostlist.')'; 
  33. $ceck = implode(", ", $exPosts); 
  34. if ($apgnsm_n_excludepostlist == '' || $apgnsm_n_excludepostlist == ' ') $includeNoPost = ''; 
  35.  
  36. $apgnsm_permission = apgnsm_permissions(); 
  37. if ($apgnsm_permission > 2 || (!$apgnsm_active && !$apgnsm_news_active)) return; 
  38.  
  39. //mysql_connect(DB_HOST, DB_USER, DB_PASSWORD); 
  40. //mysql_query("SET NAMES '".DB_CHARSET."'"); 
  41. //mysql_select_db(DB_NAME); 
  42.  
  43. $home = get_option('home') . "/"; 
  44.  
  45. $xml_sitemap_google_news = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; 
  46. $xml_sitemap_google_news .= "\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:n=\"http://www.google.com/schemas/sitemap-news/0.9\"> 
  47. <!-- Generated by Google News Sitemap ".$apgnsm_sitemap_version." --> 
  48. <!-- plugin by Andrea Pernici --> 
  49. <!-- http://andreapernici.com/wordpress/google-news-sitemap/ --> 
  50. <!-- Created ".date("F d, Y, H:i")." -->"; 
  51.  
  52. $posts = $wpdb->get_results("SELECT * FROM ".$wpdb->posts." WHERE `post_status`='publish'  
  53. AND (`post_type`='page' OR `post_type`='post') ". $includeNoCat . ' ' . $includeNoPost." GROUP BY `ID` ORDER BY `post_modified_gmt` DESC");  
  54.  
  55. $now = time(); 
  56. $twoDays = 2*24*60*60; 
  57.  
  58. foreach ($posts as $post) { 
  59. if ($apgnsm_news_active && $apgnsm_permission != 2) { 
  60. $postDate = strtotime($post->post_date); 
  61. if ($now - $postDate < $twoDays) { 
  62. $xml_sitemap_google_news .= " 
  63. <url> 
  64. <loc>".apgnsm_escapexml(get_permalink($post->ID))."</loc> 
  65. <n:news> 
  66. <n:publication> 
  67. <n:name>".$apgnsm_n_name."</n:name> 
  68. <n:language>".$apgnsm_n_lang."</n:language> 
  69. </n:publication>"; 
  70.  
  71. // Se selzionato il genere allora lo aggiungo 
  72. if ($apgnsm_n_genres == true) { 
  73. $xml_sitemap_google_news .= " 
  74. <n:genres>".$apgnsm_n_genres_type."</n:genres>"; 
  75. // Se selzionato il tipo di accesso allora lo aggiungo 
  76. if ($apgnsm_n_access == true) { 
  77. $xml_sitemap_google_news .= " 
  78. <n:access>".$apgnsm_n_access_type."</n:access>"; 
  79. }  
  80.  
  81. $xml_sitemap_google_news .= "  
  82. <n:publication_date>".str_replace(" ", "T", $post->post_date_gmt)."Z"."</n:publication_date> 
  83. <n:title>".htmlspecialchars($post->post_title)."</n:title> 
  84. </n:news> 
  85. </url>"; 
  86.  
  87. $xml_sitemap_google_news .= "\n</urlset>"; 
  88.  
  89.  
  90. if ($apgnsm_news_active && $apgnsm_permission != 2) { 
  91. $fp = fopen(ABSPATH . $apgnsm_path . "sitemap-news.xml", 'w'); 
  92. fwrite($fp, $xml_sitemap_google_news); 
  93. fclose($fp); 
  94.  
  95.  
  96. $apgnsm_last_ping = get_option('apgnsm_last_ping'); 
  97. if ((time() - $apgnsm_last_ping) > 60 * 60) { 
  98. //get_headers("http://www.google.com/webmasters/tools/ping?sitemap=" . urlencode($home . $apgnsm_path . "sitemap.xml")); //PHP5+ 
  99. $fp = @fopen("http://www.google.com/webmasters/tools/ping?sitemap=" . urlencode($home . $apgnsm_path . "sitemap-news.xml"), 80); 
  100. @fclose($fp); 
  101. update_option('apgnsm_last_ping', time());