<?php
/***
 * Template Tags
 */


if ( ! function_exists( 'tsoft_site_logo' ) ) :
	/**
 * Displays the site logo in the header area
 */
	function tsoft_site_logo() {

		if ( function_exists( 'the_custom_logo' ) ) {

			the_custom_logo();

		}

	}
endif;
 

if ( ! function_exists( 'tsoft_site_title' ) ) :
	/**
 * Displays the site title in the header area
 */
	function tsoft_site_title() {

		if ( is_home() ) : ?>

			<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>

		<?php else : ?>

            <p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>

	    <?php endif;
	}
endif;


if ( ! function_exists( 'tsoft_site_description' ) ) :
	/**
	 * Displays the site description in the header area
	 */
	function tsoft_site_description() {

		$description = get_bloginfo( 'description', 'display' ); /* WPCS: xss ok. */

		if ( $description || is_customize_preview() ) : ?>

			<p class="site-description"><?php echo $description; ?></p>

		<?php
		endif;
	}
endif;


// Display Custom Header
if ( ! function_exists( 'tsoft_display_custom_header' ) ) :

	function tsoft_display_custom_header() {

		// Get theme options from database
		$theme_options = tsoft_theme_options();

		// Hide header image on front page
		if ( true == $theme_options['custom_header_hide'] and is_front_page() ) {
			return;
		}

		// Check if page is displayed and featured header image is used
		if ( is_page() && has_post_thumbnail() ) : ?>

			<div id="custom-header-image" class="container featured-header-image">
				<?php the_post_thumbnail( 'featured-header-image' ); ?>
			</div>

		<?php
		// Check if there is a custom header image
		elseif ( get_header_image() ) : ?>

			<div id="custom-header-image" class="container">

				<?php // Check if custom header image is linked
				if ( $theme_options['custom_header_link'] <> '' ) : ?>

					<a href="<?php echo esc_url( $theme_options['custom_header_link'] ); ?>">
						<img src="<?php header_image(); ?>" srcset="<?php echo esc_attr( wp_get_attachment_image_srcset( get_custom_header()->attachment_id, 'full' ) ); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">
					</a>

				<?php else : ?>

					<img src="<?php header_image(); ?>" srcset="<?php echo esc_attr( wp_get_attachment_image_srcset( get_custom_header()->attachment_id, 'full' ) ); ?>" width="<?php echo esc_attr( get_custom_header()->width ); ?>" height="<?php echo esc_attr( get_custom_header()->height ); ?>" alt="<?php echo esc_attr( get_bloginfo( 'name', 'display' ) ); ?>">

				<?php endif; ?>

			</div>

		<?php
		endif;
	}

endif;


// Display Subtitle
function tsoft_display_subtitle() {

	if ( function_exists( 'the_subtitle' ) ) :

		the_subtitle( '<p class="subtitle">', '</p>' );

	endif;

}


// Display Post Thumbnail on single posts
function tsoft_display_thumbnail_single() {

	// Get Theme Options from Database
	$theme_options = tsoft_theme_options();

	// Display Post Thumbnail if activated
	if ( isset( $theme_options['post_thumbnails_single'] ) and $theme_options['post_thumbnails_single'] == true ) :

		the_post_thumbnail( 'post-thumbnail', array( 'class' => 'alignleft' ) );

	endif;

}


// Display Postmeta Data
if ( ! function_exists( 'tsoft_display_postmeta' ) ) :

	function tsoft_display_postmeta() {

		// Get Theme Options from Database
		$theme_options = tsoft_theme_options();

		// Display Date unless user has deactivated it via settings
		if ( true == $theme_options['meta_date'] ) :
		
			?>
			<span class="meta-date">
				<i class="fa fa-calendar" style="font-size:18px;"></i>
					<?php echo get_the_date(); ?>
			</span>
			<?php
			
		endif;

		// Display Author unless user has deactivated it via settings
		if ( true == $theme_options['meta_author'] ) :
		
			?>
			<span class="meta-author author vcard">
				<i class="fa fa-user" style="font-size:18px;"></i>
				<?php echo get_the_author(); ?>
			</span>
			<?php

		endif;

		// Display Author unless user has deactivated it via settings
		if ( true == $theme_options['meta_category'] ) :

			?>
			<span class="meta-category">
				<i class="fa fa-file" style="font-size:18px;"></i>
				<?php echo get_the_category_list( ', ' ); ?>
			</span>
			<?php

		endif;

		// Display Comments
		if ( comments_open() ) :

			?>
			<span class="meta-comments">
				<i class="fa fa-comment" style="font-size:18px;"></i>
				<?php comments_popup_link( esc_html__( 'Leave a comment', 'tsoft-info' ), esc_html__( 'One comment', 'tsoft-info' ), esc_html__( '% comments', 'tsoft-info' ) ); ?>
			</span>

			<?php
			
		endif;

	}

