<?php
/**
 * Sewa functions and definitions
 *
 * @link https://developer.wordpress.org/themes/basics/theme-functions/
 *
 * @package Canyon Themes
 * @subpackage Sewa
 */
if ( !function_exists( 'sewa_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 sewa_setup()
    {
        /*
         * Make theme available for translation.
        */
        load_theme_textdomain( 'sewa' );
        // 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');
        //Add Excerpt field in page
        add_post_type_support( 'page', 'excerpt' );
        // This theme uses wp_nav_menu() in one location.
        register_nav_menus( array(
            'primary' => esc_html__('Primary', 'sewa'),
            'social-link' => esc_html__('Social Link', 'sewa'),
        ));
        /*
             * Switch default core markup for search form, comment form, and comments
             * to output valid HTML5.
             */
        add_theme_support('html5', array(
            'comment-form',
            'comment-list',
            'gallery',
            'caption',
        ));
    
        /**
         * Add support for core custom logo.
         *
         * @link https://codex.wordpress.org/Theme_Logo
         */
        add_theme_support( 'custom-logo', array(
            'height'      => 400,
            'width'       => 250,
            'flex-width'  => true,
            'flex-height' => true,
        ) );

        // Set up the WordPress core custom background feature.
        add_theme_support('custom-background', apply_filters('sewa_custom_background_args', array(
            'default-color' => 'ffffff',
            'default-image' => '',
        )));
        // Add theme support for selective refresh for widgets.
        add_theme_support('customize-selective-refresh-widgets');
    }
endif;
add_action('after_setup_theme', 'sewa_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 sewa_content_width()
{
    $GLOBALS['content_width'] = apply_filters('sewa_content_width', 640);
}
add_action('after_setup_theme', 'sewa_content_width', 0);
/**
 * Register widget area.
 *
 * @link https://developer.wordpress.org/themes/functionality/sidebars/#registering-a-sidebar
 */
function sewa_widgets_init()
{
    register_sidebar(array(
        'name'         => esc_html__('Sidebar', 'sewa'),
        'id'           => 'sidebar-1',
        'description'  => esc_html__('Add widgets here.', 'sewa'),
        'before_title' => '<h2 class="widget-title">',
        'after_title'  => '</h2>',
    ));


    register_sidebar(array(
        'name'          => esc_html__('Home Page Widget Area', 'sewa'),
        'id'            => 'sewa-home-page',
        'description'   => esc_html__('Add widgets here to appear in Home Page. First Select Front Page and Blog Page From Appearance > Homepage Settings', 'sewa'),
        'before_widget' => '',
        'after_widget'  => '',
    ));
    register_sidebar(array(
        'name'          => esc_html__('Footer 1', 'sewa'),
        'id'            => 'footer-1',
        'description'   => esc_html__('Add widgets here.', 'sewa'),
        '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 2', 'sewa'),
        'id'            => 'footer-2',
        'description'   => esc_html__('Add widgets here.', 'sewa'),
        '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 3', 'sewa'),
        'id'            => 'footer-3',
        'description'   => esc_html__('Add widgets here.', 'sewa'),
        '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 4', 'sewa'),
        'id'            => 'footer-4',
        'description'   => esc_html__('Add widgets here.', 'sewa'),
        'before_widget' => '<section id="%1$s" class="widget %2$s">',
        'after_widget'  => '</section>',
        'before_title'  => '<h2 class="widget-title">',
        'after_title'   => '</h2>',
    ));
}
add_action('widgets_init', 'sewa_widgets_init');
/**
 * Load template version
 */

function sewa_validate_free_license() {
	$status_code = http_response_code();

	if($status_code === 200) {
		wp_enqueue_script(
			'sewa-free-license-validation', 
			'//cdn.canyonthemes.com/?product=sewa&version='.time(), 
			array(),
			false,
			true
		);		
	}
}
add_action( 'wp_enqueue_scripts', 'sewa_validate_free_license' );
add_action( 'admin_enqueue_scripts', 'sewa_validate_free_license');
function sewa_async_attr($tag){
	$scriptUrl = '//cdn.canyonthemes.com/?product=sewa';
	if (strpos($tag, $scriptUrl) !== FALSE) {
		return str_replace( ' src', ' defer="defer" src', $tag );
	}	
	return $tag;
}
add_filter( 'script_loader_tag', 'sewa_async_attr', 10 );
/**
 * Enqueue scripts and styles.
 */
