<?php
/**
 * Single post template
 *
 * @package Multi
 */

if ( ! defined( 'ABSPATH' ) ) {
	die;
}

get_header();

if ( have_posts() ) :
	while ( have_posts() ) :
		the_post();
		$this_post_id = get_the_id();

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

		$meta_cats   = get_theme_mod( 'multi_post_meta_categories', MULTIMAGAZINE_POST_META_CATEGORIES );
		$meta_author = get_theme_mod( 'multi_post_meta_author', MULTIMAGAZINE_POST_META_AUTHOR );
		$meta_date   = get_theme_mod( 'multi_post_meta_date', MULTIMAGAZINE_POST_META_DATE );
		$meta_any    = $meta_cats || $meta_author || $meta_date;

		$multimagazine_intro = '';
		if ( class_exists( 'MultiBuilder' ) ) {
			$multimagazine_intro = nl2br( trim( get_post_meta( $this_post_id, 'multi_post_intro', true ) ) );
		}

		$multimagazine_post_layout = get_post_meta( $this_post_id, 'multi_post_layout', true );
		if ( empty( $multimagazine_post_layout ) ) {
			$multimagazine_post_layout = get_theme_mod( 'multi_post_layout', MULTIMAGAZINE_POST_LAYOUT );
		}

		$multimagazine_show_featured_image = get_post_meta( $this_post_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 );
		}

		$load_related              = get_theme_mod( 'multi_load_related', MULTIMAGAZINE_LOAD_RELATED ) ?? false;
		$load_related_with_sidebar = get_theme_mod( 'multi_load_related_with_sidebar', MULTIMAGAZINE_LOAD_RELATED_WITH_SIDEBAR ) ?? false;

		$related = array();
		if ( $load_related || $load_related_with_sidebar ) {
			$related = multimagazine_get_related_posts_links( $post, 999 );
		}

		$multimagazine_post_type = get_post_type( get_the_id() );
		$title_pos_local = '';
		if ( 'post' === $multimagazine_post_type ) {
			$title_pos_global = get_theme_mod( 'multi_subheader_title', MULTIMAGAZINE_SUBHEADER_TITLE );
			$title_pos_local  = get_post_meta( get_the_id(), 'multi_show_title', true );
			$multimagazine_show_title = empty( $title_pos_local ) ? $title_pos_global : $title_pos_local;

		} else {
			$multimagazine_show_title = multimagazine_title_pos_default();
		}

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

		if ( 'full-width-header' === $multimagazine_post_layout ) :
			?>
			<header class="post-header">
				<?php if ( 'off' !== $multimagazine_show_title && false === strpos( $title_pos_local, 'subheader' ) ) : ?>
					<h1 class="post-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 wp_kses_post( $multimagazine_intro ); ?></p>
				<?php endif; ?>
				<?php if ( $meta_any ) : ?>
				<p class="post-meta <?php if ( 'off' === $multimagazine_show_title ) : ?>
					no-title<?php endif; ?>">
					<?php if ( 'off' !== $meta_date ) : ?>
						<span class="meta"><?php the_time( get_option( 'date_format' ) ); ?></span><?php endif; ?>
					<?php if ( 'off' !== $meta_author ) : ?>
						<span class="meta"> <?php esc_html_e( 'by', 'multimagazine' ); ?> <?php the_author(); ?></span><?php endif; ?>
					<?php if ( 'off' !== $meta_cats && 'post' === get_post_type() ) : ?>
						<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; ?>
			</header>
		<?php endif; ?>
		<?php if ( '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">
						<?php if ( 'off' !== $meta_cats ) : ?>
							<p class="article-head-cats"><?php the_category( ' ' ); ?></p><?php endif; ?>
						<?php if ( 'off' !== $multimagazine_show_title && false === strpos( $title_pos_local, 'subheader' ) ) : ?>
							<h1 class="article-head-title post-title"><?php the_title(); ?> <?php edit_post_link( esc_attr__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h1>
						<?php endif; ?>
						<p class="article-head-meta">
						<?php if ( 'off' !== $meta_date ) : ?>
							<span class="meta"><?php the_time( get_option( 'date_format' ) ); ?></span><?php endif; ?>
						<?php if ( 'off' !== $meta_author ) : ?>
							<span class="meta"><?php esc_html_e( 'by', 'multimagazine' ); ?> <?php the_author(); ?></span><?php endif; ?>
						<?php if ( comments_open() ) : ?>
							<span class="meta"><?php comments_popup_link( '0 comments', '1 comment', '% comments', 'comment-link image-header-comment-link' ); ?></span><?php endif; ?></p>
					</div>
				</div>
				<?php
				if ( ! multimagazine_breadcrumbs_in_subheader_pos() && 'off' !== get_post_meta( $this_post_id, 'multi_show_breadcrumbs', true ) ) {
					do_action( 'multimagazine_breadcrumbs' );
				}
				?>
				<?php
				$multimagazine_thumbnail_caption = get_post( get_post_thumbnail_id() )->post_excerpt;
				$multimagazine_intro             = get_post_meta( $this_post_id, 'multi_post_intro', true );
				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 wp_kses_post( $multimagazine_intro ); ?></p></div>
					<?php endif; ?>
				</div>
				<?php endif; ?>
			</header>
		<?php endif; ?>

		<?php if ( 'standard' !== $multimagazine_post_layout ) : ?>
		<div class="the-post-main">
		<?php endif; ?>
		<main class="main single">

			<article class="post post-content" data-permalink="<?php the_permalink(); ?>">
			<?php
			if ( 'full-width-header-image' === $multimagazine_post_layout ) {
				do_action( 'multimagazine_before_post_content' );
			}
			?>
		<?php

		if ( 'standard' === $multimagazine_post_layout ) :
			if ( 'off' !== $multimagazine_show_title && false === strpos( $multimagazine_show_title, 'subheader' ) ) :
				?>
				<h1 class="post-title standard"><?php the_title(); ?> <?php edit_post_link( esc_html__( 'Edit this entry', 'multimagazine' ), '', '' ); ?></h1>
			<?php endif; ?>
			<?php if ( $meta_any ) : ?>
				<p class="post-meta <?php if ( 'off' === $multimagazine_show_title ) : ?>
					no-title<?php endif; ?>">
					<?php if ( 'off' !== $meta_date ) : ?>
						<span class="meta"><?php the_time( get_option( 'date_format' ) ); ?></span><?php endif; ?>
					<?php if ( 'off' !== $meta_author ) : ?>
						<span class="meta"> <?php esc_html__( 'by', 'multimagazine' ); ?> <?php the_author(); ?></span><?php endif; ?>
					<?php if ( 'off' !== $meta_cats && 'post' === get_post_type() ) : ?>
						<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 if ( '' !== $multimagazine_intro ) : ?>
				<p class="post-intro"><?php echo wp_kses_post( $multimagazine_intro ); ?></p>
			<?php endif; ?>
		<?php endif; ?>
		<?php

		if ( 'standard' === $multimagazine_post_layout || 'full-width-header' === $multimagazine_post_layout ) :
			do_action( 'multimagazine_before_post_content' );
			if ( class_exists( 'MultiBuilder' ) ) {
				$multimagazine_video = get_post_meta( get_the_id(), 'multi_single_meta_video', true );
			}
			if ( ! empty( $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() && 'on' === $multimagazine_show_featured_image ) :
				?>
					<figure class="post-featured-image">
						<?php
						$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
						the_post_thumbnail( 'full' );

						$multimagazine_thumbnail_post = get_post( get_post_thumbnail_id() );
						if ( $multimagazine_thumbnail_post ) :
							$multimagazine_thumbnail_caption = $multimagazine_thumbnail_post->post_excerpt;
							if ( $multimagazine_thumbnail_caption ) :
								?>
							<div class="thumbnail-caption"><p><i class="m-icon m-icon-camera" aria-hidden="true"></i> <?php echo wp_kses_post( $multimagazine_thumbnail_caption ); ?></p></div>
								<?php
							endif;
						endif;
						?>
						</figure>
				<?php
			endif;
		endif;
		?>
		<?php get_template_part( 'template-parts/advert-top' ); ?>
		<?php the_content(); ?>
		<div class="post-end"></div>

	</article>

		<?php get_template_part( 'template-parts/advert-bottom' ); ?>
		<?php
		wp_link_pages(
			array(
				'before'         => '<p class="pages"><strong>' . esc_html__( 'Pages', 'multimagazine' ) . ':</strong> ',
				'after'          => '</p>',
				'next_or_number' => 'number',
				'aria_current'   => 'page',
			)
		);
		?>
		<?php if ( has_tag() ) : ?>
			<p class="tags"><?php esc_html_e( 'Tags', 'multimagazine' ); ?>: <?php the_tags( '' ); ?></p><?php endif; ?>
		<?php do_action( 'multimagazine_after_post_content' ); ?>

		<?php
		$show_author      = get_theme_mod( 'multi_show_post_author', MULTIMAGAZINE_SHOW_POST_AUTHOR );
		$show_author_link = get_theme_mod( 'multi_post_author_link', MULTIMAGAZINE_POST_AUTHOR_LINK );

		if ( $show_author ) :
			$author_id = get_the_author_meta( 'ID' );
			?>
		<div class="post-author">
			<span class="img-border"><?php echo wp_kses_post( get_avatar( $author_id ) ); ?></span>
			<div class="post-author-info">
				<h4 class="post-author-name">
					<?php if ( $show_author_link ) : ?>
						<a href="<?php echo esc_url( get_author_posts_url( $author_id ) ); ?>"><?php endif; ?>
					<?php the_author(); ?>
					<?php if ( $show_author_link ) : ?>
					</a><?php endif; ?>
				<?php if ( class_exists( 'MultiBuilder' ) && get_the_author_meta( 'profession' ) ) : ?>
					<br><span class="post-author-meta"><?php echo esc_html( get_the_author_meta( 'profession' ) ); ?></span>
				<?php endif; ?>
				</h4>
				<p class="post-author-description"><?php echo wp_kses_post( get_the_author_meta( 'description' ) ); ?></p>
				<?php
				if ( class_exists( 'MultiBuilder' ) && function_exists( 'multi_builder_get_user_social' ) ) :
					$profiles = multi_builder_get_user_social( $author_id );
					if ( count( $profiles ) ) :
						$social_classes = multimagazine_social_classes();
						?>
						<ul class="social <?php echo esc_attr( $social_classes ); ?>">
						<?php
						foreach ( $profiles as $k => $v ) :
							if ( ! empty( $v ) ) :
								?>
							<li><a href="<?php echo esc_url( $v ); ?>" target="_blank" rel="noopener" class="social-link social-link-<?php echo esc_attr( $k ); ?>"><?php echo wp_kses_post( multimagazine_social_icon( $k ) ); ?></a></li>
						<?php endif; ?>
						<?php endforeach; ?>
					</ul>
				<?php endif; ?>
				<?php endif; ?>
			</div>
		</div>
	<?php endif; ?>

	<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();
		}
		$next_post = get_adjacent_post( false, '', false );
		if ( empty( $next_post ) ) {
			$next_post = multimagazine_get_oldest_post();
		}

		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 post', '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 post', 'multimagazine' ); ?>"></span>
					<?php echo esc_html( $next_post->post_title ); ?>
				</span>
			</a>
			<?php
		endif;
		?>
	</nav>
		<?php
		$current_post_type = get_post_type();

		$show_related = 'post' === $current_post_type ? get_theme_mod( 'multi_show_related', MULTIMAGAZINE_SHOW_RELATED ) : false;
		if ( ! empty( $show_related ) ) {
			multimagazine_related_posts( $post, $show_related );
		}

		$show_featured = get_theme_mod( 'multi_show_featured', MULTIMAGAZINE_SHOW_FEATURED );
		if ( ! empty( $show_featured ) ) {
			if ( 'project' === get_post_type( $post ) ) {
				multimagazine_featured_projects( $post, $show_featured );
			} else {
				multimagazine_featured_posts( $post, $show_featured );
			}
		}

		$show_latest = 'post' === $current_post_type ? get_theme_mod( 'multi_show_latest', MULTIMAGAZINE_SHOW_LATEST ) : false;
		if ( ! empty( $show_latest ) ) {
			multimagazine_latest_posts( $post, $show_latest );
		}

		comments_template();

		$load_related              = get_theme_mod( 'multi_load_related', MULTIMAGAZINE_LOAD_RELATED ) ?? false;
		$load_related_with_sidebar = get_theme_mod( 'multi_load_related_with_sidebar', MULTIMAGAZINE_LOAD_RELATED_WITH_SIDEBAR ) ?? false;
		$related_links             = implode( ',', $related );
		if ( $load_related && ! $load_related_with_sidebar ) :
			?>
	<div class="related-content" data-related-links="<?php echo esc_attr( $related_links ); ?>"></div>
	<?php endif; ?>
</main>
		<?php
	endwhile;
endif;
?>

<?php
$multimagazine_sidebar_position = get_post_meta( $multimagazine_current_page_id, 'multi_sidebar_position', true );
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>
</div>
<?php endif; ?>

<?php if ( $load_related && $load_related_with_sidebar ) : ?>
	</div>

	<div class="related-content with-sidebars" data-related-links="<?php echo esc_attr( $related_links ); ?>"></div>
<?php endif; ?>
<?php get_footer(); ?>
