<?php

/***** Logo/Sitename *****/

if (!function_exists('mh_impact_lite_logo')) {
	function mh_impact_lite_logo() {
		$header_img = get_header_image();
		$header_title = get_bloginfo('name');
		$header_desc = get_bloginfo('description');
		echo '<a href="' . esc_url(home_url('/')) . '" rel="home">' . "\n";
		echo '<div class="logo-wrap" role="banner">' . "\n";
		if ($header_img) {
			echo '<img src="' . esc_url($header_img) . '" height="' . get_custom_header()->height . '" width="' . get_custom_header()->width . '" alt="' . esc_attr($header_title) . '" />' . "\n";
		}
		if (display_header_text()) {
			$text_color = get_header_textcolor();
			if ($text_color != get_theme_support('custom-header', 'default-text-color')) {
				echo '<style type="text/css" id="mh-header-css">';
					echo '.logo-title, .logo-tagline { color: #' . esc_attr($text_color) . '; }';
				echo '</style>' . "\n";
			}
			echo '<div class="logo">' . "\n";
			if ($header_title) {
				echo '<h1 class="logo-title">' . esc_attr($header_title) . '</h1>' . "\n";
			}
			if ($header_desc) {
				echo '<h2 class="logo-tagline">' . esc_attr($header_desc) . '</h2>' . "\n";
			}
			echo '</div>' . "\n";
		}
		echo '</div>' . "\n";
		echo '</a>' . "\n";
	}
}

/***** Page Title Output *****/

if (!function_exists('mh_impact_lite_page_title')) {
	function mh_impact_lite_page_title() {
		if (!is_front_page()) {
			echo '<h1 class="page-title">';
			if (is_archive()) {
				if (is_category() || is_tax()) {
					single_cat_title();
				} elseif (is_tag()) {
					single_tag_title();
				} elseif (is_author()) {
					global $author;
					$user_info = get_userdata($author);
					printf(_x('Articles by %s', 'post author', 'mh-impact-lite'), esc_attr($user_info->display_name));
				} elseif (is_day()) {
					echo get_the_date();
				} elseif (is_month()) {
					echo get_the_date('F Y');
				} elseif (is_year()) {
					echo get_the_date('Y');
				} else {
					_e('Archives', 'mh-impact-lite');
				}
			} else {
				if (is_home()) {
					echo get_the_title(get_option('page_for_posts', true));
				} elseif (is_404()) {
					_e('Page not found (404)', 'mh-impact-lite');
				} elseif (is_search()) {
					printf(__('Search Results for %s', 'mh-impact-lite'), esc_attr(get_search_query()));
				} else {
					the_title();
				}
			}
			echo '</h1>' . "\n";
		}
	}
}

/***** Output Post Meta Data *****/

if (!function_exists('mh_impact_lite_post_meta')) {
	function mh_impact_lite_post_meta() {
		echo '<p class="entry-meta">' . "\n";
			echo '<span class="entry-date updated"><i class="fa fa-calendar"></i>' . get_the_date() . '</span>' . "\n";
			echo '<span class="vcard author"><i class="fa fa-user"></i><a class="fn" href="' . esc_url(get_author_posts_url(get_the_author_meta('ID'))) . '">' . esc_html(get_the_author()) . '</a></span>' . "\n";
			if ('post' == get_post_type()) {
				echo '<span class="entry-cats"><i class="fa fa-chevron-circle-right"></i>' . get_the_category_list(', ', '') . '</span>' . "\n";
			}
		echo '</p>' . "\n";
	}
}

/***** Featured Image on Posts *****/

if (!function_exists('mh_impact_lite_featured_image')) {
	function mh_impact_lite_featured_image() {
		global $page, $post;
		if (has_post_thumbnail() && $page == '1') {
			echo "\n" . '<figure class="entry-thumbnail">' . "\n";
				the_post_thumbnail('blog');
				if (get_the_post_thumbnail_caption()) {
					echo '<figcaption class="wp-caption-text">' . wp_kses_post(get_the_post_thumbnail_caption()) . '</figcaption>' . "\n";
				}
			echo '</figure>' . "\n";
		}
	}
}

/***** Custom Excerpts *****/

