<?php

/**
 * AUX Recent Posts block
 * @since 1.0.0
 */

use WP_AUX\Services\AuxCF\BlocksService;
use WP_AUX\Services\StoryService;
use WP_AUX\Services\PostService;

// Get the recent posts; the function has a default limit of 3
$posts = aux_recent_posts(
  aux_get_field('recent_posts_types'),
  [
    'category' => aux_get_field('recent_posts_category'),
    'post_tag' => aux_get_field('recent_posts_tag')
  ]
);

$thePosts = '';
foreach ($posts as $post) {
  $postCard = PostService::postCard($post, true, true, !empty($sectionIntro));
  $postCard->setSubheader(StoryService::getOriginalDate($post) . ' &bull; ' . PostService::pageReadTime($post));

  $thePosts .= $postCard;
}

$cardsContent = <<<HTML
  <div class="card-container">
    $thePosts
  </div>
HTML;

$section = BlocksService::getSection('card-group');
$section->addComponentDataValue('cards', count($posts));
$section->setContent($cardsContent);

echo $section;
