<?php
/*
 Plugin Name: GUAR Google XML Sitemaps
 Plugin URI: http://www.getupandrunning.net/2010/06/adding-custom-content-types-to-the-sitemap/
 Description: This plugin will add your custom content types to the sitemap generated by the Google XML Sitemaps  plugin created by Arne Brachhold @ http://www.arnebrachhold.de/
 Version: 1.2
 Author: Mark Sharp
 Author URI: http://www.getupandrunning.net/
 Text Domain: sitemap
 Domain Path: /lang/
 
*/

/*
 No Options, No Admin, just add the function to the original plugin's action: sm_buildmap
 
*/

function guar_add_custom_post_types_to_google_sitemap()
{
    global $wpdb;
    
	// get an instance of the GoogleSitemapGenerator - if it's not there, just quit.
	$generatorObject = &GoogleSitemapGenerator::GetInstance(); //Please note the "&" sign for PHP4!
	if($generatorObject==null)
    {
		return;
    }
	
	// this is going to ape the original priority settings without getting too involved
	$comments = $generatorObject->GetComments();
	$totalcomments = $generatorObject->GetCommentCount($comments);
	
	// just use the priority set for posts
	$default_prio 	= $generatorObject->GetOption('pr_posts');
	$minPrio		= $generatorObject->GetOption('pr_posts_min');
	
	// get the change frequency for posts
	$cf_posts = $generatorObject->GetOption('cf_posts');

	// Get all the excluded post IDs from both explicit IDs and posts in specified categories
	// Note: the only taxonomy tested for exclusion is "category". Posts needing to be excluded in other taxonomies need to be done by postID.
	$excludes		= $generatorObject->GetOption('b_exclude');
	$excludedCat 	= get_objects_in_term($generatorObject->GetOption('b_exclude_cats'),"category");
    $excludes		= array_merge($excludes,$excludedCat);
	$excludes		= array_unique($excludes);

	// get all the available post types
	$posttypes = get_post_types();
	
	// filter out posts and pages and other assorted post_types we're not interested in
    $posttypes = guar_filter_posttypes($posttypes);
	
	// a filter hook so you can add or remove post types you want
    $posttypes = apply_filters('guar_sitemap_posttype_filter',$posttypes);
	
    if(count($posttypes>0))
	{
		// build the query
		$sql="SELECT `ID`, `post_author`, `post_date`, `post_date_gmt`, `post_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `post_type`  FROM `" . $wpdb->posts . "` WHERE ";
		$where = " (";
		foreach($posttypes as $pt)
		{
			$where .= " post_type='{$pt}' OR ";
		}
		$where = substr($where,0,strlen($where)-3);
		$where .= ") AND post_status='publish' ";
		$where.=" AND post_password='' ORDER BY post_modified DESC";
		$sql .= $where;
		
		// do the query
		$postRes = $wpdb->get_results($sql);
		
		if($postRes) {
			foreach($postRes as $post) {
				// check excludes
				if(in_array($post->ID,$excludes)) {
					continue;
				}
				
				$permalink = get_permalink($post->ID);
				// priority is a function of the post's comment count against the total comment count
				$cmtcnt = (isset($comments[$post->ID])?$comments[$post->ID]:0);
				$prio = guar_GetPostPriority($totalcomments, $cmtcnt);
				// make sure the minimum is set - again this is just using the default for post
				if($prio<$minPrio) $prio = $minPrio;
				
				// add the url to the sitemap object
				$generatorObject->AddUrl($permalink,$generatorObject->GetTimestampFromMySql(($post->post_modified_gmt && $post->post_modified_gmt!='0000-00-00 00:00:00'?$post->post_modified_gmt:$post->post_date_gmt)),$cf_posts,$prio);
			}
		}
		
	}
}

function guar_filter_posttypes($posttypes)
{
    foreach($posttypes as $key => $val)
    {
		// since the post must have a publish status, attachment, revision and nav_menu_item wouldn't show anyway
        if($val=='page'||$val=='post'||$val=='attachment'||$val=='revision'||$val=='nav_menu_item')
        {
            unset($posttypes[$key]);
        }
    }
    return $posttypes;
}

function guar_GetPostPriority($totalcomments, $commentcount = 0)
{
	
	$prio=0;
	if($totalcomments>0 && $commentcount>0) {
		$prio = round(($commentcount*100/$totalcomments)/100,1);
	} else {
		$prio = 0;
	}
	return $prio;
}

// this tells the sitemap generator that we're going to do some more work before the sitemap is generated
add_action("sm_buildmap","guar_add_custom_post_types_to_google_sitemap");
