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

// Footer sidebar
$pantry_footer_name    = pantry_get_theme_option( 'footer_widgets' );
$pantry_footer_present = ! pantry_is_off( $pantry_footer_name ) && is_active_sidebar( $pantry_footer_name );
if ( $pantry_footer_present ) {
	pantry_storage_set( 'current_sidebar', 'footer' );
	$pantry_footer_wide = pantry_get_theme_option( 'footer_wide' );
	ob_start();
	if ( is_active_sidebar( $pantry_footer_name ) ) {
		dynamic_sidebar( $pantry_footer_name );
	}
	$pantry_out = trim( ob_get_contents() );
	ob_end_clean();
	if ( ! empty( $pantry_out ) ) {
		$pantry_out          = preg_replace( "/<\\/aside>[\r\n\s]*<aside/", '</aside><aside', $pantry_out );
		$pantry_need_columns = true;   //or check: strpos($pantry_out, 'columns_wrap')===false;
		if ( $pantry_need_columns ) {
			$pantry_columns = max( 0, (int) pantry_get_theme_option( 'footer_columns' ) );			
			if ( 0 == $pantry_columns ) {
				$pantry_columns = min( 4, max( 1, pantry_tags_count( $pantry_out, 'aside' ) ) );
			}
			if ( $pantry_columns > 1 ) {
				$pantry_out = preg_replace( '/<aside([^>]*)class="widget/', '<aside$1class="column-1_' . esc_attr( $pantry_columns ) . ' widget', $pantry_out );
			} else {
				$pantry_need_columns = false;
			}
		}
		?>
		<div class="footer_widgets_wrap widget_area<?php echo ! empty( $pantry_footer_wide ) ? ' footer_fullwidth' : ''; ?> sc_layouts_row sc_layouts_row_type_normal">
			<?php do_action( 'pantry_action_before_sidebar_wrap', 'footer' ); ?>
			<div class="footer_widgets_inner widget_area_inner">
				<?php
				if ( ! $pantry_footer_wide ) {
					?>
					<div class="content_wrap">
					<?php
				}
				if ( $pantry_need_columns ) {
					?>
					<div class="columns_wrap">
					<?php
				}
				do_action( 'pantry_action_before_sidebar', 'footer' );
				pantry_show_layout( $pantry_out );
				do_action( 'pantry_action_after_sidebar', 'footer' );
				if ( $pantry_need_columns ) {
					?>
					</div><!-- /.columns_wrap -->
					<?php
				}
				if ( ! $pantry_footer_wide ) {
					?>
					</div><!-- /.content_wrap -->
					<?php
				}
				?>
			</div><!-- /.footer_widgets_inner -->
			<?php do_action( 'pantry_action_after_sidebar_wrap', 'footer' ); ?>
		</div><!-- /.footer_widgets_wrap -->
		<?php
	}
}
