<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Dicom
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function dicom_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'article-sidebar' ) ) {
		$classes[] = 'no-sidebar';
	}

	return $classes;
}
add_filter( 'body_class', 'dicom_body_classes' );

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function dicom_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
	}
}
add_action( 'wp_head', 'dicom_pingback_header' );

 /*
 * Pagination
 */
if ( ! function_exists( 'dicom_pagination' ) ) :
    function dicom_pagination() {
        ?>
        <div class="pagination-area">
			<nav aria-label="navigation">
			<?php echo paginate_links( array(
				'format' => '?paged=%#%',
				'prev_text' => '<i class="bx bx-chevrons-left"></i>',
				'next_text' => '<i class="bx bx-chevrons-right"></i>',
				)
			) ?>
			</nav>
		</div>
        <?php
    }
endif;

/**
 * If page edited by elementor
 */
if ( ! function_exists( 'dicom_is_elementor' ) ) :
	function dicom_is_elementor(){
		if ( function_exists( 'elementor_load_plugin_textdomain' ) ):
			global $post;
			if( $post != '' ):
				return \Elementor\Plugin::$instance->documents->get($post->ID)->is_built_with_elementor();
			endif;
		endif;
	}
endif;

/**
  * Dicom Preloader
*/
if ( ! function_exists( 'dicom_preloader' ) ) :
	function dicom_preloader() { 
		global $dicom_opt;

		if( isset( $dicom_opt['enable_preloader'] ) ) :
			$is_preloader = $dicom_opt['enable_preloader'];
		else:
			$is_preloader = true;
		endif;
		
        $preloader_style  = !empty($dicom_opt['preloader_style']) ? $dicom_opt['preloader_style'] : 'circle-spin';

        if( $is_preloader == true ) : 
            if ( defined( 'ELEMENTOR_VERSION' ) ) :
                if (\Elementor\Plugin::$instance->preview->is_preview_mode()) :
                    echo '';
                else:
                    if ( $preloader_style == 'text' ) :
                        if (!empty( $dicom_opt['loading_text'] ) ) : ?>
                            <div class="loader-wrapper">
								<p class="text-center"> <?php echo esc_html( $dicom_opt['loading_text'] ) ?> </p>
                            </div>
                        <?php endif;
                    elseif( $preloader_style == 'circle-spin' ) : ?>
						<div class="loader-wrapper">
							<div class="loader"></div>
							<div class="loader-section section-left"></div>
							<div class="loader-section section-right"></div>
						</div>
                    <?php else: ?>
                        <div class="loader-wrapper preloader-img">
						</div>
                    <?php endif;
                endif;
            else:
                if ( $preloader_style == 'text' ) :
                    if (!empty( $dicom_opt['loading_text'] ) ) : ?>
                        <div class="loader-wrapper">
                            <p class="text-center"> <?php echo esc_html( $dicom_opt['loading_text'] ) ?> </p>
                        </div>
                    <?php endif;
                elseif( $preloader_style == 'circle-spin' ) :
                    ?>
                    <div class="loader-wrapper">
						<div class="loader"></div>
						<div class="loader-section section-left"></div>
						<div class="loader-section section-right"></div>
					</div>
				<?php else : ?>
					<div class="loader-wrapper preloader-img">
                    </div>
                    <?php 
                endif;
            endif;
        endif;
	}
endif;

/**
 * Dicom White Logo
 */
if ( ! function_exists( 'dicom_main_logo' ) ) :
	function dicom_main_logo(){
		global $dicom_opt;
		// Dicom Logo
		if ( isset( $dicom_opt['dicom_white_logo']['url']) && $dicom_opt['dicom_white_logo']['url'] !='' ) :
			$dicom_logo = $dicom_opt['dicom_white_logo']['url'];
		else :
			$dicom_logo = 'null';
		endif;
		?>
			<?php if ( $dicom_logo != 'null' ) : ?>
				<img src="<?php echo esc_url( $dicom_logo ); ?>" alt="<?php bloginfo( 'name' ); ?>"><?php
			else : ?>
				<h2 class="main-title"><?php bloginfo( 'name' ); ?></h2> <?php  
			endif; ?>
		<?php
	}
endif;

