<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package charitable_funds
 */


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function charitable_funds_customizer_css_inline() {
	$charitable_funds_custom_css = '';
	$charitable_funds_enable_breadcrumb = get_theme_mod( 'charitable_funds_enable_breadcrumb', true );

	if ( ! $charitable_funds_enable_breadcrumb ) {
		$charitable_funds_custom_css .= 'nav.woocommerce-breadcrumb { 
            display: none; 
        }';
	}

	if ( ! empty( $charitable_funds_custom_css ) ) {
		wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_customizer_css_inline' );

function charitable_funds_custom_css() {
    $charitable_funds_banner = get_theme_mod('charitable_funds_enable_banner_section', false);
    ?>
    <style type="text/css">
        .home .top-header-part {
            z-index: 99;
            width: 100%;
            <?php if ( $charitable_funds_banner ) : ?>
                background: var(--primary-color);
                position: absolute;
            <?php else : ?>
				background: var(--primary-color);
                position: static;      
            <?php endif; ?>
        }
    </style>
    <?php
}
add_action( 'wp_head', 'charitable_funds_custom_css' );

//Global Color
function charitable_funds_customize_css() {
	$charitable_funds_primary_color = get_theme_mod( 'charitable_funds_primary_color', '#FE8802' );
    $charitable_funds_secondary_color = get_theme_mod( 'charitable_funds_secondary_color', '#2E164E' );
	$charitable_funds_custom_css = ":root { --primary-color: {$charitable_funds_primary_color}; }";
    $charitable_funds_custom_css = ":root { --secondary_color: {$charitable_funds_secondary_color}; }";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_customize_css' );

//Google Fonts
function charitable_funds_enqueue_selected_fonts() {
    $charitable_funds_fonts_url = charitable_funds_get_fonts_url();
    if (!empty($charitable_funds_fonts_url)) {
        wp_enqueue_style('charitable-funds-google-fonts', $charitable_funds_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'charitable_funds_enqueue_selected_fonts');

//Set Width
function charitable_funds_layout_customizer_css() {
	$charitable_funds_margin = get_theme_mod( 'charitable_funds_layout_width_margin', 50 );
	$charitable_funds_custom_css = "body.site-boxed--layout #page { margin: 0 {$charitable_funds_margin}px; }";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_layout_customizer_css', 20 );

//Blog Content Alignment
function charitable_funds_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$charitable_funds_layout_option = get_theme_mod( 'charitable_funds_blog_layout_option_setting', 'Left' );

	// Determine text alignment based on option
	switch ( $charitable_funds_layout_option ) {
		case 'Right':
			$charitable_funds_text_align = 'right';
			break;
		case 'Default':
			$charitable_funds_text_align = 'center';
			break;
		case 'Left':
		default:
			$charitable_funds_text_align = 'left';
			break;
	}

	// Create custom CSS
	$charitable_funds_custom_css = ".mag-post-detail { text-align: {$charitable_funds_text_align}; }";

	// Attach CSS to theme style
	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_blog_layout_customizer_css' );

//Sidebar Width Setting
function charitable_funds_sidebar_width_customizer_css() {
	$charitable_funds_sidebar_width = get_theme_mod( 'charitable_funds_sidebar_width', '30' );

	$charitable_funds_custom_css = "
		.right-sidebar .asterthemes-wrapper .asterthemes-page {
			grid-template-columns: auto {$charitable_funds_sidebar_width}%;
		}
		.left-sidebar .asterthemes-wrapper .asterthemes-page {
			grid-template-columns: {$charitable_funds_sidebar_width}% auto;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_sidebar_width_customizer_css' );


if ( ! function_exists( 'charitable_funds_get_page_title' ) ) {
    function charitable_funds_get_page_title() {
        $charitable_funds_title = '';

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

        return apply_filters('charitable_funds_page_title', $charitable_funds_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('charitable_funds_display_page_header', $charitable_funds_return);
    }
}

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

//Page Title Options
function charitable_funds_page_title_customizer_css() {
	$charitable_funds_layout_option = get_theme_mod( 'charitable_funds_page_header_layout', 'left' );
	$charitable_funds_custom_css = '';

	if ( $charitable_funds_layout_option === 'flex' ) {
		$charitable_funds_custom_css .= '
			.asterthemes-wrapper.page-header-inner {
				display: flex;
				justify-content: space-between;
				align-items: center;
			}
		';
	} else {
		$charitable_funds_custom_css .= "
			.asterthemes-wrapper.page-header-inner {
				text-align: {$charitable_funds_layout_option};
			}
		";
	}

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_page_title_customizer_css' );

//Set Height
function charitable_funds_pagetitle_height_css() {
	$charitable_funds_height = get_theme_mod( 'charitable_funds_pagetitle_height', 50 );

	$charitable_funds_custom_css = "
		header.page-header {
			padding: {$charitable_funds_height}px 0;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_pagetitle_height_css' );

//Adjust Site Logo Width
function charitable_funds_site_logo_width() {
	$charitable_funds_logo_width = get_theme_mod( 'charitable_funds_site_logo_width', 150 );

	$charitable_funds_custom_css = "
		.site-logo img {
			max-width: {$charitable_funds_logo_width}px;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_site_logo_width' );

//Menu Font Size
function charitable_funds_menu_font_size_css() {
	$charitable_funds_font_size = get_theme_mod( 'charitable_funds_menu_font_size', 12 );

	$charitable_funds_custom_css = "
		.main-navigation a {
			font-size: {$charitable_funds_font_size}px;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_menu_font_size_css' );

//Menu Font Weight
function charitable_funds_menu_font_weight_css() {
	$charitable_funds_font_weight = get_theme_mod( 'charitable_funds_menu_font_weight', 400 );

	$charitable_funds_custom_css = "
		.main-navigation a {
		    font-weight: {$charitable_funds_font_weight};
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_menu_font_weight_css' );

//Menu text transform
function charitable_funds_menu_text_transform_css() {
	$charitable_funds_text_transform = get_theme_mod( 'charitable_funds_menu_text_transform', 'capitalize' );

	$charitable_funds_custom_css = "
		.main-navigation a {
			text-transform: {$charitable_funds_text_transform};
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_menu_text_transform_css' );

// Featured Image Dimension
function charitable_funds_custom_featured_image_css() {
    $charitable_funds_dimension = get_theme_mod('charitable_funds_blog_post_featured_image_dimension', 'default');
    $charitable_funds_width = get_theme_mod('charitable_funds_blog_post_featured_image_custom_width', '');
    $charitable_funds_height = get_theme_mod('charitable_funds_blog_post_featured_image_custom_height', '');
    
    if ($charitable_funds_dimension === 'custom' && $charitable_funds_width && $charitable_funds_height) {
        $charitable_funds_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$charitable_funds_width}px !important; height: {$charitable_funds_height}px !important; }";
        wp_add_inline_style('charitable-funds-style', $charitable_funds_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'charitable_funds_custom_featured_image_css');

// Featured Image Border Radius
function charitable_funds_featured_image_border_radius_css() {
	$charitable_funds_radius = get_theme_mod( 'charitable_funds_featured_image_border_radius', 10 );

	$charitable_funds_custom_css = "
		.mag-post-single img {
			border-radius: {$charitable_funds_radius}px;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_featured_image_border_radius_css' );

//Heading Font Size
function charitable_funds_sidebar_widget_font_size_css() {
	$charitable_funds_sidebar_widget_font_size = get_theme_mod( 'charitable_funds_sidebar_widget_font_size', 24 );

	$charitable_funds_custom_css = "
		h2.wp-block-heading,
		aside#secondary .widgettitle,
		aside#secondary .widget-title {
			font-size: {$charitable_funds_sidebar_widget_font_size}px;
		}
	";

	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_sidebar_widget_font_size_css' );

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

    if ( ! $charitable_funds_related_product_show_hide ) {
        $charitable_funds_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'charitable-funds-style', $charitable_funds_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_related_product_css' );

// Product Sale Badge Position
function charitable_funds_product_sale_position_customizer_css() {
    $charitable_funds_position = get_theme_mod('charitable_funds_product_sale_position', 'left');
    $charitable_funds_css = '.woocommerce ul.products li.product .onsale {';
    if ($charitable_funds_position === 'left') {
        $charitable_funds_css .= 'right: auto; left: 0px;';
    } else {
        $charitable_funds_css .= 'left: auto; right: 0px;';
    }
    $charitable_funds_css .= '}';
    wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
}
add_action('wp_enqueue_scripts', 'charitable_funds_product_sale_position_customizer_css');

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

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

// Footer Copyright Alignment
function charitable_funds_footer_copyright_alignment_css() {
    $charitable_funds_align = get_theme_mod('charitable_funds_footer_bottom_align', 'center');
    $charitable_funds_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$charitable_funds_align};
        }
        @media screen and (max-width: 575px) {
            .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
                justify-content: center;
                text-align: center;
            }
        }
    ";
    wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
}
add_action('wp_enqueue_scripts', 'charitable_funds_footer_copyright_alignment_css');

// Footer Copyright Font Size
function charitable_funds_copyright_font_size_css() {
    $charitable_funds_font_size = get_theme_mod('charitable_funds_copyright_font_size', 16);
    $charitable_funds_css = ".site-footer-bottom .site-info span {
        font-size: {$charitable_funds_font_size}px;
    }";
    wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
}
add_action('wp_enqueue_scripts', 'charitable_funds_copyright_font_size_css');

// Preloader Background Color
function charitable_funds_preloader_background_colors_css() {
    $charitable_funds_color = get_theme_mod('charitable_funds_preloader_background_color_setting', '');
    if (!empty($charitable_funds_color)) {
        $charitable_funds_css = "#loader {
            background-color: {$charitable_funds_color};
        }";
        wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
    }
}
add_action('wp_enqueue_scripts', 'charitable_funds_preloader_background_colors_css');

// Preloader Background Image
function charitable_funds_preloader_background_image_css() {
    $charitable_funds_image_url = get_theme_mod('charitable_funds_preloader_background_image_setting', '');
    if (!empty($charitable_funds_image_url)) {
        $charitable_funds_css = "#loader {
            background-image: url('{$charitable_funds_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
    }
}
add_action('wp_enqueue_scripts', 'charitable_funds_preloader_background_image_css');

// Footer Heading Alignment
function charitable_funds_footer_heading_alignment_css() {
    $charitable_funds_align = get_theme_mod('charitable_funds_footer_header_align', 'left');
    $charitable_funds_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$charitable_funds_align};
        }
    ";
    wp_add_inline_style('charitable-funds-style', $charitable_funds_css);
}
add_action('wp_enqueue_scripts', 'charitable_funds_footer_heading_alignment_css');

//First Capital Letter
function charitable_funds_show_first_caps() {
	$charitable_funds_first_caps = get_theme_mod('charitable_funds_show_first_caps', false);
	$charitable_funds_css = '';
	if ( $charitable_funds_first_caps ) {
	$charitable_funds_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 {
		$charitable_funds_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'charitable-funds-style', $charitable_funds_css );
}
add_action( 'wp_enqueue_scripts', 'charitable_funds_show_first_caps' );

// Topbar Padding Dynamic CSS
function charitable_funds_topbar_padding_css(){
	$padding = get_theme_mod('charitable_funds_topbar_padding');
	$custom_css = ".top-header-part{
		padding-top: {$padding}px !important;
		padding-bottom: {$padding}px !important;
	}";
	wp_add_inline_style('charitable-funds-style',$custom_css);

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

// Banner Button Color
function charitable_funds_banner_button_color_css() {
    $btn_color = get_theme_mod('charitable_funds_banner_btn_color');
	$btn_bg_color = get_theme_mod('charitable_funds_banner_btn_bg_color');

	$custom_css = '';
    if ( !empty($btn_color) ) {
		$custom_css .= "
		.banner-single .banner-btn a.asterthemes-button{
			color: {$btn_color};
		}";
	}

	if ( !empty($btn_bg_color) ) {
		$custom_css .= "
		.banner-single .banner-btn a.asterthemes-button{
			background-color: {$btn_bg_color};
		}";
	}

	wp_add_inline_style('charitable-funds-style', $custom_css);
}
add_action('wp_enqueue_scripts','charitable_funds_banner_button_color_css');

// WooCommerce Product Border Radius
function charitable_funds_woocommerce_border_radius_css() {
	$charitable_funds_radius = get_theme_mod('charitable_funds_woocommerce_product_border_radius', 3);
    $charitable_funds_custom_css = "
		.woocommerce ul.products li.product {
			border-radius: {$charitable_funds_radius}px;
			overflow: hidden;
		}
	";
    wp_add_inline_style('charitable-funds-style', $charitable_funds_custom_css);
}
add_action('wp_enqueue_scripts', 'charitable_funds_woocommerce_border_radius_css');
