<?php
/**
 * Excel Blog functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Excel_Blog
 */

if ( ! function_exists( 'excel_blog_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_blog_setup() {
	/*
	 * Make theme available for translation.
	 * Translations can be filed in the /languages/ directory.
	 * If you're building a theme based on Excel Blog, use a find and replace
	 * to change 'excel-blog' to the name of your theme in all the template files.
	 */
	load_theme_textdomain( 'excel-blog', 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(
		'menu-1' => esc_html__( 'Primary', 'excel-blog' ),
	) );

	/*
	 * 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',
	) );

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

	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );
    
    /** Custom Image Sizes */
    add_image_size( 'excel-blog-cat', 263, 208, true);
    add_image_size( 'excel-blog-featured', 1170, 500, true);
    add_image_size( 'excel-blog-top-slider', 1920, 736, true);
    add_image_size( 'excel-blog-bottom-slider', 360, 220, true);
    
    /** Custom Logo */
    add_theme_support( 'custom-logo', array(    	
    	'header-text' => array( 'site-title', 'site-description' ),
    ) );
    
}
endif;
add_action( 'after_setup_theme', 'excel_blog_setup' );

if( ! function_exists( 'excel_blog_content_width' ) ) :
/**
 * 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_blog_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'excel_blog_content_width', 750 );
}
endif;
add_action( 'after_setup_theme', 'excel_blog_content_width', 0 );

if( ! function_exists( 'excel_blog_template_redirect_content_width' ) ) :
/**
* Adjust content_width value according to template.
*
* @return void
*/
function excel_blog_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;
	}
}
endif;
add_action( 'template_redirect', 'excel_blog_template_redirect_content_width' );

if( ! function_exists( 'excel_blog_scripts' ) ) :
/**
 * Enqueue scripts and styles.
 */
function excel_blog_scripts() {
	$query_args = array(
		'family' => 'Open+Sans:400,400i,700,700i|Playfair+Display|Roboto:400,500,700',
		);
    wp_enqueue_style( 'excel-blog-google-fonts', add_query_arg( $query_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( 'animate', get_template_directory_uri(). '/css/animate.css', array(), '3.5.2' );
    wp_enqueue_style( 'meanmenu', get_template_directory_uri(). '/css/meanmenu.css', array(), '2.0.7' );
    wp_enqueue_style( 'excel-blog-style', get_stylesheet_uri(), array(), EXCEL_BLOG_THEME_VERSION );
    
    wp_enqueue_script( 'owl-carousel', get_template_directory_uri() . '/js/owl.carousel.js', array( 'jquery' ), '2.2.1', true );
    wp_enqueue_script( 'jquery-meanmenu', get_template_directory_uri() . '/js/jquery.meanmenu.js', array('jquery'), '2.0.8', true );
    wp_enqueue_script( 'excel-blog-custom', get_template_directory_uri() . '/js/custom.js', array('jquery'), EXCEL_BLOG_THEME_VERSION, true );
    
    $banner_slider_auto    = get_theme_mod( 'banner_slider_auto' );
    $banner_slider_loop    = get_theme_mod( 'banner_slider_loop', '1' );
    $banner_slider_control = get_theme_mod( 'banner_slider_control', '1' );
    $banner_slider_animate = get_theme_mod( 'banner_slider_animation', 'slide' );
    $banner_slider_speed   = get_theme_mod( 'banner_slider_speed', 700 );
    $banner_slider_pause   = get_theme_mod( 'banner_slider_pause', 6000 );
    $bottom_slider_auto    = get_theme_mod( 'bottom_slider_auto', '1' );
    $bottom_slider_loop    = get_theme_mod( 'bottom_slider_loop', '1' );
    $bottom_slider_control = get_theme_mod( 'bottom_slider_control', '1' );
    $bottom_slider_speed   = get_theme_mod( 'bottom_slider_speed', 700 );
    $bottom_slider_pause   = get_theme_mod( 'bottom_slider_pause', 6000 );
    
    $array = array(
		'banner_auto'      => esc_attr( $banner_slider_auto ),
		'banner_loop'      => esc_attr( $banner_slider_loop ),
        'banner_control'   => esc_attr( $banner_slider_control ),
        'banner_animation' => esc_attr( $banner_slider_animate ),
        'banner_speed'     => absint( $banner_slider_speed ),
        'banner_pause'     => absint( $banner_slider_pause ),
        'bottom_auto'      => esc_attr( $bottom_slider_auto ),
		'bottom_loop'      => esc_attr( $bottom_slider_loop ),
        'bottom_control'   => esc_attr( $bottom_slider_control ),
        'bottom_speed'     => absint( $bottom_slider_speed ),
        'bottom_pause'     => absint( $bottom_slider_pause ),                
	);
    
    wp_localize_script( 'excel-blog-custom', 'excel_blog_data', $array );
    
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
endif;
add_action( 'wp_enqueue_scripts', 'excel_blog_scripts' );

if( ! function_exists( 'excel_blog_category_transient_flusher' ) ) :
/**
 * Flush out the transients used in excel_blog_categorized_blog.
 */
function excel_blog_category_transient_flusher() {
	if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
		return;
	}
	// Like, beat it. Dig?
	delete_transient( 'excel_blog_categories' );
}
endif;
add_action( 'edit_category', 'excel_blog_category_transient_flusher' );
add_action( 'save_post',     'excel_blog_category_transient_flusher' );

if( ! function_exists( 'excel_blog_body_classes' ) ) :
/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function excel_blog_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';
	}
    
    // Adds a class of custom-background-image to sites with a custom background image.
	if ( get_background_image() ) {
		$classes[] = 'custom-background-image';
	}
    
    // Adds a class of custom-background-color to sites with a custom background color.
    if ( get_background_color() != 'ffffff' ) {
		$classes[] = 'custom-background-color';
	}
    
    if( is_page_template( 'full-width.php' ) || ! is_active_sidebar( 'sidebar' ) ){
        $classes[] = 'full-width'; 
    }
    
	return $classes;
}
endif;
add_filter( 'body_class', 'excel_blog_body_classes' );

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

