<?php
/**
 * Template name: Magazine (horizontal)
 *
 * @package Multi
 */

get_header();
get_template_part( 'template-parts/top-slider' );
get_template_part( 'template-parts/content-start' );

$multimagazine_col_class        = 'col3';
$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
$multimagazine_meta_cats        = get_theme_mod( 'multi_post_meta_categories', MULTIMAGAZINE_POST_META_CATEGORIES );
$multimagazine_meta_author      = get_theme_mod( 'multi_post_meta_author', MULTIMAGAZINE_POST_META_AUTHOR );
$multimagazine_meta_date        = get_theme_mod( 'multi_post_meta_date', MULTIMAGAZINE_POST_META_DATE );
$multimagazine_meta_any         = 'off' !== $multimagazine_meta_cats || 'off' !== $multimagazine_meta_author || 'off' !== $multimagazine_meta_date;

if ( empty( $multimagazine_sidebar_position ) ) {
	$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
}

if ( 'off' !== $multimagazine_sidebar_position ) {
	$multimagazine_sidebar = get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) ? get_post_meta( get_the_id(), 'multi_custom_sidebar', true ) : 'default';
	if ( 'no' !== $multimagazine_sidebar ) {
		$multimagazine_col_class = 'col2';
	}
}

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();
		?>
	<div class="main magazine" role="main">
		<?php get_template_part( 'post-list-parts/content' ); ?>

		<div class="in-page-search"><?php get_search_form(); ?></div>
		<?php
		$multimagazine_options = array(
			'hierarchical' => 0,
			'parent'       => 0,
		);
		$multimagazine_cats    = get_categories( $multimagazine_options );
		foreach ( $multimagazine_cats as $multimagazine_cat ) :
			?>
			<h2 class="spec-heading <?php echo esc_attr( multimagazine_heading_style() ); ?>"><span><a href="<?php echo esc_url( get_category_link( $multimagazine_cat->term_id ) ); ?>"><?php echo esc_html( $multimagazine_cat->name ); ?></a></span></h2>
			<div class="columns">
			<?php
			$multimagazine_post_options = array(
				'post_type'      => 'post',
				'post_status'    => 'publish',
				'posts_per_page' => 3,
				'cat'            => $multimagazine_cat->term_id,
			);
			$multimagazine_posts        = new WP_Query( $multimagazine_post_options );
			if ( $multimagazine_posts->have_posts() ) :
				while ( $multimagazine_posts->have_posts() ) :
					$multimagazine_posts->the_post();
					?>
					<article <?php post_class( 'col col3' ); ?>>
						<?php
						$multimagazine_video = get_post_meta( get_the_id(), 'multi_single_meta_video', true );
						if ( $multimagazine_video ) :
							?>
						<div class="video"><?php echo wp_oembed_get( esc_url( $multimagazine_video ) );  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
							<?php
						else :
							if ( has_post_thumbnail() ) :
								?>
								<div class="img"><a href="<?php the_permalink(); ?>"><span class="img-border"><?php the_post_thumbnail( 'multimagazine-thumbnail-medium' ); ?></span></a></div>
								<?php
							endif;
						endif;
						?>
						<h3 class="post-title"><a href="<?php the_permalink(); ?>" rel="bookmark"><?php multimagazine_the_title(); ?></a> <?php edit_post_link( esc_html__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h3>
						<?php if ( $multimagazine_meta_any || comments_open() ) : ?>
						<p class="post-meta">
							<?php if ( 'off' !== $multimagazine_meta_date ) : ?>
								<span class="meta"><?php the_time( get_option( 'date_format' ) ); ?></span><?php endif; ?>
							<?php if ( 'off' !== $multimagazine_meta_author ) : ?>
								<span class="meta"><?php esc_html_e( 'by', 'multimagazine' ); ?> <?php the_author(); ?></span><?php endif; ?>
							<?php if ( 'off' !== $multimagazine_meta_cats && 0 !== count( get_the_category() ) ) : ?>
								<span class="meta"><?php the_category( ', ' ); ?></span><?php endif; ?>
							<?php if ( comments_open() ) : ?>
								<span class="meta"><?php comments_popup_link( '0', '1', '%', 'comment-link' ); ?></span>
							<?php endif; ?>
						</p>
						<?php endif; ?>
						<?php echo wp_kses_post( multimagazine_custom_excerpt( 30, $post ) ); ?>
						<p class="more"><a href="<?php the_permalink(); ?>"><span class="screen-reader-text"><?php the_title(); ?> - </span><?php esc_html_e( 'Read more', 'multimagazine' ); ?></a></p>
					</article>
					<?php
				endwhile;
				wp_reset_postdata();
			endif;
			?>
			</div>
		<?php endforeach; ?>
	</div>
		<?php
	endwhile;
endif;
?>
<?php
if ( 'off' !== $multimagazine_sidebar_position ) {
	get_sidebar();
}
?>
<?php get_footer(); ?>
