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

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

$gridpress_terms_class = '';
$gridpress_terms_str   = '';

/**
 * Safe type
 */
$gridpress_type = $gridpress_card_data['type'] ?? '';

/**
 * Post taxonomy logic
 */
if ( $gridpress_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'] ?? 0,
            $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'] ?? 0
        );
    }

} else {

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

/**
 * Show terms flag (renamed for clarity - optional improvement)
 */
$gridpress_is_show_terms = ! empty( $gridpress_display_settings['show_terms'] )
    && $gridpress_display_settings['show_terms'] === 'on';
?>

<div class="jc-cards<?php echo esc_attr( trim( $gridpress_terms_class ) ); ?>">

    <?php
    // Extract data array elements with fallbacks outside of the output context
    $gridpress_img_link = $gridpress_card_data['url'] ?? '';
    $gridpress_img_src  = $gridpress_card_data['imag_url'] ?? '';
    $gridpress_img_alt  = $gridpress_card_data['title'] ?? '';
    ?>

    <div class="jc-cards-image">
        <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 class="jc-cards-textbox">
        <?php
        // Extract and fallback outside of the output statements to satisfy the scanner
        $gridpress_card_link  = $gridpress_card_data['url'] ?? '';
        $gridpress_card_title = $gridpress_card_data['short_title'] ?? '';
        ?>
        <strong class="jc-cards-title">
            <a href="<?php echo esc_url( $gridpress_card_link ); ?>">
                <?php echo esc_html( $gridpress_card_title ); ?>
            </a>
        </strong>

        <?php if ( ! empty( $gridpress_terms_str ) && $gridpress_is_show_terms ) : ?>
            <span class="jc-cards-text">
                /
                <span class="gridpress-dynm-carditm-terms">
                    <?php echo wp_kses_post( $gridpress_terms_str ); ?>
                </span>
            </span>
        <?php endif; ?>

    </div>

</div>