if (!function_exists('mh_impact_lite_trim_excerpt')) {
	function mh_impact_lite_trim_excerpt($text = '') {
		$raw_excerpt = $text;
		if ('' == $text) {
			$mh_impact_lite_options = mh_impact_lite_theme_options();
			$text = get_the_content('');
			$text = do_shortcode($text);
			$text = apply_filters('the_content', $text);
			$text = str_replace(']]>', ']]&gt;', $text);
			$excerpt_length = apply_filters('excerpt_length', esc_attr($mh_impact_lite_options['excerpt_length']));
			$excerpt_more = apply_filters('excerpt_more', '');
			$text = wp_trim_words($text, $excerpt_length, $excerpt_more);
		}
		return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
	}
}
remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'mh_impact_lite_trim_excerpt');

/***** Add Custom Read More Link to Excerpts *****/

if (!function_exists('mh_impact_lite_custom_excerpt_more')) {
	function mh_impact_lite_custom_excerpt_more() {
		global $post;
		$mh_impact_lite_options = mh_impact_lite_theme_options();
		$excerpt = get_the_excerpt();
		$permalink = get_permalink($post->ID);
		if ($mh_impact_lite_options['excerpt_more'] != '') {
			$excerpt .= ' <a href="' . esc_url($permalink) . '" title="' . the_title_attribute('echo=0') . '">' . esc_attr($mh_impact_lite_options['excerpt_more']) . '</a>' . "\n";
		}
		return $excerpt;
	}
}
add_filter('the_excerpt', 'mh_impact_lite_custom_excerpt_more');

/***** Function for Custom Excerpt Lengths *****/

if (!function_exists('mh_impact_lite_excerpt')) {
	function mh_impact_lite_excerpt($excerpt_length) {
		$excerpt = get_the_excerpt();
		echo '<div class="mh-excerpt">' . wp_trim_words($excerpt , $excerpt_length) . '</div>' . "\n";
	}
}

/***** Pagination *****/

if (!function_exists('mh_impact_lite_pagination')) {
	function mh_impact_lite_pagination() {
		global $wp_query;
	    $big = 9999;
	    $paginate_links = paginate_links(array(
	    	'base' 		=> str_replace($big, '%#%', esc_url(get_pagenum_link($big))),
	    	'format' 	=> '?paged=%#%',
	    	'current' 	=> max(1, get_query_var('paged')),
	    	'prev_next' => true,
	    	'prev_text' => __('&laquo;', 'mh-impact-lite'),
	    	'next_text' => __('&raquo;', 'mh-impact-lite'),
	    	'total' 	=> $wp_query->max_num_pages
	    ));
	    if ($paginate_links) {
	    	echo '<div class="pagination clearfix">';
				echo $paginate_links;
			echo '</div>';
		}
	}
}

/***** Pagination for paginated Posts *****/

if (!function_exists('mh_impact_lite_posts_pagination')) {
	function mh_impact_lite_posts_pagination($content) {
		if (is_singular() && is_main_query()) {
			$content .= wp_link_pages(array('before' => '<div class="pagination clear">', 'after' => '</div>', 'link_before' => '<span class="pagelink">', 'link_after' => '</span>', 'nextpagelink' => __('&raquo;', 'mh-impact-lite'), 'previouspagelink' => __('&laquo;', 'mh-impact-lite'), 'pagelink' => '%', 'echo' => 0));
		}
		return $content;
	}
}
add_filter('the_content', 'mh_impact_lite_posts_pagination', 1);

/***** Post / Image Navigation *****/

if (!function_exists('mh_impact_lite_postnav')) {
	function mh_impact_lite_postnav() {
		global $post;
		$parent_post = get_post($post->post_parent);
		$attachment = is_attachment();
		$previous = ($attachment) ? $parent_post : get_adjacent_post(false, '', true);
		$next = get_adjacent_post(false, '', false);

		if (!$next && !$previous)
		return;

		if ($attachment) {
			$attachments = get_children(array('post_type' => 'attachment', 'post_mime_type' => 'image', 'post_parent' => $parent_post->ID));
			$count = count($attachments);
		}
		echo '<nav class="post-nav-wrap" role="navigation">' . "\n";
			echo '<ul class="post-nav clearfix">' . "\n";
				echo '<li class="post-nav-prev">' . "\n";
					if ($attachment) {
						if ($count == 1) {
							$permalink = get_permalink($parent_post);
							echo '<a href="' . $permalink . '"><i class="fa fa-chevron-left"></i>' . __('Back to post', 'mh-impact-lite') . '</a>';
						} else {
							previous_image_link('%link', '<i class="fa fa-chevron-left"></i>' . __('Previous image', 'mh-impact-lite'));
						}
					} else {
						previous_post_link('%link', '<i class="fa fa-chevron-left"></i>' . __('Previous post', 'mh-impact-lite'));
					}
				echo '</li>' . "\n";
				echo '<li class="post-nav-next">' . "\n";
					if ($attachment) {
						next_image_link('%link', __('Next image', 'mh-impact-lite') . '<i class="fa fa-chevron-right"></i>');
					} else {
						next_post_link('%link', __('Next post', 'mh-impact-lite') . '<i class="fa fa-chevron-right"></i>');
					}
				echo '</li>' . "\n";
			echo '</ul>' . "\n";
		echo '</nav>' . "\n";
	}
}