endif;


// Display Content Pagination
if ( ! function_exists( 'tsoft_display_pagination' ) ) :

	function tsoft_display_pagination() {

		global $wp_query;

		$big = 999999999; // need an unlikely integer

		 $paginate_links = paginate_links( array(
				'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
				'format' => '?paged=%#%',
				'current' => max( 1, get_query_var( 'paged' ) ),
				'total' => $wp_query->max_num_pages,
				'next_text' => '&raquo;',
				'prev_text' => '&laquo',
				'add_args' => false,
		 ) );

		 // Display the pagination if more than one page is found
		 if ( $paginate_links ) : ?>

			  <div class="post-pagination clearfix">
					<?php echo $paginate_links; ?>
			  </div>

			<?php
		endif;

	}

endif;


// Display Post Tags
if ( ! function_exists( 'tsoft_display_post_tags' ) ) :

	function tsoft_display_post_tags() {

		// Get Theme Options from Database
		$theme_options = tsoft_theme_options();

		// Display Date unless user has deactivated it via settings
		if ( isset( $theme_options['meta_tags'] ) and $theme_options['meta_tags'] == true ) :

			$tag_list = get_the_tag_list( '<ul><li>','</li><li>','</li></ul>' );

			if ( $tag_list ) :

				echo $tag_list;

			endif;

		endif;

	}

endif;


// Display Footer Text
add_action( 'tsoft_footer_text', 'tsoft_display_footer_text' );

function tsoft_display_footer_text() {
	?>

	<span class="credit-link">
		<?php printf( esc_html__( 'Powered by %1$s.', 'tsoft-info' ),
			'<a href="http://tsoft.info" title="tsoft info">Theme Soft</a>'
		); ?>
	</span>

<?php
}



// Custom Template for comments and pingbacks.
if ( ! function_exists( 'tsoft_list_comments' ) ) :

	function tsoft_list_comments( $comment, $args, $depth ) {

		$GLOBALS['comment'] = $comment;

		if ( $comment->comment_type == 'pingback' or $comment->comment_type == 'trackback' ) : ?>

			<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">
				<div class="comment-body">
					<?php esc_html_e( 'Pingback:', 'tsoft-info' ); ?> <?php comment_author_link(); ?>
					<?php edit_comment_link( esc_html__( '(Edit)', 'tsoft-info' ), '<span class="edit-link">', '</span>' ); ?>
				</div>

		<?php else : ?>

		<li <?php comment_class(); ?> id="comment-<?php comment_ID(); ?>">

			<div class="comment-body clearfix">

				<div class="comment-meta clearfix">

					<div class="comment-author vcard">
						<?php echo get_avatar( $comment, 75 ); ?>
						<?php printf( '<span class="fn">%s</span>', get_comment_author_link() ) ?>
					</div>

					<div class="commentmetadata">
						<a href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>"><?php echo get_comment_date(); ?></a>
						<p><?php echo get_comment_time(); ?></p>
						<?php edit_comment_link( esc_html__( '(Edit)', 'tsoft-info' ),'  ','' ) ?>
					</div>

				</div>

				<div class="comment-content">

					<div class="comment-entry clearfix">
						<?php comment_text(); ?>

						<?php if ( $comment->comment_approved == '0' ) : ?>
							<p class="comment-awaiting-moderation"><?php esc_html_e( 'Your comment is awaiting moderation.', 'tsoft-info' ); ?></p>
						<?php endif; ?>

						<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ) ?>
					</div>

				</div>

			</div>
<?php
	endif;

	}

endif;
