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

add_shortcode( 'idsc_card', 'idsc_card_shortcode' );

function idsc_card_shortcode( $atts, $content = '' ) {
    IDSC_Assets::flag_css();

    $atts = shortcode_atts( array(
        'image'       => '',
        'title'       => '',
        'button_text' => '',
        'button_url'  => '',
        'image_pos'   => 'top',
    ), $atts, 'idsc_card' );

    $image_pos = sanitize_html_class( $atts['image_pos'] );
    $class     = 'idsc-card idsc-card--img-' . $image_pos;

    $html = '<div class="' . esc_attr( $class ) . '">';

    // Image
    if ( ! empty( $atts['image'] ) ) {
        $img_id = intval( $atts['image'] );
        $img    = wp_get_attachment_image( $img_id, 'medium_large', false, array( 'class' => 'idsc-card__image' ) );
        if ( $img ) {
            $html .= '<div class="idsc-card__media">' . $img . '</div>';
        }
    }

    $html .= '<div class="idsc-card__body">';

    if ( ! empty( $atts['title'] ) ) {
        $html .= '<h3 class="idsc-card__title">' . esc_html( $atts['title'] ) . '</h3>';
    }

    if ( ! empty( $content ) ) {
        $html .= '<div class="idsc-card__content">' . wp_kses_post( $content ) . '</div>';
    }

    if ( ! empty( $atts['button_text'] ) && ! empty( $atts['button_url'] ) ) {
        $html .= '<a href="' . esc_url( $atts['button_url'] ) . '" class="idsc-card__button">' . esc_html( $atts['button_text'] ) . '</a>';
    }

    $html .= '</div></div>';

    return $html;
}
