<?php

/**
 * making array of custom icon classes
 * which is saved in transient
 * @return array
 */
if ( ! function_exists( 'ostech_get_fa_icons' ) ) :

	function ostech_get_fa_icons() {
		$data = get_transient( 'ostech_fa_icons' );

		if ( empty( $data ) ) {
			global $wp_filesystem;
			require_once ( ABSPATH . '/wp-admin/includes/file.php' );
			WP_Filesystem();

			$fontAwesome_file = OSTECH_ADDON_PATH . '/assets/vendors/fontawesome/css/all.min.css';
			$template_icon_file = OSTECH_ADDON_PATH . '/assets/vendors/ostech-icons/style.css';
			$content = '';

			if ( $wp_filesystem->exists( $fontAwesome_file ) ) {
				$content = $wp_filesystem->get_contents( $fontAwesome_file );
			} // End If Statement

			if ( $wp_filesystem->exists( $template_icon_file ) ) {
				$content .= $wp_filesystem->get_contents( $template_icon_file );
			} // End If Statement

			$pattern = '/\.(fa-(?:\w+(?:-)?)+):before\s*{\s*content/';
			$pattern_two = '/\.(icon-(?:\w+(?:-)?)+):before\s*{\s*content/';

			$subject = $content;

			preg_match_all( $pattern, $subject, $matches, PREG_SET_ORDER );
			preg_match_all( $pattern_two, $subject, $matches_two, PREG_SET_ORDER );

			$all_matches = array_merge( $matches, $matches_two );

			$icons = array();

			foreach ( $all_matches as $match ) {
				// $icons[] = array('value' => $match[1], 'label' => $match[1]);
				$icons[] = $match[1];
			}

			$data = apply_filters( 'ostech_elementor_icons_json', $icons );
			set_transient( 'ostech_fa_icons', $data, 10080 ); // saved for one week

		}

		return array_combine( $data, $data ); // combined for key = value
	}


endif;

if ( ! function_exists( 'ostech_get_page_by_title' ) ) {
	function ostech_get_page_by_title( $title, $post_type = 'page' ) {
		$posts = get_posts(
			array(
				'post_type' => $post_type,
				'title' => $title,
				'post_status' => 'all',
				'numberposts' => 1,
				'update_post_term_cache' => false,
				'update_post_meta_cache' => false,
				'orderby' => 'post_date ID',
				'order' => 'ASC',
			)
		);

		if ( ! empty( $posts ) ) {
			return $posts[0];
		} else {
			return null;
		}
	}
}

// custom kses allowed html
if ( ! function_exists( 'ostech_kses_allowed_html' ) ) :
	function ostech_kses_allowed_html( $tags, $context ) {
		switch ( $context ) {
			case 'ostech_allowed_tags':
				$tags = array(
					'a' => array( 'href' => array(), 'class' => array() ),
					'b' => array(),
					'br' => array(),
					'span' => array( 'class' => array(), 'data-count' => array() ),
					'img' => array( 'class' => array(), 'decoding' => array(), 'src' => array(), 'alt' => array(), 'title' => array() ),
					'i' => array( 'class' => array() ),
					'p' => array( 'class' => array() ),
					'ul' => array( 'class' => array() ),
					'li' => array( 'class' => array() ),
					'div' => array( 'class' => array() ),
					'strong' => array(),
					'sup' => array(),
				);
				return $tags;
			default:
				return $tags;
		}
	}

	add_filter( 'wp_kses_allowed_html', 'ostech_kses_allowed_html', 10, 2 );

endif;

if ( ! function_exists( 'ostech_excerpt' ) ) :

	// Post's excerpt text
	function ostech_excerpt( $get_limit_value, $echo = true ) {
		$opt = $get_limit_value;
		$excerpt_limit = ! empty( $opt ) ? $opt : 40;
		$excerpt = wp_trim_words( get_the_content(), $excerpt_limit, '' );
		if ( $echo == true ) {
			echo esc_html( $excerpt );
		} else {
			return esc_html( $excerpt );
		}
	}

endif;



if ( ! function_exists( 'ostech_post_query' ) ) {
	function ostech_post_query( $post_type ) {
		$post_list = get_posts( array(
			'post_type' => $post_type,
			'showposts' => -1,
		) );
		$posts = array();

		if ( ! empty( $post_list ) && ! is_wp_error( $post_list ) ) {
			foreach ( $post_list as $post ) {
				$options[ $post->ID ] = $post->post_title;
			}
			return $options;
		}
	}
}

