<?php
/**
 * functions.php
 * @package WordPress
 * @subpackage Froday
 * @since froday 2.5.5
 * 
 */
 
/*************************************************
## Admin style and scripts  
*************************************************/ 

function froday_admin_styles() {
     wp_enqueue_style('froday-klbtheme',    get_template_directory_uri() .'/assets/css/admin/klbtheme.css');
	 wp_enqueue_script('froday-init', 	    get_template_directory_uri() .'/assets/js/init.js', array('jquery','media-upload','thickbox'));
}
add_action('admin_enqueue_scripts', 'froday_admin_styles');

 /*************************************************
## froday Fonts
*************************************************/

function froday_fonts_url_montserrat() {
        $fonts_url = '';
 
		$montserrat = _x( 'on', 'Montserrat font: on or off', 'froday' );	

		if ( 'off' !== $montserrat ) {
		$font_families = array();
		 
		if ( 'off' !== $montserrat ) {
		$font_families[] = 'Montserrat:400,500,600';
		}
		
		$query_args = array( 
		'family' => rawurldecode( implode( '|', $font_families ) ), 
		'subset' => rawurldecode( 'latin,latin-ext' ), 
		); 
		 
		$fonts_url = add_query_arg( $query_args, 'https://fonts.googleapis.com/css' );
		}
 
return esc_url_raw( $fonts_url );
}

/*************************************************
## Styles and Scripts
*************************************************/ 
define('FRODAY_INDEX_JS',     get_template_directory_uri()  . '/assets/js');
define('FRODAY_INDEX_VENDOR', get_template_directory_uri()  . '/assets/vendors');
define('FRODAY_INDEX_CSS',    get_template_directory_uri()  . '/assets/css');

function froday_scripts() {
	
     if ( is_admin_bar_showing() ) {
       wp_enqueue_style( 'klbtheme', FRODAY_INDEX_CSS . '/admin/klbtheme.css', false, '1.0');    
     }	
	 
     if ( is_singular() ) wp_enqueue_script( 'comment-reply' );

     wp_enqueue_style( 'bootstrap',    				FRODAY_INDEX_CSS  	 . '/bootstrap.min.css', false, '1.0');
     wp_enqueue_style( 'font-awesome', 		    	FRODAY_INDEX_VENDOR  . '/font-awesome/css/font-awesome.min.css', false, '1.0');
     wp_enqueue_style( 'linearicons', 				FRODAY_INDEX_VENDOR  . '/linearicons/css/linearicons.css', false, '1.0');	
     wp_enqueue_style( 'owl-carousel', 		    	FRODAY_INDEX_VENDOR  . '/owl-carousel/owl.carousel.min.css', false, '1.0');	
     wp_enqueue_style( 'owl-theme', 	        	FRODAY_INDEX_VENDOR  . '/owl-carousel/owl.theme.min.css', false, '1.0');
     wp_enqueue_style( 'flexslider',     			FRODAY_INDEX_VENDOR  . '/flexslider/flexslider.css', false, '1.0');
     wp_enqueue_style( 'froda-base',      			FRODAY_INDEX_CSS  	 . '/base.css', false, '1.0');
     wp_enqueue_style( 'froday-custom',      		FRODAY_INDEX_CSS  	 . '/custom.css', false, '1.0');	  
     wp_enqueue_style( 'froday-font-montserrat',    froday_fonts_url_montserrat(), array(), null );	 
  	 wp_enqueue_style( 'froday-style',          	get_stylesheet_uri() );   
	 wp_style_add_data( 'froday-style', 'rtl', 'replace' );

	 $mapkey = ot_get_option('froday_mapapi');
	
     wp_enqueue_script( 'bootstrap',     	   		 FRODAY_INDEX_JS 	 . '/bootstrap.min.js', array('jquery'), '1.0', true);
     wp_enqueue_script( 'modernizr',  	       		 FRODAY_INDEX_VENDOR . '/modernizr/modernizr-2.6.2.min.js', array('jquery'), '1.0', true);
     wp_enqueue_script( 'owl-carousel',    	   		 FRODAY_INDEX_VENDOR . '/owl-carousel/owl.carousel.min.js', array('jquery'), '1.0', true);
     wp_enqueue_script( 'jquery-flexslider',    	 FRODAY_INDEX_VENDOR . '/flexslider/jquery.flexslider-min.js', array('jquery'), '1.0', true);
     wp_enqueue_script( 'jquery-countdown',    	 	 FRODAY_INDEX_VENDOR . '/countdown/jquery.countdown.js', array('jquery'), '1.0', true);
     wp_enqueue_script( 'socialite',    	 	 	 FRODAY_INDEX_JS 	 . '/socialite.min.js', array('jquery'), '1.0', true);
     wp_register_script( 'froday-countdown',    	 FRODAY_INDEX_JS 	 . '/custom/froday_countdown.js', array('jquery'), '1.0', true);
     wp_register_script( 'froday-countdown-shop',    FRODAY_INDEX_JS 	 . '/custom/froday_countdown_shop.js', array('jquery'), '1.0', true);
	 wp_register_script( 'googlemap',                'https://maps.googleapis.com/maps/api/js?key='. $mapkey .'', array('jquery'), '1.0', true);
	 if ( is_rtl() ) {
     wp_enqueue_script( 'froday-main-rtl',  	     FRODAY_INDEX_JS . '/main-rtl.js', array('jquery'), '1.0', true);
	 wp_style_add_data( 'froday-style', 'rtl', 'replace' );
	 } else {
     wp_enqueue_script( 'froday-main',  	   		 FRODAY_INDEX_JS . '/main.js', array('jquery'), '1.0', true);
	 }

    }
