<?php
/**
 * Template part for displaying posts.
 *
 * @link https://codex.wordpress.org/Template_Hierarchy
 *
 * @package enjoystyle
 */	
?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

	<header class="entry-header entry-top">	

		<div class="entry-category">
			<?php enjoystyle_first_category(); ?>
		</div>		

		<?php
			if ( is_single() ) :
				the_title( '<h1 class="entry-title">', '</h1>' );
			else :
				the_title( '<h2 class="entry-title"><a href="' . esc_url( get_permalink() ) . '" rel="bookmark">', '</a></h2>' );
			endif;
		?>

		<div class="entry-meta">
			<span class="entry-author">
				<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) ); ?>"><?php echo get_avatar( get_the_author_meta( 'ID' ), 120 ); ?></a>
				<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) ); ?>"><?php the_author_meta('display_name'); ?></a>
			</span>			
			<span class="entry-date"><?php echo get_the_date(); ?></span>
			<span class="entry-comment"><?php comments_popup_link( __('0 Comment','enjoystyle'), __('1 Comment', 'enjoystyle'), '% Comments', 'comments-link', __('comments off', 'enjoystyle'));?></span>
		</div><!-- .entry-meta -->

	</header><!-- .entry-header -->

	<div class="entry-content">

		<?php
			dynamic_sidebar('post-top-ad');

			the_content( sprintf(
				/* translators: %s: Name of current post. */
				wp_kses( __( 'Continue reading %s <span class="meta-nav">&rarr;</span>', 'enjoystyle' ), array( 'span' => array( 'class' => array() ) ) ),
				the_title( '<span class="screen-reader-text">"', '"</span>', false )
			) );

			wp_link_pages( array(
				'before' => '<div class="page-links">' . esc_html__( 'Pages:', 'enjoystyle' ),
				'after'  => '</div>',
			) );
		?>
	</div><!-- .entry-content -->

	<div class="entry-tags">

		<?php if (has_tag()) { ?><span class="tag-links"><?php the_tags(' ', ' '); ?></span><?php } ?>
			
		<?php
			edit_post_link(
				sprintf(
					/* translators: %s: Name of current post */
					esc_html__( 'Edit %s', 'enjoystyle' ),
					the_title( '<span class="screen-reader-text">"', '"</span>', false )
				),
				'<span class="edit-link">',
				'</span>'
			);
		?>
	</div><!-- .entry-tags -->

	<?php 
		dynamic_sidebar('post-bottom-ad');
	?>

</article><!-- #post-## -->

<div class="entry-footer">

	<div class="share-icons">
		
		<?php get_template_part('template-parts/entry', 'share'); ?>

	</div><!-- .share-icons -->

</div><!-- .entry-footer -->

<div class="author-box clear">
	<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) ); ?>"><?php echo get_avatar( get_the_author_meta( 'ID' ), 120 ); ?></a>
	<div class="author-meta">	
		<div class="author-name">
			<a href="<?php echo esc_url( get_author_posts_url( get_the_author_meta( 'ID' ), get_the_author_meta( 'user_nicename' ) ) ); ?>"><?php the_author_meta('display_name'); ?></a>
		</div>	
		<div class="author-desc">
			<?php echo esc_html( the_author_meta('description') ); ?>
		</div>
	</div>
</div><!-- .author-box -->

<?php	 
	// Get the taxonomy terms of the current page for the specified taxonomy.
	$terms = wp_get_post_terms( get_the_ID(), 'category', array( 'fields' => 'ids' ) );

	// Bail if the term empty.
	if ( empty( $terms ) ) {
		return;
	}

	// Posts query arguments.
	$query = array(
		'post__not_in' => array( get_the_ID() ),
		'tax_query'    => array(
			array(
				'taxonomy' => 'category',
				'field'    => 'id',
				'terms'    => $terms,
				'operator' => 'IN'
			)
		),
		'posts_per_page' => 6,
		'post_type'      => 'post',
	);

	// Allow dev to filter the query.
	$args = apply_filters( 'enjoystyle_related_posts_args', $query );

	// The post query
	$enjoystyle_related = new WP_Query( $args );

	if ( $enjoystyle_related->have_posts() ) : $i = 1; ?>

		<div class="entry-related clear">
			<h3><?php esc_html_e('Related Posts', 'enjoystyle'); ?></h3>
			<div class="related-loop ht_grid_wrap clear">
				<?php while ( $enjoystyle_related->have_posts() ) : $enjoystyle_related->the_post(); ?>
					<a class="thumbnail-link" href="<?php the_permalink(); ?>">
						<div class="hentry ht_grid_1_3 <?php if ( !has_post_thumbnail() ) { echo 'no-thumbnail'; } ?>">
							<div class="entry-inner">
								<?php if ( has_post_thumbnail() ) { ?>
									<div class="thumbnail-wrap">
										<?php 
											the_post_thumbnail(); 
										?>
									</div><!-- .thumbnail-wrap -->
								<div class="gradient"></div>
								<?php } ?>								
								<div class="entry-header">				
									<h2 class="entry-title"><?php the_title(); ?></h2>
									<div class="entry-date">
										<?php echo get_the_date(); ?>
									</div>
								</div>
							</div>
						</a>
					</div><!-- .grid -->
				<?php $i++; endwhile; ?>
			</div><!-- .related-posts -->
		</div><!-- .entry-related -->

	<?php endif;

	// Restore original Post Data.
	wp_reset_postdata();
?>