if( ! function_exists( 'excel_blog_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_blog_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_blog_move_comment_field_to_bottom' );

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

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

if( ! function_exists( 'excel_blog_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_blog_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-blog' ) . '" 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-blog' ) . '" 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-blog' ) . '" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" /></p>'; 
    
    return $fields;
    
}
endif;
add_filter( 'comment_form_default_fields', 'excel_blog_change_comment_form_default_fields' );

if( ! function_exists( 'excel_blog_change_comment_form_defaults' ) ) :
/**
 * Change Comment Form defaults
 * https://blog.josemcastaneda.com/2016/08/08/copy-paste-hurting-theme/
*/
function excel_blog_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-blog' ) . '" cols="45" rows="8" aria-required="true"></textarea></p>';
    
    return $defaults;
    
}
endif;
add_filter( 'comment_form_defaults', 'excel_blog_change_comment_form_defaults' );

if( ! function_exists( 'excel_blog_exclude_posts' ) ) :
/**
 * Function to exclude sticky post from main query
*/
function excel_blog_exclude_posts( $query ){
    
    $stickies         = get_option( 'sticky_posts' ); //get all sticky posts
    $featured_cat     = get_theme_mod( 'bottom_slider_cat' ); //from customizer
    $ed_banner_slider = get_theme_mod( 'ed_banner_slider', 'disable' ); 
    $ed_bottom_slider = get_theme_mod( 'ed_bottom_slider', 'disable' );
    
    if ( ! is_admin() && $query->is_home() && $query->is_main_query() ) {
        
        if( $stickies && ( $ed_banner_slider !== 'disable' ) ){
            $query->set( 'post__not_in', $stickies );
            $query->set( 'ignore_sticky_posts', true );
        }
        
        if( $featured_cat && ( $ed_bottom_slider !== 'disable' )){
            $query->set( 'category__not_in', array( $featured_cat ) );
        }
    }    
}
endif;
add_filter( 'pre_get_posts', 'excel_blog_exclude_posts' );