<!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 

/*
 * Beruco custom functions 
 */

class Beruco_Custom_Functions {
	
	private static $_instance = null;

	private static $beruco_options = null;
		
	public function __construct() {
		
		self::$beruco_options = Beruco_Theme_Option::$beruco_options;
						
		add_filter( 'single_template', array( $this, 'beruco_cea_cpt_custom_template' ), 99 );
		
		add_filter( 'taxonomy_template', array( $this, 'beruco_cea_cpt_custom_tax_template' ), 99 );
		
		add_action( 'save_post', array( $this, 'beruco_save_post_options' ), 10, 1 );
		
		add_action( 'wp_enqueue_scripts', array( $this, 'beruco_addon_register_scripts' ) );
		
		add_action( 'wp_body_open', array( $this, 'beruco_addon_wp_body_open' ), 10 );
		
		add_filter( 'excerpt_length', array( $this, 'beruco_custom_excerpt_length' ), 10 );
		
		add_filter( 'beruco_trigger_to_save_custom_styles', array( $this, 'beruco_trigger_to_save_custom_styles_fun' ), 10 );
		
		//Year shortcode
		add_shortcode( 'year', function($atts){ return date("Y"); } );
		//Copyright icon shortcode
		add_shortcode( 'copy', function($atts){ return '&copy;'; } );
		
		// Remove single cpt templates
		remove_filter( 'single_template', 'cea_cpt_custom_template', 10 );
		
	}
	
	function beruco_trigger_to_save_custom_styles_fun( $styles ){
		require_once BERUCO_ADDON_DIR . 'admin/extension/theme-options/googlefonts.php';
		require_once ( BERUCO_ADDON_DIR . 'admin/extension/theme-options/theme-options-css.php' );
		$custom_css = Beruco_Theme_Option::beruco_minify_css( $styles );
		return $styles .' '. $custom_css;
	}
	
	function beruco_addon_register_scripts(){
		wp_enqueue_style( 'font-awesome', BERUCO_ADDON_URL . '/assets/css/themify-icons.css', array(), '4.7.0', 'all' );
	}
	
	function beruco_custom_excerpt_length( $length ){
		$beruco_options = self::$beruco_options;
		if( isset( $beruco_options['blog-post-excerpt-length'] ) && !empty( $beruco_options['blog-post-excerpt-length'] ) ) {
			return absint( $beruco_options['blog-post-excerpt-length'] );
		}
		return $length;
	}
	
	function beruco_addon_wp_body_open(){
		$beruco_options = self::$beruco_options;	
		$dark_light = isset( $beruco_options['dark-light'] ) ? $beruco_options['dark-light'] : false;
		if( $dark_light ): ?>
			<div class="dar-light-sticky">
				<div class="dar-light-inner">
					<span class="round-ball-switch"></span>
					<i class="bi bi-sun light-mode"></i>
					<i class="bi bi-moon-fill dark-mode"></i>
				</div>
			</div>
		<?php
		endif;
	}
	
	function beruco_save_post_options($post_id){
		
		if ( ! current_user_can( 'manage_options' ) ) {
			return $post_id;
		}
		if ( isset( $_POST['beruco_options'] ) ) {
			//update_post_meta( $post_id, 'beruco_post_custom_styles', 'This is custom post styles' );
			require_once ( BERUCO_ADDON_DIR . 'admin/extension/theme-options/post-options-css.php' );
		}
		
	}
	
	function beruco_cea_cpt_custom_template( $single ) {

		global $post;
				
		/* Checks for single template by post type */
		if( $post->post_type == 'cea-portfolio' || $post->post_type == 'cea-team' || $post->post_type == 'cea-event' || $post->post_type == 'cea-service' || $post->post_type == 'cea-testimonial' ) {
			if( file_exists( BERUCO_ADDON_DIR . '/classic-elementor-addons-pro/custom-singular.php' ) ) {
				return apply_filters( 'cea_portfolio_template_path', BERUCO_ADDON_DIR . '/classic-elementor-addons-pro/custom-singular.php' );
			}		
		}

		return $single;
	}
	
	function beruco_cea_cpt_custom_tax_template( $archive ){
		if( is_tax('portfolio-categories') || is_tax('portfolio-tags') || is_tax('service-categories') || is_tax('service-tags') ){
			require_once ( BERUCO_ADDON_DIR . 'inc/cpt.elements.php' );
			if( file_exists( BERUCO_ADDON_DIR . '/classic-elementor-addons-pro/custom-archive.php' ) ) {
				return BERUCO_ADDON_DIR . '/classic-elementor-addons-pro/custom-archive.php';
			}		
		}
	}

