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

if ( ! defined( '_mohali_VERSION' ) ) {
	// Replace the version number of the theme on each release.
	define( '_mohali_VERSION', '1.0' );
}

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

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

		// Align wide support
		add_theme_support( 'align-wide' );

		/*
		 * 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', 'mohali' ),
				'menu-top' => esc_html__( 'Top', 'mohali' ),
				'mobile' => esc_html__( 'Mobile Menu', 'mohali' ),
				'social' => esc_html__( 'Social Networks', 'mohali' ),
			)
		);

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

		// Set up the WordPress core custom background feature.
		// Set up the WordPress core custom background feature.
		add_theme_support(
			'custom-background',
			apply_filters(
				'mohali_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 support for core custom logo.
		 *
		 * @link https://codex.wordpress.org/Theme_Logo
		 */
		add_theme_support(
			'custom-logo',
			array(
				'height'      => 250,
				'width'       => 250,
				'flex-width'  => true,
				'flex-height' => true,
			)
		);
		
		add_image_size( 'mohali-thumbnail-4x3', '600', '450', true );
		add_image_size( 'mohali-thumbnail-4x4', '600', '600', true );
		
	}
endif;
add_action( 'after_setup_theme', 'mohali_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 mohali_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'mohali_content_width', 768 );
}
add_action( 'after_setup_theme', 'mohali_content_width', 0 );

