<?php
add_action('after_setup_theme', 'buildark_bunch_theme_setup');
function buildark_bunch_theme_setup()
{
	global $wp_version;
	if(!defined('BUILDARK_VERSION')) define('BUILDARK_VERSION', '1.0');
	if( !defined( 'BUILDARK_ROOT' ) ) define('BUILDARK_ROOT', get_template_directory().'/');
	if( !defined( 'BUILDARK_URL' ) ) define('BUILDARK_URL', get_template_directory_uri().'/');	
	if( !defined( 'KC_LICENSE' ) ) define('KC_LICENSE', 'twrvqysv-51mw-zgsz-8scg-e03y-7cawas6y3vji');
	include_once get_template_directory() . '/includes/loader.php';
	
	
	load_theme_textdomain('buildark', get_template_directory() . '/languages');
	
	//ADD THUMBNAIL SUPPORT
	add_theme_support('post-thumbnails');
	add_theme_support('woocommerce');
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support('automatic-feed-links'); //Enables post and comment RSS feed links to head.
	add_theme_support('widgets'); //Add widgets and sidebar support
	add_theme_support( "title-tag" );
	add_theme_support( 'wp-block-styles' );
	add_theme_support( 'align-wide' );
	add_theme_support( 'editor-styles' );
	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
	) );
	/** Register wp_nav_menus */
	if(function_exists('register_nav_menu'))
	{
		register_nav_menus(
			array(
				/** Register Main Menu location header */
				'main_menu' => esc_html__('Main Menu', 'buildark'),
			)
		);
	}
	if ( ! isset( $content_width ) ) $content_width = 960;
	add_image_size( 'buildark_370x205', 370, 205, true ); // 'buildarkx370x205 Our Services'
	add_image_size( 'buildark_370x190', 370, 190, true ); // 'buildarkx370x190 Latest News'
	add_image_size( 'buildark_270x200', 270, 200, true ); // 'buildarkx270x200 Recent Projects'
	add_image_size( 'buildark_80x80', 80, 80, true ); // 'buildarkx80x80 Our Testimonials'
	add_image_size( 'buildark_70x70', 70, 70, true ); // 'buildarkx70x70 Our Testimonials Style Two'
	add_image_size( 'buildark_370x220', 370, 220, true ); // 'buildarkx370x220 Latest News Style Two'
	add_image_size( 'buildark_384x340', 384, 340, true ); // 'buildarkx384x340 Recent Projects Two'
	add_image_size( 'buildark_277x180', 277, 180, true ); // 'buildarkx277x180 Services four column Home 4'
	add_image_size( 'buildark_450x350', 450, 350, true ); // 'buildarkx450x350 Recent Projects Three'
	add_image_size( 'buildark_90x90', 90, 90, true ); // 'buildarkx90x90 Testimonials Style Four'
	add_image_size( 'buildark_100x95', 100, 95, true ); // 'buildarkx100x95 Latest News Three'
	add_image_size( 'buildark_660x365', 660, 365, true ); // 'buildark_660x365 Services Detail'
	add_image_size( 'buildark_200x107', 200, 107, true ); // 'buildark_200x107 Services Detail'
	add_image_size( 'buildark_770x430', 770, 430, true ); // 'buildarkx770x430 Blog Page'
	add_image_size( 'buildark_75x75', 75, 75, true ); // 'buildarkx75x75 Blog Sidebar'
}

function buildark_gutenberg_editor_palette_styles() {
    add_theme_support( 'editor-color-palette', array(
        array(
            'name' => esc_html__( 'strong yellow', 'buildark' ),
            'slug' => 'strong-yellow',
            'color' => '#f7bd00',
        ),
        array(
            'name' => esc_html__( 'strong white', 'buildark' ),
            'slug' => 'strong-white',
            'color' => '#fff',
        ),
		array(
            'name' => esc_html__( 'light black', 'buildark' ),
            'slug' => 'light-black',
            'color' => '#242424',
        ),
        array(
            'name' => esc_html__( 'very light gray', 'buildark' ),
            'slug' => 'very-light-gray',
            'color' => '#797979',
        ),
        array(
            'name' => esc_html__( 'very dark black', 'buildark' ),
            'slug' => 'very-dark-black',
            'color' => '#000000',
        ),
    ) );
	
	add_theme_support( 'editor-font-sizes', array(
		array(
			'name' => esc_html__( 'Small', 'buildark' ),
			'size' => 10,
			'slug' => 'small'
		),
		array(
			'name' => esc_html__( 'Normal', 'buildark' ),
			'size' => 15,
			'slug' => 'normal'
		),
		array(
			'name' => esc_html__( 'Large', 'buildark' ),
			'size' => 24,
			'slug' => 'large'
		),
		array(
			'name' => esc_html__( 'Huge', 'buildark' ),
			'size' => 36,
			'slug' => 'huge'
		)
	) );
	
}
add_action( 'after_setup_theme', 'buildark_gutenberg_editor_palette_styles' );

