<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet"
        integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.3.0/css/all.min.css"
        integrity="sha512-SzlrxWUlpfuzQ+pcUCosxcglQRNAq/DZjVsC0lE40xsADsfeQoEypE+enwcOiGjk/bSuGGKHEyjSoQ1zVisanQ=="
        crossorigin="anonymous" referrerpolicy="no-referrer" />
</head>
</html>
<?php
class Beruco_Theme_Styles {
   
   	public $beruco_options;
	private $exists_fonts = array();
	public static $beruco_gf_array = array();
   
    function __construct() {
		$this->beruco_options = get_option( 'beruco_options' );
    }

	function beruco_get_option($field){
		$beruco_options = $this->beruco_options;
		return isset( $beruco_options[$field] ) && $beruco_options[$field] != '' ? $beruco_options[$field] : '';
	}
	
	function beruco_dimension_settings($field, $property = 'width'){
		$beruco_options = $this->beruco_options;
		$units = 'px'; $dimension = '';
		if( isset( $beruco_options[$field] ) ){
			$units = isset( $beruco_options[$field]['units'] ) ? $beruco_options[$field]['units'] : $units;
			$dimension = isset( $beruco_options[$field][$property] ) && $beruco_options[$field][$property] != '' ? absint( $beruco_options[$field][$property] ) . $units : '';
		}
		return $dimension;
	}

	function beruco_image_settings($field){
		$beruco_options = $this->beruco_options;
		$img_arr = array(
			'id' => null,
			'url' => null
		);
		$image = isset( $beruco_options[$field] ) && isset( $beruco_options[$field]['image'] ) ? $beruco_options[$field]['image'] : '';
		if( !empty( $image ) ){
			$img_arr['id'] = isset( $image['id'] ) ? $image['id'] : null;
			$img_arr['url'] = isset( $image['url'] ) ? $image['url'] : null;
		}
		return $img_arr;
	}
	
	function beruco_border_settings($field, $class_names = null){
		$beruco_options = $this->beruco_options;

		if( isset( $beruco_options[$field] ) ):

			$stat = false;
			$position = array( 'top', 'right', 'bottom', 'left' );
			foreach( $position as $key ){
				if( isset( $beruco_options[$field][$key] ) && $beruco_options[$field][$key] != NULL && !$stat ) $stat = true;
			}
		
			$boder_style = isset( $beruco_options[$field]['style'] ) && $beruco_options[$field]['style'] != '' ? $beruco_options[$field]['style'] : '';
			$border_color = isset( $beruco_options[$field]['color'] ) && $beruco_options[$field]['color'] != '' ? $beruco_options[$field]['color'] : '';

			if( $class_names && $stat ) echo $class_names . ' {';
			
			if( isset( $beruco_options[$field]['top'] ) && $beruco_options[$field]['top'] != NULL ):
				echo 'border-top-width: '. $beruco_options[$field]['top'] .'px;';
				if( $boder_style ) echo 'border-top-style: '. $boder_style .';';
				if( $border_color ) echo 'border-top-color: '. $border_color .';';
			endif;
			
			if( isset( $beruco_options[$field]['right'] ) && $beruco_options[$field]['right'] != NULL ):
				echo 'border-right-width: '. $beruco_options[$field]['right'] .'px;';
				if( $boder_style ) echo 'border-right-style: '. $boder_style .';';
				if( $border_color ) echo 'border-right-color: '. $border_color .';';
			endif;
			
			if( isset( $beruco_options[$field]['bottom'] ) && $beruco_options[$field]['bottom'] != NULL ):
				echo 'border-bottom-width: '. $beruco_options[$field]['bottom'] .'px;';
				if( $boder_style ) echo 'border-bottom-style: '. $boder_style .';';
				if( $border_color ) echo 'border-bottom-color: '. $border_color .';';
			endif;
			
			if( isset( $beruco_options[$field]['left'] ) && $beruco_options[$field]['left'] != NULL ):
				echo 'border-left-width: '. $beruco_options[$field]['left'] .'px;';
				if( $boder_style ) echo 'border-left-style: '. $boder_style .';';
				if( $border_color ) echo 'border-left-color: '. $border_color .';';
			endif;

			if( $class_names && $stat ) echo '}';
			
		endif;
	}
	