add_action( 'wp_enqueue_scripts', 'froday_scripts' );

/*************************************************
## Froday Theme options
*************************************************/

	require_once get_template_directory() . '/includes/metaboxes.php';
	require_once get_template_directory() . '/includes/sanitize.php';
	require_once get_template_directory() . '/includes/woocommerce.php';
   	add_filter( 'ot_show_pages', '__return_false' );
	add_filter( 'ot_show_new_layout', '__return_false' );
	require_once get_template_directory() . '/includes/theme-options.php';
	if(function_exists('vc_set_as_theme')) { 
	   require_once get_template_directory() . '/includes/js_composer/shortcodes.php';
	}

	
/*************************************************
## Theme Setup
*************************************************/ 

if ( ! isset( $content_width ) ) $content_width = 960;

function froday_theme_setup() {
	
	add_theme_support( 'title-tag' );
	add_theme_support( 'automatic-feed-links' );
	add_theme_support( 'post-thumbnails' );
    add_theme_support( 'custom-background' );
	add_theme_support( 'post-formats', array('gallery', 'audio', 'video'));
	add_theme_support( 'wc-product-gallery-lightbox' );
	add_theme_support( 'wc-product-gallery-slider' );
	add_theme_support( 'woocommerce', array('gallery_thumbnail_image_width' => 150,'thumbnail_image_width' => 80,) );
	load_theme_textdomain( 'froday', get_template_directory() . '/languages' );

}
add_action( 'after_setup_theme', 'froday_theme_setup' );


/*************************************************
## Include the TGM_Plugin_Activation class.
*************************************************/ 

require_once get_template_directory() . '/includes/class-tgm-plugin-activation.php';

add_action( 'tgmpa_register', 'froday_register_required_plugins' );