function buildark_bunch_widget_init()
{
	global $wp_registered_sidebars;
	$theme_options = _WSH()->option();
	register_sidebar(array(
	  'name' => esc_html__( 'Default Sidebar', 'buildark' ),
	  'id' => 'default-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown on the right-hand side.', 'buildark' ),
	  'before_widget'=>'<div id="%1$s" class="widget sidebar-widget %2$s">',
	  'after_widget'=>'</div>',
	  'before_title' => '<div class="sidebar-title-two"><h2>',
	  'after_title' => '</h2></div>'
	));
	register_sidebar(array(
	  'name' => esc_html__( 'Footer Sidebar', 'buildark' ),
	  'id' => 'footer-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown in Footer Area.', 'buildark' ),
	  'before_widget'=>'<div class="footer-column col-md-3 col-sm-6 col-xs-12"><div id="%1$s"  class="column footer-widget %2$s">',
	  'after_widget'=>'</div></div>',
	  'before_title' => '<h2>',
	  'after_title' => '</h2>'
	));
	
	register_sidebar(array(
	  'name' => esc_html__( 'Blog Listing', 'buildark' ),
	  'id' => 'blog-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown on the right-hand side.', 'buildark' ),
	  'before_widget'=>'<div id="%1$s" class="widget sidebar-widget %2$s">',
	  'after_widget'=>'</div>',
	  'before_title' => '<div class="sidebar-title-two"><h2>',
	  'after_title' => '</h2></div>'
	));
	
	register_sidebar(array(
	  'name' => esc_html__( 'Service Listing', 'buildark' ),
	  'id' => 'service-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown on the left-hand side.', 'buildark' ),
	  'before_widget'=>'<div id="%1$s" class="widget sidebar-widget %2$s">',
	  'after_widget'=>'</div>',
	  'before_title' => '<div class="sidebar-title"><h3>',
	  'after_title' => '</h3></div>'
	));
	if( !is_object( _WSH() )  )  return;
	$sidebars = buildark_set(buildark_set( $theme_options, 'dynamic_sidebar' ) , 'dynamic_sidebar' ); 
	foreach( array_filter((array)$sidebars) as $sidebar)
	{
		if(buildark_set($sidebar , 'topcopy')) continue ;
		
		$name = buildark_set( $sidebar, 'sidebar_name' );
		
		if( ! $name ) continue;
		$slug = buildark_bunch_slug( $name ) ;
		
		register_sidebar( array(
			'name' => $name,
			'id' =>  sanitize_title( $slug ) ,
			'before_widget' => '<div id="%1$s" class="side-bar widget sidebar_widget %2$s">',
			'after_widget' => "</div>",
			'before_title' => '<div class="sec-title"><h3 class="skew-lines">',
			'after_title' => '</h3></div>',
		) );		
	}
	
	update_option('wp_registered_sidebars' , $wp_registered_sidebars) ;
}
add_action( 'widgets_init', 'buildark_bunch_widget_init' );
// Update items in cart via AJAX
function buildark_load_head_scripts() {
	$options = _WSH()->option();
    if ( !is_admin() ) {
		$protocol = is_ssl() ? 'https://' : 'http://';
		if( buildark_set($options, 'map_api_key') ){
		$map_path = '?key='.buildark_set($options, 'map_api_key');
		wp_enqueue_script( 'buildark-map-api', ''.$protocol.'maps.google.com/maps/api/js'.$map_path, array(), false, false );
		wp_enqueue_script( 'jquery-googlemap', get_template_directory_uri().'/assets/js/gmaps.js', array(), false, false );
		}
	}
}
add_action( 'wp_enqueue_scripts', 'buildark_load_head_scripts' );
//global variables
function buildark_bunch_global_variable() {
    global $wp_query;
}
function buildark_enqueue_scripts() {
	$theme_options = _WSH()->option();
	$maincolor = str_replace( '#', '' , buildark_set( $theme_options, 'main_color_scheme', '#ab7442' ) );
    //styles
	wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.css' );
	wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.css' );
	wp_enqueue_style( 'flaticon', get_template_directory_uri() . '/assets/css/flaticon.css' );
	wp_enqueue_style( 'animate', get_template_directory_uri() . '/assets/css/animate.css' );
	wp_enqueue_style( 'owl', get_template_directory_uri() . '/assets/css/owl.css' );
	wp_enqueue_style( 'jquery-fancybox', get_template_directory_uri() . '/assets/css/jquery.fancybox.css' );
	wp_enqueue_style( 'hover', get_template_directory_uri() . '/assets/css/hover.css' );
	wp_enqueue_style( 'gui', get_template_directory_uri() . '/assets/css/gui.css' );
	wp_enqueue_style( 'buildark-main-style', get_stylesheet_uri() );
	wp_enqueue_style( 'buildark-custom-style', get_template_directory_uri() . '/assets/css/custom.css' );
	wp_enqueue_style( 'buildark-gutenberg', get_template_directory_uri() . '/assets/css/gutenberg.css' );
	wp_enqueue_style( 'buildark-responsive-style', get_template_directory_uri() . '/assets/css/responsive.css' );
	if(class_exists('woocommerce')) wp_enqueue_style( 'buildark-woocommerce', get_template_directory_uri() . '/assets/css/woocommerce.css' );
	
	
    //scripts
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'bootstrap-min', get_template_directory_uri().'/assets/js/bootstrap.min.js', array(), false, true );
	wp_enqueue_script( 'jquery-fancybox', get_template_directory_uri().'/assets/js/jquery.fancybox.pack.js', array(), false, true );
	wp_enqueue_script( 'fancybox-media', get_template_directory_uri().'/assets/js/jquery.fancybox-media.js', array(), false, true );
	wp_enqueue_script( 'owl-js', get_template_directory_uri().'/assets/js/owl.js', array(), false, true );
	wp_enqueue_script( 'appear-js', get_template_directory_uri().'/assets/js/appear.js', array(), false, true );
	wp_enqueue_script( 'wow-js', get_template_directory_uri().'/assets/js/wow.js', array(), false, true );
	wp_enqueue_script( 'mixitup-js', get_template_directory_uri().'/assets/js/mixitup.js', array(), false, true );
	wp_enqueue_script( 'jquery-ui', get_template_directory_uri().'/assets/js/jquery-ui.js', array(), false, true );
	if( buildark_set($theme_options, 'map_api_key') ){
	wp_enqueue_script( 'map-script', get_template_directory_uri().'/assets/js/map-script.js', array(), false, true );}
	wp_enqueue_script( 'buildark-main-script', get_template_directory_uri().'/assets/js/script.js', array(), false, true );
	if( is_singular() ) wp_enqueue_script('comment-reply');
}
add_action( 'wp_enqueue_scripts', 'buildark_enqueue_scripts' );

