<?php
/**
 * @package 	WordPress
 * @subpackage 	Welfare
 * @version		1.1.1
 * 
 * Breadcrumbs Function
 * Created by CMSMasters
 * 
 */


function breadcrumbs() {
	global $post;
	
	
	$homeLink = home_url();
	
	$homeText = esc_attr__('Home', 'welfare');
	
	$sep = "\n\t" . '<span class="breadcrumbs_sep"> / </span>' . "\n\t";
	
	
	$year_format = get_the_time('Y');
	
	$month_format = get_the_time('F');
	
	$month_number_format = get_the_time('n');
	
	$day_format = get_the_time('d');
	
	$day_full_format = get_the_time('l');
	
	
	$url_year = get_year_link($year_format);
	
	$url_month = get_month_link($year_format, $month_number_format);
	
	
	echo '<a href="' . esc_url($homeLink) . '" class="cms_home">' . $homeText . '</a>' . $sep;
	
	
	if (is_single()) {
		$category = get_the_category();
		
		$num_cat = count($category);
		
		
		if ($num_cat < 1) {
			echo '<span>' . cmsms_title(get_the_ID(), false) . '</span>';
		} else if ($num_cat >= 1) {
			echo get_category_parents($category[0], true, $sep) . ' <span>' . cmsms_title(get_the_ID(), false) . '</span>';
		}
	} elseif (is_category()) {
		global $cat;
		
		
		$multiple_cats = get_category_parents($cat, true, $sep);
		
		$multiple_cats_array = explode($sep, $multiple_cats);
		
		$multiple_cats_num = count($multiple_cats_array);
		
		
		$i = 2;
		
		
		foreach ($multiple_cats_array as $single_cat) {
			echo $single_cat;
			
			
			if ($i < $multiple_cats_num) {
				echo $sep;
			}
			
			
			$i++;
		}
	} elseif (is_tag()) {
		echo '<span>' . single_tag_title('', false) . '</span>';
	} elseif (is_day()) {
		echo '<a href="' . esc_url($url_year) . '">' . $year_format . '</a>' . 
			$sep . 
			'<a href="' . esc_url($url_month) . '">' . $month_format . '</a>' . 
			$sep . 
			'<span>' . $day_format . ' (' . $day_full_format . ')</span>';
	} elseif (is_month()) {
		echo '<a href="' . esc_url($url_year) . '">' . $year_format . '</a>' . $sep . '<span>' . $month_format . '</span>';
	} elseif (is_year()) {
		echo '<span>' . $year_format . '</span>';
	} elseif (is_search()) {
		echo '<span>' . esc_html__('Search results for', 'welfare') . ": '" . get_search_query() . "'</span>";
	} elseif (is_page() && !$post->post_parent) {
		echo '<span>' . cmsms_title(get_the_ID(), false) . '</span>';
	} elseif (is_page() && $post->post_parent) {
		$post_array = get_post_ancestors($post);
		
		
		krsort($post_array);
		
		
		foreach ($post_array as $key => $postid) {
			$post_ids = get_post($postid);
			
			$title = $post_ids->post_title;
			
			
			echo '<a href="' . esc_url(get_permalink($post_ids)) . '">' . $title . '</a>' . $sep;
		}
		
		
		echo '<span>' . cmsms_title(get_the_ID(), false) . '</span>';
	} elseif (is_author()) {
		echo '<span>' . get_the_author() . '</span>';
	} elseif (is_tax('post_format')) {
		if (is_tax('post_format', 'post-format-aside')) {
			echo '<span>' . _x('Asides', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-gallery')) {
			echo '<span>' . _x('Galleries', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-image')) {
			echo '<span>' . _x('Images', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-video')) {
			echo '<span>' . _x('Videos', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-quote')) {
			echo '<span>' . _x('Quotes', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-link')) {
			echo '<span>' . _x('Links', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-status')) {
			echo '<span>' . _x('Statuses', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-audio')) {
			echo '<span>' . _x('Audio', 'post format archive title', 'welfare') . '</span>';
		} elseif (is_tax('post_format', 'post-format-chat')) {
			echo '<span>' . _x('Chats', 'post format archive title', 'welfare') . '</span>';
		}
	} elseif (is_post_type_archive()) {
		echo '<span>' . post_type_archive_title('', false) . '</span>';
	} elseif (is_tax()) {
		echo '<span>' . single_term_title('', false) . '</span>';
	} else {
		echo '<span>' . esc_html__('No breadcrumbs', 'welfare') . '</span>';
	}
}

