<?php
/**
 * The template to display the widgets area in the footer
 *
 * @package HYGIA
 * @since HYGIA 1.0.10
 */

// Footer sidebar
$hygia_footer_name    = hygia_get_theme_option( 'footer_widgets' );
$hygia_footer_present = ! hygia_is_off( $hygia_footer_name ) && is_active_sidebar( $hygia_footer_name );
if ( $hygia_footer_present ) {
	hygia_storage_set( 'current_sidebar', 'footer' );
	$hygia_footer_wide = hygia_get_theme_option( 'footer_wide' );
	ob_start();
	if ( is_active_sidebar( $hygia_footer_name ) ) {
		dynamic_sidebar( $hygia_footer_name );
	}
	$hygia_out = trim( ob_get_contents() );
	ob_end_clean();
	if ( ! empty( $hygia_out ) ) {
		$hygia_out          = preg_replace( "/<\\/aside>[\r\n\s]*<aside/", '</aside><aside', $hygia_out );
		$hygia_need_columns = true;   //or check: strpos($hygia_out, 'columns_wrap')===false;
		if ( $hygia_need_columns ) {
			$hygia_columns = max( 0, (int) hygia_get_theme_option( 'footer_columns' ) );			
			if ( 0 == $hygia_columns ) {
				$hygia_columns = min( 4, max( 1, hygia_tags_count( $hygia_out, 'aside' ) ) );
			}
			if ( $hygia_columns > 1 ) {
				$hygia_out = preg_replace( '/<aside([^>]*)class="widget/', '<aside$1class="column-1_' . esc_attr( $hygia_columns ) . ' widget', $hygia_out );
			} else {
				$hygia_need_columns = false;
			}
		}
		?>
		<div class="footer_widgets_wrap widget_area<?php echo ! empty( $hygia_footer_wide ) ? ' footer_fullwidth' : ''; ?> sc_layouts_row sc_layouts_row_type_normal">
			<?php do_action( 'hygia_action_before_sidebar_wrap', 'footer' ); ?>
			<div class="footer_widgets_inner widget_area_inner">
				<?php
				if ( ! $hygia_footer_wide ) {
					?>
					<div class="content_wrap">
					<?php
				}
				if ( $hygia_need_columns ) {
					?>
					<div class="columns_wrap">
					<?php
				}
				do_action( 'hygia_action_before_sidebar', 'footer' );
				hygia_show_layout( $hygia_out );
				do_action( 'hygia_action_after_sidebar', 'footer' );
				if ( $hygia_need_columns ) {
					?>
					</div><!-- /.columns_wrap -->
					<?php
				}
				if ( ! $hygia_footer_wide ) {
					?>
					</div><!-- /.content_wrap -->
					<?php
				}
				?>
			</div><!-- /.footer_widgets_inner -->
			<?php do_action( 'hygia_action_after_sidebar_wrap', 'footer' ); ?>
		</div><!-- /.footer_widgets_wrap -->
		<?php
	}
}
