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


// Output inline CSS based on Customizer setting

// Breadcrumb - Enable Breadcrumb.
function aster_portfolio_customizer_css_inline() {
	$aster_portfolio_custom_css = '';
	$aster_portfolio_enable_breadcrumb = get_theme_mod( 'aster_portfolio_enable_breadcrumb', true );

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

	if ( ! empty( $aster_portfolio_custom_css ) ) {
		wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
	}
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_customizer_css_inline' );

//Global Color
function aster_portfolio_customize_css() {
	$aster_portfolio_primary_color = get_theme_mod( 'aster_portfolio_primary_color', '#16BBBD' );
	$aster_portfolio_custom_css = ":root { --primary-color: {$aster_portfolio_primary_color}; }";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_customize_css' );

//Google Fonts
function aster_portfolio_enqueue_selected_fonts() {
    $aster_portfolio_fonts_url = aster_portfolio_get_fonts_url();
    if (!empty($aster_portfolio_fonts_url)) {
        wp_enqueue_style('aster-portfolio-google-fonts', $aster_portfolio_fonts_url, array(), null);
    }
}
add_action('wp_enqueue_scripts', 'aster_portfolio_enqueue_selected_fonts');

//Set Width
function aster_portfolio_layout_customizer_css() {
	$aster_portfolio_margin = get_theme_mod( 'aster_portfolio_layout_width_margin', 50 );
	$aster_portfolio_custom_css = "body.site-boxed--layout #page { margin: 0 {$aster_portfolio_margin}px; }";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_layout_customizer_css', 20 );

//Blog Content Alignment
function aster_portfolio_blog_layout_customizer_css() {
	// Retrieve the blog layout option
	$aster_portfolio_layout_option = get_theme_mod( 'aster_portfolio_blog_layout_option_setting', 'Left' );

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

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

	// Attach CSS to theme style
	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_blog_layout_customizer_css' );

//Sidebar Width Setting
function aster_portfolio_sidebar_width_customizer_css() {
	$aster_portfolio_sidebar_width = get_theme_mod( 'aster_portfolio_sidebar_width', '30' );

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

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_sidebar_width_customizer_css' );


if ( ! function_exists( 'aster_portfolio_get_page_title' ) ) {
    function aster_portfolio_get_page_title() {
        $aster_portfolio_title = '';

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

        return apply_filters('aster_portfolio_page_title', $aster_portfolio_title);
    }
}

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

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

        // Apply filters and return
        return apply_filters('aster_portfolio_display_page_header', $aster_portfolio_return);
    }
}

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

//Page Title Options
function aster_portfolio_page_title_customizer_css() {
	$aster_portfolio_layout_option = get_theme_mod( 'aster_portfolio_page_header_layout', 'left' );
	$aster_portfolio_custom_css = '';

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

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_page_title_customizer_css' );

//Set Height
function aster_portfolio_pagetitle_height_css() {
	$aster_portfolio_height = get_theme_mod( 'aster_portfolio_pagetitle_height', 50 );

	$aster_portfolio_custom_css = "
		header.page-header {
			padding: {$aster_portfolio_height}px 0;
		}
	";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_pagetitle_height_css' );

//Adjust Site Logo Width
function aster_portfolio_site_logo_width() {
	$aster_portfolio_logo_width = get_theme_mod( 'aster_portfolio_site_logo_width', 85 );

	$aster_portfolio_custom_css = "
		.site-logo img {
			max-width: {$aster_portfolio_logo_width}px;
		}
	";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_site_logo_width' );

//Menu Font Size
function aster_portfolio_menu_font_size_css() {
	$aster_portfolio_font_size = get_theme_mod( 'aster_portfolio_menu_font_size', 14 );

	$aster_portfolio_custom_css = "
		.main-navigation a {
			font-size: {$aster_portfolio_font_size}px;
		}
	";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_menu_font_size_css' );

//Menu Font Weight
function aster_portfolio_menu_font_weight_css() {
	$aster_portfolio_font_weight = get_theme_mod( 'aster_portfolio_menu_font_weight', 600 );

	$aster_portfolio_custom_css = "
		.main-navigation a {
		    font-weight: {$aster_portfolio_font_weight};
		}
	";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_menu_font_weight_css' );

//Menu text transform
function aster_portfolio_menu_text_transform_css() {
	$aster_portfolio_text_transform = get_theme_mod( 'aster_portfolio_menu_text_transform', 'capitalize' );

	$aster_portfolio_custom_css = "
		.main-navigation a {
			text-transform: {$aster_portfolio_text_transform};
		}
	";

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_menu_text_transform_css' );

// Featured Image Dimension
function aster_portfolio_custom_featured_image_css() {
    $aster_portfolio_dimension = get_theme_mod('aster_portfolio_blog_post_featured_image_dimension', 'default');
    $aster_portfolio_width = get_theme_mod('aster_portfolio_blog_post_featured_image_custom_width', '');
    $aster_portfolio_height = get_theme_mod('aster_portfolio_blog_post_featured_image_custom_height', '');
    
    if ($aster_portfolio_dimension === 'custom' && $aster_portfolio_width && $aster_portfolio_height) {
        $aster_portfolio_custom_css = "body:not(.single-post) .mag-post-single .mag-post-img img { width: {$aster_portfolio_width}px !important; height: {$aster_portfolio_height}px !important; }";
        wp_add_inline_style('aster-portfolio-style', $aster_portfolio_custom_css);
    }
}
add_action('wp_enqueue_scripts', 'aster_portfolio_custom_featured_image_css');

// Featured Image Border Radius
function aster_portfolio_featured_image_border_radius_css() {
	$aster_portfolio_radius = get_theme_mod( 'aster_portfolio_featured_image_border_radius', 10 );

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

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_featured_image_border_radius_css' );

//Heading Font Size
function aster_portfolio_sidebar_widget_font_size_css() {
	$aster_portfolio_sidebar_widget_font_size = get_theme_mod( 'aster_portfolio_sidebar_widget_font_size', 24 );

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

	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_sidebar_widget_font_size_css' );

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

    if ( ! $aster_portfolio_related_product_show_hide ) {
        $aster_portfolio_custom_css = '
            .related.products {
                display: none;
            }
        ';
        wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_custom_css );
    }
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_related_product_css' );

// Product Sale Badge Position
function aster_portfolio_product_sale_position_customizer_css() {
    $aster_portfolio_position = get_theme_mod('aster_portfolio_product_sale_position', 'left');
    $aster_portfolio_css = '.woocommerce ul.products li.product .onsale {';
    if ($aster_portfolio_position === 'left') {
        $aster_portfolio_css .= 'right: auto; left: 0px;';
    } else {
        $aster_portfolio_css .= 'left: auto; right: 0px;';
    }
    $aster_portfolio_css .= '}';
    wp_add_inline_style('aster-portfolio-style', $aster_portfolio_css);
}
add_action('wp_enqueue_scripts', 'aster_portfolio_product_sale_position_customizer_css');

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

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

// Footer Copyright Alignment
function aster_portfolio_footer_copyright_alignment_css() {
    $aster_portfolio_align = get_theme_mod('aster_portfolio_footer_bottom_align', 'center');
    $aster_portfolio_css = "
        .site-footer .site-footer-bottom .site-footer-bottom-wrapper {
            justify-content: {$aster_portfolio_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('aster-portfolio-style', $aster_portfolio_css);
}
add_action('wp_enqueue_scripts', 'aster_portfolio_footer_copyright_alignment_css');

// Footer Copyright Font Size
function aster_portfolio_copyright_font_size_css() {
    $aster_portfolio_font_size = get_theme_mod('aster_portfolio_copyright_font_size', 16);
    $aster_portfolio_css = ".site-footer-bottom .site-info span {
        font-size: {$aster_portfolio_font_size}px;
    }";
    wp_add_inline_style('aster-portfolio-style', $aster_portfolio_css);
}
add_action('wp_enqueue_scripts', 'aster_portfolio_copyright_font_size_css');

// Preloader Background Color
function aster_portfolio_preloader_background_colors_css() {
    $aster_portfolio_color = get_theme_mod('aster_portfolio_preloader_background_color_setting', '');
    if (!empty($aster_portfolio_color)) {
        $aster_portfolio_css = "#loader {
            background-color: {$aster_portfolio_color};
        }";
        wp_add_inline_style('aster-portfolio-style', $aster_portfolio_css);
    }
}
add_action('wp_enqueue_scripts', 'aster_portfolio_preloader_background_colors_css');

// Preloader Background Image
function aster_portfolio_preloader_background_image_css() {
    $aster_portfolio_image_url = get_theme_mod('aster_portfolio_preloader_background_image_setting', '');
    if (!empty($aster_portfolio_image_url)) {
        $aster_portfolio_css = "#loader {
            background-image: url('{$aster_portfolio_image_url}');
            background-size: cover;
            background-repeat: no-repeat;
            background-position: center;
        }";
        wp_add_inline_style('aster-portfolio-style', $aster_portfolio_css);
    }
}
add_action('wp_enqueue_scripts', 'aster_portfolio_preloader_background_image_css');

// Footer Heading Alignment
function aster_portfolio_footer_heading_alignment_css() {
    $aster_portfolio_align = get_theme_mod('aster_portfolio_footer_header_align', 'left');
    $aster_portfolio_css = "
        .site-footer h4,
        footer#colophon h2.wp-block-heading,
        footer#colophon .widgettitle,
        footer#colophon .widget-title {
            text-align: {$aster_portfolio_align};
        }
    ";
    wp_add_inline_style('aster-portfolio-style', $aster_portfolio_css);
}
add_action('wp_enqueue_scripts', 'aster_portfolio_footer_heading_alignment_css');

//First Capital Letter
function aster_portfolio_show_first_caps() {
	$aster_portfolio_first_caps = get_theme_mod('aster_portfolio_show_first_caps', false);
	$aster_portfolio_css = '';
	if ( $aster_portfolio_first_caps ) {
	$aster_portfolio_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 {
		$aster_portfolio_css .= '
		.mag-post-single .mag-post-detail .mag-post-excerpt p:first-of-type::first-letter {
			display: none;
		}';
	}
	wp_add_inline_style( 'aster-portfolio-style', $aster_portfolio_css );
}
add_action( 'wp_enqueue_scripts', 'aster_portfolio_show_first_caps' );

// WooCommerce Product Border Radius
function aster_portfolio_woocommerce_border_radius_css() {
	$aster_portfolio_radius = get_theme_mod('aster_portfolio_woocommerce_product_border_radius', 3);
    $aster_portfolio_custom_css = "
		.woocommerce ul.products li.product {
			border-radius: {$aster_portfolio_radius}px;
			overflow: hidden;
		}
	";
    wp_add_inline_style('aster-portfolio-style', $aster_portfolio_custom_css);
}
add_action('wp_enqueue_scripts', 'aster_portfolio_woocommerce_border_radius_css');

