<?php
/**
 * Custom functions with WP hooks for this theme
 *
 * @package Excel_Business
 */

if ( ! function_exists( 'excel_business_setup' ) ) :
/**
 * Sets up theme defaults and registers support for various WordPress features.
 *
 * Note that this function is hooked into the after_setup_theme hook, which
 * runs before the init hook. The init hook is too late for some features, such
 * as indicating support for post thumbnails.
 */
function excel_business_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Excel Business, use a find and replace
	 * to change 'excel-business' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'excel-business', get_template_directory() . '/languages' );

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

	/*
	 * Let WordPress manage the document title.
	 * By adding theme support, we declare that this theme does not use a
	 * hard-coded <title> tag in the document head, and expect WordPress to
	 * provide it for us.
	 */
	add_theme_support( 'title-tag' );

	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 *
	 * @link https://developer.wordpress.org/themes/functionality/featured-images-post-thumbnails/
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary' => esc_html__( 'Primary', 'excel-business' ),
	) );

	/*
	 * Switch default core markup for search form, comment form, and comments
	 * to output valid HTML5.
	 */
	add_theme_support( 'html5', array(
		'search-form',		
		'gallery',
		'caption',
	) );

	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'excel_business_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );
    
    // Add theme support for Responsive Videos.
	add_theme_support( 'jetpack-responsive-videos' );
    
    /* Custom Image Sizes */
    add_image_size( 'excel-business-slider', 1920, 811, true);
    add_image_size( 'excel-business-image', 1170, 420, true);
    add_image_size( 'excel-business-blog', 360, 270, true);
    add_image_size( 'excel-business-team', 263, 255, true);
    
    /* Custom Logo */
    add_theme_support( 'custom-logo', array(
    	'header-text' => array( 'site-title', 'site-description' ),
    ) );

    //Support of Exceltheme Addon Custom Post
    add_theme_support( 'excel-custom-post' );
    
    //Support for Exceltheme Addon Icon Text Widget
    add_theme_support( 'ea_icon_text_widget' );
}
endif;
add_action( 'after_setup_theme', 'excel_business_setup' );

/**
 * Set the content width in pixels, based on the theme's design and stylesheet.
 *
 * Priority 0 to make it available to lower priority callbacks.
 *
 * @global int $content_width
 */
function excel_business_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'excel_business_content_width', 750 );
}
add_action( 'after_setup_theme', 'excel_business_content_width', 0 );

/**
* Adjust content_width value according to template.
*
* @return void
*/
function excel_business_template_redirect_content_width(){
	// Full Width in the absence of sidebar.
	if( is_page_template( 'full-width.php' ) || !is_active_sidebar( 'sidebar' ) ){
	   $GLOBALS['content_width'] = 1170;        
	}else{
	   $GLOBALS['content_width'] = 750;  
	}
}
add_action( 'template_redirect', 'excel_business_template_redirect_content_width' );

/**
 * Enqueue scripts and styles.
 */
function excel_business_scripts() {
    
    $args = array(
		'family' => 'Open+Sans:300,400,400i,600,600i,700,700i,800|Abril+Fatface',
	);
    
    wp_enqueue_style( 'excel-business-google-fonts', add_query_arg( $args, "//fonts.googleapis.com/css" ) );
    wp_enqueue_style( 'font-awesome', get_template_directory_uri() . '/css/font-awesome.css', array(), '4.7.0' );
    wp_enqueue_style( 'owl-carousel', get_template_directory_uri() . '/css/owl.carousel.css', array(), '2.2.1' ); 
    wp_enqueue_style( 'owl-theme-default', get_template_directory_uri() . '/css/owl.theme.default.css', array( 'owl-carousel' ), '2.2.1' );
    wp_enqueue_style( 'animate', get_template_directory_uri() . '/css/animate.css', array( 'owl-carousel' ), EXCEL_BUSINESS_VERSION );
	wp_enqueue_style( 'jquery-sidr-light', get_template_directory_uri() . '/css/jquery.sidr.light.css', array(), '2.2.1' );
    wp_enqueue_style( 'jquery-mCustomScrollbar', get_template_directory_uri() . '/css/jquery.mCustomScrollbar.css', array(), '3.1.5' );
    wp_enqueue_style( 'excel-business-style', get_stylesheet_uri(), array(), EXCEL_BUSINESS_VERSION );

	wp_enqueue_script( 'owl-carousel', get_template_directory_uri() . '/js/owl.carousel.js', array( 'jquery' ), '2.2.1', true );
    wp_enqueue_script( 'jquery-sidr', get_template_directory_uri() . '/js/jquery.sidr.js', array( 'jquery' ), '2.2.1', true );
    wp_enqueue_script( 'jquery-mCustomScrollbar', get_template_directory_uri() . '/js/jquery.mCustomScrollbar.js', array( 'jquery' ), '3.1.5', true );
    wp_enqueue_script( 'excel-business-js', get_template_directory_uri() . '/js/custom.js', array( 'jquery' ), EXCEL_BUSINESS_VERSION, true );
    
    $auto      = get_theme_mod( 'slider_auto', true );
    $loop      = get_theme_mod( 'slider_loop', true );
    $control   = get_theme_mod( 'slider_control', true );
    $pager     = get_theme_mod( 'slider_pager', true );
    $animation = get_theme_mod( 'slider_animation' );
    
    $array = array(
		'auto'      => esc_attr( $auto ),
		'loop'      => esc_attr( $loop ),
        'control'   => esc_attr( $control ),
        'animation' => $animation,
        'pager'     => esc_attr( $pager ),
	);
    wp_localize_script( 'excel-business-js', 'excel_business_data', $array );
    
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'excel_business_scripts' );

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function excel_business_body_classes( $classes ) {
	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}
    
    if( is_page_template( 'full-width.php' ) || ! is_active_sidebar( 'sidebar' ) ){
        $classes[] = 'full-width';
    }

	return $classes;
}
add_filter( 'body_class', 'excel_business_body_classes' );

