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

// Footer sidebar
$expo_footer_name    = expo_get_theme_option( 'footer_widgets' );
$expo_footer_present = ! expo_is_off( $expo_footer_name ) && is_active_sidebar( $expo_footer_name );
if ( $expo_footer_present ) {
	expo_storage_set( 'current_sidebar', 'footer' );
	$expo_footer_wide = expo_get_theme_option( 'footer_wide' );
	ob_start();
	if ( is_active_sidebar( $expo_footer_name ) ) {
		dynamic_sidebar( $expo_footer_name );
	}
	$expo_out = trim( ob_get_contents() );
	ob_end_clean();
	if ( ! empty( $expo_out ) ) {
		$expo_out          = preg_replace( "/<\\/aside>[\r\n\s]*<aside/", '</aside><aside', $expo_out );
		$expo_need_columns = true;   //or check: strpos($expo_out, 'columns_wrap')===false;
		if ( $expo_need_columns ) {
			$expo_columns = max( 0, (int) expo_get_theme_option( 'footer_columns' ) );			
			if ( 0 == $expo_columns ) {
				$expo_columns = min( 4, max( 1, expo_tags_count( $expo_out, 'aside' ) ) );
			}
			if ( $expo_columns > 1 ) {
				$expo_out = preg_replace( '/<aside([^>]*)class="widget/', '<aside$1class="column-1_' . esc_attr( $expo_columns ) . ' widget', $expo_out );
			} else {
				$expo_need_columns = false;
			}
		}
		?>
		<div class="footer_widgets_wrap widget_area<?php echo ! empty( $expo_footer_wide ) ? ' footer_fullwidth' : ''; ?> sc_layouts_row sc_layouts_row_type_normal">
			<?php do_action( 'expo_action_before_sidebar_wrap', 'footer' ); ?>
			<div class="footer_widgets_inner widget_area_inner">
				<?php
				if ( ! $expo_footer_wide ) {
					?>
					<div class="content_wrap">
					<?php
				}
				if ( $expo_need_columns ) {
					?>
					<div class="columns_wrap">
					<?php
				}
				do_action( 'expo_action_before_sidebar', 'footer' );
				expo_show_layout( $expo_out );
				do_action( 'expo_action_after_sidebar', 'footer' );
				if ( $expo_need_columns ) {
					?>
					</div><!-- /.columns_wrap -->
					<?php
				}
				if ( ! $expo_footer_wide ) {
					?>
					</div><!-- /.content_wrap -->
					<?php
				}
				?>
			</div><!-- /.footer_widgets_inner -->
			<?php do_action( 'expo_action_after_sidebar_wrap', 'footer' ); ?>
		</div><!-- /.footer_widgets_wrap -->
		<?php
	}
}
