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

if( ! function_exists( 'prime_charity_trust_site_branding' ) ) :
/**
* Site Branding
*
* @since 1.0.0
*/
function prime_charity_trust_site_branding() { ?>
  <?php 
  $prime_charity_trust_show_contact = prime_charity_trust_get_option( 'show_header_contact_info' );
  $prime_charity_trust_location     = prime_charity_trust_get_option( 'header_location' ); 
  $prime_charity_trust_phone        = prime_charity_trust_get_option( 'header_phone' ); 
  $prime_charity_trust_email        = prime_charity_trust_get_option( 'header_email' ); 
  $prime_charity_trust_show_social  = prime_charity_trust_get_option( 'show_header_social_links' );
  $prime_charity_trust_button_txt  = prime_charity_trust_get_option( 'header_donate_btn_text' );
  $prime_charity_trust_button_url  = prime_charity_trust_get_option( 'header_donate_btn_url' );
  ?>

  <div class="header-main">
    <div class="site-branding">
      <div class="logo" >
    <?php if (has_custom_logo()) { ?>
        <div class="site-logo"style="max-width: <?php echo esc_attr(get_theme_mod('logo_width', 'auto')); ?>;">
            <?php the_custom_logo(); ?>
        </div>
    <?php } else { ?>
        <div id="site-identity">
          <h1 class="site-title" style="font-size: <?php echo esc_attr(get_theme_mod('prime_charity_trust_site_title_size', '30')); ?>px;">
            <a href="<?php echo esc_url(home_url('/')); ?>" rel="home"><?php bloginfo('name'); ?></a>
          </h1>

          <?php if (get_theme_mod('show_site_description', false)) : ?>
              <p class="site-description"><?php bloginfo('description'); ?></p>
          <?php endif; ?>
        </div><!-- #site-identity -->
    <?php } ?>
  </div>
    </div> <!-- .site-branding -->

    <div class="header-right">
      <?php 
        if ($prime_charity_trust_show_contact == true) {
      ?>
        <div class="top-header">
          <?php if ( $prime_charity_trust_location ){?>
            <p><i class="<?php echo esc_attr(get_theme_mod('prime_charity_trust_marker_icon','fas fa-map-marker-alt')); ?>"></i> <?php echo esc_html( $prime_charity_trust_location );?></p>
          <?php } ?>
          <?php if ( $prime_charity_trust_phone ){?>
            <p><a href="tel:<?php echo esc_attr($prime_charity_trust_phone);?>"><i class="<?php echo esc_attr(get_theme_mod('prime_charity_trust_phone_icon','fas fa-phone-alt')); ?>"></i> <?php echo esc_html( $prime_charity_trust_phone);?></a></p>
          <?php } ?>
          <?php if ( $prime_charity_trust_email ){?>
            <p><a href="mailto:<?php echo esc_attr($prime_charity_trust_email);?>"><i class="<?php echo esc_attr(get_theme_mod('prime_charity_trust_mail_icon','fas fa-envelope')); ?>"></i> <?php echo esc_html($prime_charity_trust_email);?></a></p>
          <?php } ?>
          <?php if ( $prime_charity_trust_show_social ){ ?>
            <div class="social-links">
              <?php 
                $prime_charity_trust_social_link1 = prime_charity_trust_get_option( 'social_link_1' );
                $prime_charity_trust_social_link2 = prime_charity_trust_get_option( 'social_link_2' );
                $prime_charity_trust_social_link3 = prime_charity_trust_get_option( 'social_link_3' );
                $prime_charity_trust_social_link4 = prime_charity_trust_get_option( 'social_link_4' );
                if ( ! empty( $prime_charity_trust_social_link1 ) ) {
                  echo '<a href="' . esc_url( $prime_charity_trust_social_link1 ) . '" target="_blank"></a>';
                }
                if ( ! empty( $prime_charity_trust_social_link2 ) ) {
                  echo '<a href="' . esc_url( $prime_charity_trust_social_link2 ) . '" target="_blank"></a>';
                } 
                if ( ! empty( $prime_charity_trust_social_link3 ) ) {
                  echo '<a href="' . esc_url( $prime_charity_trust_social_link3 ) . '" target="_blank"></a>';
                }
                if ( ! empty( $prime_charity_trust_social_link4 ) ) {
                  echo '<a href="' . esc_url( $prime_charity_trust_social_link4 ) . '" target="_blank"></a>';
                }
              ?>
            </div>
          <?php } ?>
        </div>
      <?php 
        }
      ?>

      <div class="site-menu">
          <nav id="site-navigation" class="main-navigation" role="navigation" aria-label="Primary Menu">
              <button type="button" class="menu-toggle">
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
                  <span class="icon-bar"></span>
              </button>

              <?php
              wp_nav_menu( array(
                  'theme_location' => 'primary',
                  'menu_id'        => 'primary-menu',
                  'menu_class'     => 'nav-menu',
                  'fallback_cb'    => 'prime_charity_trust_primary_navigation_fallback',
              ) );
              ?>
          </nav><!-- #site-navigation -->
          <?php if ( $prime_charity_trust_button_txt || $prime_charity_trust_button_url ){?>
            <div class="donate-btn">
              <a href="<?php echo esc_url($prime_charity_trust_button_url);?>"><?php echo esc_html($prime_charity_trust_button_txt);?></a>
            </div>
          <?php } ?>
      </div>
    </div>
  </div>
    
<?php }
endif;
add_action( 'prime_charity_trust_action_header', 'prime_charity_trust_site_branding', 10 );

