<?php
/**
 * Post card template.
 *
 * @package Zluck_Ajax_Posts_Filter_Elementor
 * @var WP_Post $post
 * @var array   $settings
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

$aapf_settings = isset( $settings ) ? $settings : array();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'aapf-post-card' ); ?>>
	<?php if ( ! empty( $aapf_settings['show_image'] ) && 'yes' === $aapf_settings['show_image'] && has_post_thumbnail() ) : ?>
		<div class="aapf-post-thumbnail">
			<a href="<?php echo esc_url( get_permalink() ); ?>" aria-label="<?php echo esc_attr( get_the_title() ); ?>">
				<?php
				$aapf_image_size = isset( $aapf_settings['image_size'] ) ? $aapf_settings['image_size'] : 'medium';
				the_post_thumbnail(
					$aapf_image_size,
					array(
						'alt' => get_the_title(),
						'loading' => 'lazy',
					)
				);
				?>
			</a>
		</div>
	<?php endif; ?>

	<div class="aapf-post-content">
		<?php if ( ! empty( $aapf_settings['show_title'] ) && 'yes' === $aapf_settings['show_title'] ) : ?>
			<?php
			$aapf_title_tag = isset( $aapf_settings['title_tag'] ) ? $aapf_settings['title_tag'] : 'h3';
			?>
			<<?php echo esc_attr( $aapf_title_tag ); ?> class="aapf-post-title">
				<a href="<?php echo esc_url( get_permalink() ); ?>"><?php echo esc_html( get_the_title() ); ?></a>
			</<?php echo esc_attr( $aapf_title_tag ); ?>>
		<?php endif; ?>

		<?php if ( ! empty( $aapf_settings['show_meta'] ) && 'yes' === $aapf_settings['show_meta'] ) : ?>
			<div class="aapf-post-meta">
				<?php
				$aapf_meta_items = isset( $aapf_settings['meta_items'] ) ? $aapf_settings['meta_items'] : array();
				aapf_render_meta_items( $aapf_meta_items, $aapf_settings );
				?>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $aapf_settings['show_excerpt'] ) && 'yes' === $aapf_settings['show_excerpt'] ) : ?>
			<div class="aapf-post-excerpt">
				<?php
				$aapf_excerpt_length = isset( $aapf_settings['excerpt_length'] ) ? absint( $aapf_settings['excerpt_length'] ) : 20;
				$aapf_excerpt = get_the_excerpt();

				if ( $aapf_excerpt_length > 0 ) {
					$aapf_excerpt = wp_trim_words( $aapf_excerpt, $aapf_excerpt_length, '...' );
				}

				echo wp_kses_post( $aapf_excerpt );
				?>
			</div>
		<?php endif; ?>

		<?php if ( ! empty( $aapf_settings['show_read_more'] ) && 'yes' === $aapf_settings['show_read_more'] ) : ?>
		<div class="aapf-post-read-more">
			<a href="<?php echo esc_url( get_permalink() ); ?>" class="aapf-read-more-link">
					<?php
					$aapf_read_more_text = isset( $aapf_settings['read_more_text'] ) ? $aapf_settings['read_more_text'] : esc_html__( 'Read More', 'zluck-ajax-posts-filter-for-elementor' );
					echo esc_html( $aapf_read_more_text );
					?>
				</a>
			</div>
		<?php endif; ?>
	</div>
</article>

