<?php
/**
 * Theme sprecific functions and definitions
 */


/**
 * Fire the wp_body_open action.
 *
 * Added for backwards compatibility to support pre 5.2.0 WordPress versions.
 */
if ( ! function_exists( 'wp_body_open' ) ) {
    function wp_body_open() {
        /**
         * Triggered after the opening <body> tag.
         */
        do_action('wp_body_open');
    }
}


/* Theme setup section
------------------------------------------------------------------- */

// Set the content width based on the theme's design and stylesheet.
if ( ! isset( $content_width ) ) $content_width = 1170; /* pixels */

// Add theme specific actions and filters
// Attention! Function were add theme specific actions and filters handlers must have priority 1
if ( !function_exists( 'healthcoach_theme_setup' ) ) {
	add_action( 'healthcoach_action_before_init_theme', 'healthcoach_theme_setup', 1 );
	function healthcoach_theme_setup() {

        // Add default posts and comments RSS feed links to head
        add_theme_support( 'automatic-feed-links' );

        // Enable support for Post Thumbnails
        add_theme_support( 'post-thumbnails' );

        // Custom header setup
        add_theme_support( 'custom-header', array('header-text'=>false));

        // Custom backgrounds setup
        add_theme_support( 'custom-background');

        // Supported posts formats
        add_theme_support( 'post-formats', array('gallery', 'video', 'audio', 'link', 'quote', 'image', 'status', 'aside', 'chat') );

        // Autogenerate title tag
        add_theme_support('title-tag');

        // Add user menu
        add_theme_support('nav-menus');

        // WooCommerce Support
        add_theme_support( 'woocommerce' );

        // Next setting from the WooCommerce 3.0+ enable built-in image zoom on the single product page
        add_theme_support( 'wc-product-gallery-zoom' );

        // Next setting from the WooCommerce 3.0+ enable built-in image slider on the single product page
        add_theme_support( 'wc-product-gallery-slider' );

        // Next setting from the WooCommerce 3.0+ enable built-in image lightbox on the single product page
        add_theme_support( 'wc-product-gallery-lightbox' );

        // Add wide and full blocks support
        add_theme_support( 'align-wide' );

		// Register theme menus
		add_filter( 'healthcoach_filter_add_theme_menus',		'healthcoach_add_theme_menus' );

		// Register theme sidebars
		add_filter( 'healthcoach_filter_add_theme_sidebars',	'healthcoach_add_theme_sidebars' );




		// Add theme required plugins
		add_filter( 'healthcoach_filter_required_plugins',		'healthcoach_add_required_plugins' );
		
		// Add preloader styles
		add_filter('healthcoach_filter_add_styles_inline',		'healthcoach_head_add_page_preloader_styles');

		// Init theme after WP is created
		add_action( 'wp',									'healthcoach_core_init_theme' );

		// Add theme specified classes into the body
		add_filter( 'body_class', 							'healthcoach_body_classes' );

		// Add data to the head and to the beginning of the body
		add_action('wp_head',								'healthcoach_head_add_page_meta', 1);
		add_action('before',								'healthcoach_body_add_toc');
		add_action('before',								'healthcoach_body_add_page_preloader');

		// Add data to the footer (priority 1, because priority 2 used for localize scripts)
		add_action('wp_footer',								'healthcoach_footer_add_views_counter', 1);
		add_action('wp_footer',								'healthcoach_footer_add_theme_customizer', 1);
		if(function_exists('healthcoach_footer_add_scroll_to_top')) {
            add_action('wp_footer', 'healthcoach_footer_add_scroll_to_top', 1);
        }

		// Set list of the theme required plugins
		healthcoach_storage_set('required_plugins', array(
			'calcfields',
			'essgrids',
			'revslider',
			'trx_utils',
			'visual_composer',
			'vc_extensions_bundle',
			'woocommerce',
			'mailchimp',
            'gdpr_framework',
            'instagram-widget-by-wpzoom',
             'contact-form-7',
             'trx_updater',
             'elegro-payment'
			)
		);

		healthcoach_storage_set('required_custom_fonts', array(
			'Amadeus'
			)
		);
		


	}
}


// Add/Remove theme nav menus
if ( !function_exists( 'healthcoach_add_theme_menus' ) ) {
	function healthcoach_add_theme_menus($menus) {
		return $menus;
	}
}


// Add theme specific widgetized areas
if ( !function_exists( 'healthcoach_add_theme_sidebars' ) ) {
	function healthcoach_add_theme_sidebars($sidebars=array()) {
		if (is_array($sidebars)) {
			$theme_sidebars = array(
				'sidebar_main'		=> esc_html__( 'Main Sidebar', 'healthcoach' ),
				'sidebar_footer'	=> esc_html__( 'Footer Sidebar', 'healthcoach' )
			);
			if (function_exists('healthcoach_exists_woocommerce') && healthcoach_exists_woocommerce()) {
				$theme_sidebars['sidebar_cart']  = esc_html__( 'WooCommerce Cart Sidebar', 'healthcoach' );
			}
			$sidebars = array_merge($theme_sidebars, $sidebars);
		}
		return $sidebars;
	}
}


