<?php
/**
 * Feminine Lifestyle Blog Dynamic Styles
 * 
 * @package Feminine_Lifestyle_Blog
*/

function feminine_lifestyle_blog_dynamic_css(){
    
    $site_title_color = get_theme_mod( 'site_title_color', '#222222' );

    $primary_color    = '#ff6030';
    $secondary_color  = '#222222';
    $body_font_color  = get_theme_mod( 'body_font_color', '#222222' );
    $bg_default_color = get_theme_mod( 'background_color', 'ffffff' );
    $background_color = '#' . $bg_default_color;

    $site_logo_size   = get_theme_mod( 'site_logo_size', 200 );

    $rgb  = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) );
    $rgb1 = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $secondary_color ) );
    $rgb2 = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $body_font_color ) );
    $rgb3 = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $background_color ) );
     
    echo "<style type='text/css' media='all'>"; ?>

    :root {
	    --primary-color: <?php echo feminine_lifestyle_blog_sanitize_hex_color( $primary_color ); ?>;
	    --secondary-color: <?php echo feminine_lifestyle_blog_sanitize_hex_color( $secondary_color ); ?>;
	    --font-color: <?php echo feminine_lifestyle_blog_sanitize_hex_color( $body_font_color ); ?>;
	    --background-color: <?php echo feminine_lifestyle_blog_sanitize_hex_color( $background_color ); ?>;
	    --primary-color-rgb: <?php printf('%1$s,%2$s,%3$s', $rgb[0], $rgb[1], $rgb[2] ); ?>;
	    --secondary-color-rgb: <?php printf('%1$s,%2$s,%3$s', $rgb1[0], $rgb1[1], $rgb1[2] ); ?>;
	    --font-color-rgb: <?php printf('%1$s,%2$s,%3$s', $rgb2[0], $rgb2[1], $rgb2[2] ); ?>;
	    --background-color-rgb: <?php printf('%1$s,%2$s,%3$s', $rgb3[0], $rgb3[1], $rgb3[2] ); ?>;
	}
    
    .site-branding .site-title-wrap .site-title a{
		color: <?php echo feminine_lifestyle_blog_sanitize_hex_color( $site_title_color ); ?>;
	}
    
    .custom-logo-link img{
	    width: <?php echo absint( $site_logo_size ); ?>px;
	    max-width: 100%;
	}

    .comment-body .reply .comment-reply-link:hover:before {
		background-image: url('data:image/svg+xml; utf-8, <svg xmlns="http://www.w3.org/2000/svg" width="18" height="15" viewBox="0 0 18 15"><path d="M934,147.2a11.941,11.941,0,0,1,7.5,3.7,16.063,16.063,0,0,1,3.5,7.3c-2.4-3.4-6.1-5.1-11-5.1v4.1l-7-7,7-7Z" transform="translate(-927 -143.2)" fill="<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) ); ?>"/></svg>');
	}

	.site-header.style-five .header-mid .search-form .search-submit:hover {
		background-image: url('data:image/svg+xml; utf-8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) ); ?>" d="M508.5 468.9L387.1 347.5c-2.3-2.3-5.3-3.5-8.5-3.5h-13.2c31.5-36.5 50.6-84 50.6-136C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c52 0 99.5-19.1 136-50.6v13.2c0 3.2 1.3 6.2 3.5 8.5l121.4 121.4c4.7 4.7 12.3 4.7 17 0l22.6-22.6c4.7-4.7 4.7-12.3 0-17zM208 368c-88.4 0-160-71.6-160-160S119.6 48 208 48s160 71.6 160 160-71.6 160-160 160z"></path></svg>');
	}

	.site-header.style-seven .header-bottom .search-form .search-submit:hover {
		background-image: url('data:image/svg+xml; utf-8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) ); ?>" d="M508.5 468.9L387.1 347.5c-2.3-2.3-5.3-3.5-8.5-3.5h-13.2c31.5-36.5 50.6-84 50.6-136C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c52 0 99.5-19.1 136-50.6v13.2c0 3.2 1.3 6.2 3.5 8.5l121.4 121.4c4.7 4.7 12.3 4.7 17 0l22.6-22.6c4.7-4.7 4.7-12.3 0-17zM208 368c-88.4 0-160-71.6-160-160S119.6 48 208 48s160 71.6 160 160-71.6 160-160 160z"></path></svg>');
	}

	.site-header.style-fourteen .search-form .search-submit:hover {
		background-image: url('data:image/svg+xml; utf-8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) ); ?>" d="M508.5 468.9L387.1 347.5c-2.3-2.3-5.3-3.5-8.5-3.5h-13.2c31.5-36.5 50.6-84 50.6-136C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c52 0 99.5-19.1 136-50.6v13.2c0 3.2 1.3 6.2 3.5 8.5l121.4 121.4c4.7 4.7 12.3 4.7 17 0l22.6-22.6c4.7-4.7 4.7-12.3 0-17zM208 368c-88.4 0-160-71.6-160-160S119.6 48 208 48s160 71.6 160 160-71.6 160-160 160z"></path></svg>');
	}
    .main-navigation .nav-menu>li.menu-item-has-children>a:where(:not(:hover))::after {
        background-image: url("data:image/svg+xml,%3Csvg class='svg-inline--fa fa-caret-down fa-w-10' aria-hidden='true' data-prefix='fas' data-icon='caret-down' role='img' xmlns='http://www.w3.org/2000/svg' viewBox='0 0 320 512' data-fa-i2svg=''%3E%3Cpath fill='<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $body_font_color ) ); ?>' d='M31.3 192h257.3c17.8 0 26.7 21.5 14.1 34.1L174.1 354.8c-7.8 7.8-20.5 7.8-28.3 0L17.2 226.1C4.6 213.5 13.5 192 31.3 192z'%3E%3C/path%3E%3C/svg%3E");
    }

	.search-results .content-area > .page-header .search-submit:hover {
		background-image: url('data:image/svg+xml; utf-8, <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="<?php echo feminine_lifestyle_blog_hash_to_percent23( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) ); ?>" d="M508.5 468.9L387.1 347.5c-2.3-2.3-5.3-3.5-8.5-3.5h-13.2c31.5-36.5 50.6-84 50.6-136C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c52 0 99.5-19.1 136-50.6v13.2c0 3.2 1.3 6.2 3.5 8.5l121.4 121.4c4.7 4.7 12.3 4.7 17 0l22.6-22.6c4.7-4.7 4.7-12.3 0-17zM208 368c-88.4 0-160-71.6-160-160S119.6 48 208 48s160 71.6 160 160-71.6 160-160 160z"></path></svg>');
	}

   <?php echo "</style>";
}
add_action( 'wp_head', 'feminine_lifestyle_blog_dynamic_css', 99 );

