<?php
if ( ! function_exists( 'dw_gamez_entry_meta' ) ) :
function dw_gamez_entry_meta() {
	$time_string = '<time class="entry-date" datetime="%1$s">%2$s</time>';

	$time_string = sprintf( $time_string,
		esc_attr( get_the_date( 'c' ) ),
		esc_html( get_the_date() )
	);

	printf( __( '<span class="posted-on"><i class="icon-time"></i> %1$s</span><span class="byline"><i class="icon-user"></i> %2$s</span>', 'dw-gamez' ),
		sprintf( '<a href="%1$s" title="%2$s" rel="bookmark">%3$s</a>',
			esc_url( get_permalink() ),
			esc_attr( get_the_time() ),
			$time_string
		),
		sprintf( '<span class="author vcard"><a class="url fn n" href="%1$s" title="%2$s">%3$s</a></span>',
			esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
			esc_attr( sprintf( __( 'View all posts by %s', 'dw-gamez' ), get_the_author() ) ),
			esc_html( get_the_author() )
		)
	);

	if ( ! post_password_required() && comments_open() && ! is_single() ) { ?>
		<span class="comments-link"><?php comments_popup_link( __( '0', 'dw-gamez' ), __( '1', 'dw-gamez' ), __( '%', 'dw-gamez' ) ); ?></span>
	<?php }
	$categories_list = get_the_category_list( __( ', ', 'dw-gamez' ) );
	if ( $categories_list ) : ?>
		<span class="cat-links">
			<?php printf( __( '<i class="icon-folder-open"></i> %1$s', 'dw-gamez' ), $categories_list ); ?>
		</span>
	<?php endif;
}
endif;

if ( ! function_exists( 'dw_gamez_pagenavi' ) ) :
function dw_gamez_pagenavi( $prev_post_label = '', $next_post_label = '' ) {
	global $wp_query;
	$total = $wp_query->max_num_pages;

	if ( $total > 1 )  {
		if ( !$current_page = get_query_var('paged') ) $current_page = 1;
		if ( is_home() ) {
			$format = '?paged=%#%';
		} else if ( !get_option('permalink_structure') ) {
			$format = '&paged=%#%';
		} else {
			$format = 'page/%#%/';
		}

		$pagging = paginate_links(array(
			'base' => get_pagenum_link(1) . '%_%',
			'format' => $format,
			'current' => $current_page,
			'total' => $total,
			'mid_size' => 4,
			'type' => 'array',
			'class' => 'pagination',
			'prev_next' => ''
		));

		if( $prev_post_label == '' ) $prev_post_label = _x( '<span class="meta-nav">&larr;</span> Older posts', 'dw-gamez' );
		if( $next_post_label == '' ) $next_post_label = _x( 'Newer posts <span class="meta-nav">&rarr;</span>', 'dw-gamez' );
		
		echo '<ul class="pagination">';
		if($current_page==1) echo '<li><span class="page-numbers disabled">'.$prev_post_label.'</span></li>';
		if(get_previous_posts_link()) echo '<li><span class="page-numbers prev">'.get_previous_posts_link( $prev_post_label ).'</span></li>';
		foreach ($pagging as $number) {
			echo '<li>'.$number.'</li>';
		}
		if(get_next_posts_link()) echo '<li><span class="page-numbers next">'.get_next_posts_link( $next_post_label ).'</span></li>';
		if($current_page==$total) echo '<li><span class="page-numbers disabled">'.$next_post_label.'</span></li>';
		echo '</ul>';
	}
}
endif;