/***** Custom Commentlist *****/

if (!function_exists('mh_impact_lite_comments')) {
	function mh_impact_lite_comments($comment, $args, $depth) {
		$GLOBALS['comment'] = $comment; ?>
		<li <?php comment_class(); ?> id="li-comment-<?php comment_ID() ?>">
			<div id="comment-<?php comment_ID(); ?>" class="clearfix">
				<div class="vcard meta">
					<?php echo get_avatar($comment->comment_author_email, 70); ?>
					<?php echo get_comment_author_link() ?> |
					<a href="<?php echo esc_url(get_comment_link($comment->comment_ID)) ?>"><?php printf(__('%1$s at %2$s', 'mh-impact-lite'), get_comment_date(),  get_comment_time()) ?></a> |
					<?php if (comments_open() && $args['max_depth']!=$depth) { ?>
						<?php comment_reply_link(array_merge($args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
					<?php } ?>
					<?php edit_comment_link(__('(Edit)', 'mh-impact-lite'),'  ','') ?>
				</div>
				<?php if ($comment->comment_approved == '0') : ?>
					<div class="comment-info">
						<?php _e('Your comment is awaiting moderation.', 'mh-impact-lite') ?>
                    </div>
				<?php endif; ?>
				<div class="comment-text">
					<?php comment_text() ?>
				</div>
			</div><?php
	}
}

/***** Custom Comment Fields *****/

if (!function_exists('mh_impact_lite_comment_fields')) {
	function mh_impact_lite_comment_fields($fields) {
		$commenter = wp_get_current_commenter();
		$req = get_option('require_name_email');
		$aria_req = ($req ? " aria-required='true'" : '');
		$consent = empty($commenter['comment_author_email']) ? '' : ' checked="checked"';
		$fields =  array(
			'author'	=>	'<p class="comment-form-author"><label for="author">' . esc_html__('Name ', 'mh-impact-lite') . '</label>' . ($req ? '<span class="required">*</span>' : '') . '<br/><input id="author" name="author" type="text" value="' . esc_attr($commenter['comment_author']) . '" size="30"' . $aria_req . ' /></p>',
			'email' 	=>	'<p class="comment-form-email"><label for="email">' . esc_html__('Email ', 'mh-impact-lite') . '</label>' . ($req ? '<span class="required">*</span>' : '' ) . '<br/><input id="email" name="email" type="text" value="' . esc_attr($commenter['comment_author_email']) . '" size="30"' . $aria_req . ' /></p>',
			'url' 		=>	'<p class="comment-form-url"><label for="url">' . esc_html__('Website', 'mh-impact-lite') . '</label><br/><input id="url" name="url" type="text" value="' . esc_attr($commenter['comment_author_url']) . '" size="30" /></p>',
			'cookies' 	=>  '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"' . $consent . ' />' . '<label for="wp-comment-cookies-consent">' . esc_html__('Save my name, email, and website in this browser for the next time I comment.', 'mh-impact-lite') . '</label></p>'
		);
		return $fields;
	}
}
add_filter('comment_form_default_fields', 'mh_impact_lite_comment_fields');

/***** Add CSS classes to body tag *****/

if (!function_exists('mh_impact_lite_body_class')) {
	function mh_impact_lite_body_class($classes) {
		$mh_impact_lite_options = mh_impact_lite_theme_options();
		$classes[] = 'mh-' . $mh_impact_lite_options['sidebar'] . '-sb';
		return $classes;
	}
}
add_filter('body_class', 'mh_impact_lite_body_class');

/***** Add CSS3 Media Queries Support for older versions of IE *****/

function mh_impact_lite_ie_media_queries() {
	echo '<!--[if lt IE 9]>' . "\n";
	echo '<script src="' . get_template_directory_uri() . '/js/css3-mediaqueries.js"></script>' . "\n";
	echo '<![endif]-->' . "\n";
}
add_action('wp_head', 'mh_impact_lite_ie_media_queries');

?>