<?php
/**
 * Custom functions that act independently of the theme templates.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package Cleanead
 */


/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function cleanead_body_classes( $classes ) {
	
	// Get Theme Options from Database
	$theme_options = cleanead_theme_options();
		
	// Switch Sidebar Layout to left
	if ( 'left-sidebar' == $theme_options['layout'] ) {
		$classes[] = 'sidebar-left';
	}
	
	// Add Sticky Header class
	if ( true == $theme_options['sticky_header'] ) {
		$classes[] = 'sticky-header';
	}
	
	// Add Small Post Layout class
	if ( ( is_archive() or is_home() ) and 'left' == $theme_options['post_layout_archives'] ) {
		$classes[] = 'post-layout-small';
	}

	return $classes;
}
add_filter( 'body_class', 'cleanead_body_classes' );

if ( ! function_exists( 'cleanead_breadcrumb' ) ) {
/*
|------------------------------------------------------------------------------
| Default Theme Breadcrumb
|------------------------------------------------------------------------------
| 
| @return void
|
*/

	function cleanead_breadcrumb() {
		$showOnHome = 0; // 1 - show breadcrumbs on the homepage, 0 - don't show
		$delimiter = '/'; // delimiter between crumbs
		$home = __('Home', 'cleanead'); // text for the 'Home' link
		$showCurrent = 1; // 1 - show current post/page title in breadcrumbs, 0 - don't show
		$before = '<span class="current">'; // tag before the current crumb
		$after = '</span>'; // tag after the current crumb
	 
	  global $post;
	  $homeLink = esc_url( home_url() );
	 
	  if (is_home() || is_front_page()) {
	 
	    if ($showOnHome == 1) echo '<div class="cleanead-breadcrumb"><a href="' . $homeLink . '">' . $home . '</a></div>';
	 
	  } else {
	 
	    echo '<div class="cleanead-breadcrumb"><a href="' . $homeLink . '">' . $home . '</a> ' . $delimiter . ' ';
	 
	    if ( is_category() ) {
	      $thisCat = get_category(get_query_var('cat'), false);
	      if ($thisCat->parent != 0) echo get_category_parents($thisCat->parent, TRUE, ' ' . $delimiter . ' ');
	      echo $before . __('Archive by category "', 'cleanead') . single_cat_title('', false) . '"' . $after;
	 
	    } elseif ( is_search() ) {
	      echo $before . __('Search results for "', 'cleanead') . get_search_query() . '"' . $after;
	 
	    } elseif ( is_day() ) {
	      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
	      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
	      echo $before . get_the_time('d') . $after;
	 
	    } elseif ( is_month() ) {
	      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
	      echo $before . get_the_time('F') . $after;
	 
	    } elseif ( is_year() ) {
	      echo $before . get_the_time('Y') . $after;
	 
	    } elseif ( is_single() && !is_attachment() ) {
	      if ( get_post_type() != 'post' ) {
	        $post_type = get_post_type_object(get_post_type());
	        $slug = $post_type->rewrite;
	        echo '<a href="' . $homeLink . '/' . $slug['slug'] . '/">' . $post_type->labels->singular_name . '</a>';
	        if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
	      } else {
	        $cat = get_the_category(); $cat = $cat[0];
	        $cats = get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
	        if ($showCurrent == 0) $cats = preg_replace("#^(.+)\s$delimiter\s$#", "$1", $cats);
	        echo $cats;
	        if ($showCurrent == 1) echo $before . get_the_title() . $after;
	      }
	 
	    } elseif ( !is_single() && !is_page() && get_post_type() != 'post' && !is_404() ) {
	      $post_type = get_post_type_object(get_post_type());
	      echo $before . $post_type->labels->singular_name . $after;
	 
	    } elseif ( is_attachment() ) {
	      $parent = get_post($post->post_parent);
	      $cat = get_the_category($parent->ID); $cat = $cat[0];
	      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
	      echo '<a href="' . get_permalink($parent) . '">' . $parent->post_title . '</a>';
	      if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
	 
	    } elseif ( is_page() && !$post->post_parent ) {
	      if ($showCurrent == 1) echo $before . get_the_title() . $after;
	 
	    } elseif ( is_page() && $post->post_parent ) {
	      $parent_id  = $post->post_parent;
	      $breadcrumbs = array();
	      while ($parent_id) {
	        $page = get_page($parent_id);
	        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
	        $parent_id  = $page->post_parent;
	      }
	      $breadcrumbs = array_reverse($breadcrumbs);
	      for ($i = 0; $i < count($breadcrumbs); $i++) {
	        echo $breadcrumbs[$i];
	        if ($i != count($breadcrumbs)-1) echo ' ' . $delimiter . ' ';
	      }
	      if ($showCurrent == 1) echo ' ' . $delimiter . ' ' . $before . get_the_title() . $after;
	 
	    } elseif ( is_tag() ) {
	      echo $before . __( 'Posts tagged "', 'cleanead' ) . single_tag_title('', false) . '"' . $after;
	 
	    } elseif ( is_author() ) {
	       global $author;
	      $userdata = get_userdata($author);
	      echo $before . __( 'Articles posted by ', 'cleanead' ) . $userdata->display_name . $after;
	 
	    } elseif ( is_404() ) {
	      echo $before . __( 'Error 404', 'cleanead' ) . $after;
	    }
	 
	    if ( get_query_var('paged') ) {
	      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
	      echo __( 'Page', 'cleanead' ) . ' ' . get_query_var('paged');
	      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
	    }
	 
	    echo '</div>';
	 
		} 
	}
}