if ( ! function_exists( 'prime_charity_trust_footer_top_section' ) ) :

/**
   * Top  Footer 
   *
   * @since 1.0.0
   */
function prime_charity_trust_footer_top_section() {
    $prime_charity_trust_footer_widgets_setting = get_theme_mod('prime_charity_trust_footer_widget_columns', '4');

    // Get Customizer Settings
    $prime_charity_trust_footer_bg_color = get_theme_mod('footer_bg_color', '#282828'); // Default color is white
    $prime_charity_trust_footer_bg_image = get_theme_mod('prime_charity_trust_footer_background_image'); // Background image

    if ($prime_charity_trust_footer_widgets_setting) { ?>
        <div class="footer-widgets-area section-gap <?php echo 'footer-' . esc_attr($prime_charity_trust_footer_widgets_setting); ?>" style="background-color: <?php echo esc_attr($prime_charity_trust_footer_bg_color); ?>; background-image: url('<?php echo esc_url($prime_charity_trust_footer_bg_image); ?>'); background-repeat: no-repeat; background-size: cover;">
            <!-- Widget area starting from here -->
            <div class="wrapper <?php echo 'footer-cols-' . $prime_charity_trust_footer_widgets_setting; ?>">
                <?php
                for ($i = 1; $i <= $prime_charity_trust_footer_widgets_setting; $i++) {
                    if (is_active_sidebar('footer-' . $i)) {
                ?>
                        <div class="custom-widget">
                            <?php dynamic_sidebar('footer-' . $i); ?>
                        </div>
                <?php
                    }
                }

                // Output default widgets if no widget is added
                for ($i = 1; $i <= $prime_charity_trust_footer_widgets_setting; $i++) {
                    if (!is_active_sidebar('footer-' . $i)) {
                        switch ($i) {
                            case 1:
                                ?>
                                <div class="custom-widget">
                                    <!-- Archive -->
                                    <aside id="archive" class="widget widget_archive" role="complementary" aria-label="secondsidebar">
                                        <h2 class="widget-title"><?php esc_html_e('Archive List', 'prime-charity-trust'); ?></h2>
                                        <ul>
                                            <?php wp_get_archives('type=monthly'); ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 2:
                                ?>
                                <div class="custom-widget">
                                    <!-- Recent Posts -->
                                    <aside id="recent-posts" class="widget widget_recent_posts" role="complementary" aria-label="thirdsidebar">
                                        <h2 class="widget-title"><?php esc_html_e('Recent Posts', 'prime-charity-trust'); ?></h2>
                                        <ul>
                                            <?php
                                            $args = array(
                                                'post_type'      => 'post',
                                                'posts_per_page' => 5,
                                            );
                                            $prime_charity_trust_recent_posts = new WP_Query($args);

                                            while ($prime_charity_trust_recent_posts->have_posts()) : $prime_charity_trust_recent_posts->the_post();
                                            ?>
                                                <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
                                            <?php endwhile; ?>
                                            <?php wp_reset_postdata(); ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 3:
                                ?>
                                <div class="custom-widget">
                                    <!-- Categories -->
                                    <aside id="categories" class="widget widget_categories" role="complementary" aria-label="fourthsidebar">
                                        <h2 class="widget-title"><?php esc_html_e('Categories', 'prime-charity-trust'); ?></h2>
                                        <ul>
                                            <?php
                                            $args = array(
                                                'title_li' => '',
                                            );
                                            wp_list_categories($args);
                                            ?>
                                        </ul>
                                    </aside>
                                </div>
                                <?php
                                break;
                            case 4:
                                ?>
                                <div class="custom-widget">
                                    <!-- Tags Widget -->
                                    <aside id="tags" class="widget widget_tags" role="complementary" aria-label="fifthsidebar">
                                        <h2 class="widget-title"><?php esc_html_e('Tags', 'prime-charity-trust'); ?></h2>
                                        <div class="tag-cloud">
                                            <?php wp_tag_cloud(); ?>
                                        </div>
                                    </aside>
                                </div>
                                <?php
                                break;
                            default:
                                break;
                        }
                    }
                }
                ?>
            </div>
        </div> <!-- widget area starting from here -->
    <?php }
}
endif;
add_action('prime_charity_trust_action_footer', 'prime_charity_trust_footer_top_section', 10);


