<?php if ( ! defined( 'ABSPATH' ) ) { exit; }
$footer_columns = max( 1, min( 4, absint( lt_theme_option( 'footer_columns' ) ) ) );
$main_office_id = absint( lt_theme_core_option( 'main_office_id' ) );
$socials = array_filter( array(
    'LinkedIn' => lt_theme_core_option( 'linkedin' ),
    'X'        => lt_theme_core_option( 'twitter' ),
    'Facebook' => lt_theme_core_option( 'facebook' ),
    'Instagram'=> lt_theme_core_option( 'instagram' ),
    'YouTube'  => lt_theme_core_option( 'youtube' ),
) );
$lt_block_footer = '';
if ( 'pattern' === lt_theme_option( 'footer_source', 'theme' ) && function_exists( 'lt_theme_render_synced_pattern' ) ) {
    $lt_block_footer = lt_theme_render_synced_pattern( absint( lt_theme_option( 'footer_pattern_id' ) ) );
}
?>
<?php if ( $lt_block_footer ) : ?>
  <div class="lt-block-template-part lt-block-template-part--footer"><?php echo $lt_block_footer; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?></div>
<?php else : ?>
<footer class="lt-site-footer">
  <div class="lt-container">
    <div class="lt-footer-grid">
      <div>
        <div class="lt-brand lt-footer-brand"><?php echo lt_theme_logo( 'footer' ); ?></div>
        <?php $desc = lt_theme_option( 'footer_description' ) ?: lt_theme_core_option( 'description' ); if ( $desc ) : ?><p><?php echo esc_html( $desc ); ?></p><?php endif; ?>
        <?php if ( lt_theme_option( 'footer_show_office' ) ) : ?>
          <?php if ( $main_office_id && 'office' === get_post_type( $main_office_id ) ) : ?>
            <address>
              <strong><?php echo esc_html( get_the_title( $main_office_id ) ); ?></strong><br>
              <?php echo nl2br( esc_html( get_post_meta( $main_office_id, 'lt_address', true ) ) ); ?>
              <?php $phone = get_post_meta( $main_office_id, 'lt_phone', true ); if ( $phone ) : ?><br><a href="tel:<?php echo esc_attr( preg_replace('/[^0-9+]/','',$phone) ); ?>"><?php echo esc_html( $phone ); ?></a><?php endif; ?>
              <?php $email = get_post_meta( $main_office_id, 'lt_email', true ); if ( $email ) : ?><br><a href="mailto:<?php echo esc_attr( antispambot( $email ) ); ?>"><?php echo esc_html( antispambot( $email ) ); ?></a><?php endif; ?>
            </address>
          <?php elseif ( lt_theme_core_option( 'general_email' ) || lt_theme_core_option( 'phone_primary' ) ) : ?>
            <p><?php if ( lt_theme_core_option( 'general_email' ) ) : ?><a href="mailto:<?php echo esc_attr( antispambot( lt_theme_core_option( 'general_email' ) ) ); ?>"><?php echo esc_html( antispambot( lt_theme_core_option( 'general_email' ) ) ); ?></a><br><?php endif; ?><?php echo esc_html( lt_theme_core_option( 'phone_primary' ) ); ?></p>
          <?php endif; ?>
        <?php endif; ?>
        <?php if ( lt_theme_option( 'footer_show_social' ) && $socials ) : ?><nav class="lt-footer-social" aria-label="<?php esc_attr_e( 'Social links', 'law-temple-theme' ); ?>"><?php foreach ( $socials as $label => $url ) : ?><a href="<?php echo esc_url( $url ); ?>" target="_blank" rel="noopener noreferrer"><?php echo esc_html( $label ); ?></a><?php endforeach; ?></nav><?php endif; ?>
      </div>

      <?php for ( $i=1; $i <= $footer_columns; $i++ ) : ?>
        <div>
          <?php if ( is_active_sidebar( 'footer-'.$i ) ) : ?>
            <?php dynamic_sidebar( 'footer-'.$i ); ?>
          <?php elseif ( 1 === $i && has_nav_menu( 'footer' ) ) : ?>
            <?php wp_nav_menu( array( 'theme_location'=>'footer','container'=>false,'fallback_cb'=>false ) ); ?>
          <?php elseif ( 2 === $i && lt_theme_option( 'footer_show_practices' ) && post_type_exists( 'practice' ) ) : ?>
            <h3><?php echo esc_html( function_exists('lt_core_label') ? lt_core_label( 'practice', __( 'Practice Areas','law-temple-theme' ) ) : __( 'Practice Areas','law-temple-theme' ) ); ?></h3>
            <ul><?php foreach ( get_posts( array( 'post_type'=>'practice','post_status'=>'publish','posts_per_page'=>6,'orderby'=>'menu_order title','order'=>'ASC' ) ) as $practice ) : ?><li><a href="<?php echo esc_url( get_permalink( $practice ) ); ?>"><?php echo esc_html( $practice->post_title ); ?></a></li><?php endforeach; ?></ul>
          <?php elseif ( 3 === $i && lt_theme_option( 'footer_show_insights' ) && post_type_exists( 'insight' ) ) : ?>
            <h3><?php echo esc_html( function_exists('lt_core_label') ? lt_core_label( 'insights', __( 'Insights','law-temple-theme' ) ) : __( 'Insights','law-temple-theme' ) ); ?></h3>
            <ul><?php foreach ( get_posts( array( 'post_type'=>'insight','post_status'=>'publish','posts_per_page'=>4,'orderby'=>'date','order'=>'DESC' ) ) as $insight ) : ?><li><a href="<?php echo esc_url( get_permalink( $insight ) ); ?>"><?php echo esc_html( $insight->post_title ); ?></a></li><?php endforeach; ?></ul>
          <?php elseif ( 3 === $i && lt_theme_option( 'newsletter_shortcode' ) ) : ?>
            <h3><?php esc_html_e( 'Newsletter', 'law-temple-theme' ); ?></h3><?php echo do_shortcode( lt_theme_option( 'newsletter_shortcode' ) ); ?>
          <?php endif; ?>
        </div>
      <?php endfor; ?>
    </div>

    <?php if ( lt_theme_option( 'footer_show_regulatory' ) && ( lt_theme_core_option( 'registration_details' ) || lt_theme_core_option( 'regulator_details' ) ) ) : ?>
      <div class="lt-footer-regulatory"><p><?php echo esc_html( trim( lt_theme_core_option( 'registration_details' ) . ' ' . lt_theme_core_option( 'regulator_details' ) ) ); ?></p></div>
    <?php endif; ?>

    <div class="lt-footer-bottom">
      <span><?php echo esc_html( lt_theme_core_option( 'copyright' ) ?: sprintf( __( '© %1$s %2$s. All rights reserved.', 'law-temple-theme' ), date_i18n( 'Y' ), lt_theme_firm_name() ) ); ?></span>
      <?php if ( has_nav_menu( 'legal' ) ) wp_nav_menu( array( 'theme_location'=>'legal','container'=>false,'fallback_cb'=>false,'depth'=>1 ) ); ?>
    </div>
  </div>
</footer>
<?php endif; ?>
<?php wp_footer(); ?></body></html>
