<?php
/**
 * Template part for displaying related posts in the sidebar
 *
 * @package MarComm-AUX
 * @since 1.0.0
 */
declare(strict_types=1);

use WP_AUX\Services\PostService;

$relatedPosts = aux_related_posts(['post_tag', 'category']);

if (!count($relatedPosts)) {
  return;
}

$label = PostService::getTypeLabelForPost();
?>
<div class="sidebar-item">
  <div class="sidebar-feed">
    <h2 class="list-heading">Related <?php echo $label; ?></h2>

    <span class="divider-hash gray-300 divider--mini"></span>
    <?php
    foreach ($relatedPosts as $relatedPost) {
      $card = PostService::postCard($relatedPost, true, false, true);
      $card->setType('sidebar');

      echo $card;
    }
    ?>
    <a href="<?php echo get_post_type_archive_link(get_post_type()); ?>" class="btn btn--primary-alt ">
      View All <?php echo $label; ?>
    </a>
  </div>
</div>
