<?php
/**
 * Latest posts functions
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}
/**
 * Displays latest posts
 *
 * @param object  $post Current post object.
 * @param integer $limit Number of posts to show.
 */
function multimagazine_latest_posts( $post, $limit ) {
	$cids                = wp_get_post_categories( $post->ID );
	$list_layout         = get_theme_mod( 'multi_related_layout', MULTIMAGAZINE_RELATED_LAYOUT );
	$multimagazine_heading_style = multimagazine_heading_style();

	$multimagazine_options = array(
		'post_type'           => 'post',
		'post_status'         => 'publish',
		'posts_per_page'      => $limit,
		'post__not_in'        => array( $post->ID ),
		'ignore_sticky_posts' => true,
	);
	$query         = new WP_Query( $multimagazine_options );
	if ( $query->have_posts() ) :
		if ( 'slider' === $list_layout ) :
				multimagazine_post_slider( array( $query ), __( 'Latest posts', 'multimagazine' ), 'latest-posts' );
		else :
			switch ( $list_layout ) {
				case 'grid':
					$container_class = 'columns';
					$item_class      = 'col col3';
					break;
				case 'list':
					$container_class = 'scrolled-list scrollable-fade';
					$item_class      = 'scrolled-list-item';
					break;
			}
			?>
			<div class="latest-posts additional-post-list">
				<h2 class="spec-heading <?php echo esc_attr( $multimagazine_heading_style ); ?>"><span><?php esc_html_e( 'Latest posts', 'multimagazine' ); ?></span></h2>
				<div class="<?php echo esc_attr( $container_class ); ?>">
					<?php
					while ( $query->have_posts() ) :
						$query->the_post();
						?>
						<article class="related-post <?php echo esc_attr( $item_class ); ?>">
						<?php if ( has_post_thumbnail() ) : ?>
						<div class="img"><a href="<?php the_permalink(); ?>"><?php the_post_thumbnail( 'multimagazine-thumbnail-slider', array() ); ?></a></div>
						<?php endif; ?>
						<h3 class="post-title"><a href="<?php the_permalink(); ?>"><?php multimagazine_the_title(); ?></a></h3>
							<?php
							if ( ! has_post_thumbnail() ) {
								echo wp_kses_post( multimagazine_custom_excerpt( 15, $post ) );
							}
							?>
						</article>
							<?php
					endwhile;
					?>
				</div>
			</div>
			<?php
		endif;
	endif;
	wp_reset_postdata();
}