if ( ! function_exists( 'ostech_custom_query_pagination' ) ) :
	/**
	 * Prints HTML with post pagination links.
	 */
	function ostech_custom_query_pagination( $paged = '', $max_page = '' ) {
		global $wp_query;
		$big = 999999999; // need an unlikely integer
		if ( ! $paged )
			$paged = get_query_var( 'paged' );
		if ( ! $max_page )
			$max_page = $wp_query->max_num_pages;

		$links = paginate_links( array(
			'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
			'format' => '?paged=%#%',
			'current' => max( 1, $paged ),
			'total' => $max_page,
			'mid_size' => 1,
			'prev_text' => '<i class="fa fa-angle-left"></i>',
			'next_text' => '<i class="fa fa-angle-right"></i>',
		) );

		echo wp_kses( $links, 'ostech_allowed_tags' );
	}
endif;

if ( ! function_exists( 'ostech_get_nav_menu' ) ) :
	function ostech_get_nav_menu() {
		$menu_list = get_terms( array(
			'taxonomy' => 'nav_menu',
			'hide_empty' => true,
		) );
		$options = [];
		if ( ! empty( $menu_list ) && ! is_wp_error( $menu_list ) ) {
			foreach ( $menu_list as $menu ) {
				$options[ $menu->slug ] = $menu->name;
			}
			return $options;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_taxonoy' ) ) :
	function ostech_get_taxonoy( $taxonoy ) {
		$taxonomy_list = get_terms( array(
			'taxonomy' => $taxonoy,
			'hide_empty' => true,
		) );
		$options = [];
		if ( ! empty( $taxonomy_list ) && ! is_wp_error( $taxonomy_list ) ) {
			foreach ( $taxonomy_list as $taxonomy ) {
				$options[ $taxonomy->slug ] = $taxonomy->name;
			}
			return $options;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_template' ) ) :
	function ostech_get_template( $template_name = null ) {
		$template_path = apply_filters( 'ostech-elementor/template-path', 'elementor-templates/' );
		$template = locate_template( $template_path . $template_name );
		if ( ! $template ) {
			$template = OSTECH_ADDON_PATH . '/elementor-templates/' . $template_name;
		}
		if ( file_exists( $template ) ) {
			return $template;
		} else {
			return false;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_sidebar_template' ) ) :
	function ostech_get_sidebar_template( $template_name = null ) {
		$template_path = apply_filters( 'ostech-sidebar/template-path', 'sidebar-widget-template/' );
		$template = locate_template( $template_path . $template_name );
		if ( ! $template ) {
			$template = OSTECH_ADDON_PATH . '/sidebar-widget-template/' . $template_name;
		}
		if ( file_exists( $template ) ) {
			return $template;
		} else {
			return false;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_elementor_option' ) ) :
	function ostech_get_elementor_option( $template_name = null ) {
		$template_path = apply_filters( 'ostech-elementor/template-options', 'elementor-options/' );
		$template = locate_template( $template_path . $template_name );
		if ( ! $template ) {
			$template = OSTECH_ADDON_PATH . '/elementor-options/' . $template_name;
		}
		if ( file_exists( $template ) ) {
			return $template;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_common_function' ) ) :
	function ostech_get_common_function( $template_name = null ) {
		$template_path = apply_filters( 'ostech-common/template-options', 'common/' );
		$template = locate_template( $template_path . $template_name );
		if ( ! $template ) {
			$template = OSTECH_ADDON_PATH . '/common/' . $template_name;
		}
		if ( file_exists( $template ) ) {
			return $template;
		}
	}
endif;

if ( ! function_exists( 'ostech_get_thumbnail_alt' ) ) :
	function ostech_get_thumbnail_alt( $thumbnail_id ) {
		return get_post_meta( $thumbnail_id, '_wp_attachment_image_alt', true );
	}
endif;


if ( ! function_exists( 'ostech_get_owl_options' ) ) :
	function ostech_get_owl_options( $settings ) {
		$loop_status = ( 'yes' == $settings['loop'] ) ? true : false;
		$nav_status = ( 'yes' == $settings['enable_nav'] ) ? true : false;
		$dots_status = ( 'yes' == $settings['enable_dots'] ) ? true : false;
		$autoplay_status = ( 'yes' == $settings['autoplay'] ) ? true : false;
		$hover_pause_status = ( 'yes' == $settings['hover_pause'] ) ? true : false;
		$center_status = ( 'yes' == $settings['center_status'] ) ? true : false;
		$url_hash_listner = ( 'yes' == $settings['url_hash_listner'] ) ? true : false;
		$delay = $settings['delay']['size'];
		$custom_animation_status = ( 'yes' == $settings['custom_animation_status'] ) ? true : false;
		$custom_animation_out = $settings['custom_animation_out'];
		$custom_animation_in = $settings['custom_animation_in'];
		if ( 'yes' == $settings['enable_nav'] ) {
			$nav_left_icon = $settings['nav_left_icon']['value'];
			$nav_right_icon = $settings['nav_right_icon']['value'];
		}
		$items = $settings['items']['size'];
		$margin = $settings['margin']['size'];
		$smart_speed = $settings['smart_speed']['size'];
		$breakpoint = $settings['breakpoint'];
		$owl_options = [ 
			"loop" => $loop_status,
			"margin" => $margin,
			"items" => $items,
			"nav" => $nav_status,
			"dots" => $dots_status,
			"smartSpeed" => $smart_speed,
		];

		if ( true === $autoplay_status ) :
			$owl_options["autoplay"] = $autoplay_status;
			$owl_options["autoplayTimeout"] = $delay;
		endif;

		if ( true === $custom_animation_status ) :
			$owl_options["animateOut"] = $custom_animation_out;
			$owl_options["animateIn"] = $custom_animation_in;
		endif;
		if ( true === $hover_pause_status ) :
			$owl_options["autoplayHoverPause"] = $hover_pause_status;
		endif;
		if ( true === $center_status ) :
			$owl_options["center"] = $center_status;
		endif;
		if ( true === $url_hash_listner ) :
			$owl_options["URLhashListener"] = $url_hash_listner;
		endif;

		if ( 'yes' == $settings['enable_nav'] ) :
			$owl_options["navText"] = [ 
				"<span class=\" $nav_left_icon \"></span>",
				"<span class=\" $nav_right_icon \"></span>"
			];
		endif;

		if ( ! empty( $breakpoint ) ) :
			$reponsive_options = [];
			foreach ( $breakpoint as $item ) :
				$reponsive_options[ $item['screen_size']['size'] ]["margin"] = $item['margin']['size'];
				$reponsive_options[ $item['screen_size']['size'] ]["items"] = $item['item']['size'];
			endforeach;
			$owl_options["responsive"] = $reponsive_options;
		endif;
		return json_encode( $owl_options );
	}
endif;

if ( ! function_exists( 'ostech_get_circle_options' ) ) :
	function ostech_get_circle_options( $arg, $condition = false ) {

		if ( ! empty( $condition ) ) :

			//Circle Options
			$arg->start_controls_section(
				'circle_options',
				[ 
					'label' => __( 'Circle Options', 'ostech-addon' ),
					'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
					'condition' => [ 
						'layout_type' => $condition
					]
				]
			);

		else :
			$arg->start_controls_section(
				'circle_options',
				[ 
					'label' => __( 'Circle Options', 'ostech-addon' ),
					'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
				]
			);
		endif;


		$arg->add_control(
			'enable_gradient_color',
			[ 
				'label' => __( 'Enable Gradient Color?', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SWITCHER,
				'label_on' => __( 'Yes', 'ostech-addon' ),
				'label_off' => __( 'No', 'ostech-addon' ),
				'return_value' => 'yes',
				'default' => 'no',
			]
		);

		$gradient_items_color = new \Elementor\Repeater();

		$gradient_items_color->add_control(
			'gradient_color',
			[ 
				'label' => esc_html__( 'Gradient Color', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::COLOR,
			]
		);

		$arg->add_control(
			'gradient_items_color',
			[ 
				'label' => __( 'Gradient Lists', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::REPEATER,
				'fields' => $gradient_items_color->get_controls(),
				'condition' => [ 
					'enable_gradient_color' => [ 'yes' ]
				]
			]
		);


		$arg->add_control(
			'fill_color',
			[ 
				'label' => esc_html__( 'Fill Color', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'default=' => '36731f',
				'condition' => [ 
					'enable_gradient_color!' => [ 'yes' ]
				]

			]
		);

		$arg->add_control(
			'empty_fill_color',
			[ 
				'label' => esc_html__( 'Empty Fill Color', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::COLOR,
				'default=' => '36731f'
			]
		);

		$arg->add_control(
			'thickness',
			[ 
				'label' => __( 'Thickness', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'count' ],
				'range' => [ 
					'count' => [ 
						'min' => 1,
						'max' => 14,
						'step' => 1,
					],
				],
				'default' => [ 
					'unit' => 'count',
					'size' => 3,
				],
			]
		);

		$arg->add_control(
			'size',
			[ 
				'label' => __( 'Size', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'count' ],
				'range' => [ 
					'count' => [ 
						'min' => 50,
						'max' => 200,
						'step' => 10,
					],
				],
				'default' => [ 
					'unit' => 'count',
					'size' => 110,
				],
			]
		);

		$arg->add_control(
			'angel',
			[ 
				'label' => __( 'Angel', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'size_units' => [ 'count' ],
				'range' => [ 
					'count' => [ 
						'min' => 0,
						'max' => 180,
						'step' => 1,
					],
				],
				'default' => [ 
					'unit' => 'count',
					'size' => 0,
				],
			]
		);

		$arg->add_control(
			'linecap',
			[ 
				'label' => __( 'Line Cap', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SELECT2,
				'default' => 'square',
				'options' => [ 
					'butt' => __( 'Butt', 'ostech-addon' ),
					'round' => __( 'Round', 'ostech-addon' ),
					'square' => __( 'Square', 'ostech-addon' ),
				]
			]
		);
		$arg->end_controls_section();
	}
endif;

if ( ! function_exists( 'ostech_apply_circle_options' ) ) :
	function ostech_apply_circle_options( $settings, $item ) {
		$value = $item['count_number']['size'] / 10;
		$decimal = $value / 10;
		$formatted_value = number_format( $decimal, 1, '.', '' );
		ob_start(); ?>
		data-options='{
		"value":
		<?php echo esc_attr( $formatted_value ); ?>,
		"thickness":
		<?php echo esc_attr( $settings['thickness']['size'] ); ?>,
		"startAngle":
		<?php echo esc_attr( $settings['angel']['size'] ); ?>,
		<?php if ( ! empty( $settings['empty_fill_color'] ) ) : ?>
			"emptyFill": "
			<?php echo esc_attr( $settings['empty_fill_color'] ); ?>",
		<?php endif; ?>
		<?php if ( ! empty( $settings['linecap'] ) ) : ?>
			"lineCap": "
			<?php echo esc_attr( $settings['linecap'] ); ?>",
		<?php endif; ?>
		"size":
		<?php echo esc_attr( $settings['size']['size'] ); ?>,
		"fill": {
		<?php if ( 'yes' == $settings['enable_gradient_color'] ) :
			// Output gradient colors
			echo '"gradient": [';

			end( $settings['gradient_items_color'] );
			$endKey = key( $settings['gradient_items_color'] );

			foreach ( $settings['gradient_items_color'] as $key => $item ) :
				echo esc_attr( '"' . $item['gradient_color'] . '"' );
				if ( $key != $endKey ) {
					echo ',';
				}
			endforeach;

			echo ']';
		else :
			// Output single fill color
			echo '"color": "' . esc_attr( $settings['fill_color'] ) . '"';
		endif; ?>
		}}'
		<?php return ob_get_clean();
	}
endif;

if ( ! function_exists( 'ostech_get_swiper_options' ) ) :
	function ostech_get_swiper_options( $settings, $pagination_id = false, $nav_prev_id = false, $nav_next_id = false ) {
		$loop_status = ( 'yes' == $settings['loop'] ) ? true : false;
		$autoplay_status = ( 'yes' == $settings['autoplay'] ) ? true : false;
		$hover_pause_status = ( 'yes' == $settings['hover_pause'] ) ? true : false;
		$delay = $settings['delay']['size'];
		$items = $settings['items']['size'];
		$margin = $settings['margin']['size'];
		$breakpoint = $settings['breakpoint'];
		$swiper_options = [];
		$swiper_options["loop"] = $loop_status;
		$swiper_options["spaceBetween"] = $margin;
		$swiper_options["spaceBetween"] = $margin;
		$swiper_options["slidesPerView"] = $items;
		$swiper_options["slidesPerView"] = $items;
		if ( true === $autoplay_status ) :
			$swiper_options["autoplay"]["delay"] = $delay;
		endif;
		if ( true === $hover_pause_status ) :
			$swiper_options["autoplayDisableOnInteraction"] = $hover_pause_status;
		endif;
		if ( 'yes' == $settings['enable_dots'] ) :
			$swiper_options["pagination"]["el"] = '#' . $pagination_id;
			$swiper_options["pagination"]["type"] = "bullets";
			$swiper_options["pagination"]["clickable"] = true;
		endif;
		if ( 'yes' == $settings['enable_nav'] ) :
			$swiper_options["navigation"]["nextEl"] = '#' . $nav_next_id;
			$swiper_options["navigation"]["prevEl"] = '#' . $nav_prev_id;
		endif;

		if ( ! empty( $breakpoint ) ) :
			$reponsive_options = [];
			foreach ( $breakpoint as $item ) :
				$reponsive_options[ $item['screen_size']['size'] ]["spaceBetween"] = $item['margin']['size'];
				$reponsive_options[ $item['screen_size']['size'] ]["slidesPerView"] = $item['item']['size'];
			endforeach;
			$swiper_options["breakpoints"] = $reponsive_options;
		endif;
		return json_encode( $swiper_options );
	}
endif;

if ( ! function_exists( 'ostech_get_elementor_carousel_options' ) ) :
	function ostech_get_elementor_carousel_options( $arg, $condition = false ) {
		include ostech_get_common_function( 'elementor-carousel-options.php' );
	}
endif;


if ( ! function_exists( 'ostech_elementor_general_style_options' ) ) :
	function ostech_elementor_general_style_options( $agrs, $label, $selector, $condition, $style = 'color', $typo = true, $color = true ) {
		include ostech_get_common_function( 'elementor-general-style-options.php' );
	}
endif;

if ( ! function_exists( 'ostech_elementor_button_style_options' ) ) :
	function ostech_elementor_button_style_options( $init, $label, $selector, $hover_bg_selector = '', $condition = 'layout_one' ) {
		include ostech_get_common_function( 'elementor-button-style-options.php' );
	}
endif;

if ( ! function_exists( 'ostech_elementor_form_style_options' ) ) :
	function ostech_elementor_form_style_options( $init, $label, $selector, $condition = 'layout_one' ) {
		include ostech_get_common_function( 'elementor-form-style-options.php' );
	}
endif;


if ( ! function_exists( 'ostech_elementor_heading_option' ) ) :
	function ostech_elementor_heading_option( $init, $label, $default = 'h2', $layout = '' ) {
		$init->add_control(
			str_replace( ' ', '_', strtolower( $label ) ) . '_tag_' . $layout,
			[ 
				'label' => esc_html__( $label . ' Tag', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SELECT,
				'label_block' => true,
				'options' => array(
					'h1' => 'H1',
					'h2' => 'H2',
					'h3' => 'H3',
					'h4' => 'H4',
					'h5' => 'H5',
					'h6' => 'H6',
					'div' => 'div',
					'span' => 'span',
					'p' => 'p',
				),
				'default' => $default,
			]
		);
	}
endif;

if ( ! function_exists( 'ostech_elementor_rendered_content' ) ) :
	function ostech_elementor_rendered_content( $init, $rendered_name, $class = '', $tag = 'p', $url_name = 'url', $extra = '' ) {

		$settings = $init->get_settings_for_display();


		if ( 'a' == $tag ) :
			$init->add_render_attribute( $rendered_name, [ 
				'class' => 'ostech-inline-editing'
			] );
		else :
			$init->add_render_attribute( $rendered_name, [ 
				'class' => $class,
			] );
		endif;

		if ( 'a' == $tag ) :
			printf(
				'<%1$s %7$s %3$s %4$s><em %2$s>%5$s</em> %6$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $rendered_name ),
				'href="' . esc_url( $settings[ $url_name ]['url'] ) . '"',
				esc_attr( ! empty( $settings[ $url_name ]['is_external'] ) ? "target=_blank" : ' ' ),
				wp_kses( $settings[ $rendered_name ], 'ostech_allowed_tags' ),
				$extra,
				'class="' . esc_attr( $class ) . '"'
			);
		else :
			printf(
				'<%1$s %2$s>%3$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $rendered_name ),
				wp_kses( $settings[ $rendered_name ], 'ostech_allowed_tags' )
			);
		endif;
	}

endif;

if ( ! function_exists( 'ostech_elementor_repeater_rendered_content' ) ) :
	function ostech_elementor_repeater_rendered_content( $init, $content, $key, $rendered_name, $class = 'ostech-default', $tag = 'p', $url_name = 'url', $extra = '' ) {
		if ( 'a' == $tag ) :
			$init->add_render_attribute(
				$key,
				[ 
					'class' => 'ostech-inline-editing'
				]
			);
		else :
			$init->add_render_attribute( $key, [ 
				'class' => $class,
			] );
		endif;


		if ( 'a' == $tag ) :
			printf(
				'<%1$s %7$s %3$s %4$s><em %2$s>%5$s</em> %6$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $key ),
				'href="' . esc_url( $content[ $url_name ]['url'] ) . '"',
				esc_attr( ! empty( $content[ $url_name ]['is_external'] ) ? "target=_blank" : ' ' ),
				wp_kses( $content[ $rendered_name ], 'ostech_allowed_tags' ),
				$extra,
				'class="' . esc_attr( $class ) . '"'
			);

		else :
			printf(
				'<%1$s %2$s>%3$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $key ),
				wp_kses( $content[ $rendered_name ], 'ostech_allowed_tags' )
			);
		endif;
	}

endif;

if ( ! function_exists( 'ostech_basic_rendered_content' ) ) :
	function ostech_basic_rendered_content( $init, $content, $rendered_name, $class = 'ostech-default', $tag = 'p', $url_name = 'url', $extra = '' ) {

		$init->add_render_attribute( $rendered_name, [ 
			'class' => $class,
		] );


		if ( 'a' == $tag ) :
			printf(
				'<%1$s %2$s %3$s %4$s>%5$s%6$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $rendered_name ),
				'href="' . esc_url( $content[ $url_name ]['url'] ) . '"',
				esc_attr( ! empty( $content[ $url_name ]['is_external'] ) ? "target=_blank" : ' ' ),
				wp_kses( $content[ $rendered_name ], 'ostech_allowed_tags' ),
				$extra,
			);

		else :
			printf(
				'<%1$s %2$s>%3$s</%1$s>',
				tag_escape( $tag ),
				$init->get_render_attribute_string( $rendered_name ),
				wp_kses( $content[ $rendered_name ], 'ostech_allowed_tags' )
			);
		endif;
	}

endif;

if ( ! function_exists( 'ostech_elementor_rendered_image' ) ) {
	function ostech_elementor_rendered_image( $content, $name, $class = '', $duration = '', $delay = '' ) {
		if ( empty( $content[ $name ] ) ) {
			return;
		}

		$image = ( $content[ $name ]["id"] != "" ) ? wp_get_attachment_image_url( $content[ $name ]["id"], "full" ) : $content[ $name ]["url"];
		if ( empty( $image ) ) {
			return;
		}

		$image_attr = '';
		$title = \Elementor\Control_Media::get_image_title( $content[ $name ] );

		if ( ! empty( $title ) ) {
			$image_attr .= 'title="' . esc_attr( $title ) . '" ';
		}

		if ( ! empty( $class ) ) {
			$image_attr .= 'class="' . esc_attr( $class ) . '" ';
		}

		if ( ! empty( $duration ) ) {
			$image_attr .= 'data-wow-duration="' . esc_attr( $duration ) . '" ';
		}

		if ( ! empty( $delay ) ) {
			$image_attr .= 'data-wow-delay="' . esc_attr( $delay ) . '" ';
		}

		printf(
			'<img src="%s" alt="%s" %s>',
			esc_url( $image ),
			esc_attr( \Elementor\Control_Media::get_image_alt( $content[ $name ] ) ),
			$image_attr
		);
	}
}


if ( ! function_exists( 'ostech_elementor_column_count_options' ) ) :
	function ostech_elementor_column_count_options( $init, $condition ) {
		$init->start_controls_section(
			'column_options',
			[ 
				'label' => __( 'Column Options', 'ostech-addon' ),
				'tab' => \Elementor\Controls_Manager::TAB_CONTENT,
				'condition' => [ 
					'layout_type' => $condition
				]
			]
		);


		$init->add_responsive_control(
			'column_count',
			[ 
				'label' => __( 'Column Width %', 'ostech-addon' ),
				'type' => \Elementor\Controls_Manager::SLIDER,
				'range' => [ 
					'%' => [ 
						'min' => 1,
						'max' => 100,
					],
				],
				'devices' => [ 'desktop', 'tablet', 'mobile' ],
				'desktop_default' => [ 
					'unit' => '%',
					'size' => '',
				],
				'tablet_default' => [ 
					'unit' => '%',
					'size' => '',
				],
				'mobile_default' => [ 
					'unit' => '%',
					'size' => '',
				],
				'selectors' => [ 
					'{{WRAPPER}} .row [class*=col-]' => 'width: {{SIZE}}% !important;',
				],
			]
		);

		$init->end_controls_section();
	}
endif;


if ( ! is_rtl() ) :

	function ostech_set_rtl_mode( $locale ) {

		$ostech_get_rtl_mode_status = get_theme_mod( 'ostech_rtl_mode', false );

		// check page rtl
		$current_page = get_page_by_path( $_SERVER['REQUEST_URI'] );
		if ( isset( $current_page->ID ) ) {
			$check_page_rtl = get_post_meta( $current_page->ID, 'ostech_enable_rtl_mode', true );
			$ostech_get_rtl_mode_status = empty( $check_page_rtl ) ? $ostech_get_rtl_mode_status : $check_page_rtl;
		}
		if ( isset( $_SERVER['SERVER_NAME'] ) && isset( $_SERVER['REQUEST_URI'] ) ) {
			//check home page
			$check_url = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
			$check_url_without_http = trim( str_replace( array( 'http://', 'https://' ), '', $check_url ), '/' );

			$site = site_url();
			$site_without_http = trim( str_replace( array( 'http://', 'https://' ), '', $site ), '/' );

			if ( $check_url_without_http == $site_without_http ) {
				$frontpage_id = get_option( 'page_on_front' );
				$check_home_page_rtl = get_post_meta( $frontpage_id, 'ostech_enable_rtl_mode', true );
				$ostech_get_rtl_mode_status = empty( $check_home_page_rtl ) ? $ostech_get_rtl_mode_status : $check_home_page_rtl;
			}

			$ostech_dynamic_rtl_mode_status = isset( $_GET['rtl_mode'] ) ? $_GET['rtl_mode'] : $ostech_get_rtl_mode_status;
			if ( 'yes' == $ostech_dynamic_rtl_mode_status ) {
				$locale = ( $ostech_dynamic_rtl_mode_status == 'yes' ) ? 'ar' : 'en_US';
			}
		}
		return $locale;
	}

	add_filter( 'locale', 'ostech_set_rtl_mode', 1, 1 );

endif;

/**
 * Automatically add product to cart on visit
 */
if ( ! function_exists( 'ostech_auto_add_product_to_cart' ) ) :
	function ostech_auto_add_product_to_cart() {
		$auto_cart_status = isset( $_GET['auto_cart'] ) ? $_GET['auto_cart'] : '';

		if ( ! is_admin() && ! empty( $auto_cart_status ) ) {
			$get_product = ostech_get_page_by_title( $auto_cart_status, $post_type = "product" ); // 64; //replace with your own product id
			$product_id = $get_product->ID;
			$found = false;
			//check if product already in cart
			if ( sizeof( WC()->cart->get_cart() ) > 0 ) {
				foreach ( WC()->cart->get_cart() as $cart_item_key => $values ) {
					$_product = $values['data'];
					if ( $_product->get_id() == $product_id )
						$found = true;
				}
				// if product not found, add it
				if ( ! $found )
					WC()->cart->add_to_cart( $product_id );
			} else {
				// if no products in cart, add it
				WC()->cart->add_to_cart( $product_id );
			}
		}
	}
	add_action( 'template_redirect', 'ostech_auto_add_product_to_cart' );
endif;


if ( ! function_exists( 'ostech_fixed_footer_class_to_html_tag' ) ) :
	function ostech_fixed_footer_class_to_html_tag( $output, $doctype ) {
		if ( 'html' !== $doctype ) {
			return $output;
		}

		return $output;
	}
endif;

if ( ! function_exists( 'ostech_product_details_social_share' ) ) :
	//social share
	function ostech_product_details_social_share() {
		global $post;
		//get current page url
		$ostech_url = urlencode_deep( get_permalink() );
		//get current page title
		$ostech_title = str_replace( ' ', '%20', get_the_title( $post->ID ) );
		//get post thumbnail for pinterest
		$ostech_thumbnail = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );

		//all social share link generate
		$facebook_share_link = 'https://www.facebook.com/sharer/sharer.php?u=' . $ostech_url;
		$twitter_share_link = 'https://twitter.com/intent/tweet?text=' . $ostech_title . '&amp;url=' . $ostech_url . '&amp;via=Crunchify';
		;
		$linkedin_share_link = 'https://www.linkedin.com/shareArticle?mini=true&url=' . $ostech_url . '&amp;title=' . $ostech_title;
		;
		$pinterest_share_link = 'https://pinterest.com/pin/create/button/?url=' . $ostech_url . '&amp;media=' . $ostech_thumbnail[0] . '&amp;description=' . $ostech_title;

		?>
		<div class="product-details__socials">
			<h4 class="product-details__socials__title">
				<?php esc_html_e( 'Share with friends', 'ostech' ); ?>
			</h4>
			<div class="product-details__socials__item">
			<a href="<?php echo esc_url( $twitter_share_link ); ?>"><i class="fab icon-x-twitter"></i></a>
			<a href="<?php echo esc_url( $facebook_share_link ); ?>"><i class="fab fa-facebook-f"></i></a>
			<a href="<?php echo esc_url( $pinterest_share_link ); ?>"><i class="fab fa-pinterest-p"></i></a>
			<a href="<?php echo esc_url( $linkedin_share_link ); ?>"><i class=" fab fa-linkedin-in"></i></a>
			</div>
		</div><!-- /.social-share -->
		<?php
	}
endif;

if ( ! function_exists( 'ostech_page_header_extra_class_callback' ) ) :
	function ostech_page_header_extra_class_callback( $class ) {
		return '';
	}
endif;
add_filter( 'ostech_page_header_extra_class', 'ostech_page_header_extra_class_callback', 10, 1 );


if ( function_exists( 'ostech_elementor_rendered_image' ) ) :

	add_filter( 'loop_shop_columns', 'ostech_loop_columns' );
	if ( ! function_exists( 'ostech_loop_columns' ) ) {
		function ostech_loop_columns() {
			$ostech_shop_layout = isset( $_GET['column'] ) ? $_GET['column'] : ( get_theme_mod( 'ostech_shop_column' ) ? get_theme_mod( 'ostech_shop_column' ) : 3 );
			return $ostech_shop_layout;
		}
	}

	add_filter( 'loop_shop_per_page', 'ostech_loop_shop_per_page', 30 );

	function ostech_loop_shop_per_page( $products ) {
		$ostech_shop_product_per_page = isset( $_GET['product_per_page'] ) ? $_GET['product_per_page'] : get_theme_mod( 'product_per_page' );
		return $ostech_shop_product_per_page;
	}

endif;

// recent post widget

// Hook the registration function to the widgets_init action
add_action( 'widgets_init', 'ostech_register_custom_recent_posts_widget' );
function ostech_register_custom_recent_posts_widget() {
	// Register the widget
	register_widget( new Layerdrops\Ostech\SidebarWidget\RecentPost() );
	register_widget( new Layerdrops\Ostech\SidebarWidget\BlogCategories());
}

/**
 * Sourcing color metabox and customizer via json file
 */
function ostech_source_color_meta( $type = null, $set_option = null, $init = null ) {
	$data = get_transient( 'ostech_css_variable' );
	if ( empty( $data ) ) {
		global $wp_filesystem;
		require_once ( ABSPATH . '/wp-admin/includes/file.php' );
		WP_Filesystem();

		$json_file = OSTECH_ADDON_PATH . '/assets/vendors/ostech-css-var/ostech-css-var.json';
		$content = '';

		if ( $wp_filesystem->exists( $json_file ) ) {
			$content = $wp_filesystem->get_contents( $json_file );
		} // End If Statement
		$data = apply_filters( 'ostech_css_variable', $content );
		set_transient( 'ostech_css_variable', $data, 10080 ); // saved for one week
	}
	$values = json_decode( $data );

	if ( 'get' === $type ) {
		$cssvariable = '';
		foreach ( $values as $name ) {
			$cssvariable .= ostech_get_color_meta( $name, 'ostech' );
		}
		return $cssvariable;
	}
	if ( 'set' === $type ) {
		ostech_set_color_meta( $set_option, $values );
	}
	if ( 'set_customizer' === $type ) {
		ostech_set_color_customizer( $init, $set_option, $values );
	}
}


/**
 * Making color metaboxes from arrays
 */
function ostech_set_color_meta( $option_name = null, $value = null ) {
	foreach ( $value as $name ) {
		$option_name->add_field(
			array(
				'name' => ucwords( str_replace( "-", " ", $name ) ),
				'id' => 'ostech_' . str_replace( "-", "_", $name ),
				'type' => 'colorpicker',
			)
		);
	}
}


/**
 * Making color customizer options from arrays
 */
function ostech_set_color_customizer( $init = null, $option_name = null, $value = null ) {
	foreach ( $value as $name ) {
		$init->customize_type_color(
			$option_name,
			'ostech_theme_color',
			$name
		);
	}
}
/**
 * Printing color variables conditional
 */
function ostech_get_color_meta( $name = null, $prefix = null ) {
	$setting_name = str_replace( "-", "_", $name );
	$customizer_option = get_theme_mod( $setting_name );
	$page_meta_option = get_post_meta( get_the_ID(), $prefix . '_' . $setting_name, true );
	$value = ! empty( $page_meta_option ) ? $page_meta_option : $customizer_option;

	return ! empty( $value ) ? "--" . $name . ":" . $value . ";" . "--" . $name . "-rgb:" . ostech_hex_to_rgb( $value ) . ";" : "";
}

/**
 * Generates json for elementor icons
 */
function ostech_get_elementor_icons( $infos ) {
	global $wp_filesystem;
	require_once ( ABSPATH . '/wp-admin/includes/file.php' );
	WP_Filesystem();
	$options = [];
	$data = get_transient( 'ostech_get_elementor_icons' );
	// create icons json for json files
	if ( empty( $data ) ) {
		foreach ( $infos as $info ) {
			$css_file = OSTECH_ADDON_PATH . '/assets/vendors/' . $info['name'] . '/style.css';
			$json_file = OSTECH_ADDON_PATH . '/assets/vendors/' . $info['name'] . '/icons.json';
			$class_pattern = $info['class'];
			$version = filemtime( $json_file );
			$content = '';
			if ( $wp_filesystem->exists( $css_file ) ) {
				$content .= $wp_filesystem->get_contents( $css_file );
			}
			$pattern = '/\.(' . $class_pattern . '-(?:\w+(?:-)?)+):before\s*{\s*content/';
			preg_match_all( $pattern, $content, $matches, PREG_SET_ORDER );

			$icons = [];

			foreach ( $matches as $match ) {
				$icons[] = $match[1];
			}

			$icons_formatted = [ 
				"icons" => $icons,
			];

			$data = apply_filters( 'ostech_get_elementor_icons', $icons_formatted );

			if ( $wp_filesystem->exists( $json_file ) ) {
				$wp_filesystem->put_contents( $json_file, json_encode( $data ), FS_CHMOD_FILE );
			}
			set_transient( 'ostech_get_elementor_icons', $data, 10080 ); // saved for one week
		}
	}
	$i = 1;
	// create icons array for elementor
	foreach ( $infos as $info ) {
		$json_file = OSTECH_ADDON_PATH . '/assets/vendors/' . $info['name'] . '/icons.json';
		$version = filemtime( $json_file );
		$options[ $info['name'] . '-' . $version ] = [ 
			'name' => $info['name'] . '-' . $version,
			'label' => 'Ostech Icons ' . $i,
			'url' => '',
			'enqueue' => array(
				OSTECH_ADDON_URL . '/assets/vendors/' . $info['name'] . '/style.css',
			),
			'prefix' => '',
			'displayPrefix' => '',
			'labelIcon' => 'icon-dairy-products',
			'ver' => $version,
			'fetchJson' => OSTECH_ADDON_URL . '/assets/vendors/' . $info['name'] . '/icons.json',
			'native' => 1,
		];
		$i++;
	}
	return $options;

}