<?php

/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package dog_pet_house
 */

 function dog_pet_house_customize_css() {
    ?>
    <style type="text/css">
        :root {
            --primary-color: <?php echo esc_html( get_theme_mod( 'dog_pet_house_primary_color', '#6839cc' ) ); ?>;
        }
    </style>
    <?php
}
add_action( 'wp_head', 'dog_pet_house_customize_css' );

function dog_pet_house_enqueue_selected_fonts() {
    $dog_pet_house_fonts_url = dog_pet_house_get_fonts_url();
    if (!empty($dog_pet_house_fonts_url)) {
        wp_enqueue_style('dog-pet-house-google-fonts', $dog_pet_house_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'dog_pet_house_enqueue_selected_fonts');

function dog_pet_house_layout_customizer_css() {
    $dog_pet_house_margin = get_theme_mod('dog_pet_house_layout_width_margin', 50);
    ?>
    <style type="text/css">
        body.site-boxed--layout #page  {
            margin: 0 <?php echo esc_attr($dog_pet_house_margin); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_layout_customizer_css');

function dog_pet_house_blog_layout_customizer_css() {
    // Retrieve the blog layout option
    $dog_pet_house_blog_layout_option = get_theme_mod('dog_pet_house_blog_layout_option_setting', 'Left');

    // Initialize custom CSS variable
    $dog_pet_house_custom_css = '';

    // Generate custom CSS based on the layout option
    if ($dog_pet_house_blog_layout_option === 'Default') {
        $dog_pet_house_custom_css .= '.mag-post-detail { text-align: center; }';
    } elseif ($dog_pet_house_blog_layout_option === 'Left') {
        $dog_pet_house_custom_css .= '.mag-post-detail { text-align: left; }';
    } elseif ($dog_pet_house_blog_layout_option === 'Right') {
        $dog_pet_house_custom_css .= '.mag-post-detail { text-align: right; }';
    }

    // Output the combined CSS
    ?>
    <style type="text/css">
        <?php echo wp_kses($dog_pet_house_custom_css, array( 'style' => array(), 'text-align' => array() )); ?>
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_blog_layout_customizer_css');

// Featured Image Dimension
function dog_pet_house_custom_featured_image_css() {
    $dog_pet_house_dimension = get_theme_mod('dog_pet_house_blog_post_featured_image_dimension', 'default');
    $dog_pet_house_width = get_theme_mod('dog_pet_house_blog_post_featured_image_custom_width', '');
    $dog_pet_house_height = get_theme_mod('dog_pet_house_blog_post_featured_image_custom_height', '');
    
    if ($dog_pet_house_dimension === 'custom' && $dog_pet_house_width && $dog_pet_house_height) {
        $dog_pet_house_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$dog_pet_house_width}px !important; height: {$dog_pet_house_height}px !important; }";
        wp_add_inline_style('dog-pet-house-style', $dog_pet_house_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'dog_pet_house_custom_featured_image_css');

// Featured Image Border Radius
function dog_pet_house_featured_image_border_radius_css() {
    $dog_pet_house_featured_image_border_radius = get_theme_mod('dog_pet_house_featured_image_border_radius', 10);
    ?>
    <style type="text/css">  
        .mag-post-single img {
            border-radius: <?php echo esc_attr($dog_pet_house_featured_image_border_radius); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_featured_image_border_radius_css');

function dog_pet_house_sidebar_width_customizer_css() {
    $dog_pet_house_sidebar_width = get_theme_mod('dog_pet_house_sidebar_width', '30');
    ?>
    <style type="text/css">
        .right-sidebar .asterthemes-wrapper .asterthemes-page {
            grid-template-columns: auto <?php echo esc_attr($dog_pet_house_sidebar_width); ?>%;
        }
        .left-sidebar .asterthemes-wrapper .asterthemes-page {
            grid-template-columns: <?php echo esc_attr($dog_pet_house_sidebar_width); ?>% auto;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_sidebar_width_customizer_css');

if ( ! function_exists( 'dog_pet_house_get_page_title' ) ) {
    function dog_pet_house_get_page_title() {
        $dog_pet_house_title = '';

        if (is_404()) {
            $dog_pet_house_title = esc_html__('Page Not Found', 'dog-pet-house');
        } elseif (is_search()) {
            $dog_pet_house_title = esc_html__('Search Results for: ', 'dog-pet-house') . esc_html(get_search_query());
        } elseif (is_home() && !is_front_page()) {
            $dog_pet_house_title = esc_html__('Blogs', 'dog-pet-house');
        } elseif (function_exists('is_shop') && is_shop()) {
            $dog_pet_house_title = esc_html__('Shop', 'dog-pet-house');
        } elseif (is_page()) {
            $dog_pet_house_title = get_the_title();
        } elseif (is_single()) {
            $dog_pet_house_title = get_the_title();
        } elseif (is_archive()) {
            $dog_pet_house_title = get_the_archive_title();
        } else {
            $dog_pet_house_title = get_the_archive_title();
        }

        return apply_filters('dog_pet_house_page_title', $dog_pet_house_title);
    }
}

if ( ! function_exists( 'dog_pet_house_has_page_header' ) ) {
    function dog_pet_house_has_page_header() {
        // Default to true (display header)
        $dog_pet_house_return = true;

        // Custom conditions for disabling the header
        if ('hide-all-devices' === get_theme_mod('dog_pet_house_page_header_visibility', 'all-devices')) {
            $dog_pet_house_return = false;
        }

        // Apply filters and return
        return apply_filters('dog_pet_house_display_page_header', $dog_pet_house_return);
    }
}

if ( ! function_exists( 'dog_pet_house_page_header_style' ) ) {
    function dog_pet_house_page_header_style() {
        $dog_pet_house_style = get_theme_mod('dog_pet_house_page_header_style', 'default');
        return apply_filters('dog_pet_house_page_header_style', $dog_pet_house_style);
    }
}

function dog_pet_house_page_title_customizer_css() {
    $dog_pet_house_layout_option = get_theme_mod('dog_pet_house_page_header_layout', 'left');
    ?>
    <style type="text/css">
        .asterthemes-wrapper.page-header-inner {
            <?php if ($dog_pet_house_layout_option === 'flex') : ?>
                display: flex;
                justify-content: space-between;
                align-items: center;
            <?php else : ?>
                text-align: <?php echo esc_attr($dog_pet_house_layout_option); ?>;
            <?php endif; ?>
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_page_title_customizer_css');

function dog_pet_house_pagetitle_height_css() {
    $dog_pet_house_height = get_theme_mod('dog_pet_house_pagetitle_height', 50);
    ?>
    <style type="text/css">
        header.page-header {
            padding: <?php echo esc_attr($dog_pet_house_height); ?>px 0;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_pagetitle_height_css');

function dog_pet_house_site_logo_width() {
    $dog_pet_house_site_logo_width = get_theme_mod('dog_pet_house_site_logo_width', 200);
    ?>
    <style type="text/css">
        .site-logo img {
            max-width: <?php echo esc_attr($dog_pet_house_site_logo_width); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_site_logo_width');

function dog_pet_house_menu_font_size_css() {
    $dog_pet_house_menu_font_size = get_theme_mod('dog_pet_house_menu_font_size', 15);
    ?>
    <style type="text/css">
        .main-navigation a {
            font-size: <?php echo esc_attr($dog_pet_house_menu_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_menu_font_size_css');

//Menu Font Weight
function dog_pet_house_menu_font_weight_css() {
	$dog_pet_house_font_weight = get_theme_mod( 'dog_pet_house_menu_font_weight', 400);

	$dog_pet_house_custom_css = "
		.main-navigation a {
		    font-weight: {$dog_pet_house_font_weight};
		}
	";

	wp_add_inline_style( 'dog-pet-house-style', $dog_pet_house_custom_css );
}
add_action( 'wp_enqueue_scripts', 'dog_pet_house_menu_font_weight_css' );

function dog_pet_house_sidebar_widget_font_size_css() {
    $dog_pet_house_sidebar_widget_font_size = get_theme_mod('dog_pet_house_sidebar_widget_font_size', 21);
    ?>
    <style type="text/css">
        h2.wp-block-heading,aside#secondary .widgettitle,aside#secondary .widget-title {
            font-size: <?php echo esc_attr($dog_pet_house_sidebar_widget_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_sidebar_widget_font_size_css');

// Woocommerce Related Products Settings
function dog_pet_house_related_product_css() {
    $dog_pet_house_related_product_show_hide = get_theme_mod('dog_pet_house_related_product_show_hide', true);

    if ( $dog_pet_house_related_product_show_hide != true) {
        ?>
        <style type="text/css">
            .related.products {
                display: none;
            }
        </style>
        <?php
    }
}
add_action('wp_head', 'dog_pet_house_related_product_css');

// Woocommerce Product Sale Position 
function dog_pet_house_product_sale_position_customizer_css() {
    $dog_pet_house_layout_option = get_theme_mod('dog_pet_house_product_sale_position', 'left');
    ?>
    <style type="text/css">
        .woocommerce ul.products li.product .onsale{
            <?php if ($dog_pet_house_layout_option === 'left') : ?>
                right: auto;
                left: 0px;
            <?php else : ?>
                left: auto;
                right: 0px;
            <?php endif; ?>
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_product_sale_position_customizer_css'); 

//Footer Social Icon Alignment
function dog_pet_house_footer_icons_alignment_css() {
    $dog_pet_house_footer_social_align = get_theme_mod( 'dog_pet_house_footer_social_align', 'center' );   
    ?>
    <style type="text/css">
        .socialicons {
            text-align: <?php echo esc_attr( $dog_pet_house_footer_social_align ); ?> 
        }

        /* Mobile Specific */
        @media screen and (max-width: 575px) {
            .socialicons {
                text-align: center;
            }
        }
    </style>
    <?php
}
add_action( 'wp_head', 'dog_pet_house_footer_icons_alignment_css' );

//Copyright Alignment
function dog_pet_house_footer_copyright_alignment_css() {
    $dog_pet_house_footer_bottom_align = get_theme_mod( 'dog_pet_house_footer_bottom_align', 'center' );   
    ?>
    <style type="text/css">
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: <?php echo esc_attr( $dog_pet_house_footer_bottom_align ); ?> 
        }

        /* Mobile Specific */
        @media screen and (max-width: 575px) {
            .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
                justify-content: center;
                text-align:center;
            }
        }
    </style>
    <?php
}
add_action( 'wp_head', 'dog_pet_house_footer_copyright_alignment_css' );

//Copyright Font Size
function dog_pet_house_copyright_font_size_css() {
    $dog_pet_house_copyright_font_size = get_theme_mod('dog_pet_house_copyright_font_size', 16);
    ?>
    <style type="text/css">
        .site-footer-bottom .site-info span {
            font-size: <?php echo esc_attr($dog_pet_house_copyright_font_size); ?>px;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_copyright_font_size_css');

// Preloader Background Color Setting
function dog_pet_house_preloader_background_colors_css() {
    $dog_pet_house_preloader_background_color_setting = get_theme_mod('dog_pet_house_preloader_background_color_setting', '');
        // Only output CSS if a color is set
        if (empty($dog_pet_house_preloader_background_color_setting)) {
            return;
        }
    ?>
    <style type="text/css">
        #loader {
            background-color: <?php echo esc_attr($dog_pet_house_preloader_background_color_setting); ?>;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_preloader_background_colors_css');

// Preloader Background Image Setting
function dog_pet_house_preloader_background_image_css() {
    $dog_pet_house_preloader_background_image_setting = get_theme_mod('dog_pet_house_preloader_background_image_setting', '');
        // Only output CSS if the background image is set
        if (empty($dog_pet_house_preloader_background_image_setting)) {
            return;
        }
    ?>
    <style type="text/css">
        #loader {
            background-image: url('<?php echo esc_url($dog_pet_house_preloader_background_image_setting); ?>');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }
    </style>
    <?php
}
add_action('wp_head', 'dog_pet_house_preloader_background_image_css');

//Footer Heading Alignment
function dog_pet_house_footer_heading_alignment_css() {
    $dog_pet_house_footer_header_align = get_theme_mod( 'dog_pet_house_footer_header_align', 'left' );   
    ?>
    <style type="text/css">
        .site-footer h4, footer#colophon h2.wp-block-heading, footer#colophon .widgettitle, footer#colophon .widget-title {
            text-align: <?php echo esc_attr( $dog_pet_house_footer_header_align ); ?> 
        }
    </style>
    <?php
}
add_action( 'wp_head', 'dog_pet_house_footer_heading_alignment_css' );

//First Capital Letter
function dog_pet_house_show_first_caps() {
	$dog_pet_house_first_caps = get_theme_mod('dog_pet_house_show_first_caps', false);
	$dog_pet_house_css = '';
	if ( $dog_pet_house_first_caps ) {
	$dog_pet_house_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
		    font-size: 55px;
			font-weight: 600;
			margin-right: 6px;
			line-height: 1;
			display: inline-block;
			vertical-align: baseline;
		}';
	} else {
		$dog_pet_house_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'dog-pet-house-style', $dog_pet_house_css );
}
add_action( 'wp_enqueue_scripts', 'dog_pet_house_show_first_caps' );