/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function mohali_widgets_init() {
	register_sidebar(
		array(
			'name'          => esc_html__( 'Sidebar', 'mohali' ),
			'id'            => 'sidebar-1',
			'description'   => esc_html__( 'Add widgets here.', 'mohali' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<h2 class="widget-title">',
			'after_title'   => '</h2>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Widgets Area 1', 'mohali' ),
			'id'            => 'footer-1',
			'description'   => esc_html__( 'Add widgets here.', 'mohali' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<div class="widget-title">',
			'after_title'   => '</div>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Widgets Area 2', 'mohali' ),
			'id'            => 'footer-2',
			'description'   => esc_html__( 'Add widgets here.', 'mohali' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<div class="widget-title">',
			'after_title'   => '</div>',
		)
	);
	register_sidebar(
		array(
			'name'          => esc_html__( 'Footer Widgets Area 3', 'mohali' ),
			'id'            => 'footer-3',
			'description'   => esc_html__( 'Add widgets here.', 'mohali' ),
			'before_widget' => '<section id="%1$s" class="widget %2$s">',
			'after_widget'  => '</section>',
			'before_title'  => '<div class="widget-title">',
			'after_title'   => '</div>',
		)
	);
}
add_action( 'widgets_init', 'mohali_widgets_init' );

/**
 * Enqueue scripts and styles.
 */
function mohali_scripts() {
	wp_enqueue_style( 'mohali-style', get_stylesheet_uri(), array(), _mohali_VERSION );
	wp_enqueue_style('bootstrap', get_template_directory_uri() . '/lib/bootstrap/bootstrap.min.css'); 
	wp_enqueue_style('fontawesome', get_template_directory_uri() . '/lib/font-awesome/css/all.min.css'); 
	wp_enqueue_style('sidr-dark-css', get_template_directory_uri() . '/lib/sidr/stylesheets/jquery.sidr.dark.min.css'); 
	wp_enqueue_style('mohali-primary-font', 'https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Poppins&display=swap" rel="stylesheet' );
	wp_enqueue_style('mohali-secondary-font', 'https://fonts.googleapis.com/css2?family=Bebas+Neue&family=Poppins&display=swap" rel="stylesheet' );
	wp_enqueue_style('mohali-core', get_template_directory_uri() . '/design-files/core/core.css'); 
	wp_enqueue_style('mohali-header', get_template_directory_uri() . '/design-files/header/'.esc_html(get_theme_mod('mohali-header-style','style2')).'/header.css'); 
	wp_enqueue_style('mohali-blog-style1', get_template_directory_uri() . '/design-files/blog-style/blog-style1.css'); 
	wp_enqueue_style('mohali-single', get_template_directory_uri() . '/design-files/single/single.css');
	wp_enqueue_style('mohali-sidebar', get_template_directory_uri() . '/design-files/sidebar/sidebar.css'); 
	wp_enqueue_style('mohali-footer', get_template_directory_uri() . '/design-files/footer/footer.css'); 
	wp_enqueue_style('mohali-featured-modules', get_template_directory_uri() . '/design-files/featured-modules/featured-modules.css'); 

	wp_enqueue_script( 'mohali-navigation', get_template_directory_uri() . '/js/navigation.js', array(), _mohali_VERSION, true );
	wp_enqueue_script( 'owl-carousel', get_template_directory_uri() . '/lib/owl-carousel/dist/owl.carousel.js', array('jquery'), '2.3.4', true );
	wp_enqueue_script( 'sidr', get_template_directory_uri() . '/lib/sidr/jquery.sidr.min.js', array('jquery'), '2.2.1', true );
	wp_enqueue_script( 'mohali-theme-js', get_template_directory_uri() . '/js/theme.js', array('sidr','owl-carousel'), _mohali_VERSION, true );


	if ( is_singular() && comments_open() && get_option( 'thread_comments' ) ) {
		wp_enqueue_script( 'comment-reply' );
	}
}
add_action( 'wp_enqueue_scripts', 'mohali_scripts' );

/**
 * Filter the excerpt length to 50 words.
 *
 * @param int $length Excerpt length.
 * @return int 
 */
function mohali_excerpt_length( $length ) {
		if (!is_admin() ) { 
			return 30; 
		} 
		return $length;

}
add_filter( 'excerpt_length', 'mohali_excerpt_length', 999 );

function mohali_excerpt_more( $more ) {
	if (!is_admin() ) {
		return '....'; 
	} 
	return $more;

}
add_filter( 'excerpt_more', 'mohali_excerpt_more' );

/**
 * Function to wrap the entire site in a container and bootstrap row.
 *
 */
function mohali_after_header_divs() {
    // Check if Elementor is active and the current page uses an Elementor template
    $is_elementor_template = defined('ELEMENTOR_PATH') && class_exists('Elementor\Plugin') && is_page_template('elementor_header_footer');

    // Output opening container div if not an Elementor template
    if (!$is_elementor_template) {
        echo '<div class="container">';
		echo '<div class="row">';
    }
}
add_action('mohali_after_header', 'mohali_after_header_divs'); 

function mohali_before_footer_divs() {
   
    // Check if Elementor is active and the current page uses an Elementor template
    $is_elementor_template = defined('ELEMENTOR_PATH') && class_exists('Elementor\Plugin') && is_page_template('elementor_header_footer');

    // Output closing container div if not an Elementor template
    if (!$is_elementor_template) {
		echo '</div><!-- .row -->';
        echo '</div><!-- .container -->';
    }
}
add_action('mohali_before_footer', 'mohali_before_footer_divs');



/**
 * Function to wrap the entire site in a container and bootstrap row.
 *
 */
function mohali_before_loop_divs() {
	$layout = "grid-".mohali_blog_layout_setting();
	?>
		<div class="row grid-row <?php echo esc_attr($layout); ?>"><?php
}
add_action('mohali_before_loop','mohali_before_loop_divs'); 

function mohali_after_loop_divs() {
	?></div><!--#.grid-row--><?php
}
add_action( 'mohali_after_loop', 'mohali_after_loop_divs' );

/**
 * Featured Modules
 */

function mohali_insert_featured_modules() {
	if (is_home()) :
		$i = 0;
		for ($i = 1; $i < 3; $i++) :
			mohali_display_featured_module( get_theme_mod( 'mohali-fa-style-'.$i, 'style1' ), get_theme_mod('mohali-fa-enable-'.$i, 0) ,get_theme_mod( 'mohali-fa-cat-'.$i, 0 ), get_theme_mod( 'mohali-fa-show-title-'.$i, true ));
		endfor;
	endif;	
}
add_action('mohali_after_header','mohali_insert_featured_modules'); 


/**
 * Function to alter the width of sidebar and content area based on diff pages.
 *
 */
function mohali_sidebar_setting() {
	if (is_search())	
		$setting = get_theme_mod('mohali-primary-width-search', 'no-sidebar-narrow-primary');
	if (is_single())
		$setting = get_theme_mod('mohali-primary-width-single-post', 'no-sidebar-narrow-primary');
	if (is_page())
		$setting = get_theme_mod('mohali-primary-width-page', 'no-sidebar-narrow-primary');
	if (is_archive())
		$setting = get_theme_mod('mohali-primary-width-archives', 'no-sidebar-narrow-primary');
	if (is_home())
		$setting = get_theme_mod('mohali-primary-width-home', 'right-sidebar');	 
	return $setting;
}
	
function mohali_primary_width() {
	$setting = mohali_sidebar_setting();
		
	switch ($setting) {
		case 'no-sidebar':
			$class = 'col-md-12';
			break;
		case 'right-sidebar':
			$class = 'col-md-8';
			break;
		case 'right-sidebar-narrow':
			$class = 'col-md-9';
			break;
		case 'no-sidebar-narrow-primary':
			$class = 'col-md-8 offset-md-2 offset-sm-0 offset-lg-2';
			break;	
		default:
			$class = 'col-md-8 offset-md-2 offset-sm-0 offset-lg-2';
			break;					
	}
	echo esc_html($class); 
}
add_action('mohali_primary_width_class','mohali_primary_width');

function mohali_secondary_width() {
	$setting = mohali_sidebar_setting();
	
	switch ($setting) {
		case 'no-sidebar':
			$class = 'd-none';
			break;
		case 'no-sidebar-narrow-primary':
			$class = 'd-none';
			break;	
		case 'right-sidebar':
			$class = 'col-md-4';
			break;
		case 'right-sidebar-narrow':
			$class = 'col-md-3';
			break;
		default:
			$class = 'd-none';
			break;					
	}
	echo esc_html($class); 
}
add_action('mohali_secondary_width_class','mohali_secondary_width');


/**
 * LessCSS PHP Color Darken/Lighten
 */
function mohali_darken_color($color_code,$percentage_adjuster = 0) {
	$percentage_adjuster = round($percentage_adjuster/100,2);
	if(preg_match("/#/",$color_code)) {
		$hex = str_replace("#","",$color_code);
		$r = (strlen($hex) == 3)? hexdec(substr($hex,0,1).substr($hex,0,1)):hexdec(substr($hex,0,2));
		$g = (strlen($hex) == 3)? hexdec(substr($hex,1,1).substr($hex,1,1)):hexdec(substr($hex,2,2));
		$b = (strlen($hex) == 3)? hexdec(substr($hex,2,1).substr($hex,2,1)):hexdec(substr($hex,4,2));
		$r = round($r - ($r*$percentage_adjuster));
		$g = round($g - ($g*$percentage_adjuster));
		$b = round($b - ($b*$percentage_adjuster));
 
		return "#".str_pad(dechex( max(0,min(255,$r)) ),2,"0",STR_PAD_LEFT)
			.str_pad(dechex( max(0,min(255,$g)) ),2,"0",STR_PAD_LEFT)
			.str_pad(dechex( max(0,min(255,$b)) ),2,"0",STR_PAD_LEFT);
	}
}

/**
 * Implement the Custom Colors feature.
 */
function mohali_colors_override(){ ?>
 <style>
 	:root {
		 --mohali-primary: <?php echo esc_html(get_theme_mod('mohali-primary-color','#ff005b')); ?>;
		 --mohali-primary-text: <?php echo esc_html(get_theme_mod('mohali-primary-text-color','#f9ffe7')); ?>;
		 --mohali-background-main: <?php echo esc_html(get_theme_mod('background_color','#ffffff')); ?>;
		 --mohali-background-plain: <?php echo esc_html(get_theme_mod('mohali-bg-plain-color','#fff')); ?>;

		 --mohali-background-darker: <?php echo esc_html(get_theme_mod('mohali-background-darker-color','#eeeeee')); ?>;
		 
		 --mohali-secondary: <?php echo esc_html(get_theme_mod('mohali-secondary-color','#747474')); ?>;
		 --mohali-secondary-text: <?php echo esc_html(get_theme_mod('mohali-secondary-text-color','#FFFFFF')); ?>;
		 --mohali-secondary-dark: <?php echo esc_html(get_theme_mod('mohali-secondary-dark-color','#076402')); ?>;
		 
		 --mohali-text-dark: <?php echo esc_html(get_theme_mod('mohali-text-dark-color','#111')); ?>;
		 --mohali-text: <?php echo esc_html(get_theme_mod('mohali-text-color','#555')); ?>;
		 --mohali-text-light: <?php echo esc_html(get_theme_mod('mohali-text-light-color','#777')); ?>;
		 --mohali-menu-text: <?php echo esc_html(get_theme_mod('mohali-menu-text-color','#232323')); ?>;
		 
		 --mohali-header-background: <?php echo esc_html(get_theme_mod('mohali-header-bg-color','#fff')); ?>;
		 --mohali-header-text: <?php echo esc_html(get_theme_mod('mohali-header-content-color','#FFFFFF')); ?>;
		 --mohali-header-lighter: <?php echo esc_html(get_theme_mod('mohali-header-bg-lighter-color','#222222')); ?>;
		 --mohali-top-bar-text: <?php echo esc_html(get_theme_mod('mohali-top-bar-text-color','#FFFDEC')); ?>;
		 --mohali-top-bar-background: <?php echo esc_html(get_theme_mod('mohali-top-bar-bg-color','#3a3a3a')); ?>;
		 
		 --mohali-mobile-header-background: <?php echo esc_html(get_theme_mod('mohali-header-mobile-bg-color','#FFFFFF')); ?>;
		 --mohali-mobile-header-text: <?php echo esc_html(get_theme_mod('mohali-header-mobile-text-color','#222222')); ?>;
	 }
	 
 </style>
<?php 
}
add_action( 'wp_head', 'mohali_colors_override' );

/**
 * Implement Some Additional CSS based on theme mods.
 */
function mohali_custom_css() {
	
	if (get_theme_mod('mohali-enable-top-bar')) {
		?><style>
			#masthead.style1 #top-bar, #masthead.style2 #top-bar {
				background: <?php echo esc_attr(get_theme_mod('mohali-top-bar-bg-color','#000000')); ?>;
			}
		</style><?php
	}
}
add_action( 'wp_head', 'mohali_custom_css' );