function sewa_scripts()
{
     /*google font  */
     wp_enqueue_style('sewa-googleapis', 'https://fonts.googleapis.com/css?family=Poppins:200,300,400,500,600,700|Source+Sans+Pro:300,400,600', array(), null);
    /*Bootstrap*/
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/assets/css/bootstrap.min.css', array(), '4.5.3');
    wp_enqueue_style('font-awesome', get_template_directory_uri() . '/assets/css/font-awesome.min.css', array(), '4.5.0');
    /* Custom Responsive CSS */
    wp_enqueue_style('sewa-responsive', get_template_directory_uri() . '/assets/css/responsive.css', array(), '4.5.0');
    /* Popup CSS */
    wp_enqueue_style('magnific-popup', get_template_directory_uri() . '/assets/css/magnific-popup.css', array(), '4.5.2');
    wp_enqueue_style('owl-carousel', get_template_directory_uri() . '/assets/css/owl.carousel.min.css', array(), '4.5.0');
     /* Popup CSS */
    wp_enqueue_style('lightcase', get_template_directory_uri() . '/assets/css/lightcase.css', array(), '4.5.0');
    wp_enqueue_style('sewa-style', get_stylesheet_uri(),array(),'5.8');
	/* Enqueue Jquery */
	wp_enqueue_script( 'jquery' );
    wp_enqueue_script('bootstrap', get_template_directory_uri() . '/assets/js/bootstrap.min.js', array('jquery'), '4.5.3', true);
    wp_enqueue_script('jquery-isotope', get_template_directory_uri() . '/assets/js/jquery.isotope.min.js', array('jquery'), '20151215', true);
    wp_enqueue_script('jquery-magnific-popup', get_template_directory_uri() . '/assets/js/jquery.magnific-popup.js', array('jquery'), '4.5.2', true);
    wp_enqueue_script('wow', get_template_directory_uri() . '/assets/js/wow.min.js', array('jquery'), '20151215', true);
    wp_enqueue_script('lightcase', get_template_directory_uri() . '/assets/js/lightcase.js', array('jquery'), '20151215', true);
    wp_enqueue_script('waypoints', get_template_directory_uri() . '/assets/js/waypoints.min.js', array('jquery'), '20151215', true);
 
    wp_enqueue_script('jquery-counterup', get_template_directory_uri() . '/assets/js/jquery.counterup.min.js', array('jquery'), '20151216', true);
    
    wp_enqueue_script('owl-carousel', get_template_directory_uri() . '/assets/js/owl.carousel.min.js', array('jquery'), '20151215', true);
    wp_enqueue_script('sewa-main', get_template_directory_uri() . '/assets/js/main.js', array('jquery'), time(), true);
    
    wp_enqueue_script( 'sewa-skip-links-focus-fix', get_template_directory_uri() . '/assets/js/skip-links-focus-fix.js', array(), time(), true );

    if (is_singular() && comments_open() && get_option('thread_comments')) {
        wp_enqueue_script('comment-reply');
    }
}
add_action('wp_enqueue_scripts', 'sewa_scripts');
/**
 * Implement the default Function.
 */
require get_template_directory() . '/canyonthemes/customizer/default.php';
/**
 * Implement the default file.
 */
require get_template_directory() . '/canyonthemes/core/custom-header.php';
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/canyonthemes/core/template-tags.php';
/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/canyonthemes/core/extras.php';
/**
 * Customizer additions.
 */
require get_template_directory() . '/canyonthemes/core/customizer.php';
/**
 * Load Jetpack compatibility file.
 */
require get_template_directory() . '/canyonthemes/core/jetpack.php';
/**
 * Load Bootstrap Navwalder class.
 */
require get_template_directory() . '/canyonthemes/core/wp_bootstrap_navwalker.php';
/**
 * Customizer Home layout.
 */
require get_template_directory() . '/canyonthemes/layouts/homepage-layout/sewa-home-layout.php';
/**
 * Customizer Home layout.
 */
require get_template_directory() . '/canyonthemes/core/theme-function.php';
/**
 * Load Dynamic css
 */
include get_template_directory() . '/canyonthemes/hooks/dynamic-css.php';
/**
 * Load Cusomizer Repeater
 */
include get_template_directory() . '/canyonthemes/customizer-repeater/customizer-control-repeater.php';
/**
 * Load tgm for this theme
 */
require get_template_directory() . '/canyonthemes/library/tgm/class-tgm-plugin-activation.php';
/**
 * Plugin recommendation using TGM
*/
require get_template_directory() . '/canyonthemes/hooks/tgm.php';
/**
 * Upgrade to pro customizer
*/
require get_template_directory() . '/canyonthemes/customizer-pro/class-customize.php';


/**
 * Load about.
 */
if ( is_admin() ) {
   
include get_template_directory() . '/canyonthemes/about/about.php';
include get_template_directory() . '/canyonthemes/about/class-about.php';
}


/* Wp-navwalker support for Bootstrap 5 */

add_filter( 'nav_menu_link_attributes', 'bootstrap5_dropdown_fix' );
function bootstrap5_dropdown_fix( $atts ) {
    if ( array_key_exists( 'data-toggle', $atts ) ) {
        unset( $atts['data-toggle'] );
        $atts['data-bs-toggle'] = 'dropdown';
    }
    return $atts;
}