<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Code_Manas
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function code_manas_body_classes( $classes ) {
	$classes[] = 'code-manas-vertical-tab';
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'sidebar-1' ) || ( code_manas_is_woo_active() && ( is_cart() || is_checkout() || is_account_page() ) ) ) {
		$classes[] = 'no-sidebar';
	}

	if ( is_404() ) {
		$classes[] = 'no-sidebar';
	}

	// Add class for header alignment
	if ( get_theme_mod( 'header_alignment', 0 ) ) {
		$classes[] = 'centered-header';
	}

	// Add class for sticky primary menu
	if ( get_theme_mod( 'primary_menu_sticky', 1 ) ) {
		$classes[] = 'menu-sticky';
	}

	// Add class for site layout
	if ( 'boxed' === get_theme_mod( 'site_layout', 'boxed' ) ) {
		$classes[] = 'layout-boxed';
	} else {
		$classes[] = 'layout-wide';
	}

	// Add class for archive/search/blog page sidebar position
	if ( is_active_sidebar( 'sidebar-1' ) && code_manas_is_archive() ) {
		$archive_layout = get_theme_mod( 'archive_layout', 'rs' );
		switch ( $archive_layout ) {
			case 'rs':
				$classes[] = 'right-sidebar';
			break;

			case 'ls':
				$classes[] = 'left-sidebar';
			break;

			case 'ns':
				$classes[] = 'no-sidebar';
			break;
			
			default:
				$classes[] = 'no-sidebar';
			break;
		}
	}

	if ( code_manas_is_woo_active() && is_store_notice_showing() ) {
		$classes[] = 'woo-store-notice-enabled';
	}
		
	// Add class for single posts sidebar position
	if ( is_active_sidebar( 'sidebar-1' ) && is_single() ) {
		$posts_layout = get_theme_mod( 'posts_layout', 'rs' );
		switch ( $posts_layout ) {
			case 'rs':
				$classes[] = 'right-sidebar';
			break;

			case 'ls':
				$classes[] = 'left-sidebar';
			break;

			case 'ns':
				$classes[] = 'no-sidebar';
			break;
			
			default:
				$classes[] = 'right-sidebar';
			break;
		}
	}

	// Add class for single pages sidebar position
	if ( is_active_sidebar( 'sidebar-1' ) && is_page() ) {
		$posts_layout = get_theme_mod( 'pages_layout', 'rs' );
		switch ( $posts_layout ) {
			case 'rs':
				$classes[] = 'right-sidebar';
			break;

			case 'ls':
				$classes[] = 'left-sidebar';
			break;

			case 'ns':
				$classes[] = 'no-sidebar';
			break;
			
			default:
				$classes[] = 'right-sidebar';
			break;
		}
	}
	return $classes;
}
add_filter( 'body_class', 'code_manas_body_classes' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function code_manas_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'code_manas_pingback_header' );

/**
 * Check if the current page is archive/search/blog
 * @return bool True if the current page is archive or search or blog
 */
function code_manas_is_archive() {
	return is_archive() || is_search() || is_home();
}

/**
 * Count the active footer sidebars
 * @return int Number of active footer sidebars
 */
function code_manas_count_active_footer_sidebars() {
	$count = 0;
	for ( $j=2; $j < 6; $j++ ) {
		if ( is_active_sidebar( 'sidebar-' . $j ) ) {
			$count++;
		}
	}
	
	if ( $count === 1 ) {
		$count = 12;
	} elseif ( 2 === $count ) {
		$count = 6;
	} elseif ( 3 === $count ) {
		$count = 4;
	} elseif ( 4 === $count ) {
		$count = 3;
	} elseif ( 4 < $count ) {
		$count = 4;
	} 

	return $count;
}