function froday_register_required_plugins() {
	
	$url = 'http://klbtheme.com/froday/plugins/';
	$mainurl = 'http://klbtheme.com/plugins/';

	$plugins = array(
		
        array(
            'name'                  => esc_html__('Meta Box','froday'),
            'slug'                  => 'meta-box',
        ),

        array(
            'name'                  => esc_html__('Contact Form 7','froday'),
            'slug'                  => 'contact-form-7',
        ),

        array(
            'name'                  => esc_html__('WooCommerce','froday'),
            'slug'                  => 'woocommerce',
        ),
		
		array(
            'name'                  => esc_html__('MailChimp Subscribe','froday'),
            'slug'                  => 'mailchimp-for-wp',
        ),

		array(
            'name'                  => esc_html__('Recent Tweets Widget','froday'),
            'slug'                  => 'recent-tweets-widget',
        ),

		array(
            'name'                  => esc_html__('Instagram Widget','froday'),
            'slug'                  => 'instagram-widget-by-wpzoom',
        ),

		array(
            'name'                  => esc_html__('Products Filter','froday'),
            'slug'                  => 'woocommerce-products-filter',
        ),

        array(
            'name'                  => esc_html__('Theme Options','froday'),
            'slug'                  => 'option-tree',
            'source'                => $url . 'option-tree.zip',
            'required'              => false,
            'version'               => '2.6.0',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),

        array(
            'name'                  => esc_html__('WPBakery Page Builder','froday'),
            'slug'                  => 'js_composer',
            'source'                => $mainurl . 'js-composer.zip',
            'required'              => false,
            'version'               => '6.2.0',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),

        array(
            'name'                  => esc_html__('Klb Shortcode','froday'),
            'slug'                  => 'klb-shortcode',
            'source'                => $url . 'klb-shortcode.zip',
            'required'              => false,
            'version'               => '1.9.2',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),
		
        array(
            'name'                  => esc_html__('Revolution Slider','froday'),
            'slug'                  => 'revslider',
            'source'                => $mainurl . 'revslider.zip',
            'required'              => false,
            'version'               => '6.2.15',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),

        array(
            'name'                  => esc_html__('Envato Market Master','froday'),
            'slug'                  => 'envato-market',
            'source'                => $mainurl . 'envato-market.zip',
            'required'              => true,
            'version'               => '2.0.3',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),

        array(
            'name'                  => esc_html__('Demo Installation','froday'),
            'slug'                  => 'easy_installer',
            'source'                => $url . 'easy_installer.zip',
            'required'              => false,
            'version'               => '1.2',
            'force_activation'      => false,
            'force_deactivation'    => false,
            'external_url'          => '',
        ),


	);

	$config = array(
		'id'           => 'froday',                 // Unique ID for hashing notices for multiple instances of TGMPA.
		'default_path' => '',                      // Default absolute path to bundled plugins.
		'menu'         => 'tgmpa-install-plugins', // Menu slug.
		'parent_slug'  => 'themes.php',            // Parent menu slug.
		'capability'   => 'edit_theme_options',    // Capability needed to view plugin install page, should be a capability associated with the parent menu used.
		'has_notices'  => true,                    // Show admin notices or not.
		'dismissable'  => true,                    // If false, a user cannot dismiss the nag message.
		'dismiss_msg'  => '',                      // If 'dismissable' is false, this message will be output at top of nag.
		'is_automatic' => false,                   // Automatically activate plugins after installation or not.
		'message'      => '',                      // Message to output right before the plugins table.
	);

	tgmpa( $plugins, $config );
}

/*************************************************
## Froday Register Menu 
*************************************************/

function froday_register_menus() {
	register_nav_menus( array( 'main-menu' => esc_html__('Primary Navigation Menu','froday')) ); 
	if(ot_get_option('froday_top_header') == "on"){
	register_nav_menus( array( 'top-left-menu' => esc_html__('Top Left Menu','froday')) );     
	register_nav_menus( array( 'top-right-menu' => esc_html__('Top Right Menu','froday')) );
	}
}
add_action('init', 'froday_register_menus');

/*************************************************
## Froday Menu
*************************************************/ 
class froday_description_walker extends Walker_Nav_Menu {


