<?php
/**
 * Template part for displaying posts
 *
 * @link https://developer.wordpress.org/themes/basics/template-hierarchy/
 *
 * @package blueboxed
 */

?>

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
	<header class="entry-header mb-2">
		<?php
		$is_single_post = is_singular( 'post' );

		if (is_singular()):
			the_title('<h1 class="entry-title">', '</h1>');
		else:
			the_title('<h2 class="entry-title"><a href="' . esc_url(get_permalink()) . '" rel="bookmark">', '</a></h2>');
		endif;

		if ( $is_single_post ) :
			$word_count      = str_word_count( wp_strip_all_tags( get_post_field( 'post_content', get_the_ID() ) ) );
			$reading_minutes = max( 1, (int) ceil( $word_count / 220 ) );
			$categories_list = get_the_category_list( esc_html__( ', ', 'blueboxed' ) );
			?>
			<div class="blueboxed-single-kpis" aria-label="<?php echo esc_attr__( 'Post highlights', 'blueboxed' ); ?>">
				<?php if ( $categories_list ) : ?>
					<span class="blueboxed-chip blueboxed-chip-category"><?php echo wp_kses_post( $categories_list ); ?></span>
				<?php endif; ?>
				<span class="blueboxed-chip"><?php echo esc_html( sprintf( __( '%d min read', 'blueboxed' ), $reading_minutes ) ); ?></span>
				<span class="blueboxed-chip"><?php echo esc_html( get_the_date() ); ?></span>
			</div>
			<?php
		endif;

		if ('post' === get_post_type()):
			?>
			<div class="entry-meta">
				<?php
				blueboxed_posted_on();
				blueboxed_posted_by();
				?>
			</div><!-- .entry-meta -->
		<?php endif; ?>
	</header><!-- .entry-header -->
	<div class="content-posts">
		<?php blueboxed_post_thumbnail(); ?>

		<div class="entry-content">
			<?php
			if (is_single()):
				echo '<div class="entry-content-inner">';
				the_content();
				echo '</div>';

				wp_link_pages(
					array(
						'before' => '<nav class="page-links" aria-label="' . esc_attr__('Page', 'blueboxed') . '">',
						'after' => '</nav>',
						/* translators: %: Page number. */
						'pagelink' => esc_html__('Page %', 'blueboxed'),
					)
				);

			else:
				$card_categories = get_the_category_list( esc_html__( ', ', 'blueboxed' ) );
				$card_words      = str_word_count( wp_strip_all_tags( get_the_excerpt() ) );
				$card_minutes    = max( 1, (int) ceil( $card_words / 220 ) );
				if ( $card_categories ) :
					?>
					<div class="blueboxed-card-meta">
						<span class="blueboxed-card-chip"><?php echo wp_kses_post( $card_categories ); ?></span>
						<span class="blueboxed-card-chip"><?php echo esc_html( sprintf( __( '%d min read', 'blueboxed' ), $card_minutes ) ); ?></span>
					</div>
					<?php
				endif;
				the_excerpt();
				?>
				<div class="readmore-meta mb-2">
					<a href="<?php the_permalink(); ?>">
						<?php echo __('Read More', 'blueboxed') ?>
					</a>
				</div>
				<?php
			endif;
			?>
		</div><!-- .entry-content -->
	</div>

</article><!-- #post-<?php the_ID(); ?> -->
