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

/**
 * define theme info
 * @since 1.0.0
 * */
 
 if (is_child_theme()){
	$theme = wp_get_theme();
	$parent_theme = $theme->Template;
	$theme_info = wp_get_theme($parent_theme);
}else{
	$theme_info = wp_get_theme();
}

define('ETHRIK_DEV_MODE',true);
$ethrik_version = ETHRIK_DEV_MODE ? time() : $theme_info->get('Version');
define('ETHRIK_NAME',$theme_info->get('Name'));
define('ETHRIK_VERSION',$ethrik_version);
define('ETHRIK_AUTHOR',$theme_info->get('Author'));
define('ETHRIK_AUTHOR_URI',$theme_info->get('AuthorURI'));


/**
 * Define Const for theme Dir
 * @since 1.0.0
 * */

define('ETHRIK_THEME_URI', get_template_directory_uri());
define('ETHRIK_IMG', ETHRIK_THEME_URI . '/assets/images');
define('ETHRIK_CSS', ETHRIK_THEME_URI . '/assets/css');
define('ETHRIK_JS', ETHRIK_THEME_URI . '/assets/js');
define('ETHRIK_THEME_DIR', get_template_directory());
define('ETHRIK_IMG_DIR', ETHRIK_THEME_DIR . '/assets/images');
define('ETHRIK_CSS_DIR', ETHRIK_THEME_DIR . '/assets/css');
define('ETHRIK_JS_DIR', ETHRIK_THEME_DIR . '/assets/js');
define('ETHRIK_INC', ETHRIK_THEME_DIR . '/inc');
define('ETHRIK_THEME_OPTIONS',ETHRIK_INC .'/theme-options');
define('ETHRIK_THEME_OPTIONS_IMG',ETHRIK_THEME_OPTIONS .'/img');

/**
 * 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 ethrik_setup(){
	
	// make the theme available for translation
	load_theme_textdomain( 'ethrik', get_template_directory() . '/languages' );
	
	// add support for post formats
    add_theme_support('post-formats', [
        'standard', 'image', 'video', 'audio','gallery', 'link', 'status'
    ]);

    // add support for automatic feed links
    add_theme_support('automatic-feed-links');

    // let WordPress manage the document title
    add_theme_support('title-tag');
	
	// add editor style theme support
	function ethrik_theme_add_editor_styles() {
		add_editor_style( 'custom-style.css' );
	}
	add_action( 'admin_init', 'ethrik_theme_add_editor_styles' );

    // add support for post thumbnails
    add_theme_support('post-thumbnails');
	
	// hard crop center center
    set_post_thumbnail_size(803, 490, ['center', 'center']);
	add_image_size( 'ethrik-box-slider-small', 96, 96, true );
	
	
	// register navigation menus
    register_nav_menus(
        [
            'primary' => esc_html__('Primary Menu', 'ethrik'),
            'footermenu' => esc_html__('Footer Menu', 'ethrik'),
            'mobile' => esc_html__('Mobile Menu', 'ethrik'),
        ]
    );
	
	
	// HTML5 markup support for search form, comment form, and comments
    add_theme_support('html5', array(
        'search-form', 'comment-form', 'comment-list', 'gallery', 'caption', 'style', 'script'
    ));
	
	
	/**
	 * Add support for core custom logo.
	 *
	 * @link https://codex.wordpress.org/Theme_Logo
	 */
	add_theme_support( 'custom-logo', array(
		'height'      => 150,
		'width'       => 300,
		'flex-width'  => true,
		'flex-height' => true,
	) );
	
	
	// Add theme support for selective refresh for widgets.
	add_theme_support( 'customize-selective-refresh-widgets' );
	
	
	/*
     * Enable support for wide alignment class for Gutenberg blocks.
     */
    add_theme_support( 'align-wide' );
    add_theme_support( 'editor-styles' );
    add_theme_support( 'wp-block-styles' );
		
}

add_action('after_setup_theme', 'ethrik_setup');

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
*/
 
function ethrik_widget_init() {
	
	register_sidebar( array (
		'name' => esc_html__('Blog widget area', 'ethrik'),
		'id' => 'sidebar-1',
		'description' => esc_html__('Blog Sidebar Widget.', 'ethrik'),
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h3 class="widget-title">',
		'after_title' => '</h3>',
		
	) );
	
	register_sidebar( array(
		'name'          => esc_html__( 'Footer Subscribe Area', 'ethrik' ),
		'id'            => 'footer-subscribe-area',
		'description'   => esc_html__( 'Add mailchimp shortcode here', 'ethrik' ),
		'before_widget' => '<div id="%1$s" class="footer-widget widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h4 class="widget-title">',
		'after_title'   => '</h4>',
	) );
					
}