/**
 * Adds custom classes to the array of post classes.
 *
 * @param array $classes Classes for the post element.
 * @return array
 */
function excel_business_post_classes( $classes ) {
	
    if( is_search() || is_post_type_archive( 'ea_portfolio' ) ){
        $classes[] = 'post';
    }
        
	return $classes;
}
add_filter( 'post_class', 'excel_business_post_classes' );

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function excel_business_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'excel_business_pingback_header' );

/**
 * Flush out the transients used in excel_business_categorized_blog.
 */
function excel_business_category_transient_flusher() {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	// Like, beat it. Dig?
	delete_transient( 'excel_business_categories' );
}
add_action( 'edit_category', 'excel_business_category_transient_flusher' );
add_action( 'save_post',     'excel_business_category_transient_flusher' );

if( ! function_exists( 'excel_business_exclude_cat' ) ) :
/**
 * Exclude post with Category from blog and archive page. 
*/
function excel_business_exclude_cat( $query ){
    $cat = get_theme_mod( 'exclude_categories' );
    
    if( $cat && ! is_admin() && $query->is_main_query() ){
        $cat = array_diff( array_unique( $cat ), array('') );
        if( $query->is_home() || $query->is_archive() ) {
			$query->set( 'category__not_in', $cat );
		}
    }    
}
endif;
add_filter( 'pre_get_posts', 'excel_business_exclude_cat' );

if ( ! function_exists( 'excel_business_excerpt_more' ) ) :
/**
 * Replaces "[...]" (appended to automatically generated excerpts) with ... * 
 */
function excel_business_excerpt_more() {
	return ' &hellip; ';
}
endif;
add_filter( 'excerpt_more', 'excel_business_excerpt_more' );

if ( ! function_exists( 'excel_business_excerpt_length' ) ) :
/**
 * Changes the default 55 character in excerpt 
*/
function excel_business_excerpt_length( $length ) {
	return is_admin() ? $length : 25;    
}
endif;
add_filter( 'excerpt_length', 'excel_business_excerpt_length', 999 );

if( ! function_exists( 'excel_business_move_comment_field_to_bottom' ) ) :
/**
 * Hook to move comment text field to the bottom in WP 4.4
 * 
 * @link http://www.wpbeginner.com/wp-tutorials/how-to-move-comment-text-field-to-bottom-in-wordpress-4-4/  
 */
function excel_business_move_comment_field_to_bottom( $fields ) {
    $comment_field = $fields['comment'];
    unset( $fields['comment'] );
    $fields['comment'] = $comment_field;
    return $fields;
}
endif;
add_filter( 'comment_form_fields', 'excel_business_move_comment_field_to_bottom' );

if( ! function_exists( 'excel_business_change_comment_form_default_fields' ) ) :
/**
 * Change Comment form default fields i.e. author, email & url.
 * https://blog.josemcastaneda.com/2016/08/08/copy-paste-hurting-theme/
*/
function excel_business_change_comment_form_default_fields( $fields ){
    
    // get the current commenter if available
    $commenter = wp_get_current_commenter();
 
    // core functionality
    $req = get_option( 'require_name_email' );
    $aria_req = ( $req ? " aria-required='true'" : '' );    
 
    // Change just the author field
    $fields['author'] = '<p class="comment-form-author"><input id="author" name="author" placeholder="' . esc_attr__( 'Name*', 'excel-business' ) . '" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';
    
    $fields['email'] = '<p class="comment-form-email"><input id="email" name="email" placeholder="' . esc_attr__( 'Email*', 'excel-business' ) . '" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>';
    
    $fields['url'] = '<p class="comment-form-url"><input id="url" name="url" placeholder="' . esc_attr__( 'Website', 'excel-business' ) . '" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>'; 
    
    return $fields;
    
}
endif;
add_filter( 'comment_form_default_fields', 'excel_business_change_comment_form_default_fields' );

if( ! function_exists( 'excel_business_change_comment_form_defaults' ) ) :
/**
 * Change Comment Form defaults
 * https://blog.josemcastaneda.com/2016/08/08/copy-paste-hurting-theme/
*/
function excel_business_change_comment_form_defaults( $defaults ){
    
    // Change the "cancel" to "I would rather not comment" and use a span instead
    $defaults['comment_field'] = '<p class="comment-form-comment"><label for="comment"></label><textarea id="comment" name="comment" placeholder="' . esc_attr__( 'Comment', 'excel-business' ) . '" cols="45" rows="8" aria-required="true"></textarea></p>';
    
    return $defaults;
    
}
endif;
add_filter( 'comment_form_defaults', 'excel_business_change_comment_form_defaults' );