// Add theme required plugins
if ( !function_exists( 'healthcoach_add_required_plugins' ) ) {
	function healthcoach_add_required_plugins($plugins) {
		$plugins[] = array(
			'name' 		=> esc_html__('Healthcoach Utilities', 'healthcoach'),
			'version'	=> '3.1.1',					// Minimal required version
			'slug' 		=> 'trx_utils',
			'source'	=> healthcoach_get_file_dir('plugins/install/trx_utils.zip'),
			'required' 	=> true
		);
		return $plugins;
	}
}

//------------------------------------------------------------------------ 
// One-click import support 
//------------------------------------------------------------------------ 

// Set theme specific importer options 
if ( ! function_exists( 'healthcoach_importer_set_options' ) ) {
    add_filter( 'trx_utils_filter_importer_options', 'healthcoach_importer_set_options', 9 );
    function healthcoach_importer_set_options( $options=array() ) {
        if ( is_array( $options ) ) {
            // Save or not installer's messages to the log-file 
            $options['debug'] = false;
            // Prepare demo data 
            if ( is_dir( HEALTHCOACH_THEME_PATH . 'demo/' ) ) {
                $options['demo_url'] = HEALTHCOACH_THEME_PATH . 'demo/';
            } else {
                $options['demo_url'] = esc_url( healthcoach_get_protocol().'://demofiles.ancorathemes.com/healthcoach/' ); // Demo-site domain
            }

            // Required plugins 
            $options['required_plugins'] =  array(
                'calculated-fields-form',
                'instagram-feed',
                'essential-grid',
                'revslider',
                'trx_utils',
                'js_composer',
                'vc_extensions_bundle',
                'woocommerce',
                'mailchimp-for-wp',
                'contact-form-7'
            );

            $options['theme_slug'] = 'healthcoach';

            // Set number of thumbnails to regenerate when its imported (if demo data was zipped without cropped images) 
            // Set 0 to prevent regenerate thumbnails (if demo data archive is already contain cropped images) 
            $options['regenerate_thumbnails'] = 3;
            // Default demo 
            $options['files']['default']['title'] = esc_html__( 'Healthcoach Demo', 'healthcoach' );
            $options['files']['default']['domain_dev'] = esc_url(healthcoach_get_protocol().'://healthcoach.dv.ancorathemes.com'); // Developers domain
            $options['files']['default']['domain_demo']= esc_url(healthcoach_get_protocol().'://healthcoach.ancorathemes.com'); // Demo-site domain

        }
        return $options;
    }
}


// Add data to the head and to the beginning of the body
//------------------------------------------------------------------------

// Add theme specified classes to the body tag
if ( !function_exists('healthcoach_body_classes') ) {
	function healthcoach_body_classes( $classes ) {

		$classes[] = 'healthcoach_body';
		$classes[] = 'body_style_' . trim(healthcoach_get_custom_option('body_style'));
		$classes[] = 'body_' . (healthcoach_get_custom_option('body_filled')=='yes' ? 'filled' : 'transparent');
		$classes[] = 'article_style_' . trim(healthcoach_get_custom_option('article_style'));
		
		$blog_style = healthcoach_get_custom_option(is_singular() && !healthcoach_storage_get('blog_streampage') ? 'single_style' : 'blog_style');
		$classes[] = 'layout_' . trim($blog_style);
		$classes[] = 'template_' . trim(healthcoach_get_template_name($blog_style));
		
		$body_scheme = healthcoach_get_custom_option('body_scheme');
		if (empty($body_scheme)  || healthcoach_is_inherit_option($body_scheme)) $body_scheme = 'original';
		$classes[] = 'scheme_' . $body_scheme;

		$top_panel_position = healthcoach_get_custom_option('top_panel_position');
		if (!healthcoach_param_is_off($top_panel_position)) {
			$classes[] = 'top_panel_show';
			$classes[] = 'top_panel_' . trim($top_panel_position);
		} else 
			$classes[] = 'top_panel_hide';
		$classes[] = healthcoach_get_sidebar_class();

		if (healthcoach_get_custom_option('show_video_bg')=='yes' && (healthcoach_get_custom_option('video_bg_youtube_code')!='' || healthcoach_get_custom_option('video_bg_url')!=''))
			$classes[] = 'video_bg_show';

		if (!healthcoach_param_is_off(healthcoach_get_theme_option('page_preloader')))
			$classes[] = 'preloader';

		return $classes;
	}
}


// Add page meta to the head
if (!function_exists('healthcoach_head_add_page_meta')) {
	function healthcoach_head_add_page_meta() {
		?>
		<meta charset="<?php bloginfo( 'charset' ); ?>" />
		<meta name="viewport" content="width=device-width, initial-scale=1<?php if (healthcoach_get_theme_option('responsive_layouts')=='yes') echo ', maximum-scale=1'; ?>">
		<meta name="format-detection" content="telephone=no">
	
		<link rel="profile" href="//gmpg.org/xfn/11" />
		<link rel="pingback" href="<?php bloginfo( 'pingback_url' ); ?>" />
		<?php
	}
}

