<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @category WordPress
 * @package  Acoustics
 * @author   CodeGearThemes<info@codegearthemes.com>
 * @version  1.0.0
 *
 */

if (!function_exists('wp_body_open')) {
	/**
	 * Shim for wp_body_open, ensuring backward compatibility with versions of WordPress older than 5.2.
	 */
	function wp_body_open(){
		do_action('wp_body_open');
	}
}

/**
 * Include a skip to content link at the top of the page so that users can bypass the menu.
 */
function acoustic_skip_link(){ ?>
	<a class="skip-link screen-reader-text" href="#content"><?php echo __('Skip to the content', 'acoustics'); ?></a>
<?php
}
add_action('wp_body_open', 'acoustic_skip_link', 5);

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function acoustics_body_classes($classes)
{
	// 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')) {
		$classes[] = 'no-sidebar';
	}

	return $classes;
}

add_filter('body_class', 'acoustics_body_classes');

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

/**
 * Sidebar
 */
function acoustics_sidebar_callback()
{

	if (!apply_filters('acoustics_sidebar_enable', true)) {
		return;
	}

	get_sidebar();
}
add_action('acoustics_sidebar', 'acoustics_sidebar_callback');

/**
 * Sidebar single & page
 */
function acoustics_single_sidebar()
{

	global $post;

	if (!isset($post)) {
		return;
	}

	if (is_single() && 'post' == get_post_type()) {

		$acoustics_sidebar = get_theme_mod('acoustics_post_layout', 'right-sidebar');
		if ($acoustics_sidebar != 'no-sidebar') {
			add_filter('acoustics_sidebar_enable', function () {
				return true;
			});
			add_filter('acoustics_content_class', function () {

				$acoustics_sidebar = get_theme_mod('acoustics_post_layout', 'right-sidebar');
				$classes = 'large--two-thirds medium--three-quarters small--one-whole';
				if ('left-sidebar' === $acoustics_sidebar) {
					$classes .= ' omega';
				}
				return $classes;
			});
		} else {
			add_filter('acoustics_sidebar_enable', '__return_false');
			add_filter('acoustics_content_class', function () {
				return 'one-whole no-sidebar';
			});
		}
	} elseif (is_page()) {

		$acoustics_sidebar = get_theme_mod('acoustics_page_layout', 'no-sidebar');
		if ($acoustics_sidebar != 'no-sidebar') {
			add_filter('acoustics_sidebar_enable', function () {
				return true;
			});
			add_filter('acoustics_content_class', function () {

				$acoustics_sidebar = get_theme_mod('acoustics_page_layout', 'no-sidebar');
				$classes = 'large--two-thirds medium--three-quarters small--one-whole';
				if ('left-sidebar' === $acoustics_sidebar) {
					$classes .= ' omega';
				}
				return $classes;
			});
		} else {
			add_filter('acoustics_sidebar_enable', '__return_false');
			add_filter('acoustics_content_class', function () {
				return 'one-whole no-sidebar';
			});
		}
	} elseif (is_archive()) {

		$acoustics_sidebar = get_theme_mod('acoustics_archive_layout', 'left-sidebar');
		if ($acoustics_sidebar != 'no-sidebar') {
			add_filter('acoustics_sidebar_enable', function () {
				return true;
			});
			add_filter('acoustics_content_class', function () {

				$acoustics_sidebar = get_theme_mod('acoustics_archive_layout', 'left-sidebar');
				$classes = 'large--two-thirds medium--three-quarters small--one-whole';
				if ('left-sidebar' === $acoustics_sidebar) {
					$classes .= ' omega';
				}
				return $classes;
			});
		} else {
			add_filter('acoustics_sidebar_enable', '__return_false');
			add_filter('acoustics_content_class', function () {
				return 'one-whole no-sidebar';
			});
		}
	} elseif (is_home()) {
		$acoustics_sidebar = get_theme_mod('acoustics_archive_layout', 'left-sidebar');
		if ($acoustics_sidebar != 'no-sidebar') {
			add_filter('acoustics_sidebar_enable', function () {
				return true;
			});
			add_filter('acoustics_content_class', function () {

				$acoustics_sidebar = get_theme_mod('acoustics_archive_layout', 'left-sidebar');
				$classes = 'large--two-thirds medium--three-quarters small--one-whole';
				if ('left-sidebar' === $acoustics_sidebar) {
					$classes .= ' omega';
				}
				return $classes;
			});
		} else {
			add_filter('acoustics_sidebar_enable', '__return_false');
			add_filter('acoustics_content_class', function () {
				return 'one-whole no-sidebar';
			});
		}
	} else {
		add_filter('acoustics_sidebar_enable', '__return_false', 100);
		add_filter('acoustics_content_class', function () {
			return 'one-whole no-sidebar';
		}, 100);
	}
}
add_action('wp', 'acoustics_single_sidebar', 10);
