<?php 
if ( ! function_exists( 'ismoderna_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 ismoderna_setup() {
		/*
	     * Make theme available for translation.
	     * Translations can be filed in the /languages/ directory.
	     * If you're building a theme based on Theme Palace, use a find and replace
	     * to change 'enigma' to the name of your theme in all the template files.
	     */
	    load_theme_textdomain( 'ismoderna' );

		// Add default posts and comments RSS feed links to head.
		add_theme_support( 'automatic-feed-links' );
        
		// Add Woocommerce Plugin Support In ISModerna Theme...
		add_theme_support('woocommerce');

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

		/*
		 * Switch default core markup for search form, comment form, and comments
		 * to output valid HTML5.
		*/
		$args = array(
				'search-form',
				'comment-form',
				'comment-list',
				'gallery',
				'caption',
				'style',
				'script',
			);

		add_theme_support( 'html5', $args );

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

		add_theme_support( 'wp-block-styles' );
		add_theme_support( 'align-wide' ); 
		add_theme_support('responsive-embeds');

		// Add theme support for selective refresh for widgets.
		add_theme_support( 'customize-selective-refresh-widgets' );

	    /* Add editor style. */
	    add_theme_support('editor-styles');
	    add_theme_support('dark-editor-style');

	     /*
	     * This theme styles the visual editor to resemble the theme style,
	     * specifically font, colors, icons, and column width.
	    */
	    add_editor_style('/assets/css/editor-style.css');
		 
		// add excerpt support for pages
		add_post_type_support( 'page', 'excerpt' );

		/**
		 * Add support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		*/
		$args = array(
				'height'      => 250,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			);
		add_theme_support( 'custom-logo', $args);
	}
endif;
add_action( 'after_setup_theme', 'ismoderna_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 ismoderna_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'ismoderna_content_width', 640 );
}
add_action( 'after_setup_theme', 'ismoderna_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
*/
function ismoderna_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'ismoderna' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'ismoderna' ),
			'before_widget' => '<div id="%1$s" class="sidebar-Categories %2$s">',
			'after_widget'  => '</div>',
			'before_title'  => '<h5 class="widgets-title">',
			'after_title'   => '</h5>',
		)
	);

	// Footer Sidebar
	register_sidebar(array(
		'name' => esc_html__( 'Footer Widget Area 1', 'ismoderna'),
		'id' => 'footer-widget-area-1',
		'description' => esc_html__( 'The footer widget area 1', 'ismoderna'),
		'before_widget' => '<div id="%1$s" class="widgets-col %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="footer-title">',
		'after_title' => '</h4>',
	));	
	
	register_sidebar(array(
		'name' => esc_html__( 'Footer Widget Area 2', 'ismoderna'),
		'id' => 'footer-widget-area-2',
		'description' => esc_html__( 'The footer widget area 2', 'ismoderna'),
		'before_widget' => '<div id="%1$s" class="contact-us %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="footer-title">',
		'after_title' => '</h4>',
	));	
	
	register_sidebar(array(
		'name' => esc_html__( 'Footer Widget Area 3', 'ismoderna'),
		'id' => 'footer-widget-area-3',
		'description' => esc_html__( 'The footer widget area 3', 'ismoderna'),
		'before_widget' => '<div id="%1$s" class="contact-us %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="footer-title">',
		'after_title' => '</h4>',
	));	
	
	register_sidebar(array(
		'name' => esc_html__( 'Footer Widget Area 4', 'ismoderna'),
		'id' => 'footer-widget-area-4',
		'description' => esc_html__( 'The footer widget area 4', 'ismoderna'),
		'before_widget' => '<div id="%1$s" class="contact-us %2$s">',
		'after_widget' => '</div>',
		'before_title' => '<h4 class="footer-title">',
		'after_title' => '</h4>',
	));	
}
add_action( 'widgets_init', 'ismoderna_widgets_init' );

