<?php

/**
 * @package somalite
 */

/**
 *  Defining Constants
 */

// Core Constants
define('SOMALITE_REQUIRED_PHP_VERSION', '5.6');
define('SOMALITE_DIR_PATH', get_template_directory());
define('SOMALITE_THEME_AUTH', 'https://www.spiraclethemes.com/');
define('SOMALITE_DIR_URI', get_template_directory_uri());

// Register Custom Navigation Walker
require_once (get_template_directory() . '/inc/wp_bootstrap_navwalker.php');


/**
 * Check for minimum PHP version requirement
 *
 */
function somalite_check_theme_setup($oldtheme_name, $oldtheme) {
    // Compare versions.
    if (version_compare(phpversion(), SOMALITE_REQUIRED_PHP_VERSION, '<')):
        // Theme not activated info message.
        add_action('admin_notices', 'somalite_php_admin_notice');
        function somalite_php_admin_notice() {
            ?>
              <div class="update-nag">
                  <?php esc_html_e('You need to update your PHP version to a minimum of 5.6 to run Somalite Theme.', 'somalite'); ?> <br />
                  <?php esc_html_e('Actual version is:', 'somalite') ?> <strong><?php echo phpversion(); ?></strong>, <?php esc_html_e('required is', 'somalite') ?> <strong><?php echo SOMALITE_REQUIRED_PHP_VERSION; ?></strong>
              </div>
            <?php
        }
        // Switch back to previous theme.
        switch_theme($oldtheme->stylesheet);
        return false;
    endif;
}
add_action('after_switch_theme', 'somalite_check_theme_setup', 10, 2);