if ( ! defined( 'PRIME_CHARITY_TRUST_FOOTER_URL' ) ) {
  define( 'PRIME_CHARITY_TRUST_FOOTER_URL', esc_url( 'https://www.themeignite.com/products/charitable-trust-wordpress-theme/', 'prime-charity-trust') );
}
if ( ! function_exists( 'prime_charity_trust_footer_section' ) ) :

  /**
   * Footer copyright
   *
   * @since 1.0.0
   */
  function prime_charity_trust_footer_section() {
    // Get footer settings
    $prime_charity_trust_footer_setting  = prime_charity_trust_get_option( 'prime_charity_trust_footer_setting' );

    if ( $prime_charity_trust_footer_setting ){ ?>
      <div class="site-info">
        <div class="wrapper">
          <span class="copyright">
            <?php 
              // Get the copyright text from the theme options
              $prime_charity_trust_copyright_footer = prime_charity_trust_get_option('copyright_text'); 

              // Display the copyright text if it is set, otherwise display the default text
              if ( ! empty( $prime_charity_trust_copyright_footer ) ) {
                echo wp_kses_post( $prime_charity_trust_copyright_footer );
              } else {
                echo esc_html__( '&copy; ', 'prime-charity-trust' ) . date_i18n( esc_html__( 'Y', 'prime-charity-trust' ) ); 
                echo ' <a href="' . esc_url( home_url( '/' ) ) . '">' . esc_html( get_bloginfo( 'name' ) ) . '</a>' . esc_html__( '. All Rights Reserved.', 'prime-charity-trust' );
              }
            ?>
          </span>
          <span class="by">
            <a href="<?php echo esc_url( PRIME_CHARITY_TRUST_FOOTER_URL ); ?>" rel="nofollow" target="_blank"><?php echo esc_html( 'Prime Charity Trust' ); ?></a>
            <?php echo esc_html__( ' By ', 'prime-charity-trust' ); ?>
            <a href="<?php echo esc_url( 'https://themeignite.com' ); ?>" rel="nofollow" target="_blank"><?php echo esc_html__( 'Themeignite', 'prime-charity-trust' ); ?></a>
            <?php echo sprintf( esc_html__( ' Powered By %s', 'prime-charity-trust' ), '<a href="'. esc_url( __( 'https://wordpress.org/', 'prime-charity-trust' ) ) .'" target="_blank">WordPress</a>.' ); ?>
            <?php if ( function_exists( 'the_privacy_policy_link' ) ) {
              echo get_the_privacy_policy_link();
            } ?>
          </span>
        </div>
      </div>
    <?php } ?>

    <?php
    // Scroll to top button
    $prime_charity_trust_scroll_top  = prime_charity_trust_get_option( 'header_donate_scroll_to_top', true );
    if ( $prime_charity_trust_scroll_top ){ ?>
      <a id="button"><i class="<?php echo esc_attr(get_theme_mod('prime_charity_trust_scroll_icon','fas fa-arrow-up')); ?>"></i></a>
    <?php }
  }