if ( ! function_exists( 'dicom_mobile_logo' ) ) :
	function dicom_mobile_logo(){
		global $dicom_opt;
		// Dicom Logo
		if ( isset( $dicom_opt['dicom_mobile_logo']['url']) && $dicom_opt['dicom_mobile_logo']['url'] !='' ) :
			$dicom_logo = $dicom_opt['dicom_mobile_logo']['url'];
		else :
			$dicom_logo = 'null';
		endif;
		?>
			<?php if ( $dicom_logo != 'null' ) : ?>
				<img src="<?php echo esc_url($dicom_logo); ?>" alt="<?php bloginfo( 'name' ); ?>"><?php
			else : ?>
				<h2 class="main-title"><?php bloginfo( 'name' ); ?></h2> <?php  
			endif; ?>
		<?php
	}
endif;

/**
 * Dicom Black Logo
*/
if ( ! function_exists( 'dicom_black_logo' ) ) :
	function dicom_black_logo(){
		global $dicom_opt;
		// Dicom Logo
		if ( isset( $dicom_opt['dicom_black_logo']['url'] ) && $dicom_opt['dicom_black_logo']['url'] !='' ) :
			$dicom_black_logo = $dicom_opt['dicom_black_logo']['url'];
		else :
			$dicom_black_logo = 'null';
		endif;
		?>
			<?php if ( $dicom_black_logo != 'null' ) : ?>
				<img src="<?php echo esc_url( $dicom_black_logo ); ?>" alt="<?php bloginfo( 'name' ); ?>"><?php
			else : ?>
				<h2 class="optional-title"><?php bloginfo( 'name' ); ?></h2> <?php  
			endif; ?>
		<?php
	}
endif;

/**
 * 
 * Dicom Modal
 * 
*/
if ( ! function_exists( 'dicom_modal' ) ) {
	function dicom_modal() { 
		global $dicom_opt;

		// Modal Data
		if ( isset( $dicom_opt['modal_contact'] ) ) :
			$enable_burger_menu		= $dicom_opt['enable_burger_menu'];
			$modal_gallery          = $dicom_opt['modal_gallery'];
			$modal_contact          = $dicom_opt['modal_contact'];
			$modal_contact_link     = $dicom_opt['modal_contact_link'];
			$modal_or               = $dicom_opt['modal_or'];
			$modal_email            = $dicom_opt['modal_email'];
			$modal_email_link       = $dicom_opt['modal_email_link'];
			$social_link            = $dicom_opt['social_link'];
			$follow_text            = $dicom_opt['follow_text'];
		else:
			$enable_burger_menu		= false;
			$social_link            = false;
			$modal_gallery 	        = '';
			$modal_contact 	        = '';
			$modal_contact_link 	= '';
			$modal_or 		        = '';
			$modal_email 		    = '';
			$modal_email_link 		= '';
			$follow_text 		    = '';
		endif;

		// WP admin bar
		$hide_wp_nav = 'hide-wp-nav';
		?>
		<?php if ( $enable_burger_menu == true ) : ?>
			<div class="sidebarModal modal right fade  <?php if ( is_user_logged_in() ) { echo esc_attr( $hide_wp_nav ); } ?>" id="sidebarModals" tabindex="-1" role="dialog" aria-hidden="true">
				<div class="modal-dialog" role="document">
					<div class="modal-content">
						<button type="button" class="close" data-bs-dismiss="modal"><i class='bx bx-x'></i></button>

						<div class="modal-body">
							<div class="logo">
								<a href="<?php echo esc_url( home_url( '/' ) ); ?>" class="d-inline-block">
									<?php dicom_black_logo() ?>
								</a>
							</div>

							<div class="instagram-list">
								<div class="row">
									<?php $modal_gallerys = explode(",", $modal_gallery); 

									if( $modal_gallerys[0] !='') { ?>
										<?php foreach( $modal_gallerys as $image ) { ?>
											<div class="col-lg-4 col-md-4 col-sm-4 col-6">
												<div class="box">
													<img src="<?php $imag_var = wp_get_attachment_image_src($image, 'full'); echo esc_url($imag_var[0]); ?>" alt="<?php esc_attr_e( 'Gallery Images', 'dicom' ); ?>">
												</div>
											</div>
										<?php } ?>
									<?php } ?>

								</div>
							</div>
							
							<div class="sidebar-contact-info">
								<h2>
									<a href="<?php echo esc_url( $modal_contact_link ); ?>"><?php echo esc_html( $modal_contact ); ?></a>
									<span><?php echo esc_html( $modal_or ); ?></span>
									<a href="<?php echo esc_url( $modal_email_link ); ?>"><?php echo esc_html( $modal_email ); ?></a>
								</h2>
							</div>

							<?php if ( $social_link == true ) : ?>
								<ul class="social-list">
									<li><span><?php echo esc_html( $follow_text ); ?></span></li>
									<?php dicom_social_link(); ?>
								</ul>
							<?php endif; ?>
						</div>
					</div>
				</div>
			</div>
		<?php endif; ?>
		
		<?php
	}
}