/**
 * Enqueue scripts and styles.
*/
function ismoderna_scripts() {
	
	
	wp_enqueue_style('bootstrap', get_template_directory_uri()   . '/assets/css/bootstrap.css');
	wp_enqueue_style('animations', get_template_directory_uri()  . '/assets/css/animate.css');
	wp_enqueue_style('fontawesome6.4.0', get_template_directory_uri(). '/assets/fontawesome6.6.0/css/all.css');
	wp_enqueue_style( 'slicknav',   get_template_directory_uri() . '/assets/css/slicknav.css' );
	wp_enqueue_style( 'slick',   get_template_directory_uri() . '/assets/css/slick.css' );
	wp_enqueue_style( 'slicktheme',   get_template_directory_uri() . '/assets/css/slick-theme.css' );
	wp_enqueue_style( 'lity',   get_template_directory_uri() . '/assets/css/lity.css' );
	wp_enqueue_style('ismoderna-style-1', get_template_directory_uri() . '/assets/css/custom-style.css');
	wp_enqueue_style('ismoderna-reponsive', get_template_directory_uri() . '/assets/css/responsive.css');
	wp_enqueue_style('ismoderna-WooCommerce.css', get_template_directory_uri() . '/assets/css/WooCommerce.css');
	wp_enqueue_style( 'ismoderna-style', get_stylesheet_uri() );
	/* Web Fonts */		
    wp_enqueue_style('ismoderna-google-fonts',esc_url(ismoderna_fonts_url()));
	$font_var          = '300,400,600,700,900,300italic,400italic,600italic,700italic,900italic';
        $http              = ( ! empty( $_SERVER['HTTPS'] ) ) ? "https" : "http";
        $main_heading_font = str_replace( ' ' , '+', get_theme_mod( 'main_heading_font', 'Open Sans' ) );
        $menu_font         = str_replace( ' ' , '+', get_theme_mod( 'menu_font', 'Open Sans' ) );
        $theme_title       = str_replace( ' ' , '+', get_theme_mod( 'theme_title', 'Open Sans' ) );
        $desc_font_all     = str_replace( ' ' , '+', get_theme_mod( 'desc_font_all', 'Open Sans' ) );

        wp_enqueue_style('main_heading_font', $http . '://fonts.googleapis.com/css?family=' . $main_heading_font . ':' . $font_var);	
        wp_enqueue_style('menu_font', $http . '://fonts.googleapis.com/css?family=' . $menu_font . ':' . $font_var);	
        wp_enqueue_style('theme_title', $http . '://fonts.googleapis.com/css?family=' . $theme_title . ':' . $font_var);
        wp_enqueue_style('desc_font_all', $http . '://fonts.googleapis.com/css?family=' . $desc_font_all . ':' . $font_var);
    
	wp_enqueue_script( 'ismoderna-navigation', get_template_directory_uri() . '/assets/js/navigation.js', array(), '', true );
	wp_enqueue_script('bootstrap', get_template_directory_uri()  . '/assets/js/bootstrap.js', array( 'jquery' ), true, true);
	
	wp_enqueue_script( 'jquery-slicknav',  get_template_directory_uri() . '/assets/js/jquery.slicknav.js', array( 'jquery' ), true, true); 
    wp_enqueue_script( 'slick-js', 	       get_template_directory_uri() . '/assets/js/slick.js', array( 'jquery' ), true, true);
    wp_enqueue_script( 'lity', 	       get_template_directory_uri() . '/assets/js/lity.js', array( 'jquery' ), true, true);
    wp_enqueue_script('imagesloaded', get_template_directory_uri() . '/assets/js/imagesloaded.js', array( 'jquery' ), true, true);
    
    wp_enqueue_script( 'wow', 	       get_template_directory_uri() . '/assets/js/wow.js', array( 'jquery' ), true, true);
	wp_enqueue_script('ismoderna-custom-script', get_template_directory_uri() . '/assets/js/main.js', array( 'jquery' ), true, true);
	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'ismoderna_scripts' );


/* Add Typography Settings code start */	
add_action('wp_enqueue_scripts', 'ismoderna_custom_css');
function ismoderna_custom_css()
{
    $output = '';
	
	$output .= ' .site-title a, .site-description {
		font-family: ' . get_theme_mod('main_heading_font', 'Open Sans') . '!important;
		}
		 .navbar-light .navbar-nav .nav-link, .dropdown-item{
			font-family: ' . get_theme_mod('menu_font', 'Open Sans') . '!important ;
		}
		h1, h2, h3, h4, h5, h6{
			font-family: ' . get_theme_mod('theme_title', 'Open Sans') . ';
		}
       
		
		 html, body, p, table, caption, tbody, tfoot, thead, tr, th, td, input, button, select, textarea, span, a, ul.detail-meta-tag li{
			font-family: ' . get_theme_mod('desc_font_all', 'Open Sans') . ';
		}';

    
    $output     .='
    .logo img{
        height:'.esc_attr(get_theme_mod('logo_height','100')).'px;
        width:'.esc_attr(get_theme_mod('logo_width','100')).'px;
    }';
    //custom css
    $custom_css = get_theme_mod('custom_css') ; 
    if (!empty ($custom_css)) {
        $output .= get_theme_mod('custom_css') . "\n";
    }

    wp_register_style('custom-header-style', false);
    wp_enqueue_style('custom-header-style', get_template_directory_uri() . '/css/custom-header-style.css');
    wp_add_inline_style('custom-header-style', $output);
}
/* Add Typography Settings code end */

if ( ! function_exists( 'ismoderna_fonts_url' ) ) :
    /**
     * Register Google fonts.
     *
     * Create your own ismoderna_fonts_url() function to override in a child theme.
     *
     * @since league 1.0
     *
     * @return string Google fonts URL for the theme.
     */
    function ismoderna_fonts_url()
    {
        $fonts_url = '';
        $fonts     = array();
        $subsets   = 'latin,latin-ext';

        if ( 'off' !== _x( 'on', 'Poppins font: on or off', 'ismoderna' ) )
        {
            $fonts[] = 'Poppins:wght@100;200;300;400;500;600;700;800;900';
        }
        

        if ( $fonts ) {
            $fonts_url = add_query_arg( array(
                'family' => urlencode( implode( '|', $fonts ) ),
                'subset' => urlencode( $subsets ),
            ), 'https://fonts.googleapis.com/css' );
        }
        return esc_url_raw( $fonts_url );
    }
endif;