<?php
/**
 * Ecommerce Solution functions and definitions
 * @package Ecommerce Solution
 */

// Breadcrumb
function ecommerce_solution_the_breadcrumb() {
    if (!is_home()) {
        echo '<a href="' . esc_url(home_url()) . '">' . esc_html(get_bloginfo('name')) . '</a>';
        if (is_category() || is_single()) {
            the_category(', ');
            if (is_single()) {
                echo '<span>' . esc_html(get_the_title()) . '</span>';
            }
        } elseif (is_page()) {
            echo '<span>' . esc_html(get_the_title()) . '</span>';
        }
    }
}

// Theme setup
function ecommerce_solution_setup() {
    $GLOBALS['content_width'] = apply_filters( 'ecommerce_solution_content_width', 640 );
    load_theme_textdomain( 'ecommerce-solution', get_template_directory() . '/languages' );
    add_theme_support( 'automatic-feed-links' );
    add_theme_support( 'post-thumbnails' );
    add_theme_support( 'align-wide' );
    add_theme_support( 'wp-block-styles' );
    add_theme_support( 'woocommerce' );
    add_theme_support( 'wc-product-gallery-zoom' );
    add_theme_support( 'wc-product-gallery-slider' );
    add_theme_support( 'title-tag' );
    add_theme_support( 'custom-logo', array('height' => 240, 'width' => 240, 'flex-height' => true) );
    add_image_size('ecommerce-solution-homepage-thumb',240,145,true);
    register_nav_menus( array(
        'primary' => __( 'Primary Menu', 'ecommerce-solution' ),
        'topmenus' => __( 'Top Menu', 'ecommerce-solution' ),
    ) );
    add_theme_support( 'custom-background', array('default-color' => 'f1f1f1') );
    add_theme_support( 'post-formats', array('image','video','gallery','audio') );
    add_theme_support( 'html5', array('comment-form','comment-list','gallery','caption') );
    add_theme_support('responsive-embeds');
    add_editor_style( array( 'css/editor-style.css', ecommerce_solution_font_url() ) );
}
add_action( 'after_setup_theme', 'ecommerce_solution_setup' );

// Font URL
function ecommerce_solution_font_url(){
    $font_family = array(
        'Roboto:ital,wght@0,400;0,700;1,400;1,700',
        'Montserrat:ital,wght@0,400;0,700;1,400;1,700',
        'Lato:ital,wght@0,400;0,700;1,400;1,700'
    );
    $query_args = array('family' => rawurlencode(implode('|',$font_family)));
    $fonts_url = add_query_arg($query_args,'//fonts.googleapis.com/css');
    return wptt_get_webfont_url( esc_url_raw( $fonts_url ) );
}

// Enqueue styles and scripts
function ecommerce_solution_scripts() {
    wp_enqueue_style( 'ecommerce-solution-font', ecommerce_solution_font_url(), array() );
    wp_enqueue_style( 'ecommerce-solution-style', get_stylesheet_uri() );
    wp_style_add_data( 'ecommerce-solution-style', 'rtl', 'replace' );
    wp_enqueue_style( 'bootstrap-css', get_template_directory_uri() . '/css/bootstrap.css');
    wp_enqueue_style( 'font-awesome-css', get_template_directory_uri().'/css/fontawesome-all.css' );
    wp_enqueue_style( 'ecommerce-solution-block-style', get_theme_file_uri('/css/blocks.css') );

    if (get_theme_mod('ecommerce_solution_sidebar_animation', true)) {
        wp_enqueue_script( 'jquery-wow', get_template_directory_uri() . '/js/wow.js', array('jquery') );
        wp_enqueue_style( 'animate-css', get_template_directory_uri().'/css/animate.css' );
    }

    // Inline CSS
    $body_color = esc_html(get_theme_mod('ecommerce_solution_body_color',''));
    $custom_css = "body { color: {$body_color}!important; }";
    wp_add_inline_style('ecommerce-solution-style', $custom_css);

    // Scripts
    wp_enqueue_script( 'tether-js', get_template_directory_uri() . '/js/tether.js', array('jquery'),'',true);
    wp_enqueue_script( 'bootstrap-js', get_template_directory_uri() . '/js/bootstrap.js', array('jquery'),'',true);
    wp_enqueue_script( 'jquery-superfish', get_template_directory_uri() . '/js/jquery.superfish.js', array('jquery'),'',true);
    wp_enqueue_script( 'ecommerce-solution-custom-scripts-jquery', get_template_directory_uri() . '/js/custom.js', array('jquery') );

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

// WooCommerce filters
function ecommerce_solution_loop_columns() {
    return get_theme_mod( 'ecommerce_solution_per_columns', 4 );
}
add_filter('loop_shop_columns', 'ecommerce_solution_loop_columns');

function ecommerce_solution_shop_per_page( $cols ) {
    return get_theme_mod( 'ecommerce_solution_product_per_page', 9 );
}
add_filter( 'loop_shop_per_page', 'ecommerce_solution_shop_per_page', 20 );

// Theme constants
function ecommerce_solution_theme_setup() {
    define('ECOMMERCE_SOLUTION_LIVE_DEMO','https://demos.buywptemplates.com/bwt-ecommerce-solution-pro/');
    define('ECOMMERCE_SOLUTION_BUY_PRO','https://www.buywptemplates.com/products/ecommerce-wordpress-template');
    define('ECOMMERCE_SOLUTION_PRO_DOC','https://demos.buywptemplates.com/demo/docs/bwt-ecommerce-solution-pro/');
    define('ECOMMERCE_SOLUTION_FREE_DOC','https://demos.buywptemplates.com/demo/docs/free-bwt-ecommerce-solution/');
    define('ECOMMERCE_SOLUTION_PRO_SUPPORT','https://buywptemplates.com/pages/community');
    define('ECOMMERCE_SOLUTION_FREE_SUPPORT','https://wordpress.org/support/theme/ecommerce-solution/');
    define('ECOMMERCE_SOLUTION_REVIEW','https://wordpress.org/support/theme/ecommerce-solution/reviews/');
    define('ECOMMERCE_SOLUTION_CREDIT','https://www.buywptemplates.com/products/free-ecommerce-wordpress-template');
}
add_action( 'after_setup_theme', 'ecommerce_solution_theme_setup' );

function ecommerce_solution_credit(){
    echo "<a href=".esc_url(ECOMMERCE_SOLUTION_CREDIT).">".esc_html__('Ecommerce WordPress Theme','ecommerce-solution')."</a>";
}

// Includes
require get_template_directory() . '/inc/custom-header.php';
require get_template_directory() . '/inc/template-tags.php';
require get_template_directory() . '/inc/customizer.php';
require get_template_directory() . '/inc/dashboard/get_started_info.php';
require get_template_directory() . '/inc/about.php';
require get_template_directory() . '/inc/contact.php';
require get_template_directory() . '/wptt-webfont-loader.php';
require get_template_directory() . '/inc/tgm/tgm.php';

// Disable WooCommerce setup wizard
add_filter( 'woocommerce_enable_setup_wizard', '__return_false' );