if (!function_exists('somalite_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 somalite_setup() {
        /*
         * Make theme available for translation.
         * Translations can be filed in the /languages/ directory.
         * If you're building a theme based on soma, use a find and replace
         * to change 'somalite' to the name of your theme in all the template files.
        */
        load_theme_textdomain('somalite', 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');
        add_image_size('somalite-blog-footer-thumb', 100, 65);
        add_image_size('somalite-blog-section-thumb', 360, 239);

        // This theme uses wp_nav_menu() in two locations.
        register_nav_menus(array('primary' => esc_html__('Primary', 'somalite'), 'footer' => esc_html__('Footer Menu', 'somalite'),));

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

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


        // Remove theme support for new widgets block editor
        remove_theme_support( 'widgets-block-editor' );

        /**
         * somalite theme info
         */
        require get_template_directory() . '/inc/theme-info.php';

        /**
         * About page instance
         */
        $config = array();
        Somalite_About_Page::init($config);

        //woocommerce support
        add_theme_support( 'woocommerce' );
    	add_theme_support( 'wc-product-gallery-zoom' );
    	add_theme_support( 'wc-product-gallery-lightbox' );
    	add_theme_support( 'wc-product-gallery-slider' );
    }
endif;
add_action('after_setup_theme', 'somalite_setup');


/**
 * Custom Logo
 *
 */
if (!function_exists('somalite_logo_setup')):
    function somalite_logo_setup() {
        add_theme_support('custom-logo', array('height' => 60, 'width' => 135, 'flex-height' => true, 'flex-width' => true,));
    }
endif;
add_action('after_setup_theme', 'somalite_logo_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
 */
if (!function_exists('somalite_content_width')):
    function somalite_content_width() {
        $GLOBALS['content_width'] = apply_filters('somalite_content_width', 640);
    }
endif;
add_action('after_setup_theme', 'somalite_content_width', 0);


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

        register_sidebar( array(
            'name'          => esc_html__( 'Woocommerce Sidebar', 'somalite' ),
            'id'            => 'woosidebar',
            'description'   => esc_html__( 'Add widgets here.', 'somalite' ),
            'before_widget' => '<aside id="%1$s" class="widget %2$s">',
            'after_widget'  => '</aside>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) ); 


        register_sidebar( array(
            'name'          => esc_html__( 'Footer Column1', 'somalite' ),
            'id'            => 'footer-column1',
            'description'   => esc_html__( 'Add widgets here.', 'somalite' ),
            'before_widget' => '<div id="%1$s" class="section %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) );

        register_sidebar( array(
            'name'          => esc_html__( 'Footer Column2', 'somalite' ),
            'id'            => 'footer-column2',
            'description'   => esc_html__( 'Add widgets here.', 'somalite' ),
            'before_widget' => '<div id="%1$s" class="section %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) );

        register_sidebar( array(
            'name'          => esc_html__( 'Footer Column3', 'somalite' ),
            'id'            => 'footer-column3',
            'description'   => esc_html__( 'Add widgets here.', 'somalite' ),
            'before_widget' => '<div id="%1$s" class="section %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) );

        register_sidebar( array(
            'name'          => esc_html__( 'Footer Column4', 'somalite' ),
            'id'            => 'footer-column4',
            'description'   => esc_html__( 'Add widgets here.', 'somalite' ),
            'before_widget' => '<div id="%1$s" class="section %2$s">',
            'after_widget'  => '</div>',
            'before_title'  => '<h4 class="widget-title">',
            'after_title'   => '</h4>',
        ) );
    }
endif;
add_action('widgets_init', 'somalite_widgets_init');


/**
 * Display Dynamic CSS.
 */
function somalite_dynamic_css_wrap() {
    require_once (get_parent_theme_file_path('/css/dynamic.css.php'));
    ?>
        <style type="text/css" id="soma-dynamic-style">
            <?php echo somalite_dynamic_css_stylesheet(); ?>
        </style>
    <?php
}
add_action('wp_head', 'somalite_dynamic_css_wrap');


/**
 * Admin Scripts
 */
if (!function_exists('somalite_admin_scripts')):
    function somalite_admin_scripts($hook) {
        if ('appearance_page_somalite-theme-info' != $hook) return;
        wp_enqueue_style('soma-info-css', get_template_directory_uri() . '/css/soma-theme-info.css', false);
    }
endif;
add_action('admin_enqueue_scripts', 'somalite_admin_scripts');


/**
 * Enqueue scripts and styles.
 */
function somalite_scripts() {
    wp_enqueue_style('bootstrap', get_template_directory_uri() . '/css/bootstrap.css', array(), '3.3.7');
    wp_enqueue_style('fontawesome', get_template_directory_uri() . '/css/fontawesome.css', array(), '5.10.1');
    wp_enqueue_style('linearicons', get_template_directory_uri() . '/css/icon-font.css', array(), '1.0.0');
    wp_enqueue_style('soma-body-google-font', somalite_body_google_fonts_url(), array(), '1.0');
    wp_enqueue_style('soma-heading-google-font', somalite_heading_google_fonts_url(), array(), '1.0');
    wp_enqueue_style('magnific-popup-css', get_template_directory_uri() . '/css/magnific-popup.css', array(), '1.1.0');
    wp_enqueue_style('soma-woocommerce-style', get_template_directory_uri() . '/css/woocommerce-style.css', array(), wp_get_theme()->get('Version'));
    wp_enqueue_style('soma-style', get_template_directory_uri() . '/css/soma-style.css', array(), wp_get_theme()->get('Version'));
    wp_enqueue_style('soma-page-builder', get_template_directory_uri() . '/css/page-builder.css', array(), '1.0');
    if(true===get_theme_mod( 'sm_animation_display',true )) :
    	wp_enqueue_style('animate', get_template_directory_uri() . '/css/animate.css', array(), '1.0');
    endif;
    if (is_singular() && comments_open() && get_option('thread_comments')) :
        wp_enqueue_script('comment-reply');
    endif;
    wp_enqueue_script('bootstrap-js', get_template_directory_uri() . '/js/bootstrap.js', array(), '3.3.7', true);
    wp_enqueue_script('jquery-easing', get_template_directory_uri() . '/js/jquery.easing.1.3.js', array('jquery'), '1.3', true);
    wp_enqueue_script('modernizr', get_template_directory_uri() . '/js/modernizr.js', array(), '2.6.2', true);
    wp_enqueue_script('wow', get_template_directory_uri() . '/js/wow.js', array(), '1.1.2', true);
    wp_enqueue_script('parallax', get_template_directory_uri() . '/js/parallax.js', array(), '1.4.2', true);
    wp_enqueue_script('jquery-appear', get_template_directory_uri() . '/js/jquery.appear.js', array(), '1.0', true);
    wp_enqueue_script('jquery-countTo', get_template_directory_uri() . '/js/jquery.countTo.js', array(), '1.0', true);
    wp_enqueue_script('jquery-magnific', get_template_directory_uri() . '/js/jquery.magnific-popup.js', array(), '1.1.0', true);
    wp_enqueue_script('soma-script', get_template_directory_uri() . '/js/soma-main.js', array('jquery'), wp_get_theme()->get('Version'), true);
    if(true===get_theme_mod( 'sm_sticky_menu',false )) :
    	wp_enqueue_script('soma-sticky', get_template_directory_uri() . '/js/sticky.js', array('jquery'), wp_get_theme()->get('Version'), true);
    endif;
    wp_enqueue_script('html5shiv', get_template_directory_uri() . '/js/html5shiv.js', array(), '3.7.3');
    wp_script_add_data('html5shiv', 'conditional', 'lt IE 9');
    wp_enqueue_script('respond', get_template_directory_uri() . '/js/respond.js', array(), '1.3.0');
    wp_script_add_data('respond', 'conditional', 'lt IE 9');
}
add_action('wp_enqueue_scripts', 'somalite_scripts');


/**
 * Body Google Font
 */
if (!function_exists('somalite_body_google_fonts_url')):
    function somalite_body_google_fonts_url() {
        $body_font = esc_html(get_theme_mod('sm_body_font_name', 'Lato:wght@300;400;700;900&display=swap'));
        if ($body_font) {
            $fonts_url = 'https://fonts.googleapis.com/css2?family='.$body_font;
        }
        return $fonts_url;
    }
endif;


/**
 * Heading Google Font
 */
if (!function_exists('somalite_heading_google_fonts_url')):
    function somalite_heading_google_fonts_url() {
        $heading_font = esc_html(get_theme_mod('sm_heading_font_name', 'Lato:wght@300;400;700;900&display=swap'));
        if ($heading_font) {
            $fonts_url = 'https://fonts.googleapis.com/css2?family='.$heading_font;
        }
        return $fonts_url;
    }
endif;


/**
 * Soma Excerpt Length
 */
if (!function_exists('somalite_excerpt_length')):
    function somalite_excerpt_length($length) {
        return 50;
    }
endif;
add_filter('excerpt_length', 'somalite_excerpt_length');


/**
 * Registers an editor stylesheet for the theme.
 */
if (!function_exists('somalite_theme_add_editor_styles')):
    function somalite_theme_add_editor_styles() {
        add_editor_style(get_template_directory_uri() . '/css/custom-editor-style.css');
    }
endif;
add_action('admin_init', 'somalite_theme_add_editor_styles');


/**
 * Custom search form
 */
if (!function_exists('somalite_search_form')):
    function somalite_search_form($form) {
        $form = '<form method="get" id="searchform" class="searchform" action="' . home_url('/') . '" >
                  <div class="search">
                    <input type="text" value="' . get_search_query() . '" class="blog-search" name="s" id="s" placeholder="' . esc_attr__('Search here.', 'somalite') . '">
                    <label for="searchsubmit" class="search-icon"><i class="fas fa-search"></i></label>
                    <input type="submit" id="searchsubmit" value="' . esc_attr__('Search', 'somalite') . '" />
                  </div>
                  </form>';
        return $form;
    }
endif;
add_filter('get_search_form', 'somalite_search_form', 100);


/**
 * Custom product search form
 */
if (!function_exists('somalite_product_search_form')):
    function somalite_product_search_form($form) {
        $form = '<form method="get" id="searchform" class="searchform" action="' . home_url('/') . '" >
                  <div class="search">
                    <input type="text" value="' . get_search_query() . '" class="product-search" name="s" id="s" placeholder="' . esc_attr__('Search products.', 'somalite') . '">
                    <label for="searchsubmit" class="search-icon"><i class="fas fa-search"></i></label>
                    <input type="hidden" name="post_type" value="product" />
                    <input type="submit" id="searchsubmit" value="' . esc_attr__('Search', 'somalite') . '" />
                  </div>
                  </form>';
        return $form;
    }
endif;
add_filter('get_product_search_form', 'somalite_product_search_form', 100);


/**
 * Search Filter
 */
if (!function_exists('somalite_search_filter')):
    function somalite_search_filter($query) {
        if ($query->is_search && is_post_type_archive('product')) {
            $query->set('post_type', 'product');
        }
        return $query;
    }
endif;
add_filter('pre_get_posts', 'somalite_search_filter');


/**
 * Soma Excerpt More
 */
if (!function_exists('somalite_excerpt_more')):
    function somalite_excerpt_more($more) {
        return ' &hellip;';
    }
endif;
add_filter('excerpt_more', 'somalite_excerpt_more');


/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function somalite_pingback_header() {
    if (is_singular() && pings_open()) {
        printf('<link rel="pingback" href="%s">' . "\n", get_bloginfo('pingback_url'));
    }
}
add_action('wp_head', 'somalite_pingback_header');


/**
 * Soma Moving Comment fields
 */
if (!function_exists('somalite_move_comment_field_to_bottom')):
    function somalite_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', 'somalite_move_comment_field_to_bottom');


/**
 * Soma Add Widgets to Site Origin Page Builder Group
 */
if (!function_exists('somalite_add_widget_tabs')):
    function somalite_add_widget_tabs($tabs) {
        $tabs[] = array('title' => esc_attr__('Soma Widgets', 'somalite'), 'filter' => array('groups' => array('soma')));
        return $tabs;
    }
endif;
add_filter('siteorigin_panels_widget_dialog_tabs', 'somalite_add_widget_tabs', 20);


/**
 * Using home page title instead of site name
 */
if (!function_exists('somalite_breadcrumb_title')):
    function somalite_breadcrumb_title($title, $type, $id) {
        if ($type[0] === 'home') {
            $title = get_the_title(get_option('page_on_front'));
        }
        return $title;
    }
endif;
add_filter('bcn_breadcrumb_title', 'somalite_breadcrumb_title', 42, 3);


/**
 *  Set homepage and blog page after demo import
 */
function somalite_after_import_setup() {
    //Assign menus to their locations
    $main_menu = get_term_by('name', 'Primary', 'nav_menu');
    set_theme_mod('nav_menu_locations', array('primary' => $main_menu->term_id,));
    //Assign front page
    $front_page = get_page_by_title('Home');
    $blog_page = get_page_by_title('Blog');
    update_option('show_on_front', 'page');
    update_option('page_on_front', $front_page->ID);
    update_option('page_for_posts', $blog_page->ID);
}
add_action('pt-ocdi/after_import', 'somalite_after_import_setup');


/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer/customizer.php';
/**
 * Different Header Styles.
 */
require get_template_directory() . '/inc/header-functions.php';
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';
/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-functions.php';
/**
 * Custom functions that act independently of the theme templates.
 */
require get_template_directory() . '/inc/extras.php';
/**
 * Load Widgets
 */
require get_template_directory() . '/inc/widgets.php';
/**
 * Load Woocommerce functions
 */
require get_template_directory() . '/inc/woocommerce-functions.php';
/**
 * Notices
 */
require_once get_parent_theme_file_path( '/inc/activation/class-welcome-notice.php' );
require_once get_parent_theme_file_path( '/inc/activation/class-rating-notice.php' );
/**
 * Upgrade Pro
 */
require_once (trailingslashit(get_template_directory()) . 'soma-pro/class-customize.php');