/**
 * Function for sanitizing Hex color 
 */
function feminine_lifestyle_blog_sanitize_hex_color( $color ){
	if ( '' === $color )
		return '';

    // 3 or 6 hex digits, or the empty string.
	if ( preg_match('|^#([A-Fa-f0-9]{3}){1,2}$|', $color ) )
		return $color;
}

/**
 * convert hex to rgb
 * @link http://bavotasan.com/2011/convert-hex-color-to-rgb-using-php/
*/
function feminine_lifestyle_blog_hex2rgb($hex) {
   $hex = str_replace("#", "", $hex);

   if(strlen($hex) == 3) {
      $r = hexdec(substr($hex,0,1).substr($hex,0,1));
      $g = hexdec(substr($hex,1,1).substr($hex,1,1));
      $b = hexdec(substr($hex,2,1).substr($hex,2,1));
   } else {
      $r = hexdec(substr($hex,0,2));
      $g = hexdec(substr($hex,2,2));
      $b = hexdec(substr($hex,4,2));
   }
   $rgb = array($r, $g, $b);
   //return implode(",", $rgb); // returns the rgb values separated by commas
   return $rgb; // returns an array with the rgb values
}

/**
 * Convert '#' to '%23'
*/
function feminine_lifestyle_blog_hash_to_percent23( $color_code ){
    $color_code = str_replace( "#", "%23", $color_code );
    return $color_code;
}

if ( ! function_exists( 'feminine_lifestyle_blog_gutenberg_inline_style' ) ) : 
/**
 * Gutenberg Dynamic Style
 */
function feminine_lifestyle_blog_gutenberg_inline_style(){
 
    $primary_color = '#ff6030';
    $secondary_color = '#222222';
            
    $rgb  = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) );
    $rgb2  = feminine_lifestyle_blog_hex2rgb( feminine_lifestyle_blog_sanitize_hex_color( $secondary_color ) );

    $custom_css = ':root .block-editor-page {
        --primary-color: ' . feminine_lifestyle_blog_sanitize_hex_color( $primary_color ) . ';
        --primary-color-rgb: ' . sprintf( '%1$s, %2$s, %3$s', $rgb[0], $rgb[1], $rgb[2] ) . ';
        --secondary-color: ' . feminine_lifestyle_blog_sanitize_hex_color( $secondary_color ) . ';
        --secondary-color-rgb: ' . sprintf( '%1$s, %2$s, %3$s', $rgb2[0], $rgb2[1], $rgb2[2] ) . ';
    }';

    return $custom_css;
}
endif;
