<?php
/**
 * Page template
 *
 * @package Multi
 */

get_header();

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();

		$multimagazine_current_page_id = get_the_id();
		$multimagazine_post_layout     = get_post_meta( $multimagazine_current_page_id, 'multi_post_layout', true );
		$is_cart               = get_the_ID() === absint( get_option( 'woocommerce_cart_page_id' ) );
		$is_checkout           = get_the_ID() === absint( get_option( 'woocommerce_checkout_page_id' ) );
		$is_thanks             = class_exists( 'WooCommerce' ) && is_order_received_page();

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

		if ( $is_cart || $is_checkout || $is_thanks ) :
			?>
			<div class="checkout">
				<?php
				get_template_part( 'woocommerce/checkout-steps' );
				the_content();
				?>
			</div>
			<?php
		else :

			if ( empty( $multimagazine_post_layout ) ) {
				$multimagazine_post_layout = get_theme_mod( 'multi_post_layout', MULTIMAGAZINE_POST_LAYOUT );
				if ( 'full-width-header-image' === $multimagazine_post_layout ) {
					$multimagazine_post_layout = 'standard';
				}
			}

			$multimagazine_show_featured_image = get_post_meta( $multimagazine_current_page_id, 'multi_show_featured_image', true );
			if ( empty( $multimagazine_show_featured_image ) ) {
				$multimagazine_show_featured_image = get_theme_mod( 'multi_show_featured_image', MULTIMAGAZINE_SHOW_FEATURED_IMAGE );
			} else {
				$multimagazine_show_featured_image = 'on' === $multimagazine_show_featured_image;
			}

			$multimagazine_intro = get_post_meta( $multimagazine_current_page_id, 'multi_post_intro', true );

			if ( 'standard' !== $multimagazine_post_layout ) :
				?>
				<div class="page-wrapper">
				<?php
			endif;

			if ( 'full-width-header' === $multimagazine_post_layout ) :
				?>
			<header class="post-header">
			<h1 class="post-title"><?php the_title(); ?> <?php edit_post_link( esc_html__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h1>
				<?php
				if ( '' !== $multimagazine_intro ) :
					?>
					<p class="post-intro"><?php echo esc_html( $multimagazine_intro ); ?></p>
					<?php
				endif;
				?>
			</header>

				<?php
			elseif ( 'full-width-header-image' === $multimagazine_post_layout ) :
				?>

				<header class="post-header with-image">
					<div class="article-head">
					<?php
					if ( has_post_thumbnail() ) :
						?>
						<div class="article-head-image">
							<?php the_post_thumbnail( 'full' ); ?>
						</div>
						<?php
					endif;
					?>
						<div class="article-head-content">
							<h1 class="article-head-title post-title"><?php the_title(); ?> <?php edit_post_link( esc_attr__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h1>
						</div>
					</div>
					<?php
					if ( ! multimagazine_breadcrumbs_in_subheader_pos() && 'off' !== get_post_meta( get_the_id(), 'multi_show_breadcrumbs', true ) ) {
						do_action( 'multimagazine_breadcrumbs' );
					}
					?>
					<?php
					$multimagazine_thumbnail_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
					if ( $multimagazine_thumbnail_caption || $multimagazine_intro ) :
						?>
					<div class="article-neck">
						<?php
						$multimagazine_thumbnail_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
						if ( $multimagazine_thumbnail_caption ) :
							?>
							<div class="thumbnail-caption"><p><i class="m-icon m-icon-camera" aria-hidden="true"></i> <?php echo esc_html( $multimagazine_thumbnail_caption ); ?></p></div>
							<?php
						endif;
						if ( '' !== $multimagazine_intro ) :
							?>
						<div class="article-head-excerpt"><p><?php echo esc_html( $multimagazine_intro ); ?></p></div>
						<?php endif; ?>
					</div>
					<?php endif; ?>
				</header>


			<?php endif; ?>

		<div class="main single" role="main">

			<article class="page page-content">
				<?php
				if ( 'standard' === $multimagazine_post_layout ) :
					$multimagazine_show_title = multimagazine_title_pos_default();
					if ( 'off' !== $multimagazine_show_title && ! empty( get_the_title() ) ) :
						?>
						<h1 class="page-title"><?php the_title(); ?> <?php edit_post_link( esc_html__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h1>
						<?php
					endif;
					?>
					<?php
					if ( '' !== $multimagazine_intro ) :
						?>
						<p class="post-intro"><?php echo esc_html( $multimagazine_intro ); ?></p>
						<?php
					endif;
					?>
				<?php endif; ?>
				<?php
				if ( 'standard' === $multimagazine_post_layout || 'full-width-header' === $multimagazine_post_layout ) :
					$multimagazine_video = get_post_meta( $multimagazine_current_page_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
					elseif ( has_post_thumbnail() && $multimagazine_show_featured_image ) :
						?>
						<p>
						<?php
						$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
						the_post_thumbnail( 'full' );
						?>
						</p>
						<?php
					endif;
				endif;
				?>
				<?php
				the_content();
				wp_link_pages(
					array(
						'before'         => '<p class="pages"><strong>' . esc_html__( 'Pages', 'multimagazine' ) . ':</strong> ',
						'after'          => '</p>',
						'next_or_number' => 'number',
					)
				);
				?>
			</article>
			<?php if ( get_theme_mod( 'multi_pages_show_page_nav', MULTIMAGAZINE_PAGES_SHOW_PAGE_NAV ) ) : ?>
			<nav class="post-nav" aria-label="<?php esc_attr_e( 'Post navigation', 'multimagazine' ); ?>">
				<?php
				$prev_post = get_adjacent_post( false, '', true );
				if ( empty( $prev_post ) ) {
					$prev_post = multimagazine_get_newest_post( 'page' );
				}
				$next_post = get_adjacent_post( false, '', false );
				if ( empty( $next_post ) ) {
					$next_post = multimagazine_get_oldest_post( 'page' );
				}

				if ( ! empty( $prev_post ) ) :
					$prev_id = $prev_post->ID;
					?>
					<a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>" class="post-nav-link prev cf">
						<i class="m-icon m-icon-long-arrow-left" aria-hidden="true"></i>
						<span class="post-nav-link-content">
							<span class="info" data-label="<?php esc_attr_e( 'Previous page', 'multimagazine' ); ?>"></span>
							<?php echo esc_html( $prev_post->post_title ); ?>
						</span>
					</a>
					<?php
				endif;

				if ( ! empty( $next_post ) ) :
					$next_id = $next_post->ID;
					?>
					<a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>" class="post-nav-link next cf">
						<i class="m-icon m-icon-long-arrow-right" aria-hidden="true"></i>
						<span class="post-nav-link-content">
							<span class="info" data-label="<?php esc_attr_e( 'Next page', 'multimagazine' ); ?>"></span>
							<?php echo esc_html( $next_post->post_title ); ?>
						</span>
					</a>
					<?php
				endif;
				?>
			</nav>
			<?php endif; ?>

				<?php comments_template(); ?>
		</div>

			<?php
			if ( ! $is_cart && ! $is_checkout && ! $is_thanks ) {
				$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
				global $multimagazine_sidebar_pos_global;
				if ( empty( $multimagazine_sidebar_position ) ) {
					$multimagazine_sidebar_position = $multimagazine_sidebar_pos_global;
				}
				if ( 'off' !== $multimagazine_sidebar_position ) {
					get_sidebar();
				}
			}

			if ( 'standard' !== $multimagazine_post_layout ) :
				?>
		</div><!-- Page wrapper -->
		<?php endif; ?>
	<?php endif; ?>
		<?php
	endwhile;
endif;
?>
<?php get_footer(); ?>
