gglstmp_sitemapcreate

The Google Sitemap by BestWebSoft gglstmp sitemapcreate function.

Description

gglstmp_sitemapcreate(); 

Usage

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

Defined (1)

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

/google-sitemap-plugin.php  
  1. function gglstmp_sitemapcreate() { 
  2. global $wpdb; 
  3.  
  4. $gglstmp_options = get_option( 'gglstmp_options' ); 
  5.  
  6. $taxonomies = array(); 
  7. foreach ( $gglstmp_options['taxonomy'] as $val ) { 
  8. $taxonomies[] = $val; 
  9.  
  10. $xml = new DomDocument( '1.0', 'utf-8' ); 
  11. $home_url = site_url( '/' ); 
  12. $xml_stylesheet_path = ( defined( 'WP_CONTENT_DIR' ) )? $home_url . basename( WP_CONTENT_DIR ) : $home_url . 'wp-content'; 
  13. $xml_stylesheet_path .= ( defined( 'WP_PLUGIN_DIR' ) ) ? '/' . basename( WP_PLUGIN_DIR ) . '/google-sitemap-plugin/sitemap.xsl' : '/plugins/google-sitemap-plugin/sitemap.xsl'; 
  14.  
  15. $xslt = $xml->createProcessingInstruction( 'xml-stylesheet', "type=\"text/xsl\" href=\"$xml_stylesheet_path\"" ); 
  16. $xml->appendChild( $xslt ); 
  17. $gglstmp_urlset = $xml->appendChild( $xml->createElementNS( 'http://www.sitemaps.org/schemas/sitemap/0.9', 'urlset' ) ); 
  18.  
  19. /** add home page */ 
  20. $frontpage_id = get_option( 'page_on_front' ); 
  21. if ( empty( $frontpage_id ) || ! in_array( 'page', $gglstmp_options['post_type'] ) ) { 
  22. $url = $gglstmp_urlset->appendChild( $xml->createElement( 'url' ) ); 
  23. $loc = $url->appendChild( $xml->createElement( 'loc' ) ); 
  24. $loc->appendChild( $xml->createTextNode( home_url( '/' ) ) ); 
  25. $lastmod = $url->appendChild( $xml->createElement( 'lastmod' ) ); 
  26. $lastmod->appendChild( $xml->createTextNode( date( 'Y-m-d\TH:i:sP', time() ) ) ); 
  27. $changefreq = $url->appendChild( $xml->createElement( 'changefreq' ) ); 
  28. $changefreq->appendChild( $xml->createTextNode( 'monthly' ) ); 
  29. $priority = $url->appendChild( $xml->createElement( 'priority' ) ); 
  30. $priority->appendChild( $xml->createTextNode( 1.0 ) ); 
  31. /** getting an array of the excluded post ids of 'forum', 'topic' and 'reply' post types (hidden and private bbPress forum posts) */ 
  32. $excluded_posts_array = $wpdb->get_col( "SELECT `ID` FROM $wpdb->posts WHERE `post_status` IN ('hidden', 'private') AND `post_type` IN ('forum', 'topic', 'reply')" ); 
  33. if ( ! empty( $excluded_posts_array ) ) { 
  34. $excluded_posts_string = implode( ', ', $excluded_posts_array ); 
  35. while ( true ) { 
  36. $hidden_child_array = $wpdb->get_col( "SELECT `ID` FROM $wpdb->posts WHERE `post_status` NOT IN ('hidden', 'private') AND `post_type` IN ('forum', 'topic', 'reply') AND `post_parent` IN ($excluded_posts_string)" ); 
  37. if ( ! empty( $hidden_child_array ) ) { 
  38. $excluded_posts_array = array_merge( $excluded_posts_array, $hidden_child_array ); 
  39. $excluded_posts_string = implode( ', ', $hidden_child_array ); 
  40. } else { 
  41. break 1; 
  42.  
  43. if ( ! empty( $gglstmp_options['post_type'] ) ) { 
  44. $args = array( 
  45. 'posts_per_page' => -1,  
  46. 'exclude' => $excluded_posts_array,  
  47. 'post_type' => $gglstmp_options['post_type'],  
  48. 'post_status' => 'publish' 
  49. );  
  50. $loc = get_posts( $args ); 
  51. if ( ! empty( $loc ) ) { 
  52. foreach ( $loc as $val ) { 
  53. $gglstmp_url = $gglstmp_urlset->appendChild( $xml->createElement( 'url' ) ); 
  54. $loc = $gglstmp_url->appendChild( $xml->createElement( 'loc' ) ); 
  55. $permalink = get_permalink( $val ); 
  56. $loc->appendChild( $xml->createTextNode( $permalink ) ); 
  57. $lastmod = $gglstmp_url->appendChild( $xml->createElement( 'lastmod' ) ); 
  58. $now = $val->post_modified; 
  59. $date = date( 'Y-m-d\TH:i:sP', strtotime( $now ) ); 
  60. $lastmod->appendChild( $xml->createTextNode( $date ) ); 
  61. $changefreq = $gglstmp_url->appendChild( $xml->createElement( 'changefreq' ) ); 
  62. $changefreq->appendChild( $xml->createTextNode( 'monthly' ) ); 
  63. $priority = $gglstmp_url->appendChild( $xml->createElement( 'priority' ) ); 
  64. $priority->appendChild( $xml->createTextNode( 1.0 ) ); 
  65. if ( ! empty( $taxonomies ) ) { 
  66. foreach ( $taxonomies as $value ) { 
  67.  
  68. $terms = get_terms( $value, 'hide_empty=1' ); 
  69.  
  70. if ( ! empty( $terms ) && ! is_wp_error( $terms ) ) { 
  71. foreach ( $terms as $term_value ) { 
  72. $gglstmp_url = $gglstmp_urlset->appendChild( $xml->createElement( 'url' ) ); 
  73. $loc = $gglstmp_url->appendChild( $xml->createElement( 'loc' ) ); 
  74. $permalink = get_term_link( (int)$term_value->term_id, $value ); 
  75. $loc->appendChild( $xml->createTextNode( $permalink ) ); 
  76. $lastmod = $gglstmp_url->appendChild( $xml->createElement( 'lastmod' ) ); 
  77.  
  78. $now = $wpdb->get_var( "SELECT `post_modified` FROM $wpdb->posts, $wpdb->term_relationships WHERE `post_status` = 'publish' AND `term_taxonomy_id` = " . $term_value->term_taxonomy_id . " AND $wpdb->posts.ID= $wpdb->term_relationships.object_id ORDER BY `post_modified` DESC" ); 
  79. $date = date( 'Y-m-d\TH:i:sP', strtotime( $now ) ); 
  80. $lastmod->appendChild( $xml->createTextNode( $date ) ); 
  81. $changefreq = $gglstmp_url -> appendChild( $xml->createElement( 'changefreq' ) ); 
  82. $changefreq->appendChild( $xml->createTextNode( 'monthly' ) ); 
  83. $priority = $gglstmp_url->appendChild( $xml->createElement( 'priority' ) ); 
  84. $priority->appendChild( $xml->createTextNode( 1.0 ) ); 
  85.  
  86. $xml->formatOutput = true; 
  87.  
  88. if ( ! is_writable( ABSPATH ) ) 
  89. @chmod( ABSPATH, 0755 ); 
  90.  
  91. if ( is_multisite() ) { 
  92. $home_url = preg_replace( "/[^a-zA-Z*-**-*0-9\s]/", "_", str_replace( 'http://', '', str_replace( 'https://', '', site_url() ) ) ); 
  93. $xml->save( ABSPATH . 'sitemap_' . $home_url . '.xml' ); 
  94. } else { 
  95. $xml->save( ABSPATH . 'sitemap.xml' ); 
  96.  
  97. if ( is_multisite() ) { 
  98. $home_url = preg_replace( "/[^a-zA-Z*-**-*0-9\s]/", "_", str_replace( 'http://', '', str_replace( 'https://', '', site_url() ) ) ); 
  99. $xml_file = 'sitemap_' . $home_url . '.xml'; 
  100. } else { 
  101. $xml_file = 'sitemap.xml'; 
  102.  
  103. $xml_path = ABSPATH . $xml_file; 
  104. $xml_url = site_url( '/' ) . $xml_file; 
  105. if ( file_exists( $xml_path ) ) { 
  106. $gglstmp_options['sitemap'] = array( 
  107. 'file' => $xml_file,  
  108. 'path' => $xml_path,  
  109. 'loc' => $xml_url,  
  110. 'lastmod' => date( 'Y-m-d\TH:i:sP', filemtime( $xml_path ) ) 
  111. ); 
  112. update_option( 'gglstmp_options', $gglstmp_options );