// Add page preloader styles to the head
if (!function_exists('healthcoach_head_add_page_preloader_styles')) {
	function healthcoach_head_add_page_preloader_styles($css) {
		if (($preloader=healthcoach_get_theme_option('page_preloader'))!='none') {
			$image = healthcoach_get_theme_option('page_preloader_image');
			$bg_clr = healthcoach_get_scheme_color('bg_color');
			$link_clr = healthcoach_get_scheme_color('text_link');
			$css .= '
				#page_preloader {
					background-color: '. esc_attr($bg_clr) . ';'
					. ($preloader=='custom' && $image
						? 'background-image:url('.esc_url($image).');'
						: ''
						)
				    . '
				}
				.preloader_wrap > div {
					background-color: '.esc_attr($link_clr).';
				}';
		}
		return $css;
	}
}

// Add TOC anchors to the beginning of the body 
if (!function_exists('healthcoach_body_add_toc')) {
	function healthcoach_body_add_toc() {
		// Add TOC items 'Home' and "To top"
		if (healthcoach_get_custom_option('menu_toc_home')=='yes' && function_exists('healthcoach_sc_anchor'))
			healthcoach_show_layout(healthcoach_sc_anchor(array(
				'id' => "toc_home",
				'title' => esc_html__('Home', 'healthcoach'),
				'description' => esc_html__('{{Return to Home}} - ||navigate to home page of the site', 'healthcoach'),
				'icon' => "icon-home",
				'separator' => "yes",
				'url' => esc_url(home_url('/'))
				)
			)); 
		if (healthcoach_get_custom_option('menu_toc_top')=='yes' && function_exists('healthcoach_sc_anchor'))
			healthcoach_show_layout(healthcoach_sc_anchor(array(
				'id' => "toc_top",
				'title' => esc_html__('To Top', 'healthcoach'),
				'description' => esc_html__('{{Back to top}} - ||scroll to top of the page', 'healthcoach'),
				'icon' => "icon-double-up",
				'separator' => "yes")
				)); 
	}
}

// Add page preloader to the beginning of the body
if (!function_exists('healthcoach_body_add_page_preloader')) {
	function healthcoach_body_add_page_preloader() {
		if ( ($preloader=healthcoach_get_theme_option('page_preloader')) != 'none' && ( $preloader != 'custom' || ($image=healthcoach_get_theme_option('page_preloader_image')) != '')) {
			?><div id="page_preloader"><?php
				if ($preloader == 'circle') {
					?><div class="preloader_wrap preloader_<?php echo esc_attr($preloader); ?>"><div class="preloader_circ1"></div><div class="preloader_circ2"></div><div class="preloader_circ3"></div><div class="preloader_circ4"></div></div><?php
				} else if ($preloader == 'square') {
					?><div class="preloader_wrap preloader_<?php echo esc_attr($preloader); ?>"><div class="preloader_square1"></div><div class="preloader_square2"></div></div><?php
				}
			?></div><?php
		}
	}
}


// Add data to the footer
//------------------------------------------------------------------------

// Add post/page views counter
if (!function_exists('healthcoach_footer_add_views_counter')) {
	
	function healthcoach_footer_add_views_counter() {
		// Post/Page views counter
		get_template_part(healthcoach_get_file_slug('templates/_parts/views-counter.php'));
	}
}

// Add theme customizer
if (!function_exists('healthcoach_footer_add_theme_customizer')) {
	function healthcoach_footer_add_theme_customizer() {
		// Front customizer
		if (healthcoach_get_custom_option('show_theme_customizer')=='yes') {
			require_once HEALTHCOACH_FW_PATH . 'core/core.customizer/front.customizer.php';
		}
	}
}



// Add theme required plugins
if ( !function_exists( 'healthcoach_add_trx_utils' ) ) {
    add_filter( 'trx_utils_active', 'healthcoach_add_trx_utils' );
    function healthcoach_add_trx_utils($enable=true) {
        return true;
    }
}

// Return text for the "I agree ..." checkbox
if ( ! function_exists( 'healthcoach_trx_utils_privacy_text' ) ) {
    add_filter( 'trx_utils_filter_privacy_text', 'healthcoach_trx_utils_privacy_text' );
    function healthcoach_trx_utils_privacy_text( $text='' ) {
        return healthcoach_get_privacy_text();
    }
}



// Add class trx_utils_activated
if(!function_exists('healthcoach_add_body_class')) {
    if(!function_exists ( 'trx_utils_require_shortcode')){
        add_filter( 'body_class', 'healthcoach_add_body_class' );
        function healthcoach_add_body_class($classes){
            $classes[] = 'default_theme';
            return $classes;
        }
    }
}


// Include framework core files
//-------------------------------------------------------------------
require_once trailingslashit( get_template_directory() ) . 'fw/loader.php';
?>