<?php
defined( 'ABSPATH' ) || exit( 'No Access!' );

global $labels;
$ht_posts = irk_get_head_tag('ht-posts', ['class' => 'title']);

$p_args = array(
	'post_type'      => $post_type,
	'posts_per_page' => $count ? $count : 10,
);

switch ( $post_type ) {
	default:
	case 'post':
		$cat = $category;
		$tax = 'category';
		break;
	case 'news':
		$cat = $news_cat;
		$tax = 'news_cat';
		break;
	case 'blog':
		$cat = $blog_cat;
		$tax = 'blog_cat';
		break;
}

if ( $cat && $cat != '-1' ) {
	$tax_query = array(
		'taxonomy' => $tax,
		'terms'    => $cat,
	);
	if ( $include_children ) {
		$tax_query['include_children'] = true;
	} else {
		$tax_query['field'] = 'term_id';
		$tax_query['operator'] = 'AND';
		$tax_query['include_children'] = false;
	}
	$p_args['tax_query'][0] = $tax_query;
}

if ( ! empty( $post_in ) ) {
	$p_args['post__in'] = explode( ',', $post_in );
}

switch ( $orderby ) {
	default:
	case 'default':
		$p_args['orderby'] = 'date';
		break;
	case 'random':
		$p_args['orderby'] = 'rand';
		break;
	case 'comment-count':
		$p_args['orderby'] = 'comment_count';
		break;
}

$posts = new WP_Query( $p_args );
?>
<?php if ( $posts->have_posts() ): ?>
    <ul>
		<?php while ( $posts->have_posts() ): ?>
			<?php
			$posts->the_post();
			$post_id = $posts->post->ID;
			?>
            <li>
				<?php echo get_the_post_thumbnail( $post_id, 'woocommerce_gallery_thumbnail', [ 'class' => 'img-fluid' ] ); ?>
                <?php echo $ht_posts['open']; ?>
                    <a href="<?php echo get_permalink(); ?>" title="<?php echo get_the_title(); ?>"><?php echo get_the_title(); ?></a>
                <?php echo $ht_posts['close']; ?>
				<?php if ( $date ): ?>
                    <span class="post-date"><?php echo get_the_date(); ?></span>
				<?php endif; ?>
            </li>
		<?php endwhile; ?>
		<?php wp_reset_postdata(); ?>
    </ul>
	<?php
	if ( $view_all && empty( $post_in ) ) {
		if ( $cat == - 1 || $cat == '' ) {
			if ( $post_type != 'post' ) {
				echo '<a href="' . get_post_type_archive_link( $post_type ) . '" class="view-all">' . esc_html( $labels->get_label( 'view-all-posts' ) ) . '</a>';
			}
		} else {
			echo '<a href="' . get_category_link( $cat ) . '" class="view-all">' . esc_html( $labels->get_label( 'view-all-posts' ) ) . '</a>';
		}
	}
	?>
<?php endif;