if (! function_exists('cleanead_related_posts') ):
/*
|------------------------------------------------------------------------------
| Related Posts
|------------------------------------------------------------------------------
|
| You can show related posts by Categories or Tags. 
| It has two options to show related posts
|
| 1. Thumbnail related posts (default)
| 2. List of related posts
| 
| @return void
|
*/

	function cleanead_related_posts() {
		global $post;

		$theme_options = cleanead_theme_options();
		$numberRelated = 6;
		$taxonomy = $theme_options['related_posts'];

		$args =  array();

		if ($taxonomy == 'tag') {

			$tags = wp_get_post_tags($post->ID);
			$arr_tags = array();
			foreach($tags as $tag) {
				array_push($arr_tags, $tag->term_id);
			}
			
			if (!empty($arr_tags)) { 
			    $args = array(  
				    'tag__in' => $arr_tags,  
				    'post__not_in' => array($post->ID),  
				    'posts_per_page'=> $numberRelated,
			    ); 
			}

		} else {

			 $args = array( 
			 	'category__in' => wp_get_post_categories($post->ID), 
			 	'posts_per_page' => $numberRelated, 
			 	'post__not_in' => array($post->ID) 
			 );

		}

		if (! empty($args) ) {
			$posts = get_posts($args);

			if ($posts) { ?>
				<div class="related-posts clearfix">
					<h2 class="related-title"><?php _e('Related Posts', 'cleanead') ?></h2>
					<ul class="related-list">
					<?php
					foreach ($posts as $p) :
						?>
						<li>
							<a href="<?php echo get_the_permalink($p->ID) ?>"><?php echo get_the_title($p->ID) ?></a>
						</li>
						<?php
					endforeach;
					?>
					</ul>
				</div>
			<?php
			}
		}
	}
endif;

if ( ! function_exists( 'cleanead_excerpt_more' ) ) {
	/**
	|------------------------------------------------------------------------------
	| Excerpt ending
	|------------------------------------------------------------------------------
	| 
	| @return string
	|
	*/

	function cleanead_excerpt_more( $more ) {
		$theme_options = cleanead_theme_options();

		return $theme_options['excerpt_more'];		

	}
	
}

add_filter( 'excerpt_more', 'cleanead_excerpt_more' );

if ( ! function_exists( 'cleanead_excerpt_length' ) ) {

	/**
	|------------------------------------------------------------------------------
	| Excerpt length
	|------------------------------------------------------------------------------
	| 
	| @return integer
	|
	*/

	function cleanead_excerpt_length($length) {

		$theme_options = cleanead_theme_options();

		$number = intval ( $theme_options['excerpt_length'] ) > 0 ?  intval ( $theme_options['excerpt_length'] ) : $length;
		return $number;
	}
	
}

add_filter( 'excerpt_length', 'cleanead_excerpt_length', 999 );


if ( ! function_exists( 'cleanead_author_box' ) ) {

	/**
	|------------------------------------------------------------------------------
	| Author Box
	|------------------------------------------------------------------------------
	*/

	function cleanead_author_box() {
		?>
		<div class="author-bio">
			<div class="bio-avatar"><?php echo get_avatar(get_the_author_meta('user_email'),'180'); ?></div>
			<div class="author-des">
				<p class="bio-name"><?php _e('About Author:', 'cleanead') ?> <?php the_author_posts_link(); ?></p>
				<p class="bio-desc"><?php the_author_meta('description'); ?></p>
				<div class="clear"></div>
			</div>
		</div>
		<?php
	}
}