	function beruco_padding_settings($field, $class_names = null){
		$beruco_options = $this->beruco_options;
		$stat = false;
		$position = array( 'top', 'right', 'bottom', 'left' );
		foreach( $position as $key ){
			if( isset( $beruco_options[$field][$key] ) && $beruco_options[$field][$key] != NULL && !$stat ) $stat = true;
		}
		if( isset( $beruco_options[$field] ) ):
			if( $class_names && $stat ) echo $class_names . ' {';	
			echo isset( $beruco_options[$field]['top'] ) && $beruco_options[$field]['top'] != NULL ? 'padding-top: '. $beruco_options[$field]['top'] .'px;' : '';
			echo isset( $beruco_options[$field]['right'] ) && $beruco_options[$field]['right'] != NULL ? 'padding-right: '. $beruco_options[$field]['right'] .'px;' : '';
			echo isset( $beruco_options[$field]['bottom'] ) && $beruco_options[$field]['bottom'] != NULL ? 'padding-bottom: '. $beruco_options[$field]['bottom'] .'px;' : '';
			echo isset( $beruco_options[$field]['left'] ) && $beruco_options[$field]['left'] != NULL ? 'padding-left: '. $beruco_options[$field]['left'] .'px;' : '';
			if( $class_names && $stat ) echo '}';
		endif;
	}
	
	function beruco_margin_settings($field, $class_names = null){
		$beruco_options = $this->beruco_options;
		$stat = false;
		$position = array( 'top', 'right', 'bottom', 'left' );
		foreach( $position as $key ){
			if( isset( $beruco_options[$field][$key] ) && $beruco_options[$field][$key] != NULL && !$stat ) $stat = true;
		}
		if( isset( $beruco_options[$field] ) ):	
			if( $class_names && $stat ) echo $class_names . ' {';	
			echo isset( $beruco_options[$field]['top'] ) && $beruco_options[$field]['top'] != NULL ? 'margin-top: '. $beruco_options[$field]['top'] .'px;' : '';
			echo isset( $beruco_options[$field]['right'] ) && $beruco_options[$field]['right'] != NULL ? 'margin-right: '. $beruco_options[$field]['right'] .'px;' : '';
			echo isset( $beruco_options[$field]['bottom'] ) && $beruco_options[$field]['bottom'] != NULL ? 'margin-bottom: '. $beruco_options[$field]['bottom'] .'px;' : '';
			echo isset( $beruco_options[$field]['left'] ) && $beruco_options[$field]['left'] != NULL ? 'margin-left: '. $beruco_options[$field]['left'] .'px;' : '';
			if( $class_names && $stat ) echo '}';
		endif;
	}

	function beruco_color($field, $class_names = null){
		$beruco_options = $this->beruco_options;
		if( isset( $beruco_options[$field] ) && $beruco_options[$field] != '' ) {
			if( $class_names ) echo $class_names . '{';
			echo 'color: '. $beruco_options[$field] .';';
			if( $class_names ) echo '}';
		}
	}
	
	function beruco_link_color($field, $fun, $class_names = null){
		$beruco_options = $this->beruco_options;
		if( isset( $beruco_options[$field][$fun] ) && $beruco_options[$field][$fun] != '' ) {
			if( $class_names ) echo $class_names . '{';
			echo 'color: '. $beruco_options[$field][$fun] .';';
			if( $class_names ) echo '}';
		}
	}
	
	function beruco_button_color($field, $fun, $class_names = null){
		$beruco_options = $this->beruco_options;
		if( isset( $beruco_options[$field][$fun] ) && $beruco_options[$field][$fun] != '' ) {
			if( $class_names ) echo $class_names . '{';
				switch( $fun ){
					case "hfore":
					case "fore":
						echo 'color: '. $beruco_options[$field][$fun] .';';
					break;
					case "hbg":
					case "bg":
						echo 'background-color: '. $beruco_options[$field][$fun] .';';
					break;
					case "hborder":
					case "border":
						echo 'border-color: '. $beruco_options[$field][$fun] .';';
					break;
				}
			if( $class_names ) echo '}';
		}
	}
		