endif;
add_action( 'prime_charity_trust_action_footer', 'prime_charity_trust_footer_section', 20 );


if ( ! function_exists( 'prime_charity_trust_footer_sidebar_class' ) ) :
  /**
   * Count the number of footer sidebars to enable dynamic classes for the footer
   *
   * @since prime_charity_trust 0.1
   */
  function prime_charity_trust_footer_sidebar_class() {
    $data = array();
    $prime_charity_trust_active_sidebar = array();
      $count = 0;

      if ( is_active_sidebar( 'footer-1' ) ) {
        $prime_charity_trust_active_sidebar[]   = 'footer-1';
          $count++;
      }

      if ( is_active_sidebar( 'footer-2' ) ){
        $prime_charity_trust_active_sidebar[]   = 'footer-2';
          $count++;
    }

      if ( is_active_sidebar( 'footer-3' ) ){
        $prime_charity_trust_active_sidebar[]   = 'footer-3';
          $count++;
      }

      if ( is_active_sidebar( 'footer-4' ) ){
        $prime_charity_trust_active_sidebar[]   = 'footer-4';
          $count++;
      }

      $class = '';

      switch ( $count ) {
          case '1':
            $class = 'col-1';
            break;
          case '2':
            $class = 'col-2';
            break;
          case '3':
            $class = 'col-3';
            break;
          case '4':
            $class = 'col-4';
            break;
          case '0':
            $class = 'col-4';
            break;
      }

    $data['active_sidebar'] = $prime_charity_trust_active_sidebar;
    $data['class']        = $class;

      return $data;
  }
endif;

if ( ! function_exists( 'prime_charity_trust_banner_header' ) ) :
  /**
   * Page Header
  */
  function prime_charity_trust_banner_header() { 
     
      $prime_charity_trust_show_page_title    = prime_charity_trust_get_option( 'prime_charity_trust_show_page_title' );

      if ( is_front_page() && ! is_home() )
          return;

      // Get the custom header image from the customizer
      $prime_charity_trust_custom_header_image = get_theme_mod('prime_charity_trust_header_image');
      $prime_charity_trust_header_image = $prime_charity_trust_custom_header_image ? $prime_charity_trust_custom_header_image : get_header_image();

      if ( is_singular() ) :
          $prime_charity_trust_header_image = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : $prime_charity_trust_header_image;
      endif;
      ?>

      <?php echo '<div id="content" class= "wrapper section-gap">';
  }
endif;
add_action( 'prime_charity_trust_banner_header', 'prime_charity_trust_banner_header', 10 );



if ( ! function_exists( 'prime_charity_trust_posts_tags' ) ) :
  /**
   * Prints HTML with meta information for the current post-date/time and author.
   */
  function prime_charity_trust_posts_tags() {
    // Hide category and tag text for pages.
    if ( 'post' === get_post_type() && has_tag() ) { ?>
      <div class="tags-links">
          <h2 class="side-heading">Tags:</h2>
          <?php 
          $tags = get_the_tags();
          if ($tags) {
              foreach ($tags as $tag) {
                  echo '<span><a href="' . esc_url(get_tag_link($tag->term_id)) . '">' . esc_html($tag->name) . '</a></span>'; // WPCS: XSS OK.
              }
          } 
          ?>
      </div>
<!-- .tags-links -->
    <?php } 
  }
endif;