<?php
/**
 * Renders dynamic CSS from customize.
 *
 * @since 1.0.0
 *
 * @package Glaze_Blog_Lite
 */

if ( ! function_exists( 'glaze_blog_lite_dynamic_style' ) ) {
	/**
	 * Function to load dynamic styles.
	 *
	 * @since  1.0.0
	 *
	 * @access public
	 */
	function glaze_blog_lite_dynamic_style() {

		$display_srcoll_top = glaze_blog_lite_get_option( 'display_scroll_top_one' );

		$enable_cursive_site_title = glaze_blog_lite_get_option( 'enable_cursive_site_title' );

		$site_identity_section_padding = glaze_blog_lite_get_option( 'site_identity_section_padding' );

		$enable_cursive_post_meta = glaze_blog_lite_get_option( 'enable_cursive_post_meta' );

		$carousel_height = glaze_blog_lite_get_option( 'carousel_height' );

		$css = '<style>';

		if ( false === $display_srcoll_top ) {
			$css .= '#glaze-toTop {';
			$css .= 'display: none !important;';
			$css .= '}';
		}

		if ( $enable_cursive_site_title ) {
			$css .= '.header-style-1 .site-title, .header-style-2 .site-title {';
			$css .= 'font-family: "Pacifico", cursive;';
			$css .= '}';
		}

		if ( $site_identity_section_padding ) {
			$css .= '@media (min-width: 1024px) {';
			$css .= '.header-style-1 .mid-header {';
			$css .= 'padding: ' . esc_attr( $site_identity_section_padding ) . 'px 0px;';
			$css .= '}';
			$css .= '}';
		}

		if ( $enable_cursive_post_meta ) {
			$css .= '.entry-cats ul li a, .entry-metas ul li.posted-by a, .author-box .author-name h3 {';
			$css .= 'font-family: "Pacifico", cursive;';
			$css .= '}';
		}

		if ( $carousel_height ) {
			$css .= '@media(min-width: 992px) {';
			$css .= '.banner-style-1 .post-thumb {';
			$css .= 'height: ' . esc_attr( $carousel_height ) . 'px;';
			$css .= '}';
			$css .= '}';
		}

		$css .= '</style>';

		echo glaze_blog_lite_minify_css( $css ); // phpcs:ignore
	}
}
add_action( 'wp_head', 'glaze_blog_lite_dynamic_style' );


if ( ! function_exists( 'glaze_blog_lite_minify_css' ) ) {
	/**
	 * Simple minification of CSS codes.
	 *
	 * @since 1.0.9
	 *
	 * @param string $css CSS codes.
	 * @return string
	 */
	function glaze_blog_lite_minify_css( $css ) {

		$css = preg_replace( '/\s+/', ' ', $css );
		$css = preg_replace( '/\/\*[^\!](.*?)\*\//', '', $css );
		$css = preg_replace( '/(,|:|;|\{|}) /', '$1', $css );
		$css = preg_replace( '/ (,|;|\{|})/', '$1', $css );
		$css = preg_replace( '/(:| )0\.([0-9]+)(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}.${2}${3}', $css );
		$css = preg_replace( '/(:| )(\.?)0(%|em|ex|px|in|cm|mm|pt|pc)/i', '${1}0', $css );

		return trim( (string) $css );
	}
}