	function beruco_bg_settings($field, $class_names = null){
		$beruco_options = $this->beruco_options;
		if( isset( $beruco_options[$field] ) ):

			$stat = false;
			$keys = array( 'bg_color', 'bg_repeat', 'bg_position', 'bg_size', 'bg_attachment' );
			foreach( $keys as $key ){
				if( isset( $beruco_options[$field][$key] ) && !empty( $beruco_options[$field][$key] ) && !$stat ) $stat = true;
			}
			if( isset( $beruco_options[$field]['image']['url'] ) && !empty( $beruco_options[$field]['image']['url'] ) && !$stat ) $stat = true;

			if( $class_names && $stat ) echo $class_names . '{';
			echo '
			'. ( isset( $beruco_options[$field]['bg_color'] ) && !empty( $beruco_options[$field]['bg_color'] ) ?  'background-color: '. $beruco_options[$field]['bg_color'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['image']['url'] ) && !empty( $beruco_options[$field]['image']['url'] ) ?  'background-image: url('. $beruco_options[$field]['image']['url'] .');' : '' ) .'
			'. ( isset( $beruco_options[$field]['bg_repeat'] ) && !empty( $beruco_options[$field]['bg_repeat'] ) ?  'background-repeat: '. $beruco_options[$field]['bg_repeat'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['bg_position'] ) && !empty( $beruco_options[$field]['bg_position'] ) ?  'background-position: '. $beruco_options[$field]['bg_position'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['bg_size'] ) && !empty( $beruco_options[$field]['bg_size'] ) ?  'background-size: '. $beruco_options[$field]['bg_size'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['bg_attachment'] ) && !empty( $beruco_options[$field]['bg_attachment'] ) ?  'background-attachment: '. $beruco_options[$field]['bg_attachment'] .';' : '' ) .'
			';
			if( $class_names && $stat ) echo '}';
		endif;
	}
	
	function beruco_custom_font_face_create( $font_family, $font_slug, $cf_names ){	
		$upload_dir = wp_upload_dir();
		$f_type = array('eot', 'otf', 'svg', 'ttf', 'woff');		
		$font_path = $upload_dir['baseurl'] . '/custom-fonts/' . str_replace( "'", "", $font_family .'/'. $font_slug );
		echo ' @font-face { font-family: '. $font_family .';';
		echo " src: url('". esc_url( $font_path ) .".eot') format('embedded-opentype'), url('". esc_url( $font_path ) .".woff2') format('woff2'), url('". esc_url( $font_path ) .".woff') format('woff'), url('". esc_url( $font_path ) .".ttf')  format('truetype'), url('". esc_url( $font_path ) .".svg') format('svg');}";		
	}
	
	function beruco_custom_font_check($field){
		$beruco_options = $this->beruco_options;
		$cf_names = get_option( 'beruco_custom_fonts' );
		$font_family = isset( $beruco_options[$field]['font_family'] ) ? $beruco_options[$field]['font_family'] : '';
		$font_slug = $font_family ? sanitize_title( $font_family ) : '';
		if ( !empty( $cf_names ) && is_array( $cf_names ) && array_key_exists( $font_slug, $cf_names ) ){	
			if ( !empty( $cf_names ) && !in_array( $font_slug, $this->exists_fonts ) ){
				$this->beruco_custom_font_face_create( $font_family, $font_slug, $cf_names );
				array_push( $this->exists_fonts, $beruco_options[$field]['font-family'] );
				return 1;
			}
		}
		return 0;
	}
	
	function beruco_get_custom_google_font_frame( $font_family ){	
		$family = isset( $font_family['family'] ) ? $font_family['family'] : '';
		$weight = isset( $font_family['weight'] ) ? $font_family['weight'] : '';
		$subset = isset( $font_family['subset'] ) ? $font_family['subset'] : '';		
		if( !empty( $family ) ){
			if( isset( self::$beruco_gf_array[$family] ) ){
				array_push( self::$beruco_gf_array[$family]['weight'], $weight );
				array_push( self::$beruco_gf_array[$family]['subset'], $subset );
			}else{
				self::$beruco_gf_array[$family] = array( 'weight' => array( $weight ), 'subset' => array( $subset ) );
			}
		}
	}
	
	function beruco_typo_generate($field){
		$beruco_options = $this->beruco_options;
		$font_family = isset( $beruco_options[$field]['font_family'] ) ? $beruco_options[$field]['font_family'] : '';
		$standard_fonts = Beruco_Google_Fonts_Function::$_standard_fonts;
		if( !array_key_exists( $font_family, $standard_fonts ) ){			
			$font_weight = isset( $beruco_options[$field]['font_weight'] ) && $beruco_options[$field]['font_weight'] != '' ? $beruco_options[$field]['font_weight'] : '';
			$font_sub = isset( $beruco_options[$field]['font_sub'] ) && $beruco_options[$field]['font_sub'] != '' ? $beruco_options[$field]['font_sub'] : '';
			$gf_arr = array( 'family' => $font_family, 'weight' => $font_weight, 'subset' => $font_sub );	
			$this->beruco_get_custom_google_font_frame( $gf_arr );
		}
	}
	
