<?php
add_action('after_setup_theme', 'manufacto_bunch_theme_setup');
function manufacto_bunch_theme_setup()
{
	global $wp_version;
	if(!defined('MANUFACTO_VERSION')) define('MANUFACTO_VERSION', '1.0');
	if( !defined( 'MANUFACTO_ROOT' ) ) define('MANUFACTO_ROOT', get_template_directory().'/');
	if( !defined( 'MANUFACTO_URL' ) ) define('MANUFACTO_URL', get_template_directory_uri().'/');	
	include_once get_template_directory() . '/includes/loader.php';
	
	
	load_theme_textdomain('manufacto', get_template_directory() . '/languages');
	
	//ADD THUMBNAIL SUPPORT
	add_theme_support('post-thumbnails');
	add_theme_support( 'post-formats', array( 'gallery' ) );
	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" );
	/*
	 * 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', 'manufacto'),
			)
		);
	}
	if ( ! isset( $content_width ) ) $content_width = 960;
	add_image_size( 'manufacto_370x218', 370, 218, true ); // 'manufacto_370x218 Provide Services'
	add_image_size( 'manufacto_270x200', 270, 218, true ); // 'manufacto_270x200 Our Projects'
	add_image_size( 'manufacto_480x300', 480, 300, true ); // 'manufacto_480x300 Ongoing Projects'
	add_image_size( 'manufacto_101x101', 101, 101, true ); // 'manufacto_101x101 Our Testimonials'
	add_image_size( 'manufacto_265x355', 265, 355, true ); // 'manufacto_265x355 Our Team'
	add_image_size( 'manufacto_370x260', 370, 260, true ); // 'manufacto_370x260 Latest News'
	add_image_size( 'manufacto_770x274', 770, 274, true ); // 'manufacto_770x274 Our Projects 2'
	add_image_size( 'manufacto_370x574', 370, 574, true ); // 'manufacto_370x574 Our Projects 2'
	add_image_size( 'manufacto_370x274', 370, 274, true ); // 'manufacto_370x274 Our Projects 2'
	add_image_size( 'manufacto_1170x540', 1170, 540, true ); // 'manufacto_1170x540 Our News'
	add_image_size( 'manufacto_80x70', 80, 70, true ); // 'manufacto_80x70 Recent News Widget'
	add_image_size( 'manufacto_50x50', 50, 50, true ); // 'manufacto_50x50 Latest News Widget'
}
function manufacto_bunch_widget_init()
{
	global $wp_registered_sidebars;
	$theme_options = _WSH()->option();
	//Footer Widget
	if( class_exists( 'Bunch_About_us' ) )register_widget( 'Bunch_About_us' );
	if( class_exists( 'Bunch_Latest_News' ) )register_widget( 'Bunch_Latest_News' );
	if( class_exists( 'Bunch_Our_Solutions' ) )register_widget( 'Bunch_Our_Solutions' );
	//Services Widget
	if( class_exists( 'Bunch_servies' ) )register_widget( 'Bunch_servies' );
	if( class_exists( 'Bunch_Brochures' ) )register_widget( 'Bunch_Brochures' );
	if( class_exists( 'Bunch_Contact_Us' ) )register_widget( 'Bunch_Contact_Us' );
	//Blog Widget
	if( class_exists( 'Bunch_Recent_News' ) )register_widget( 'Bunch_Recent_News' );
	
	
	
	
	register_sidebar(array(
	  'name' => esc_html__( 'Default Sidebar', 'manufacto' ),
	  'id' => 'default-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown on the right-hand side.', 'manufacto' ),
	  'before_widget'=>'<div id="%1$s" class="widget sidebar-widget %2$s">',
	  'after_widget'=>'</div>',
	  'before_title' => '<div class="sidebar-title"><h2>',
	  'after_title' => '</h2></div>'
	));
	register_sidebar(array(
	  'name' => esc_html__( 'Footer Sidebar', 'manufacto' ),
	  'id' => 'footer-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown in Footer Area.', 'manufacto' ),
	  'before_widget'=>'<div class="footer-column col-md-3 col-sm-6 col-xs-12"><div id="%1$s"  class="footer-widget %2$s">',
	  'after_widget'=>'</div></div>',
	  'before_title' => '<h2>',
	  'after_title' => '</h2>'
	));
	
	register_sidebar(array(
	  'name' => esc_html__( 'Blog Listing', 'manufacto' ),
	  'id' => 'blog-sidebar',
	  'description' => esc_html__( 'Widgets in this area will be shown on the right-hand side.', 'manufacto' ),
	  'before_widget'=>'<div id="%1$s" class="widget sidebar-widget %2$s">',
	  'after_widget'=>'</div>',
	  'before_title' => '<div class="sidebar-title"><h2>',
	  'after_title' => '</h2></div>'
	));
	if( !is_object( _WSH() )  )  return;
	$sidebars = manufacto_set(manufacto_set( $theme_options, 'dynamic_sidebar' ) , 'dynamic_sidebar' ); 
	foreach( array_filter((array)$sidebars) as $sidebar)
	{
		if(manufacto_set($sidebar , 'topcopy')) continue ;
		
		$name = manufacto_set( $sidebar, 'sidebar_name' );
		
		if( ! $name ) continue;
		$slug = manufacto_bunch_slug( $name ) ;
		
		register_sidebar( array(
			'name' => $name,
			'id' =>  sanitize_title( $slug ) ,
			'before_widget' => '<div id="%1$s" class="widget sidebar-widget %2$s">',
			'after_widget' => "</div>",
			'before_title' => '<div class="sidebar-title"><h2>',
			'after_title' => '</h2></div>',
		) );		
	}
	
	update_option('wp_registered_sidebars' , $wp_registered_sidebars) ;
}
add_action( 'widgets_init', 'manufacto_bunch_widget_init' );
// Update items in cart via AJAX
function manufacto_load_head_scripts() {
	$options = _WSH()->option();
    if ( !is_admin() ) {
		$protocol = is_ssl() ? 'https://' : 'http://';
		$map_path = '?key='.manufacto_set($options, 'map_api_key');
		wp_enqueue_script( 'manufacto-map-api', ''.$protocol.'maps.google.com/maps/api/js'.$map_path, array(), false, false );
	}
}
add_action( 'wp_enqueue_scripts', 'manufacto_load_head_scripts' );
//global variables
function manufacto_bunch_global_variable() {
    global $wp_query;
}

function manufacto_enqueue_scripts() {
	$theme_options = _WSH()->option();
	if(manufacto_set( $theme_options, 'color_scheme_style') == 'custom_color'){
	$maincolor = str_replace( '#', '' , manufacto_set( $theme_options, 'main_color_scheme', '#ffb200' ) );
	}
	elseif(manufacto_set( $theme_options, 'color_scheme_style') == 'predefined_color'){
	$maincolor = str_replace( '#', '' , manufacto_set( $theme_options, 'predefined_color_scheme', '#ffb200' ) );	
	}
	else{
	$maincolor = str_replace( '#', '' , '#ffb200' );	
	}
	//styles
	wp_enqueue_style( 'bootstrap', get_template_directory_uri() . '/css/bootstrap.css' );
	wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.css' );
	wp_enqueue_style( 'flaticon', get_template_directory_uri() . '/css/flaticon.css' );
	wp_enqueue_style( 'animate', get_template_directory_uri() . '/css/animate.css' );
	wp_enqueue_style( 'owl-theme', get_template_directory_uri() . '/css/owl.css' );
	wp_enqueue_style( 'fancybox', get_template_directory_uri() . '/css/jquery.fancybox.min.css' );
	wp_enqueue_style( 'hover', get_template_directory_uri() . '/css/hover.css' );
	wp_enqueue_style( 'gui', get_template_directory_uri() . '/css/gui.css' );
	wp_enqueue_style( 'bootstrap-touchspin', get_template_directory_uri() . '/css/jquery.bootstrap-touchspin.css' );
	wp_enqueue_style( 'manufacto-main-style', get_stylesheet_uri() );
	wp_enqueue_style( 'manufacto-custom-style', get_template_directory_uri() . '/css/custom.css' );
	wp_enqueue_style( 'manufacto-responsive', get_template_directory_uri() . '/css/responsive.css' );
	wp_enqueue_style( 'manufacto-theme-color', get_template_directory_uri() . '/css/color-themes/default-theme.css' );
	if(class_exists('woocommerce')) wp_enqueue_style( 'manufacto_woocommerce', get_template_directory_uri() . '/css/woocommerce.css' );
	wp_enqueue_style( 'manufacto-main-color', get_template_directory_uri() . '/css/color.php?main_color='.$maincolor );
	wp_enqueue_style( 'manufacto-color-panel', get_template_directory_uri() . '/css/color-panel.css' );
	
	
    //scripts
	wp_enqueue_script( 'jquery-ui-core' );
	wp_enqueue_script( 'bootstrap', get_template_directory_uri().'/js/bootstrap.min.js', array(), false, true );
	wp_enqueue_script( 'fancybox-pack', get_template_directory_uri().'/js/jquery.fancybox.js', array(), false, true );
	wp_enqueue_script( 'owl', get_template_directory_uri().'/js/owl.js', array(), false, true );
	wp_enqueue_script( 'appear', get_template_directory_uri().'/js/appear.js', array(), false, true );
	wp_enqueue_script( 'wow', get_template_directory_uri().'/js/wow.js', array(), false, true );
	wp_enqueue_script( 'isotope', get_template_directory_uri().'/js/isotope.js', array(), false, true );
	wp_enqueue_script( 'map-script', get_template_directory_uri().'/js/map-script.js', array(), false, true );
	wp_enqueue_script( 'manufacto-main-script', get_template_directory_uri().'/js/script.js', array(), false, true );
	if( is_singular() ) wp_enqueue_script('comment-reply');
	
}
add_action( 'wp_enqueue_scripts', 'manufacto_enqueue_scripts' );

/*-------------------------------------------------------------*/
function manufacto_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.
    */
    $pt_sans = _x( 'on', 'PT+Sans font: on or off', 'manufacto' );
	$raleway = _x( 'on', 'Raleway font: on or off', 'manufacto' );
	
	
    if ( 'off' !== $pt_sans || 'off' !== $raleway ) {
        $font_families = array();
 
        if ( 'off' !== $pt_sans ) {
            $font_families[] = 'PT Sans:400,400i,700,700i';
        }
		
		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('manufacto'.'_theme_options');
		if ( manufacto_set( $opt, 'body_custom_font' ) ) {
			if ( $custom_font = manufacto_set( $opt, 'body_font_family' ) )
				$font_families[] = $custom_font . ':300,300i,400,400i,600,700';
		}
		if ( manufacto_set( $opt, 'use_custom_font' ) ) {
			$font_families[] = manufacto_set( $opt, 'h1_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = manufacto_set( $opt, 'h2_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = manufacto_set( $opt, 'h3_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = manufacto_set( $opt, 'h4_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = manufacto_set( $opt, 'h5_font_family' ) . ':300,300i,400,400i,600,700';
			$font_families[] = manufacto_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 manufacto_theme_slug_scripts_styles() {
    wp_enqueue_style( 'manufacto-theme-slug-fonts', manufacto_theme_slug_fonts_url(), array(), null );
}
add_action( 'wp_enqueue_scripts', 'manufacto_theme_slug_scripts_styles' );
/*---------------------------------------------------------------------*/
function manufacto_add_editor_styles() {
    add_editor_style( 'custom-editor-style.css' );
}
add_action( 'admin_init', 'manufacto_add_editor_styles' );
/**
 * WooCommerce Extra Feature
 * --------------------------
 *
 * Change number of related products on product page
 * Set your own value for 'posts_per_page'
 *
 */ 
function manufacto_woo_related_products_limit() {
  global $product;
	
	$args['posts_per_page'] = 6;
	return $args;
}
add_filter( 'woocommerce_output_related_products_args', 'manufacto_jk_related_products_args' );
  function manufacto_jk_related_products_args( $args ) {
	$args['posts_per_page'] = 3; // 4 related products
	$args['columns'] = 3; // arranged in 2 columns
	return $args;
}