<?php
/**
 * Post excerpt.
 *
 * @package Kutby
 * @since   2.9.0
 */

declare(strict_types=1);

namespace Kutby\Cards\Parts;

use Kutby\Cards\Contracts\CardPartInterface;
use Kutby\Cards\Support\CardContext;

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

/**
 * Class Excerpt
 */
final class Excerpt implements CardPartInterface {

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

		$words   = (int) $ctx->get( 'excerpt_words', 22 );
		$excerpt = wp_trim_words( get_the_excerpt( $ctx->post() ), max( 5, $words ) );
		if ( '' === $excerpt ) {
			return;
		}
		?>
		<p class="kutby-card__excerpt"><?php echo esc_html( $excerpt ); ?></p>
		<?php
	}
}