add_action('widgets_init', 'ethrik_widget_init');

/**
 * Register custom fonts.
 */
if ( ! function_exists( 'ethrik_fonts_url' ) ) :
/**
 * Register Google fonts for Blessing.
 *
 * Create your own ethrik_fonts_url() function to override in a child theme.
 *
 * @since Blessing 1.0
 *
 * @return string Google fonts URL for the theme.
 */
function ethrik_fonts_url() {
	$fonts_url = '';
	$font_families     = array();
	$subsets   = 'latin,latin-ext';
	$mfont = ethrik_get_customize_option( 'body_font', [] );
	//$sfont = ethrik_get_option( 'second_font', [] );

	if ( empty( $sfont['font-family'] ) ) {
		$font_families[] = 'Manrope:200,200i,300,300i,400,400i,500,500i,600,600i,700,700i,800,800i';
	}

	//if ( empty( $mfont['font-family'] ) ) {
		//$font_families[] = 'Nunito Sans:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i';
	//}

	if ( $font_families ) {
		$fonts_url = add_query_arg( array(
			'family' => urlencode( implode( '|', $font_families ) ),
			'subset' => urlencode( $subsets ),
		), 'https://fonts.googleapis.com/css' );
	}
	return esc_url_raw( $fonts_url );
}
endif;
 
/**
 * Enqueue scripts and styles.
 */
function ethrik_scripts() {
	
	// Theme CSS 
	
	// Add custom fonts, used in the main stylesheet.
	wp_enqueue_style( 'etrhik-fonts', ethrik_fonts_url(), array(), null );

	wp_enqueue_style( 'themefont-awesome', ETHRIK_CSS . '/font-awesome.css');
	wp_enqueue_style( 'icon-font',  ETHRIK_CSS . '/icon-font.css' );
	wp_enqueue_style( 'remix-font',  ETHRIK_CSS . '/remixicon.css' );
	wp_enqueue_style( 'animate',  ETHRIK_CSS . '/animate.css' );
	wp_enqueue_style( 'magnific-popup',  ETHRIK_CSS . '/magnific-popup.css' );
	wp_enqueue_style( 'owl-carousel',  ETHRIK_CSS . '/owl.carousel.min.css' );
	wp_enqueue_style( 'owl-theme',  ETHRIK_CSS . '/owl.theme.min.css' );
	wp_enqueue_style( 'slick',  ETHRIK_CSS . '/slick.css' );
	wp_enqueue_style( 'slicknav',  ETHRIK_CSS . '/slicknav.css' );
	wp_enqueue_style( 'theme-fonts', ETHRIK_CSS . '/theme-fonts.css', array(), '1.0', 'all');
	wp_enqueue_style( 'ethrik-dark',  ETHRIK_CSS . '/dark-mode.css' );

	if (is_rtl()) {
		wp_enqueue_style( 'bootstrap', ETHRIK_CSS . '/bootstrap.min.css', array(), '4.0', 'all');
		wp_enqueue_style( 'ethrik-main',  ETHRIK_CSS . '/main.css' );
		wp_enqueue_style( 'ethrik-responsive',  ETHRIK_CSS . '/responsive.css' );
		wp_enqueue_style( 'ethrik-rtl',  ETHRIK_CSS . '/rtl.css' );
		
	} else {
		wp_enqueue_style( 'bootstrap', ETHRIK_CSS . '/bootstrap.min.css', array(), '4.0', 'all');
		wp_enqueue_style( 'ethrik-main',  ETHRIK_CSS . '/main.css' );
		wp_enqueue_style( 'ethrik-responsive',  ETHRIK_CSS . '/responsive.css' );
	}

	wp_enqueue_style( 'ethrik-style', get_stylesheet_uri() );
	
	// Theme JS
	
	wp_enqueue_script( 'bootstrap',  ETHRIK_JS . '/bootstrap.min.js', array( 'jquery' ),  '4.0', true );
	wp_enqueue_script( 'popper',  ETHRIK_JS . '/popper.min.js', array( 'jquery' ),  '1.0', true );
	wp_enqueue_script( 'jquery-magnific-popup',  ETHRIK_JS . '/jquery.magnific-popup.min.js', array( 'jquery' ),  '1.0', true );
	wp_enqueue_script( 'jquery-appear',  ETHRIK_JS . '/jquery.appear.min.js', array( 'jquery' ),  '1.0', true );
	wp_enqueue_script( 'owl-carousel',  ETHRIK_JS . '/owl.carousel.min.js', array( 'jquery' ),  '1.0', true );
	wp_enqueue_script( 'slick', ETHRIK_JS . '/slick.js', array( 'jquery' ), '1.0', true );
	wp_enqueue_script( 'jquery-slicknav', ETHRIK_JS . '/jquery.slicknav.min.js', array( 'jquery' ), '1.0', true );
	
	// Custom JS Scripts
	
	wp_enqueue_script( 'ethrik-scripts',  ETHRIK_JS . '/scripts.js', array( 'jquery' ),  '1.0', true );
	
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
	

}

