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

if( ! function_exists( 'prime_spa_center_site_branding' ) ) :
/**
* Site Branding
*
* @since 1.0.0
*/
function prime_spa_center_site_branding() { ?>
  <?php
  $prime_spa_center_button_text  = prime_spa_center_get_option( 'header_donate_btn_text');
  $prime_spa_center_button_text = ! empty( $prime_spa_center_button_text ) ? $prime_spa_center_button_text : 'Appointments';
  $prime_spa_center_button_url  = prime_spa_center_get_option( 'header_donate_btn_url' );
  $prime_spa_center_sticky_header = prime_spa_center_get_option('prime_spa_center_sticky_header');

  ?>
  <div id="header-box" class="header-main" data-sticky="<?php echo $prime_spa_center_sticky_header; ?>">
    <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', '100')); ?>px;">
            <?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_spa_center_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">
      <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_spa_center_primary_navigation_fallback',
              ) );
              ?>
          </nav><!-- #site-navigation -->
          <?php if ( $prime_spa_center_button_text || $prime_spa_center_button_url ){?>
            <div class="donate-btn">
              <a href="<?php echo esc_url($prime_spa_center_button_url);?>"><?php echo esc_html($prime_spa_center_button_text);?><i class="<?php echo esc_attr(get_theme_mod('prime_spa_center_button_icon','fas fas fa-calendar-alt')); ?>"></i></a>
            </div>
          <?php } ?>
      </div>
    </div>
  </div>
    
<?php }
endif;
add_action( 'prime_spa_center_action_header', 'prime_spa_center_site_branding', 10 );

if ( ! function_exists( 'prime_spa_center_footer_top_section' ) ) :

  /**
   * Top  Footer 
   *
   * @since 1.0.0
   */