	public static function beruco_social_links() {
		$beruco_options = self::$beruco_options;		
		$social_links = isset( $beruco_options['social-links'] ) ? $beruco_options['social-links'] : '';
		$social_url = isset( $social_links['url'] ) ? $social_links['url'] : '';		
		$social_links = isset( $social_links['enabled'] ) && !empty( $social_links['enabled'] ) ? $social_links['enabled'] : '';
		$social_links = apply_filters( 'beruco_available_social_links', $social_links );		
		if( !empty( $social_links ) && is_array( $social_links ) ){
			wp_enqueue_style('font-awesome');
			$social_class = '';
			$social_class .= isset( $beruco_options['social-icons-layout'] ) ? ' social-' . $beruco_options['social-icons-layout'] : '';
			$social_class .= isset( $beruco_options['social-icons-fore'] ) ? ' social-' . $beruco_options['social-icons-fore'] : '';
			$social_class .= isset( $beruco_options['social-icons-hfore'] ) ? ' social-' . $beruco_options['social-icons-hfore'] : '';
			$social_class .= isset( $beruco_options['social-icons-bg'] ) ? ' social-' . $beruco_options['social-icons-bg'] : '';
			$social_class .= isset( $beruco_options['social-icons-hbg'] ) ? ' social-' . $beruco_options['social-icons-hbg'] : '';

			$target_window = isset( $beruco_options['social-icon-window'] ) ? $beruco_options['social-icon-window'] : ''; ?>

			<ul class="nav social-icons<?php echo esc_attr( $social_class ); ?>"<?php if( !empty( $target_window ) ) echo ' target="'. esc_attr( $target_window ) .'"'; ?>><?php
			foreach( $social_links as $key => $icon_class ){
				if( isset( $social_links[$key] ) ){
					$url = isset( $social_url[$key] ) ? $social_url[$key] : '#';
					echo '<li><a class="social-'. esc_attr( $key ) .'" href="'. esc_url( $url ) .'"><span class="'. esc_attr( $icon_class ) .'"></span></a>';
				}
			} ?>
			</ul><!-- .social-icons --><?php
		}
	}
	
	public static function beruco_social_share() { //Beruco_Wp_Elements::beruco_social_share()
		$beruco_options = self::$beruco_options;		
		$social_share = isset( $beruco_options['social-share'] ) ? $beruco_options['social-share'] : '';	
		$social_share = isset( $social_share['enabled'] ) && !empty( $social_share['enabled'] ) ? $social_share['enabled'] : '';
		$social_share = apply_filters( 'beruco_available_social_share', $social_share );		
		if( !empty( $social_share ) && is_array( $social_share ) ){
			$post_id = get_the_ID();
			$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post_id ), 'large' );
			$image_url = !empty( $image ) && isset( $image[0] ) ? $image[0] : '';
			$post_title = get_the_title();
			$site_name = get_bloginfo( 'name' );
			wp_enqueue_style('font-awesome');
			echo '<ul class="nav social-share">';
			foreach( $social_share as $key => $icon_class ){
				switch($key){
					case "facebook":
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="http://www.facebook.com/sharer.php?u=%s&t=%s" target="_blank" rel="nofollow" class="social-facebook share-fb"><i class="%s"></i></a></li>',
							esc_url( home_url( '/' ) ),
							urlencode( get_permalink( $post_id ) ),
							urlencode( $post_title ),
							esc_attr( $icon_class )
					 	);
					break;
					case "twitter":
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="https://twitter.com/share?hashtags=sharing&text=%s&via=%s - %s" class="social-twitter share-twitter" title="%s" target="_blank" rel="nofollow"><i class="%s"></i></a></li>', 
							esc_url( home_url( '/' ) ),
							urlencode( $post_title ),
							urlencode( $site_name ),
							esc_url( get_permalink( $post_id ) ),
							esc_html__( 'Click to send this page to Twitter!', 'beruco-addon' ),
							esc_attr( $icon_class )
						);
					break;
					case "linkedin":						
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="http://www.linkedin.com/shareArticle?mini=true&url=%s&title=%s&summary=&source=%s" class="social-linkedin share-linkedin" target="_blank" rel="nofollow"><i class="%s"></i></a></li>',
							esc_url( home_url( '/' ) ),
							esc_url( get_permalink( $post_id ) ),
							urlencode( $post_title ),
							get_bloginfo('name'),
							esc_attr( $icon_class )	
						);
					break;
					case "instagram":
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="https://www.instagram.com/?url=%s" class="social-instagram share-instagram" target="_blank" rel="nofollow"><i class="%s"></i></a></li>',
							esc_url( home_url( '/' ) ),
							esc_url( get_permalink( $post_id ) ),
							esc_attr( $icon_class )	
						);
					break;
					case "pinterest":
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="http://pinterest.com/pin/create/button/?url=%s&amp;media=%s&description=%s" class="social-pinterest share-pinterest" target="_blank" rel="nofollow"><i class="%s"></i></a></li>',
							esc_url( home_url( '/' ) ),
							esc_url( get_permalink( $post_id ) ),
							esc_url( $image_url ),
							urlencode( $post_title ),
							esc_attr( $icon_class )
						);
					break;
					case "whatsapp":
						echo sprintf( 
							'<li class="nav-item"><a href="%s" data-href="https://api.whatsapp.com/send?text=%s %s" target="_blank" rel="nofollow" class="social-whatsapp share-whatsapp"><i class="%s"></i></a></li>',
							esc_url( home_url( '/' ) ),
							urlencode( $post_title ),
							urlencode( get_permalink( $post_id ) ),
							esc_attr( $icon_class )
						);
					break;
				}
			}
			echo '</ul><!-- .social-share -->';
		}
	}

    public static function instance() {
		if ( is_null( self::$_instance ) ) {
			self::$_instance = new self();
		}
		return self::$_instance;
	}

}
Beruco_Custom_Functions::instance();