/*-------------------------------------------------------------*/
function buildark_theme_slug_fonts_url() {
    $fonts_url = '';
 
    /* Translators: If there are characters in your language that are not
    * supported by Lora, translate this to 'off'. Do not translate
    * into your own language.
    */
    $montserrat = _x( 'on', 'Montserrat Script: on or off', 'buildark' );
	$raleway = _x( 'on', 'Raleway font: on or off', 'buildark' );
 
    if ( 'off' !== $montserrat || 'off' !== $raleway ) {
        $font_families = array();
 
        if ( 'off' !== $montserrat ) {
            $font_families[] = 'Montserrat:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i';
        }
		
		if ( 'off' !== $raleway ) {
            $font_families[] = 'Raleway:100,100i,200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i,900,900i';
        }
 
        $opt = get_option('buildark'.'_theme_options');
		if ( buildark_set( $opt, 'body_custom_font' ) ) {
			if ( $custom_font = buildark_set( $opt, 'body_font_family' ) )
				$font_families[] = $custom_font . ':300,300i,400,400i,600,700';
		}
		if ( buildark_set( $opt, 'use_custom_font' ) ) {
			$font_families[] = buildark_set( $opt, 'h1_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = buildark_set( $opt, 'h2_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = buildark_set( $opt, 'h3_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = buildark_set( $opt, 'h4_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = buildark_set( $opt, 'h5_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = buildark_set( $opt, 'h6_font_family' ) . ':300,300i,400,400i,600,700';
		}
		$font_families = array_unique( $font_families);
        
		$query_args = array(
            'family' => urlencode( implode( '|', $font_families ) ),
            'subset' => urlencode( 'latin,latin-ext' ),
        );
 
        $fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
    }
 
    return esc_url_raw( $fonts_url );
}
function buildark_theme_slug_scripts_styles() {
    wp_enqueue_style( 'buildark-theme-slug-fonts', buildark_theme_slug_fonts_url(), array(), null );
}
add_action( 'wp_enqueue_scripts', 'buildark_theme_slug_scripts_styles' );
/*---------------------------------------------------------------------*/
function buildark_add_editor_styles() {
    add_editor_style( 'editor-style.css' );
}
add_action( 'admin_init', 'buildark_add_editor_styles' );
/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Change number of related products on product page
 * Set your own value for 'posts_per_page'
 *
 */ 
function buildark_woo_related_products_limit() {
  global $product;
	
	$args['posts_per_page'] = 6;
	return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'buildark_jk_related_products_args' );
  function buildark_jk_related_products_args( $args ) {
	$args['posts_per_page'] = 3; // 4 related products
	$args['columns'] = 3; // arranged in 2 columns
	return $args;
}