function prime_spa_center_footer_top_section() {
    $prime_spa_center_footer_widgets_setting = get_theme_mod('prime_spa_center_footer_widget_columns', '4');

    // Get Customizer Settings
    $prime_spa_center_footer_bg_color = get_theme_mod('footer_bg_color', '#282828'); // Default color is white
    $prime_spa_center_footer_bg_image = get_theme_mod('prime_spa_center_footer_background_image'); // Background image

    if ($prime_spa_center_footer_widgets_setting) { ?>
        <div class="footer-widgets-area section-gap <?php echo 'footer-' . esc_attr($prime_spa_center_footer_widgets_setting); ?>" style="background-color: <?php echo esc_attr($prime_spa_center_footer_bg_color); ?>; background-image: url('<?php echo esc_url($prime_spa_center_footer_bg_image); ?>'); background-repeat: no-repeat; background-size: cover;">
            <!-- Widget area starting from here -->
            <div class="wrapper <?php echo 'footer-cols-' . $prime_spa_center_footer_widgets_setting; ?>">
                <?php
                for ($i = 1; $i <= $prime_spa_center_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_spa_center_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 wow bounceInUp center delay-1000" data-wow-duration="2s" role="complementary" aria-label="<?php esc_attr_e('secondsidebar', 'prime-spa-center'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Archive List', 'prime-spa-center'); ?></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 wow bounceInUp center delay-1000" data-wow-duration="2s" role="complementary" aria-label="<?php esc_attr_e('thirdsidebar', 'prime-spa-center'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Recent Posts', 'prime-spa-center'); ?></h2>
                                        <ul>
                                            <?php
                                            $args = array(
                                                'post_type'      => 'post',
                                                'posts_per_page' => 5,
                                            );
                                            $prime_spa_center_recent_posts = new WP_Query($args);

                                            while ($prime_spa_center_recent_posts->have_posts()) : $prime_spa_center_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 wow bounceInUp center delay-1000" data-wow-duration="2s" role="complementary" aria-label="<?php esc_attr_e('fourthsidebar', 'prime-spa-center'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Categories', 'prime-spa-center'); ?></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 wow bounceInUp center delay-1000" data-wow-duration="2s" role="complementary" aria-label="<?php esc_attr_e('fifthsidebar', 'prime-spa-center'); ?>">
                                        <h2 class="widget-title"><?php esc_html_e('Tags', 'prime-spa-center'); ?></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_spa_center_action_footer', 'prime_spa_center_footer_top_section', 10);

if ( ! function_exists( 'prime_spa_center_footer_section' ) ) :

  if ( ! defined( 'PRIME_SPA_CENTER_FOOTER_URL' ) ) {
  define( 'PRIME_SPA_CENTER_FOOTER_URL', esc_url( 'https://www.themeignite.com/products/free-spa-wordpress-theme', 'prime-spa-center') );
}

  /**
   * Footer copyright
   *
   * @since 1.0.0
   */
  function prime_spa_center_footer_section() { ?>
    <?php
    $prime_spa_center_footer_setting  = prime_spa_center_get_option( 'prime_spa_center_footer_setting' );
     if ( $prime_spa_center_footer_setting ){ ?>
      <div class="site-info">
        <?php 
          $prime_spa_center_copyright_footer = prime_spa_center_get_option('copyright_text'); 
          if ( ! empty( $prime_spa_center_copyright_footer ) ) {
            $prime_spa_center_copyright_footer = wp_kses_data( $prime_spa_center_copyright_footer );
          }
        ?>
        <div class="wrapper">
          <span class="copy-right"><a href="<?php echo esc_url( PRIME_SPA_CENTER_FOOTER_URL) ?>"><?php echo esc_html($prime_spa_center_copyright_footer);?></a></span>
        </div><!-- .wrapper --> 
      </div> <!-- .site-info -->
    <?php } ?>

    <?php
    $prime_spa_center_scroll_top  = prime_spa_center_get_option( 'header_donate_scroll_to_top' ,true);
     if ( $prime_spa_center_scroll_top ){ ?>
        <a id="button"><i class="fas fa-arrow-up"></i></a>
    <?php } ?>
  <?php }

endif;
add_action( 'prime_spa_center_action_footer', 'prime_spa_center_footer_section', 20 );

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

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

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

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

      if ( is_active_sidebar( 'footer-4' ) ){
        $prime_spa_center_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_spa_center_active_sidebar;
    $data['class']        = $class;

      return $data;
  }
endif;

if( ! function_exists( 'prime_spa_center_banner_header' ) ) :
    /**
     * Page Header
    */
    function prime_spa_center_banner_header() { 

      $prime_spa_center_show_header_image  = prime_spa_center_get_option( 'show_header_image' );
      $prime_spa_center_show_page_title    = prime_spa_center_get_option( 'show_page_title' );

      if ( is_front_page() && ! is_home() )
          return;
      $prime_spa_center_header_image = get_header_image();
      if ( is_singular() ) :
          $prime_spa_center_header_image = has_post_thumbnail() ? get_the_post_thumbnail_url( get_the_id(), 'full' ) : $prime_spa_center_header_image;
      endif;
      ?>

      <div id="page-site-header" class="<?php echo esc_attr($prime_spa_center_show_header_image); ?> <?php echo esc_attr($prime_spa_center_show_page_title); ?>" style="background-image: url('<?php echo esc_url( $prime_spa_center_header_image ); ?>');">
      <div class="overlay"></div>
        <header class='page-header'>
          <div class="wrapper">
            <?php prime_spa_center_banner_title();?>
          </div><!-- .wrapper -->
        </header>
      </div><!-- #page-site-header -->
      <?php echo '<div id="content" class= "wrapper section-gap">';
    }
endif;
add_action( 'prime_spa_center_banner_header', 'prime_spa_center_banner_header', 10 );

if( ! function_exists( 'prime_spa_center_banner_title' ) ) :
/**
 * Page Header
*/
function prime_spa_center_banner_title(){ 
    if ( ( is_front_page() && is_home() ) || is_home() ){ 
        $prime_spa_center_your_latest_posts_title = prime_spa_center_get_option( 'your_latest_posts_title' );?>
        <h2 class="page-title"><?php echo esc_html($prime_spa_center_your_latest_posts_title); ?></h2><?php
    }

    if( is_singular() ) {
        the_title( '<h2 class="page-title">', '</h2>' );
    }       

    if( is_archive() ){
        the_archive_description( '<div class="archive-description">', '</div>' );
        the_archive_title( '<h2 class="page-title">', '</h2>' );
    }

    if( is_search() ){ ?>
      <h2 class="page-title"><?php echo esc_html( 'Search Results for: ', 'prime-spa-center' ) . get_search_query(); ?></h2>
    <?php }
    
    if( is_404() ) {
        echo '<h2 class="page-title">' . esc_html__( 'Error 404', 'prime-spa-center' ) . '</h2>';
    }
}
endif;

if ( ! function_exists( 'prime_spa_center_posts_tags' ) ) :
  /**
   * Prints HTML with meta information for the current post-date/time and author.
   */
  function prime_spa_center_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;