// Search Overlay
if ( ! function_exists( 'dicom_search' ) ) {
	function dicom_search() { 
		global $dicom_opt; 
		if( isset( $dicom_opt['search_placeholder_text'] ) && $dicom_opt['search_placeholder_text'] != '' ) {
			$dicom_search_placeholder = $dicom_opt['search_placeholder_text'];
		} else {
			$dicom_search_placeholder = esc_html__('Search here', 'dicom');
		} ?>
		<div class="search-overlay">
			<div class="d-table">
				<div class="d-table-cell">
					<div class="search-overlay-layer"></div>
					<div class="search-overlay-layer"></div>
					<div class="search-overlay-layer"></div>
					
					<div class="search-overlay-close">
						<span class="search-overlay-close-line"></span>
						<span class="search-overlay-close-line"></span>
					</div>

					<div class="search-overlay-form">
						<form role="search" method="get" id="search2" action="<?php echo esc_url( home_url( '/' ) ); ?>">
							<input type="text" class="input-search" placeholder="<?php echo esc_attr( $dicom_search_placeholder ); ?>" value="<?php echo get_search_query(); ?>" name="s" id="s2" required>
							<button type="submit"><i class="flaticon-search"></i></button>
						</form>
					</div>
				</div>
			</div>
		</div>
		<?php
	}
}

// Dicom Page Shape Images
if ( ! function_exists( 'dicom_page_shape_images' ) ) :
	function dicom_page_shape_images() {
        global $dicom_opt;
        
        $enable_shape_images = !empty($dicom_opt['enable_shape_images']) ? $dicom_opt['enable_shape_images'] : '';

        if( $enable_shape_images == true  ):
            ?>
				<div class="shape2">
					<?php if( $dicom_opt['shape_image1']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image1']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape2.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
				<div class="shape3">
					<?php if( $dicom_opt['shape_image2']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image2']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape3.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
				<div class="shape5">
					<?php if( $dicom_opt['shape_image3']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image3']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape5.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
				<div class="shape6">
					<?php if( $dicom_opt['shape_image4']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image4']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape6.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
				<div class="shape7">
					<?php if( $dicom_opt['shape_image5']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image5']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape7.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
				<div class="shape8">
					<?php if( $dicom_opt['shape_image6']['url'] != ''  ): ?>
                        <img src="<?php echo esc_url( $dicom_opt['shape_image6']['url'] ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php else: ?>
                        <img src="<?php echo esc_url( DICOM_IMG .'/shape/shape8.png' ); ?>" alt="<?php esc_attr_e( 'Shape Image', 'dicom' ); ?>">
                    <?php endif; ?>
				</div>
            <?php 
        endif;
	}
endif;

/**
 * Dicom RTL
*/
if( ! function_exists( 'dicom_rtl' ) ):
	function dicom_rtl() {
		global $dicom_opt;

		if(	isset( $dicom_opt['dicom_enable_rtl'])  ):
			$dicom_rtl_opt = $dicom_opt['dicom_enable_rtl'];
		else:
			$dicom_rtl_opt = 'disable';
		endif;

		if ( isset( $_GET['rtl'] ) ) {
			$dicom_rtl_opt = $_GET['rtl'];
		}

		if( $dicom_rtl_opt == 'enable' ):
			$dicom_rtl = true;
		else:
			$dicom_rtl = false;
		endif;
		
		return $dicom_rtl;
	}
endif;

// Live demo rtl link on function.php
if( dicom_rtl() == true ):
	function dicom_menu_anchors($items, $args) {
		foreach ($items as $key => $item) {
			if ($item->object == 'page') {
				$item->url = $item->url.'?rtl=enable';
			}
		}

		return $items;
	}
	add_filter('wp_nav_menu_objects', 'dicom_menu_anchors', 10, 2);
endif;