    function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ){
        $id_field = $this->db_fields['id'];
        if ( is_object( $args[0] ) ) {
            $args[0]->has_children = ! empty( $children_elements[$element->$id_field] );
        }
        return parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
    }
      function start_el(&$output, $object, $depth = 0, $args = Array() , $current_object_id = 0) {
           
           global $wp_query;

           $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

           $class_names = $value = '';
		   
		   $classes = empty( $object->classes ) ? array() : (array) $object->classes;
           $icon_class = $classes[0];
		   $classes = array_slice($classes,1);
		   
		   $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $object ) );
		   $class_names = 'class=" '. esc_attr( $class_names ) . '"';


			$output .= $indent . '<li ' . $value . $class_names .'>';

			$datahover = str_replace(' ','',$object->title);
			
			$attributes = ! empty( $object->url ) ? ' href="'   . esc_attr( $object->url ) .'"' : '';	

			$object_output = $args->before;

			$object_output .= '<a'. $attributes .'  >';
			$object_output .= $args->link_before .  apply_filters( 'the_title', $object->title, $object->ID ) . '';
	        $object_output .= $args->link_after;
			$object_output .= '</a>';


			$object_output .= $args->after;

			$output .= apply_filters( 'walker_nav_menu_start_el', $object_output, $object, $depth, $args );            	              	
      }
}



add_filter('nav_menu_css_class' , 'froday_nav_class' , 10 , 2);
function froday_nav_class($classes, $item){
     if( in_array('current-menu-item', $classes) ){
             $classes[] = 'active';
     }
     return $classes;
}

add_filter('nav_menu_css_class' , 'froday_nav_class1' , 10 , 2);
function froday_nav_class1($classes, $item){
     if( in_array('current-menu-parent', $classes) ){
             $classes[] = 'active';
     }
     return $classes;
}

/*************************************************
## Word Limiter
*************************************************/ 
function froday_limit_words($string, $limit) {
	$words = explode(' ', $string);
	return implode(' ', array_slice($words, 0, $limit));
}


/*************************************************
## Coupon Page
*************************************************/ 

function froday_add_excerpt_support_for_pages() {
	add_post_type_support( 'shop_coupon', 'thumbnail' );
	add_post_type_support( 'shop_coupon', 'editor' );
}
add_action( 'init', 'froday_add_excerpt_support_for_pages' );

/*************************************************
## Widgets
*************************************************/ 

function froday_widgets_init() {
	register_sidebar( array(
	  'name' => esc_html__( 'Blog Sidebar', 'froday' ),
	  'id' => 'blog-sidebar',
	  'description'   => esc_html__( 'These are widgets for the Blog page.','froday' ),
	  'before_widget' => '<div class="col-xs-12"><div class="widget klbblogwidget panel ptb-30 prl-20 %2$s">',
	  'after_widget'  => '</div></div>',
	  'before_title'  => '<h3 class="widget-title h-title mb-30">',
	  'after_title'   => '</h3>'
	) );

	register_sidebar( array(
	  'name' => esc_html__( 'Shop Sidebar', 'froday' ),
	  'id' => 'shop-sidebar',
	  'description'   => esc_html__( 'These are widgets for the Footer.','froday' ),
	  'before_widget' => '<div class="col-xs-12"><div class="widget panel pt-20 pb-20 prl-20 %2$s">',
	  'after_widget'  => '</div></div>',
	  'before_title'  => '<h3 class="widget-title h-title mb-30">',
	  'after_title'   => '</h3>'
	) );

	register_sidebar( array(
	  'name' => esc_html__( 'Footer First Column', 'froday' ),
	  'id' => 'footer-1',
	  'description'   => esc_html__( 'These are widgets for the Footer.','froday' ),
	  'before_widget' => '<div class="klbfooterwidget %2$s">',
	  'after_widget'  => '</div>',
	  'before_title'  => '<h2>',
	  'after_title'   => '</h2>'
	) );

	register_sidebar( array(
	  'name' => esc_html__( 'Footer Second Column', 'froday' ),
	  'id' => 'footer-2',
	  'description'   => esc_html__( 'These are widgets for the Footer.','froday' ),
	  'before_widget' => '<div class="klbfooterwidget %2$s">',
	  'after_widget'  => '</div>',
	  'before_title'  => '<h2>',
	  'after_title'   => '</h2>'
	) );

	register_sidebar( array(
	  'name' => esc_html__( 'Footer Third Column', 'froday' ),
	  'id' => 'footer-3',
	  'description'   => esc_html__( 'These are widgets for the Footer.','froday' ),
	  'before_widget' => '<div class="klbfooterwidget %2$s">',
	  'after_widget'  => '</div>',
	  'before_title'  => '<h2>',
	  'after_title'   => '</h2>'
	) );
	
	register_sidebar( array(
	  'name' => esc_html__( 'Footer Fourth Column', 'froday' ),
	  'id' => 'footer-4',
	  'description'   => esc_html__( 'These are widgets for the Footer.','froday' ),
	  'before_widget' => '<div class="klbfooterwidget %2$s">',
	  'after_widget'  => '</div>',
	  'before_title'  => '<h2>',
	  'after_title'   => '</h2>'
	) );
}
add_action( 'widgets_init', 'froday_widgets_init' );

