<?php
/**
 * Custom theme functions.
 *
 * This file contains hook functions attached to theme hooks.
 *
 * @package charitable_trust
 */

if ( ! function_exists( 'charitable_trust_skip_to_content' ) ) :
	/**
	 * Add Skip to content.
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_skip_to_content() {
	?><a class="skip-link screen-reader-text" href="#content"><?php esc_html_e( 'Skip to content', 'charitable-trust' ); ?></a><?php
	}
endif;

add_action( 'charitable_trust_action_before', 'charitable_trust_skip_to_content', 15 );

// Middle Header

if ( ! function_exists( 'charitable_trust_site_branding' ) ) :

	/**
	 * Site branding.
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_site_branding() {

		$charitable_trust_show_top_header = charitable_trust_get_option( 'charitable_trust_show_top_header' );
		$charitable_trust_header_top_button = charitable_trust_get_option( 'charitable_trust_header_top_button' );
		$charitable_trust_header_top_buttonlink = charitable_trust_get_option( 'charitable_trust_header_top_buttonlink' );
		$charitable_trust_show_social_icon = charitable_trust_get_option( 'charitable_trust_show_social_icon' );
		$charitable_trust_header_social_facebook_link = charitable_trust_get_option( 'charitable_trust_header_social_facebook_link' );
		$charitable_trust_header_social_twitter_link = charitable_trust_get_option( 'charitable_trust_header_social_twitter_link' );
		$charitable_trust_header_social_instagram_link = charitable_trust_get_option( 'charitable_trust_header_social_instagram_link' );
		$charitable_trust_header_social_youtube_link = charitable_trust_get_option( 'charitable_trust_header_social_youtube_link' );

		$charitable_trust_header_top_text_heading = charitable_trust_get_option( 'charitable_trust_header_top_text_heading' );
		$charitable_trust_header_top_text = charitable_trust_get_option( 'charitable_trust_header_top_text' );
		$charitable_trust_data_sticky = charitable_trust_get_option( 'charitable_trust_show_data_sticky_setting' );
		
		?>

		<div class="top-header">
			<div class="container">
			<?php if ( true === $charitable_trust_show_top_header ) : ?>
					<div class="row header-top py-1 px-3">
						<div class="col-xl-9 col-lg-9 col-md-9 col-12 text-md-start text-center align-self-center">
							<?php if( !empty($charitable_trust_header_top_text) || !empty($charitable_trust_header_top_text_heading)):?>
								<div class="mb-0 adver-text"><h6 class="d-inline"><?php echo esc_html($charitable_trust_header_top_text_heading) .' : ';?></h6><span><?php echo esc_html($charitable_trust_header_top_text);?></span></div>
							<?php endif; ?>
						</div>
						<?php if ( true === $charitable_trust_show_social_icon ) : ?>
							<div class="col-xl-3 col-lg-3 col-md-3 col-12 text-md-end text-center header-social-icon align-self-center my-md-0 my-2">
								<?php if( !empty($charitable_trust_header_social_facebook_link) || !empty($charitable_trust_header_social_twitter_link) || !empty($charitable_trust_header_social_instagram_link) || !empty($charitable_trust_header_social_youtube_link) ):?>
									<?php if( !empty($charitable_trust_header_social_facebook_link) ):?>
										<a href="<?php echo esc_url($charitable_trust_header_social_facebook_link);?>" target="_blank" ><span class="dashicons dashicons-facebook-alt"></span></a>
									<?php endif; ?>
									<?php if( !empty($charitable_trust_header_social_twitter_link) ):?>
										<a href="<?php echo esc_url($charitable_trust_header_social_twitter_link);?>" target="_blank" ><span class="dashicons dashicons-twitter"></span></a>
									<?php endif; ?>
									<?php if( !empty($charitable_trust_header_social_instagram_link) ):?>
										<a href="<?php echo esc_url($charitable_trust_header_social_instagram_link);?>" target="_blank" ><span class="dashicons dashicons-instagram"></span></a>
									<?php endif; ?>
									<?php if( !empty($charitable_trust_header_social_youtube_link) ):?>
										<a href="<?php echo esc_url($charitable_trust_header_social_youtube_link);?>" target="_blank" ><span class="dashicons dashicons-youtube"></span></a>
									<?php endif; ?>
								<?php endif;?>
							</div>
						<?php endif;?>
					</div>
				<?php endif; ?>
			</div>
		</div>
		<div id="middle-header" class="py-2" data-sticky= "<?php echo esc_attr($charitable_trust_data_sticky); ?>">
			<div class="container">
				<div class="row">
					<div class="col-xl-2 col-lg-2 col-md-4 col-sm-4 col-12 align-self-center">
						<div class="site-branding mb-3 mb-lg-0 text-md-start text-center">
							<?php charitable_trust_the_custom_logo(); ?>
							<?php $charitable_trust_show_title = charitable_trust_get_option( 'charitable_trust_show_title' ); ?>
							<?php $charitable_trust_show_tagline = charitable_trust_get_option( 'charitable_trust_show_tagline' ); ?>
							<?php if ( true === $charitable_trust_show_title || true === $charitable_trust_show_tagline ) :  ?>
								<div id="site-identity" class="text-center text-md-start text-lg-start">
									<?php if ( true === $charitable_trust_show_title ) :  ?>
										<?php if ( is_front_page() ) : ?>
											<h1 class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></h1>
										<?php else : ?>
											<p class="site-title"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php bloginfo( 'name' ); ?></a></p>
										<?php endif; ?>
									<?php endif; ?>
									<?php if ( true === $charitable_trust_show_tagline ) :  ?>
										<p class="site-description"><?php bloginfo( 'description' ); ?></p>
									<?php endif; ?>
								</div>
							<?php endif; ?>
						</div>
					</div>
					<div class="col-xl-7 col-lg-7 col-md-4 col-sm-4 col-4 align-self-center text-md-start text-center">
						<div class="navigation_header">
							<div class="toggle-nav mobile-menu">
								<button onclick="charitable_trust_openNav()"><i class="fa-solid fa-bars"></i></button>
							</div>
							<div id="mySidenav" class="nav sidenav">
								<nav id="site-navigation" class="main-navigation navbar navbar-expand-xl" aria-label="<?php esc_attr_e( 'Top Menu', 'charitable-trust' ); ?>">
									<?php {
										wp_nav_menu(
											array(
												'theme_location' => 'primary',
												'menu_class'     => 'menu', 
												'items_wrap'     => '<ul id="%1$s" class="%2$s">%3$s</ul>',
												'fallback_cb' => 'wp_page_menu',
											)
										);
									} ?>
								</nav>
								<a href="javascript:void(0)" class="closebtn mobile-menu" onclick="charitable_trust_closeNav()"><i class="fas fa-times"></i></a>
							</div>
						</div>
					</div>
					<div class="col-xl-3 col-lg-3 col-md-4 col-sm-4 col-8 align-self-center text-end">
						<?php if( !empty($charitable_trust_header_top_button) || !empty($charitable_trust_header_top_buttonlink) ):?>
						    <p class="header-button mb-0">
						    	<a href="<?php echo esc_url($charitable_trust_header_top_buttonlink);?>" target='_blank'><?php echo esc_html($charitable_trust_header_top_button);?><i class="fa-solid fa-hand-holding-hand"></i></a>
						   	</p>
						<?php endif;?>
					</div>
				</div>
			</div>
		</div>
		
	    <?php
	}

endif;

add_action( 'charitable_trust_action_header', 'charitable_trust_site_branding' );

/////////////////////////////////// copyright start /////////////////////////////

if ( ! function_exists( 'charitable_trust_footer_copyright' ) ) :

	/**
	 * Footer copyright
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_footer_copyright() {

		// Check if footer is disabled.
		$charitable_trust_footer_status = apply_filters( 'charitable_trust_filter_footer_status', true );
		if ( true !== $charitable_trust_footer_status ) {
			return;
		}

		// Copyright content.
		$charitable_trust_copyright_text = charitable_trust_get_option( 'charitable_trust_copyright_text' );
		$charitable_trust_copyright_text = apply_filters( 'charitable_trust_filter_copyright_text', $charitable_trust_copyright_text );
		if ( ! empty( $charitable_trust_copyright_text ) ) {
			$charitable_trust_copyright_text = wp_kses_data( $charitable_trust_copyright_text );
		}

		// Powered by content.
		$charitable_trust_powered_by_text = sprintf( __( 'Charitable Trust by %s', 'charitable-trust' ), '<span>' . __( 'Mizan Themes', 'charitable-trust' ) . '</span>' );
		?>

		<div class="colophon-inner">
		    <?php if ( ! empty( $charitable_trust_copyright_text ) ) : ?>
			    <div class="colophon-column">
			    	<div class="copyright">
					<a href="<?php echo esc_url('https://www.mizanthemes.com/products/free-charitable-trust-wordpress-theme','charitable-trust'); ?>"><?php echo $charitable_trust_copyright_text; ?></a>
			    	</div><!-- .copyright -->
			    </div><!-- .colophon-column -->
		    <?php endif; ?>

		    <?php if ( ! empty( $charitable_trust_powered_by_text ) ) : ?>
			    <div class="colophon-column">
			    	<div class="site-info">
						<?php echo $charitable_trust_powered_by_text; ?>
			    	</div><!-- .site-info -->
			    </div><!-- .colophon-column -->
		    <?php endif; ?>
		</div><!-- .colophon-inner -->
		
	    <?php
	}

endif;

add_action( 'charitable_trust_action_footer', 'charitable_trust_footer_copyright', 10 );

// /////////////////////////////////sidebar//////////////////

if ( ! function_exists( 'charitable_trust_add_sidebar' ) ) :

	/**
	 * Add sidebar.
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_add_sidebar() {

		global $post;

		$charitable_trust_global_layout = charitable_trust_get_option( 'charitable_trust_global_layout' );
		$charitable_trust_global_layout = apply_filters( 'charitable_trust_filter_theme_global_layout', $charitable_trust_global_layout );

		// Check if single.
		if ( $post && is_singular() ) {
			$charitable_trust_post_options = get_post_meta( $post->ID, 'charitable_trust_theme_settings', true );
			if ( isset( $charitable_trust_post_options['post_layout'] ) && ! empty( $charitable_trust_post_options['charitable_trust_post_layout'] ) ) {
				$charitable_trust_global_layout = $charitable_trust_post_options['charitable_trust_post_layout'];
			}
		}

		// Include primary sidebar.
		if ( 'no-sidebar' !== $charitable_trust_global_layout ) {
			get_sidebar();
		}
		// Include Secondary sidebar.
		switch ( $charitable_trust_global_layout ) {
			case 'three-columns':
			get_sidebar( 'secondary' );
			break;

			default:
			break;
		}

		// Include Secondary sidebar 1.
		switch ( $charitable_trust_global_layout ) {
			case 'four-columns':
			get_sidebar( 'secondary' );
			break;

			default:
			break;
		}

	}

endif;

add_action( 'charitable_trust_action_sidebar', 'charitable_trust_add_sidebar' );

//////////////////////////////////////// single page


if ( ! function_exists( 'charitable_trust_add_image_in_single_display' ) ) :

	/**
	 * Add image in single post.
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_add_image_in_single_display() {

		global $post;

		if ( has_post_thumbnail() ) {

			$values = get_post_meta( $post->ID, 'charitable_trust_theme_settings', true );
			$charitable_trust_theme_settings_single_image = isset( $values['charitable_trust_single_image'] ) ? esc_attr( $values['charitable_trust_single_image'] ) : '';

			if ( ! $charitable_trust_theme_settings_single_image ) {
				$charitable_trust_theme_settings_single_image = charitable_trust_get_option( 'charitable_trust_single_image' );
			}

			if ( 'disable' !== $charitable_trust_theme_settings_single_image ) {
				$args = array(
					'class' => 'alignleft',
				);
				the_post_thumbnail( esc_attr( $charitable_trust_theme_settings_single_image ), $args );
			}
		}

	}

endif;

add_action( 'charitable_trust_single_image', 'charitable_trust_add_image_in_single_display' );

if ( ! function_exists( 'charitable_trust_footer_goto_top' ) ) :

	/**
	 * Go to top.
	 *
	 * @since 1.0.0
	 */
	function charitable_trust_footer_goto_top() {
        
        $charitable_trust_show_scroll_to_top = charitable_trust_get_option( 'charitable_trust_show_scroll_to_top' );
        if ( true === $charitable_trust_show_scroll_to_top ) :
		echo '<a id="scrollToTopBtn" href="#page">
				<svg id="progressCircle" width="50" height="50" aria-hidden="true">
					<circle cx="25" cy="25" r="22" stroke-width="4" fill="none"/>
				</svg>
				<i class="fa-solid fa-arrow-up"></i>
				</a>';
		endif;

	}

endif;

add_action( 'charitable_trust_action_after', 'charitable_trust_footer_goto_top', 20 );