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

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

/**
 * Safe variables
 */
$gridpress_type    = $gridpress_card_data['type'] ?? '';
$gridpress_id      = $gridpress_card_data['id'] ?? 0;
$gridpress_url     = $gridpress_card_data['url'] ?? '';
$gridpress_image   = $gridpress_card_data['imag_url'] ?? '';
$gridpress_title   = $gridpress_card_data['title'] ?? '';
$gridpress_counter = $gridpress_card_counter ?? '';

/**
 * Taxonomy logic
 */
if ( $gridpress_type === 'post' ) {

    if ( ! 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_id
        );
    }
}

/**
 * Build safe class attribute
 */
$gridpress_class_attr = trim(
    'jc-cards jc-cards-item ' .
    sanitize_html_class( $gridpress_counter ) . ' ' .
    $gridpress_terms_class
);

// Split space-separated classes, sanitize each item, and safely implode them
$gridpress_sanitized_classes = implode( ' ', array_map( 'sanitize_html_class', explode( ' ', $gridpress_class_attr ) ) );
?>
<div class="<?php echo esc_attr( $gridpress_sanitized_classes ); ?>">
    <div class="jc-cards-image">
        <a class="jc-cards-image-wrap" href="<?php echo esc_url( $gridpress_url ); ?>">
            <img
                src="<?php echo esc_url( $gridpress_image ); ?>"
                alt="<?php echo esc_attr( $gridpress_title ); ?>"
            >
        </a>
    </div>
</div>