<?php
$query_url = basename($_SERVER['REQUEST_URI'], '?' . $_SERVER['QUERY_STRING']);

if ($query_url == 'sitemap.xml') {

	header ("Content-Type:application/xml");
	echo '<?xml version="1.0" encoding="utf-8"?>'; 
	?>
	<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
	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">
	<?php
	
	/* Pages */
	$args = array('posts_per_page'=> -1);
	$pages = get_pages($args);
	
	/* Posts */
	$args = array('posts_per_page'=> -1);
	$posts = get_posts($args);
	
	/* Categories */
	$args = array(
		'hide_empty' => 0,
	); 
	$categories = get_categories($args);
	
	/* Query */
	
	if ($pages) {
		foreach ($pages as $page) {
			$template_file = get_post_meta($page->ID, '_wp_page_template', true);
			if($template_file == 'landing.php' ) continue;
			echo '<url><loc>'.urldecode(get_page_link($page->ID)).'</loc></url>';
		}
	}
	
	if ($posts) {
		foreach ($posts as $post) {
			echo '<url><loc>'.urldecode(get_permalink($post->ID)).'</loc></url>';
		}
	}
	
	if ($categories) {
		foreach ($categories as $category) {
			echo '<url><loc>'.urldecode(get_category_link($category->term_id)).'</loc></url>';
		}
	}
	?>
	</urlset>
<?php
} elseif ($query_url == 'sitemap') {
	
	global $wp, $wpdb;
	$wp->request = 'sitemap';
	
	_wp_admin_bar_init();
	
	get_header();
	?>
	</div> <!-- // top_wrap -->
			
	<div class="grid">
	
		<?php
		$frag = new IMI_Fragment_Cache( 'crb' , strtok( $_SERVER['REQUEST_URI'], '?' ) );
		
		if ( !$frag->output() ) {
		?>
		
			<?php the_breadcrumbs(); ?>
		
			<?php
			
			$frag->store();
		}
		?>
		
		<main id="main_internal" class="category ib full_width"><div class="divin">
			
			<article>
				
				<h1 class="c1"><?php echo __( 'Sitemap', 'profile' ); ?></h1>
				
				<section class="content_part">
				
				<?php
				$top_cats = get_categories(array('hide_empty' => 0, 'orderby' => 'name', 'parent' => 0));
				
				echo '<ul class="sitemap">';
				
				if (@$top_cats) {
					
					$GLOBALS['exclude'] = array();
					
					foreach ($top_cats as $t_c) {
						
						sitemap_print_posts($t_c->term_id, 'category', 'post');
						
					}
					
					unset($GLOBALS['exclude']);
				}
				
				$labs = get_terms( array( 'taxonomy' => 'labs', 'hide_empty' => false ) );
				
				if ( !empty( $labs ) ) {
					
					$GLOBALS['exclude'] = array();
					
					foreach ( $labs as $lab ) {
						sitemap_print_posts( $lab->term_id, 'labs', 'lab' );
					}
					
					unset($GLOBALS['exclude']);
				}
				
				$exclude_landing = '';
				$template = 'landing.php';
				$landing = $wpdb->get_col("SELECT post_id FROM $wpdb->postmeta WHERE meta_key = '_wp_page_template' AND meta_value = '$template'");
				
				foreach ($landing as $l) {
					$exclude_landing .= $l . ',';
				}
				
				wp_list_pages('title_li=&echo=1&exclude='.$exclude_landing);
				
				echo '</ul>';
				?>
					
				</section>
				
			</article>
		
		</div></main>
			
	</div>
	
	<?php
	
	get_footer();
	
} else {
	
	wp_die(__( 'Something went wrong', 'profile' ));
	
}
?>	