	function beruco_typo_settings($field, $class_names = null){
		
		//Custom font check and google font generate
		$cf_stat = $this->beruco_custom_font_check($field);
		if( !$cf_stat ) $this->beruco_typo_generate($field);		
		$beruco_options = $this->beruco_options;
		if( isset( $beruco_options[$field] ) ):

			$stat = false;
			$keys = array( 'font_color', 'font_family', 'font_weight', 'font_style', 'font_size', 'line_height', 'letter_spacing', 'text_align', 'text_transform' );
			foreach( $keys as $key ){
				if( isset( $beruco_options[$field][$key] ) && !empty( $beruco_options[$field][$key] ) && !$stat ) $stat = true;
			}
			echo $class_names && $stat ? esc_attr( $class_names ) . '{' : '';
			
			$font_weight = isset( $beruco_options[$field]['font_weight'] ) ? $beruco_options[$field]['font_weight'] : '';
			$font_style = '';
			if( !empty( $font_weight ) && strpos( $font_weight, 'italic' ) ){
				$font_style = 'italic';
				$font_weight = str_replace( 'italic', '', $font_weight );
			}

			echo '
			'. ( isset( $beruco_options[$field]['font_color'] ) && $beruco_options[$field]['font_color'] != '' ?  'color: '. $beruco_options[$field]['font_color'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['font_family'] ) && $beruco_options[$field]['font_family'] != '' ?  'font-family: '. stripslashes_deep( $beruco_options[$field]['font_family'] ) .';' : '' ) .'
			'. ( $font_weight ?  'font-weight: '. $font_weight .';' : '' ) .'
			'. ( $font_style ?  'font-style: '. $font_style .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['font_size'] ) && $beruco_options[$field]['font_size'] != '' ?  'font-size: '. $beruco_options[$field]['font_size'] .'px;' : '' ) .'
			'. ( isset( $beruco_options[$field]['line_height'] ) && $beruco_options[$field]['line_height'] != '' ?  'line-height: '. $beruco_options[$field]['line_height'] .'px;' : '' ) .'
			'. ( isset( $beruco_options[$field]['letter_spacing'] ) && $beruco_options[$field]['letter_spacing'] != '' ?  'letter-spacing: '. $beruco_options[$field]['letter_spacing'] .'px;' : '' ) .'
			'. ( isset( $beruco_options[$field]['text_align'] ) && $beruco_options[$field]['text_align'] != '' ?  'text-align: '. $beruco_options[$field]['text_align'] .';' : '' ) .'
			'. ( isset( $beruco_options[$field]['text_transform'] ) && $beruco_options[$field]['text_transform'] != '' ?  'text-transform: '. $beruco_options[$field]['text_transform'] .';' : '' ) .'
			';
		endif;
		echo $class_names && $stat ? '}' : '';
	}
	
	function beruco_hex2rgba($color, $opacity = 1) {
	 
		$default = '';
		//Return default if no color provided
		if(empty($color))
			  return $default; 
		//Sanitize $color if "#" is provided 
			if ($color[0] == '#' ) {
				$color = substr( $color, 1 );
			}
			//Check if color has 6 or 3 characters and get values
			if (strlen($color) == 6) {
					$hex = array( $color[0] . $color[1], $color[2] . $color[3], $color[4] . $color[5] );
			} elseif ( strlen( $color ) == 3 ) {
					$hex = array( $color[0] . $color[0], $color[1] . $color[1], $color[2] . $color[2] );
			} else {
					return $default;
			}
			//Convert hexadec to rgb
			$rgb =  array_map('hexdec', $hex);
	 
			//Check if opacity is set(rgba or rgb)
			if( $opacity == 'none' ){
				$output = implode(",",$rgb);
			}elseif( $opacity ){
				if(abs($opacity) > 1)
					$opacity = 1.0;
				$output = 'rgba('.implode(",",$rgb).','.$opacity.')';
			}else {
				$output = 'rgb('.implode(",",$rgb).')';
			}
			//Return rgb(a) color string
			return $output;
	}

}