if ( ! function_exists( 'dw_gamez_comment' ) ) :
function dw_gamez_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;

	if ( 'pingback' == $comment->comment_type || 'trackback' == $comment->comment_type ) : ?>

	<li id="comment-<?php comment_ID(); ?>" <?php comment_class(); ?>>
		<div class="comment-body">
			<?php _e( 'Pingback:', 'dw-gamez' ); ?> <?php comment_author_link(); ?> <?php edit_comment_link( __( 'ویرایش', 'dw-gamez' ), '<span class="edit-link">', '</span>' ); ?>
		</div>

	<?php else : ?>

	<li id="comment-<?php comment_ID(); ?>" <?php comment_class( empty( $args['has_children'] ) ? '' : 'parent' ); ?>>
		<article id="div-comment-<?php comment_ID(); ?>" class="comment-body">
			<footer class="comment-meta">
				<div class="comment-author vcard">
					<?php if ( 0 != $args['avatar_size'] ) echo get_avatar( $comment, 60 ); ?>
					<?php printf( __( '%s', 'dw-gamez' ), sprintf( '<cite class="fn">%s</cite>', get_comment_author_link() ) ); ?>
				</div>

				<div class="comment-metadata">
					<a class="comment-datetime" href="<?php echo esc_url( get_comment_link( $comment->comment_ID ) ); ?>">
						<i class="icon-time"></i>
						<time datetime="<?php comment_time( 'c' ); ?>">
							<?php printf( _x( '%1$s' . '<span>' . ' در %2$s' . '</span>' , '1: date, 2: time', 'dw-gamez' ), get_comment_date(), get_comment_time() ); ?>
						</time>
					</a>
					<?php edit_comment_link( __( '<i class="icon-pencil"></i> ویرایش', 'dw-gamez' ), '<span class="edit-link">', '</span>' ); ?>
				</div>

				<span class="reply">
					<?php comment_reply_link( array_merge( $args, array( 'add_below' => 'div-comment', 'reply_text' => '<i class="icon-reply"></i> پاسخ' ,'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
				</span>
			</footer>

			<div class="comment-content">
				<?php comment_text(); ?>

				<?php if ( '0' == $comment->comment_approved ) : ?>
					<p class="comment-awaiting-moderation"><?php _e( 'نظر شما در انتظار تایید است', 'dw-gamez' ); ?></p>
				<?php endif; ?>
			</div>
		</article>

	<?php
	endif;
}
endif;

if ( ! function_exists( 'dw_gamez_content_nav' ) ) :
function dw_gamez_content_nav( $nav_id ) {
	global $wp_query, $post;

	if ( is_single() ) {
		$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
		$next = get_adjacent_post( false, '', false );

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

	if ( $wp_query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
		return;

	$nav_class = ( is_single() ) ? 'post-navigation' : 'paging-navigation';

	?>
	<nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">
		<h1 class="screen-reader-text sr-only"><?php _e( 'Post navigation', 'dw-gamez' ); ?></h1>

	<?php if ( is_single() ) : ?>

		<?php previous_post_link( '<div class="nav-previous">%link</div>', '<span class="meta-nav">' . _x( '<i class="icon-chevron-left"></i>', 'Previous post link', 'dw-gamez' ) . '</span> %title' ); ?>
		<?php next_post_link( '<div class="nav-next">%link</div>', '%title <span class="meta-nav">' . _x( '<i class="icon-chevron-right"></i>', 'Next post link', 'dw-gamez' ) . '</span>' ); ?>

	<?php elseif ( $wp_query->max_num_pages > 1 && ( is_home() || is_archive() || is_search() ) ) : // navigation links for home, archive, and search pages ?>

		<?php if ( get_next_posts_link() ) : ?>
		<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'dw-gamez' ) ); ?></div>
		<?php endif; ?>

		<?php if ( get_previous_posts_link() ) : ?>
		<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'dw-gamez' ) ); ?></div>
		<?php endif; ?>

	<?php endif; ?>

	</nav>
	<?php
}
endif;

if ( ! function_exists( 'dw_gamez_related_post' ) ) :
function dw_gamez_related_post($post_id) {
	$tags = wp_get_post_tags($post_id);
	if ($tags) {
		$tag_ids = array();
		foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
		$args = array (
			'tag__in' => $tag_ids,
			'post__not_in' => array($post_id),
			'posts_per_page' => 3,
			'ignore_sticky_posts'=>1 
		);
	} else {
		$args = array (
			'post__not_in' => array($post_id),
			'posts_per_page' => 3,
			'ignore_sticky_posts'=>1 
		);
	} ?>

<?php $related_query = new wp_query( $args ); ?>
	<?php if ( $related_query->have_posts() ) : ?>
	<div class="related-posts">
		<h2 class="related-posts-title"><?php _e( '<span>مقالات </span>مرتبط ', 'dw-gamez' ); ?></h2>
		<div class="related-content">
			<div class="row">
				<?php while ( $related_query->have_posts() ) : $related_query->the_post(); ?>
				<article class="related-post clearfix col-sm-4">
					<?php if(has_post_thumbnail()) : ?>
					<div class="entry-thumbnail">
						<a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_post_thumbnail('homepage-narrow'); ?></a>
					</div>
					<?php endif; ?>
					<h3 class="related-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php the_title(); ?></a></h3>
					<div class="related-meta"><time class="related-date"><a href="<?php the_permalink(); ?>" rel="bookmark"><i class="icon-time"></i> <?php echo get_the_date('d M, Y'); ?></a></time></div>
				</article>
				<?php endwhile; ?>
			</div>
		</div>
	</div>
	<?php endif; ?>
<?php wp_reset_query(); ?>
<?php }
endif;