<?php

if ( ! defined( 'ABSPATH' ) ) { return; }

$qo = get_queried_object();
if ( ! ( $qo instanceof \WP_Term ) ) { return; }

$taxonomy = (string) $qo->taxonomy;
$term_id  = (int) $qo->term_id;
$footer   = '';

if ( $taxonomy === 'product_brand' && class_exists( 'AIEO_DMM_Brand_Extras_Data' ) ) {
    $row    = AIEO_DMM_Brand_Extras_Data::get( $taxonomy, $term_id );
    $footer = (string) ( $row['footer_text'] ?? '' );
} elseif ( $taxonomy === 'pa_need' && class_exists( 'AIEO_DMM_Need_Extras_Data' ) ) {
    $row    = AIEO_DMM_Need_Extras_Data::get( $taxonomy, $term_id );
    $footer = (string) ( $row['footer_text'] ?? '' );
} elseif ( $taxonomy === 'product_cat' && class_exists( 'AIEO_DMM_Category_Extras' ) ) {
    $row    = AIEO_DMM_Category_Extras::get( $taxonomy, $term_id );
    $footer = (string) ( $row['footer_text'] ?? '' );
}

$footer = (string) apply_filters( 'aieo_taxonomy_archive_footer_text', $footer, $taxonomy, $term_id, $qo );
$footer = trim( $footer );
if ( $footer === '' ) {
    return;
}

$wrapper_attrs = function_exists( 'get_block_wrapper_attributes' )
    ? get_block_wrapper_attributes( array(
        'class'         => 'aieo-tax-footer aieo-tax-footer--' . sanitize_html_class( $taxonomy ),
        'data-taxonomy' => $taxonomy,
        'data-term-id'  => (string) $term_id,
    ) )
    : 'class="aieo-tax-footer aieo-tax-footer--' . esc_attr( sanitize_html_class( $taxonomy ) ) . '"';

ob_start();
?>
<section <?php echo $wrapper_attrs; ?>>
    <div class="aieo-tax-footer__body">
        <?php echo wp_kses_post( $footer ); ?>
    </div>
</section>
<?php
echo ob_get_clean(); 
