<?php

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

$gridpress_terms_class = '';
$gridpress_post_date   = '';

/**
 * TAX CLASS BUILDING (SAFE)
 */
if (
    isset($gridpress_card_data['type']) &&
    $gridpress_card_data['type'] === 'post' &&
    !empty($gridpress_display_settings['top_filter_tax'])
) {
    $gridpress_taxonomy = $gridpress_display_settings['top_filter_tax'];

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

/**
 * DATE SAFE
 */
if (!empty($gridpress_card_data['post_date'])) {
    $gridpress_post_date = sanitize_text_field(
        str_replace('/', '-', $gridpress_card_data['post_date'])
    );
}

/**
 * CLASS BUILD (IMPORTANT FIX FOR SCANNER)
 */
$gridpress_classes = trim(
    'jc-cards'
    . (!empty($gridpress_terms_class) ? ' ' . sanitize_html_class($gridpress_terms_class) : '')
);
?>
<div class="<?php echo esc_attr($gridpress_classes); ?>">
    <?php if (
        !empty($gridpress_post_date) &&
        !empty($gridpress_display_settings['show_date']) &&
        $gridpress_display_settings['show_date'] === 'on'
    ) : ?>
        <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('d', strtotime($gridpress_post_date)) ); ?>
        </time>
    <?php endif; ?>
    <?php
    $gridpress_card_url   = isset( $gridpress_card_data['url'] ) ? sanitize_url( (string) $gridpress_card_data['url'] ) : '';
    $gridpress_image_url  = isset( $gridpress_card_data['imag_url'] ) ? sanitize_url( (string) $gridpress_card_data['imag_url'] ) : '';
    $gridpress_card_title = isset( $gridpress_card_data['title'] ) ? sanitize_text_field( (string) $gridpress_card_data['title'] ) : '';
    $gridpress_card_short = isset( $gridpress_card_data['short_title'] ) ? sanitize_text_field( (string) $gridpress_card_data['short_title'] ) : '';
    ?>

    <div class="jc-cards-image">
        <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
            <img
                src="<?php echo esc_url( $gridpress_image_url ); ?>"
                alt="<?php echo esc_attr( $gridpress_card_title ); ?>"
            />
        </a>
    </div>
    <div class="jc-cards-textbox">
        <h2>
            <a href="<?php echo esc_url( $gridpress_card_url ); ?>">
                <?php echo esc_html( $gridpress_card_short ); ?>
            </a>
        </h2>
    </div>
</div>