<?php
if ( ! function_exists( 'coupon_flash_deals_setup' ) ) :
function coupon_flash_deals_setup() {

	add_theme_support( "woocommerce" );	

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

	add_theme_support( "responsive-embeds" );

	/*
	 * Let WordPress manage the document title.
	 */
	add_theme_support( 'title-tag' );
	
	
	add_theme_support( 'custom-header' );
	
	//Add selective refresh for sidebar widget
	add_theme_support( 'customize-selective-refresh-widgets' );
	
	/*
	 * Enable support for Post Thumbnails on posts and pages.
	 */
	add_theme_support( 'post-thumbnails' );

	// This theme uses wp_nav_menu() in one location.
	register_nav_menus( array(
		'primary_menu' => esc_html__( 'Primary Menu', 'coupon-flash-deals' ),
	) );

	/*
	 * 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',
	) );
	
	/*
	 * Enable support for custom logo.
	 */
	add_theme_support('custom-logo');

	// -- Disable Custom Colors
	add_theme_support( 'disable-custom-colors' );
		
	// Gutenberg wide images.
	add_theme_support( 'align-wide' );
		
	/*
	 * This theme styles the visual editor to resemble the theme style,
	 * specifically font, colors, icons, and column width.
	 */
	add_editor_style( array( 'css/editor-style.css' ) );
	
	// Set up the WordPress core custom background feature.
	add_theme_support( 'custom-background', apply_filters( 'coupon_flash_deals_custom_background_args', array(
		'default-color' => 'ffffff',
		'default-image' => '',
	) ) );

	load_theme_textdomain( 'coupon-flash-deals', get_stylesheet_directory() . '/languages' );

        /**
     * Get Started.
     */
    require( get_template_directory() . '/inc/started/main.php');

    /**
     * Customizer additions.
     */
     require_once get_template_directory() . '/inc/customizer.php';
     
}
endif;
add_action( 'after_setup_theme', 'coupon_flash_deals_setup' );

    /**
     * All Styles & Scripts.
     */
    require_once get_template_directory() . '/inc/enqueue.php';

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

    /**
     * Sidebar.
     */
    require_once get_template_directory() . '/inc/sidebar/sidebar.php';

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

    /**
     * Custom functions that act independently of the theme templates.
     */
    require_once get_template_directory() . '/inc/extras.php';

    /**
     * Load Jetpack compatibility file.
     */
    require_once get_template_directory() . '/inc/jetpack.php';

    /**
     * Load Web Font
     */
    require_once get_template_directory() . '/inc/wptt-webfont-loader.php';

      /**
     * Load Recommended Plugin
     */
    require_once get_template_directory() . '/inc/importer/config.php';

    /**
     * Called all the Customize file.
     */
    require( get_template_directory() . '/inc/customize/premium.php');

    /**
     * Admin notice function.
     */
    require_once get_template_directory() . '/inc/admin-notice/admin.php';


/*
 * Enable support for Post Formats.
 *
 * See: https://codex.wordpress.org/Post_Formats
*/
add_theme_support( 'post-formats', array('image','video','gallery','audio',) );

/**
 * 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 coupon_flash_deals_content_width() {
	$GLOBALS['content_width'] = apply_filters( 'coupon_flash_deals_content_width', 1170 );
}
add_action( 'after_setup_theme', 'coupon_flash_deals_content_width', 0 );

/**
 * Change number or products per row to 3
 */
add_filter('loop_shop_columns', 'coupon_flash_deals_loop_columns', 999);
if (!function_exists('coupon_flash_deals_loop_columns')) {
	function coupon_flash_deals_loop_columns() {
		return 3; // 3 products per row
	}
}

function coupon_flash_deals_get_page_id_by_title($coupon_flash_deals_pagename){

    $args = array(
        'post_type' => 'page',
        'posts_per_page' => 1,
        'post_status' => 'publish',
        'title' => $coupon_flash_deals_pagename
    );
    $query = new WP_Query( $args );

    $page_id = '1';
    if (isset($query->post->ID)) {
        $page_id = $query->post->ID;
    }

    return $page_id;
}

