
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

/* -----------------------------------------------------------------------------
 * Register Sidebars
 * -------------------------------------------------------------------------- */
add_action( 'widgets_init', 'presso_register_sidebars' );
if ( ! function_exists( 'presso_register_sidebars' ) ) {
	function presso_register_sidebars() {
		/**
		 * Blog widget sidebar
		 */
		register_sidebar( array(
			'name' => esc_html__( 'Blog Sidebar', 'presso' ),
			'id'   => 'blog-sidebar',
			'description' => esc_html__( 'These are widgets for the Blog sidebar.', 'presso' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title"><span>',
			'after_title'   => '</span></h3>'
		) );

		/**
		 * Page widget sidebar
		 */
		register_sidebar( array(
			'name' => esc_html__( 'Page Sidebar', 'presso' ),
			'id'   => 'page-sidebar',
			'description' => esc_html__( 'These are widgets for a static page.', 'presso' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title"><span>',
			'after_title'   => '</span></h3>'
		) );

		/**
		 * Mobile widget sidebar
		 */
		register_sidebar( array(
			'name' => esc_html__( 'Mobile Sidebar', 'presso' ),
			'id'   => 'mobile-sidebar',
			'description' => esc_html__( 'These are widgets for the mobile sidebar.', 'presso' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title"><span>',
			'after_title'   => '</span></h3>'
		) );
		
		/**
		 * Footer sidebar
		 */
		register_sidebars( 4, array(
			'name' => esc_html__( 'Footer Sidebar %d', 'presso' ),
			'id'   => 'footer-sidebar',
			'description' => esc_html__( 'These are widgets for site footer.', 'presso' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title"><span>',
			'after_title'   => '</span></h3>'
		) );

		/**
		 * Custom sidebars
		 */
		register_sidebars( 6, array(
			'name' => esc_html__( 'Custom Sidebar %d', 'presso' ),
			'id'   => 'custom-sidebar',
			'description' => esc_html__( 'These are widgets for custom sidebar.', 'presso' ),
			'before_widget' => '<div id="%1$s" class="widget %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h3 class="widget-title"><span>',
			'after_title'   => '</span></h3>'
		) );


		/**
		 * Additional sidebars
		 */
		
		presso_register_additional_sidebars( presso_get_theme_option( 'additional_sidebars' ) );
	}
}

if ( ! function_exists( 'presso_register_additional_sidebars' ) ) {
	function presso_register_additional_sidebars( $additional_sidebars ) {
		if ( ! empty( $additional_sidebars ) ) {
			foreach ( $additional_sidebars as $sidebar_slug ) {
				register_sidebar( array(
					'name' => ucfirst( $sidebar_slug ),
					'id'   => $sidebar_slug,
					'description' => sprintf( esc_html__( 'These are widgets for %s. You can delete this sidebar at theme options panel', 'presso' ), $sidebar_slug ),
					'before_widget' => '<div id="%1$s" class="widget %2$s">',
					'after_widget'  => '</div>',
					'before_title'  => '<h3 class="widget-title"><span>',
					'after_title'   => '</span></h3>'
				) );
			}
		}
	}
}



/*//////////////////////////////////////
// Apply Theme's Sidebar Wrapper for https://wordpress.org/plugins/custom-sidebars/
//////////////////////////////////////*/

add_filter( 'cs_sidebar_params', 'presso_apply_sidebar_wrapper' );
if ( ! function_exists( 'presso_apply_sidebar_wrapper' ) ) {
	function presso_apply_sidebar_wrapper( $sidebar ) {
		$sidebar['before_widget'] = '<div id="%1$s" class="widget %2$s">';
		$sidebar['after_widget'] = '</div>';
		$sidebar['before_title'] = '<h3 class="widget-title"><span>';
		$sidebar['after_title'] = '</span></h3>';

		return $sidebar;
	}
}




/* -----------------------------------------------------------------------------
 * Sidebar position functions
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_get_sidebar_position' ) ) {
	function presso_get_sidebar_position() {

		if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
			$sidebar_position = presso_get_theme_option( 'bbpress_sidebar_position' );

		} elseif ( is_single() || is_page() ) {
			global $post;
			$post_sidebar_position = get_post_meta( $post->ID, 'vw_sidebar_position', true );

			if ( $post_sidebar_position != 'default' && ! empty( $post_sidebar_position ) ) {
				$sidebar_position = $post_sidebar_position;

			} else {
				$sidebar_position = presso_get_theme_option( 'post_sidebar_position' );

			}
			
		} else if ( is_archive() ) {
			$sidebar_position = presso_get_theme_option( 'archive_sidebar_position' );

		} else if ( is_search() ) {
			$sidebar_position = presso_get_theme_option( 'search_sidebar_position' );

		} else {
			$sidebar_position = presso_get_theme_option( 'blog_sidebar_position' );

		}

		return apply_filters( 'presso_filter_sidebar_position', $sidebar_position );
	}
}

if ( ! function_exists( 'presso_has_sidebar' ) ) {
	function presso_has_sidebar() {
		return presso_get_sidebar_position() != 'no';
	}
}

if ( ! function_exists( 'presso_the_sidebar_position_class' ) ) {
	function presso_the_sidebar_position_class() {
		echo presso_get_sidebar_position_class();
	}
}

if ( ! function_exists( 'presso_get_sidebar_position_class' ) ) {
	function presso_get_sidebar_position_class() {
		return esc_attr( sprintf( 'vw-content-area--%s-sidebar', presso_get_sidebar_position() ) );
	}
}





/* -----------------------------------------------------------------------------
 * Get blog sidebar functions
 * -------------------------------------------------------------------------- */

if ( ! function_exists( 'presso_get_sidebar' ) ) {
	function presso_get_sidebar() {

		if ( function_exists( 'is_bbpress' ) && is_bbpress() ) {
			$sidebar = presso_get_theme_option( 'bbpress_sidebar' );

		} elseif ( is_single() || is_page() ) {
			global $post;
			$post_sidebar = get_post_meta( $post->ID, 'vw_sidebar', true );

			if ( $post_sidebar != 'default' && ! empty( $post_sidebar ) ) {
				$sidebar = $post_sidebar;

			} else {
				$sidebar = presso_get_theme_option( 'post_sidebar' );

			}
			
		} else if ( is_archive() ) {
			$sidebar = presso_get_theme_option( 'archive_sidebar' );

		} else if ( is_search() ) {
			$sidebar = presso_get_theme_option( 'search_sidebar' );
			
		} else {
			$sidebar = presso_get_theme_option( 'blog_sidebar' );

		}

		return apply_filters( 'presso_filter_sidebar', $sidebar );
	}
}


/* -----------------------------------------------------------------------------
 * Show the warning when there is no widget in sidebar
 * -------------------------------------------------------------------------- */
if ( ! function_exists( 'presso_show_no_widget_warning' ) ) {
	function presso_show_no_widget_warning() {
		if ( current_user_can( 'manage_options' ) ) {
			printf( '<div class="vw-no-widget-warning"><i class="icon-entypo-attention"></i> Please add widgets into <a href="%s" target="_blank">sidebar</a>.</div>', esc_url( admin_url( 'widgets.php' ) ) );
		}
	}
}


/* -----------------------------------------------------------------------------
 * Sticky Sidebar
 * -------------------------------------------------------------------------- */


add_action( 'wp', 'presso_setup_sticky_sidebar' );
if ( ! function_exists( 'presso_setup_sticky_sidebar' ) ) {
	function presso_setup_sticky_sidebar() {
		/* add this only frontend to prevent a problem of widget lost when saving widget */
		if ( presso_get_theme_option( 'enable_sticky_sidebar' ) ) {
			add_action( 'dynamic_sidebar_before', 'presso_ssb_dynamic_sidebar_before' );
			add_action( 'dynamic_sidebar_after', 'presso_ssb_dynamic_sidebar_after' );
		}
	}
}

if ( ! function_exists( 'presso_ssb_dynamic_sidebar_before' ) ) {
	function presso_ssb_dynamic_sidebar_before() {
		echo '<div class="vw-sticky-sidebar theiaStickySidebar">';
	}
}

if ( ! function_exists( 'presso_ssb_dynamic_sidebar_after' ) ) {
	function presso_ssb_dynamic_sidebar_after() {
		echo '</div>';
	}
}