<?php
if ( ! defined( 'ABSPATH' ) ) exit;

/**
 * Safe defaults
 */
$gridpress_card_data        = $gridpress_card_data ?? [];
$gridpress_display_settings = $gridpress_display_settings ?? [];

/**
 * Settings flags (prevents undefined index warnings)
 */
$gridpress_show_terms = !empty($gridpress_display_settings['show_terms']) && $gridpress_display_settings['show_terms'] === 'on';
$gridpress_show_date  = !empty($gridpress_display_settings['show_date']) && $gridpress_display_settings['show_date'] === 'on';

/**
 * Base variables
 */
$gridpress_terms_class = isset($gridpress_terms_class) ? $gridpress_terms_class : '';
$gridpress_post_date   = !empty($gridpress_card_data['post_date'])
    ? str_replace('/', '-', $gridpress_card_data['post_date'])
    : '';

/**
 * Terms logic
 */
$gridpress_terms_str = '';

if ( ! empty($gridpress_card_data['type']) && $gridpress_card_data['type'] === 'post' ) {

    if ( ! empty($gridpress_display_settings['top_filter_tax']) ) {

        $gridpress_taxonomy   = $gridpress_display_settings['top_filter_tax'];
        $gridpress_tax_terms  = wp_get_post_terms(
            $gridpress_card_data['id'],
            $gridpress_taxonomy,
            ['fields' => 'names']
        );

        if ( ! is_wp_error($gridpress_tax_terms) && ! empty($gridpress_tax_terms) ) {
            $gridpress_terms_str = implode(', ', $gridpress_tax_terms);
        }

        $gridpress_terms_class = apply_filters(
            'gridpress_card_item_term_classes',
            $gridpress_terms_class,
            $gridpress_taxonomy,
            $gridpress_card_data['id']
        );
    }

} else {
    $gridpress_terms_str = ! empty($gridpress_card_data['category'])
        ? sanitize_text_field($gridpress_card_data['category'])
        : '';
}
?>

<div class="<?php echo esc_attr(trim('jc-cards ' . $gridpress_terms_class)); ?>">
    <div class="jc-cards-textbox">
        <?php if ( ! empty( $gridpress_terms_str ) && $gridpress_show_terms ) : ?>
            <strong class="jc-cards-category">
                <?php echo wp_kses_post( $gridpress_terms_str ); ?>
            </strong>
        <?php endif; ?>
        <?php
        // Extract and fallback outside of the echo statements to satisfy the scanner
        $gridpress_card_url   = $gridpress_card_data['url'] ?? '';
        $gridpress_card_title = $gridpress_card_data['short_title'] ?? '';
        ?>
        <h2>
            <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
                <?php echo esc_html( $gridpress_card_title ); ?>
            </a>
        </h2>
    </div>

    <div class="jc-cards-holder">

        <?php if ( ! empty($gridpress_post_date) && $gridpress_show_date ) : ?>
            <time class="jc-cards-date"
                  datetime="<?php echo esc_attr(wp_date('Y-m-d', strtotime($gridpress_post_date))); ?>">
                <strong class="jc-cards-day">
                    <?php echo esc_html(wp_date('d', strtotime($gridpress_post_date))); ?>
                </strong>
                <?php echo esc_html(wp_date('M, y', strtotime($gridpress_post_date))); ?>
            </time>
        <?php endif; ?>

        <div class="jc-cards-image">
            <?php
            // Extract and fallback outside of the output statements to clear the scanner error
            $gridpress_img_link  = $gridpress_card_data['url'] ?? '';
            $gridpress_img_src   = $gridpress_card_data['imag_url'] ?? '';
            $gridpress_img_alt   = $gridpress_card_data['title'] ?? '';
            ?>
            <a href="<?php echo esc_url( $gridpress_img_link ); ?>">
                <img
                    src="<?php echo esc_url( $gridpress_img_src ); ?>"
                    alt="<?php echo esc_attr( $gridpress_img_alt ); ?>"
                >
            </a>
        </div>
    </div>
</div>