<?php
/**
 * Single post sidebar template part.
 *
 * Includes a dynamic Table of Contents,
 * trending posts and a topic cloud.
 *
 * @package pawwell
 */

defined( 'ABSPATH' ) || exit;
?>

<!-- Table of contents -->
<div class="widget widget-toc" id="tocWidget">
	<h3 class="widget-title">
		<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M8 6h13M8 12h13M8 18h13M3 6h.01M3 12h.01M3 18h.01"/></svg>
		<?php esc_html_e( 'In This Article', 'pawwell' ); ?>
	</h3>
	<ul class="toc-list" id="tocList" aria-label="<?php esc_attr_e( 'Table of contents', 'pawwell' ); ?>"></ul>
</div>

<!-- Trending posts -->
<div class="widget">
	<h3 class="widget-title">
		<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M12 3c1 3-1 5-2 6-1.5 1.5-3 3-3 6a5 5 0 0 0 10 0c0-2-1-4-2-5-1 1-2 1-2-1 0-2-1-4-1-6z"/></svg>
		<?php esc_html_e( 'Trending Now', 'pawwell' ); ?>
	</h3>
	<?php
	$trending_posts = new WP_Query( array(
		'posts_per_page'      => 4,
		'post__not_in'        => array( get_the_ID() ),
		'meta_key'            => 'post_views_count',
		'orderby'             => 'meta_value_num',
		'order'               => 'DESC',
		'ignore_sticky_posts' => 1,
	) );

	if ( ! $trending_posts->have_posts() ) {
		$trending_posts = new WP_Query( array(
			'posts_per_page'      => 4,
			'post__not_in'        => array( get_the_ID() ),
			'ignore_sticky_posts' => 1,
		) );
	}

	if ( $trending_posts->have_posts() ) :
		$rank = 0;
		while ( $trending_posts->have_posts() ) :
			$trending_posts->the_post();
			$rank++;
			$cats = get_the_category();
			$pcat = ! empty( $cats ) ? $cats[0] : null;
			$rtime = function_exists( 'pawwell_estimated_reading_time' ) ? pawwell_estimated_reading_time( get_the_content() ) : 5;
	?>
	<div class="popular-item">
		<a href="<?php the_permalink(); ?>" class="popular-thumb<?php echo ! has_post_thumbnail() ? ' no-image' : ''; ?>">
			<?php if ( has_post_thumbnail() ) : ?>
				<?php the_post_thumbnail( 'thumbnail' ); ?>
			<?php endif; ?>
			<span class="popular-num"><?php echo esc_html( $rank ); ?></span>
		</a>
		<div class="popular-content">
			<?php if ( $pcat ) : ?>
			<span class="popular-cat"><?php echo esc_html( $pcat->name ); ?></span>
			<?php endif; ?>
			<a href="<?php the_permalink(); ?>" class="popular-title"><?php the_title(); ?></a>
			<span class="popular-date">
				<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><circle cx="12" cy="12" r="9"/><path d="M12 7v5l3 2"/></svg>
				<?php echo esc_html( $rtime ); ?> <?php esc_html_e( 'min read', 'pawwell' ); ?>
			</span>
		</div>
	</div>
	<?php
		endwhile;
		wp_reset_postdata();
	endif;
	?>
</div>

<!-- Topics -->
<div class="widget">
	<h3 class="widget-title">
		<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true"><path d="M3 12V4a1 1 0 0 1 1-1h8l9 9-9 9-9-9z"/><circle cx="7.5" cy="7.5" r="1.4"/></svg>
		<?php esc_html_e( 'Topics', 'pawwell' ); ?>
	</h3>
	<div class="tag-cloud">
		<?php
		$tags = get_tags( array(
			'orderby' => 'count',
			'order'   => 'DESC',
			'number'  => 12,
		) );
		if ( ! empty( $tags ) ) :
			foreach ( $tags as $tag ) :
		?>
			<a href="<?php echo esc_url( get_tag_link( $tag->term_id ) ); ?>" class="tag"><?php echo esc_html( $tag->name ); ?></a>
		<?php
			endforeach;
		else :
			esc_html_e( 'No topics yet.', 'pawwell' );
		endif;
		?>
	</div>
</div>
