<?php
/**
 * Secondary compact news list for news and category pages.
 *
 * @package Gustavo_Portfolio
 */

$gustavoo_args = isset( $args ) && is_array( $args ) ? $args : array();
$gustavoo_query_args = array(
	'post_type'           => 'post',
	'post_status'         => 'publish',
	'posts_per_page'      => 4,
	'post__not_in'        => array_map( 'absint', $gustavoo_args['exclude'] ?? array() ),
	'ignore_sticky_posts' => true,
	'orderby'             => 'date',
	'order'               => 'DESC',
);

if ( ! empty( $gustavoo_args['category_id'] ) ) {
	$gustavoo_query_args['cat'] = absint( $gustavoo_args['category_id'] );
}

$gustavoo_latest_posts = new WP_Query( $gustavoo_query_args );

if ( ! $gustavoo_latest_posts->have_posts() ) {
	return;
}
?>
<section class="latest-news-list" aria-labelledby="latest-news-list-title">
	<h2 id="latest-news-list-title" class="latest-news-list__heading"><?php esc_html_e( 'Últimas notícias', 'gustavoo-portfolio' ); ?></h2>
	<div class="latest-news-list__items">
		<?php while ( $gustavoo_latest_posts->have_posts() ) : $gustavoo_latest_posts->the_post(); ?>
			<?php $gustavoo_categories = get_the_category(); $gustavoo_category = $gustavoo_categories ? $gustavoo_categories[0] : null; $gustavoo_image_url = gustavoo_portfolio_get_news_image_url(); ?>
			<article <?php post_class( 'latest-news-list__item' ); ?>>
				<a class="latest-news-list__image" href="<?php the_permalink(); ?>" aria-hidden="true" tabindex="-1">
					<?php if ( $gustavoo_image_url ) : ?><img src="<?php echo esc_url( $gustavoo_image_url ); ?>" loading="lazy" decoding="async" alt=""><?php endif; ?>
				</a>
				<div class="latest-news-list__body">
					<?php if ( $gustavoo_category ) : ?><a class="latest-news-card__category" href="<?php echo esc_url( get_category_link( $gustavoo_category ) ); ?>"><?php echo esc_html( $gustavoo_category->name ); ?></a><?php endif; ?>
					<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
					<time datetime="<?php echo esc_attr( get_the_date( DATE_W3C ) ); ?>"><?php echo esc_html( get_the_date( 'D, d/m/Y · H:i' ) ); ?></time>
				</div>
			</article>
		<?php endwhile; ?>
	</div>
</section>
<?php wp_reset_postdata(); ?>
