<?php
/**
 * Article meta (author / date).
 *
 * @package Kutby
 * @since   2.8.0
 */

declare(strict_types=1);

namespace Kutby\Hero\Components;

use Kutby\Hero\Contracts\HeroComponentInterface;
use Kutby\Hero\Support\HeroContext;
use WP_Post;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Class ArticleMeta
 */
final class ArticleMeta implements HeroComponentInterface {

	/**
	 * {@inheritdoc}
	 */
	public static function render( HeroContext $ctx, array $args = array() ): void {
		if ( ! $ctx->bool( 'show_meta' ) ) {
			return;
		}

		$post = $args['post'] ?? null;
		if ( ! $post instanceof WP_Post ) {
			return;
		}

		$show_author = $ctx->bool( 'show_author' );
		$show_date   = $ctx->bool( 'show_date' );
		if ( ! $show_author && ! $show_date ) {
			return;
		}
		?>
		<div class="kutby-hero-meta">
			<?php if ( $show_author ) : ?>
				<span class="kutby-hero-meta__author">
					<?php echo esc_html( get_the_author_meta( 'display_name', (int) $post->post_author ) ); ?>
				</span>
			<?php endif; ?>
			<?php if ( $show_date ) : ?>
				<time class="kutby-hero-meta__date" datetime="<?php echo esc_attr( get_the_date( DATE_W3C, $post ) ); ?>">
					<?php echo esc_html( get_the_date( '', $post ) ); ?>
				</time>
			<?php endif; ?>
		</div>
		<?php
	}
}