/*************************************************
## Custom body classes
*************************************************/

function froday_body_classes( $classes ) {

	$classes[] = 'wide-layout preloader-active';
	
	return $classes;
}
add_filter( 'body_class', 'froday_body_classes' );

/*************************************************
##  Additional Post Class
*************************************************/
function froday_classes($c) {

       $c[] = 'entry panel';

       return $c;
}
add_filter('post_class', 'froday_classes');

/*************************************************
## Excerpt More
*************************************************/ 

function froday_excerpt_more($more) {
  global $post;
  return '<footer class="entry-footer text-right">
          <a href="'. esc_url(get_permalink($post->ID)) . '" class="more-link btn btn-link">' . esc_html__('READ MORE', 'froday') . ' <i class="icon fa fa-long-arrow-right"></i></a>
         </footer>';
  }
 add_filter('excerpt_more', 'froday_excerpt_more');
 
 /*************************************************
## Froday Comment
*************************************************/

if ( ! function_exists( 'froday_comment' ) ) :
 function froday_comment( $comment, $args, $depth ) {
  $GLOBALS['comment'] = $comment;
  switch ( $comment->comment_type ) :
   case 'pingback' :
   case 'trackback' :
  ?>

   <article class="post pingback">
   <p><?php esc_html_e( 'Pingback:', 'froday' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( '(Edit)', 'froday' ), ' ' ); ?></p>
  <?php
    break;
   default :
  ?>
  
		<li class="comment media">
			<figure class="comment-author-thumb media-left">
					<img class="media-object" src="<?php echo get_avatar_url( $comment, 100 ); ?>" alt="<?php comment_author(); ?>" />
			</figure>
			<article class="comment-wrapper media-body">
				<!-- Comment Metadata -->
				<div class="comment-meta mb-5">
					<?php comment_reply_link( array_merge( $args, array( 'depth' => $depth, 'max_depth' => $args['max_depth'] ) ) ); ?>
					<h5 class="comment-author-name mb-5 "><?php comment_author(); ?></h5>
					<span class="color-muted"><?php comment_date(); ?></span>
				</div>
				<!-- End Comment Metadata -->

				<!-- Comment Content -->
				<div class="comment-content color-mid klb-post">
					<p><?php comment_text(); ?></p>
					<?php if ( $comment->comment_approved == '0' ) : ?>
					<em><?php esc_html_e( 'Your comment is awaiting moderation.', 'froday' ); ?></em>
					<?php endif; ?>
					<article class="clearfix" id="comment-<?php comment_ID(); ?>"></article>
				</div>
				<!-- End Comment Content -->
			</article>
		</li>
  <?php
    break;
  endswitch;
 }
endif;

 /*************************************************
## Add a pingback url auto-discovery header for single posts, pages, or attachments.
*************************************************/

function froday_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'froday_pingback_header' );