/**
 * Implement Logo Max Height Limiter
 */
function mohali_logo_max_height(){ ?>
 <style>
	 #masthead #site-branding .custom-logo {
		 max-height: <?php echo esc_attr(get_theme_mod('mohali-logo-max-height', 90))."px"; ?> !important;
	 }
 </style>
<?php 
}
add_action( 'wp_head', 'mohali_logo_max_height' );


/*
* Get Blog Posts Layout for Archive, Home & Tax Pages
*
*/ 
function mohali_blog_layout_setting() {
	if (is_search())	
		$setting = get_theme_mod('mohali-search-results-layout', 'style1');
	if (is_archive())
		$setting = get_theme_mod('mohali-archives-layout', 'style1');
	if (is_home())
		$setting = get_theme_mod('mohali-home-layout', 'style2');	
	
	return $setting;
}

function mohali_blog_layout_display() {
	$layout = mohali_blog_layout_setting();
	
	get_template_part( 'template-parts/blog-style/content', $layout );
}
add_action('mohali_blog_layout','mohali_blog_layout_display');

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Implement the Featured Modules
 */
require get_template_directory() . '/inc/display-featured-modules.php';

/**
 * Implement the Multiple Masthead Styles
 */
require get_template_directory() . '/inc/display-masthead.php';


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


/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer/customizer.php';



/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
	require get_template_directory() . '/inc/jetpack.php';
}