add_action( 'wp_enqueue_scripts', 'ethrik_scripts' );


/*
* Include codester helper functions
* @since 1.0.0
*/

if ( file_exists( ETHRIK_INC.'/cs-framework-functions.php' ) ) {
	require_once  ETHRIK_INC.'/cs-framework-functions.php';
}

if ( file_exists( ETHRIK_INC.'/nft-items.php' ) ) {
	require_once  ETHRIK_INC.'/nft-items.php';
}

/**
 * Theme option panel & Metaboxes.
*/
 if ( file_exists( ETHRIK_THEME_OPTIONS.'/theme-options.php' ) ) {
	require_once  ETHRIK_THEME_OPTIONS.'/theme-options.php';
}

if ( file_exists( ETHRIK_THEME_OPTIONS.'/theme-metabox.php' ) ) {
	require_once  ETHRIK_THEME_OPTIONS.'/theme-metabox.php';
}

if ( file_exists( ETHRIK_THEME_OPTIONS.'/theme-customizer.php' ) ) {
	require_once  ETHRIK_THEME_OPTIONS.'/theme-customizer.php';
}


if ( file_exists( ETHRIK_THEME_OPTIONS.'/theme-inline-styles.php' ) ) {
	require_once  ETHRIK_THEME_OPTIONS.'/theme-inline-styles.php';
}


/**
 * Required plugin installer 
*/
require get_template_directory() . '/inc/required-plugins.php';


/**
 * Custom template tags & functions for this theme.
*/
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/template-functions.php';

/**
 * Load WooCommerce compatibility file.
 */
if ( class_exists( 'woocommerce' ) ) {
	require get_template_directory() . '/inc/woocommerce.php';
}


/**
 * 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 ethrik_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'ethrik_content_width', 640 );
}

add_action( 'after_setup_theme', 'ethrik_content_width', 0 );

/**
 * Nav menu fallback function
*/

function ethrik_fallback_menu() {
	get_template_part( 'template-parts/default', 'menu' );
}

function ethrik_enable_svg_upload( $upload_mimes ) {
    $upload_mimes['svg'] = 'image/svg+xml';
    $upload_mimes['svgz'] = 'image/svg+xml';
    return $upload_mimes;
}
add_filter( 'upload_mimes', 'ethrik_enable_svg_upload', 10, 1 );


//Get layout shop page.
if ( ! function_exists( 'ethrik_blog_col' ) ) :
	function ethrik_blog_col() {
		// Get layout.
		
		$page_layout = ethrik_get_option( 'ethrik_blog_col_layout' );
		return $page_layout;
	}
endif;


/**
 * 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
 */
if ( ! function_exists( 'ethrik_blog_layout_columns' ) ) :
	function ethrik_blog_layout_columns() {

		$blog_layout_width = array();

		// Check if layout is one column.
		if ( 'right-sidebar' === ethrik_blog_col() && is_active_sidebar( 'sidebar-1' ) ) {
			$blog_layout_width[] = 'col-lg-8 col-sm-12';
		}elseif ('left-sidebar' === ethrik_blog_col() && is_active_sidebar( 'sidebar-1' ) ) {
			$blog_layout_width[] = 'col-lg-8 col-sm-12 blog-custom-left-sidebar';
		} else {
			$blog_layout_width[] = 'col-lg-12 col-md-12 col-sm-12';
		}

		// return the $classes array
    	echo implode( ' ', $blog_layout_width );
	}
endif;