function coupon_flash_deals_customize_remove_register() {
    global $wp_customize;

    $wp_customize->remove_setting( 'display_header_text' );
    $wp_customize->remove_control( 'display_header_text' );
    
}
add_action( 'customize_register', 'coupon_flash_deals_customize_remove_register', 11 );

/* 
* Logo Resizer
*/
function coupon_flash_deals_logo_resizer_setting() {

    $coupon_flash_deals_theme_logo_size_css = '';
    $coupon_flash_deals_logo_resizer_setting = get_theme_mod('coupon_flash_deals_logo_resizer_setting');

	$coupon_flash_deals_theme_logo_size_css = '
		.custom-logo{
			height: '.esc_attr($coupon_flash_deals_logo_resizer_setting).'px !important;
			width: '.esc_attr($coupon_flash_deals_logo_resizer_setting).'px !important;
		}
	';
    wp_add_inline_style( 'coupon-flash-deals-style',$coupon_flash_deals_theme_logo_size_css );

}
add_action( 'wp_enqueue_scripts', 'coupon_flash_deals_logo_resizer_setting' );


/**
 * Function that returns if the menu is sticky
 */
if (!function_exists('coupon_flash_deals_sticky_header')):
    function coupon_flash_deals_sticky_header()
    {
        $coupon_flash_deals_is_sticky = get_theme_mod('coupon_flash_deals_sticky_header_setting', false);

        if ($coupon_flash_deals_is_sticky == false):
            return 'not-sticky';
        else:
            return 'is-sticky-on';
        endif;
    }
endif;

$separator = ' → '; // Define the separator outside

function coupon_flash_deals_breadcrumb() {
    global $separator; // Make the separator accessible inside the function

    if (is_home()) {
        echo "<span>Home</span>";
    } else if (!is_home()) {
        echo '<a href="' . home_url() . '">Home</a>' . $separator;

        if (is_archive()) {
            if (is_category()) {
                // If it's a category archive page
                echo "<span>";
                single_cat_title();
                echo "</span>";
            } elseif (is_tag()) {
                // If it's a tag archive page
                echo "<span>";
                single_tag_title();
                echo "</span>";
            } elseif (is_date()) {
                // If it's a date-based archive page
                echo "<span>";
                echo get_the_date('F Y');
                echo "</span>";
            } elseif (is_author()) {
                echo '<span>Author: ';
                the_author();
                echo '</span>';
            } else {
                // For other archive pages
                echo post_type_archive_title() . $separator;
            }
        } elseif (is_single()) {
            // Check if it's a single product (or any custom post type)
            if ('product' === get_post_type()) {
                // WooCommerce product breadcrumb
                echo '<a href="' . get_permalink(wc_get_page_id('shop')) . '">Shop</a>' . $separator;
                echo "<span>";
                the_title();
                echo "</span>";
            } else {
                // For regular posts or categories
                the_category(', ');
                echo $separator; // Add the separator here
                echo "<span>";
                the_title();
                echo "</span>";
            }
        } elseif (is_page()) {
            // For regular pages
            echo "<span>";
            the_title();
            echo "</span>";
        } elseif (is_search()) {
            // Search results
            echo '<span>Search Results for: ' . get_search_query() . '</span>';
        } elseif (is_404()) {
            echo "<span>404</span>";
        } else {
            echo "<span>";
            the_title();
            echo "</span>";
        }
    }
}

function get_page_id_by_title($pagename){

    $args = array(
        'post_type' => 'page',
        'posts_per_page' => 1,
        'post_status' => 'publish',
        'title' => $pagename
    );
    $query = new WP_Query( $args );

    $page_id = '1';
    if (isset($query->post->ID)) {
        $page_id = $query->post->ID;
    }

    return $page_id;
}