<?php
/**
 * Top slider template
 *
 * @package Multi
 */

global $post;
if (is_home()) {
	$blog_id = get_option( 'page_for_posts' );
	$multimagazine_show_top_slider = get_post_meta( $blog_id, 'multi_show_top_slider', true );
} else {
	$multimagazine_show_top_slider = empty( $post ) ? false : get_post_meta( $post->ID, 'multi_show_top_slider', true );
}
if ( empty( $multimagazine_show_top_slider ) ) {
	$multimagazine_show_top_slider = get_theme_mod( 'multi_top_slider', MULTIMAGAZINE_TOP_SLIDER );
}

if ( 'off' !== $multimagazine_show_top_slider ) {
	$limit = 10;
	switch ( $multimagazine_show_top_slider ) {
		case 'featured':
			$multimagazine_options = array(
				'post_type'           => 'post',
				'post_status'         => 'publish',
				'posts_per_page'      => $limit,
				'post__not_in'        => array( $post->ID ),
				'meta_key'            => 'multi_featured', // phpcs:ignore
				'meta_value'          => 1, // phpcs:ignore
				'ignore_sticky_posts' => 1,
			);
			break;
		case 'related':
			$cids          = wp_get_post_categories( $post->ID );
			$multimagazine_options = array(
				'post_type'           => 'post',
				'post_status'         => 'publish',
				'category__in'        => $cids,
				'posts_per_page'      => $limit,
				'post__not_in'        => array( $post->ID ),
				'ignore_sticky_posts' => 1,
			);
			break;
		case 'latest':
			$multimagazine_options = array(
				'post_type'           => 'post',
				'post_status'         => 'publish',
				'posts_per_page'      => $limit,
				'post__not_in'        => array( $post->ID ),
				'ignore_sticky_posts' => 1,
			);
			break;
		default:
			$multimagazine_options = false;
	}

	if ( ! empty( $multimagazine_options ) ) {
		$items = new WP_Query( $multimagazine_options );
		if ( $items->have_posts() ) :
			?>
		<div class="top-slider">
			<p class="controls"><a href="#" class="top-slider-arrow prev m-icon m-icon-long-arrow-left" aria-label="<?php esc_attr_e( 'Previous', 'multimagazine' ); ?>"></a><a href="#" class="top-slider-arrow next m-icon m-icon-long-arrow-right" aria-label="<?php esc_attr_e( 'Next', 'multimagazine' ); ?>"></a></p>
			<div class="slider-box splide">
				<div class="splide__track">
					<div class="splide__list">
				<?php
				while ( $items->have_posts() ) :
					$items->the_post();
					?>
					<article class="top-slider-item clickable splide__slide">
						<?php if ( has_post_thumbnail() ) : ?>
							<div class="top-slider-item-thumbnail"><a href="<?php the_permalink(); ?>"><span class="img-border">
							<?php
							the_post_thumbnail(
								'multimagazine-thumbnail-mini',
								array(
									'width'  => 100,
									'height' => 63,
								)
							);
							?>
							</span></a></div>
						<?php endif; ?>
						<p class="top-slider-item-title"><a href="<?php the_permalink(); ?>" class="main-link"><?php multimagazine_the_title(); ?></a></p>
						<p><?php echo wp_kses_post( multimagazine_custom_excerpt( 10, $post ) ); ?><a href="<?php the_permalink(); ?>" class="top-slider-more"><span class="screen-reader-text"><?php the_title(); ?> - </span><?php esc_html_e( 'Read more', 'multimagazine' ); ?></a></p>
					</article>
				<?php endwhile; ?>
					</div>
				</div>
			</div>
		</div>
			<?php
		endif;
		wp_reset_postdata();
	}
}
