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

if( ! function_exists( 'mavix_beat_site_branding' ) ) :
/**
* Site Branding
*
* @since 1.0.0
*/
function mavix_beat_site_branding() { ?>
        <div class="header-top">
            <div class="wrapper">
                <div class="site-branding">
                    <div class="site-logo">
                        <?php if(has_custom_logo()):?>
                            <?php the_custom_logo();?>
                        <?php endif;?>
                    </div><!-- .site-logo -->

                    <div id="site-identity">
                        <h1 class="site-title">
                            <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">  <?php bloginfo( 'name' ); ?></a>
                        </h1>

                        <?php 
                            $mavix_beat_description = get_bloginfo( 'description', 'display' );
                            if ( $mavix_beat_description || is_customize_preview() ) : ?>
                            <p class="site-description"><?php echo esc_html($mavix_beat_description);?></p>
                        <?php endif; ?>
                    </div><!-- #site-identity -->
                </div> <!-- .site-branding -->

                <?php $mavix_beat_show_contact_details  = mavix_beat_get_option( 'mavix_beat_show_contact_details' );

                if( true == mavix_beat_get_option('mavix_beat_show_contact_details') ) { ?>
                    <div class="site-contact-details clear">
                        <?php
                        $mavix_beat_address_one   = mavix_beat_get_option( 'mavix_beat_address_one' );
                        $mavix_beat_address_two   = mavix_beat_get_option( 'mavix_beat_address_two' );
                        $mavix_beat_phone_number  = mavix_beat_get_option( 'mavix_beat_phone_number' ); 
                        $mavix_beat_opening_time  = mavix_beat_get_option( 'mavix_beat_opening_time' ); 
                        $mavix_beat_email_id      = mavix_beat_get_option( 'mavix_beat_email_id' ); 
                        $mavix_beat_support_text  = mavix_beat_get_option( 'mavix_beat_support_text' ); 

                        ?>

                        <button type="button" class="top-header-menu-toggle">
                            <i class="fa-solid fa-bars-staggered open-icon"></i>
                            <i class="fa-solid fa-xmark close-icon"></i>
                        </button>

                        <ul>
                            <?php if ( ! empty( $mavix_beat_address_one ) || ! empty( $mavix_beat_address_two ) ) : ?>
                                <li>
                                    <i class="fa-regular fa-map"></i>
                                    <div class="list-details">

                                        <?php if ( ! empty( $mavix_beat_address_one ) ) : ?>
                                            <h2><?php echo esc_html( $mavix_beat_address_one ); ?></h2>
                                        <?php endif; ?>

                                        <?php if ( ! empty( $mavix_beat_address_two ) ) : ?>
                                            <span><?php echo esc_html( $mavix_beat_address_two ); ?></span>
                                        <?php endif; ?>

                                    </div><!-- .list-details -->
                                </li>
                            <?php endif; ?>

                            <?php if ( ! empty( $mavix_beat_phone_number ) || ! empty( $mavix_beat_opening_time ) ) : ?>
                                <li>
                                    <i class="fa-solid fa-mobile-screen-button"></i>
                                    <div class="list-details">

                                        <?php if ( ! empty( $mavix_beat_phone_number ) ) : ?>
                                            <h2><?php echo esc_html( $mavix_beat_phone_number ); ?></h2>
                                        <?php endif; ?>

                                        <?php if ( ! empty( $mavix_beat_opening_time ) ) : ?>
                                            <span><?php echo esc_html( $mavix_beat_opening_time ); ?></span>
                                        <?php endif; ?>

                                    </div><!-- .list-details -->
                                </li>
                            <?php endif; ?>

                            <?php if ( ! empty( $mavix_beat_email_id ) || ! empty( $mavix_beat_support_text ) ) : ?>
                                <li>
                                    <i class="fa-regular fa-envelope"></i>
                                    <div class="list-details">

                                        <?php if ( ! empty( $mavix_beat_email_id ) ) : ?>
                                            <h2><?php echo esc_html( $mavix_beat_email_id ); ?></h2>
                                        <?php endif; ?>

                                        <?php if ( ! empty( $mavix_beat_support_text ) ) : ?>
                                            <span><?php echo esc_html( $mavix_beat_support_text ); ?></span>
                                        <?php endif; ?>

                                    </div><!-- .list-details -->
                                </li>
                            <?php endif; ?>
                        </ul>
                    </div><!-- .site-contact-details -->
                <?php } ?>
            </div><!-- .wrapper -->
        </div><!-- .header-top -->

        <div class="header-bottom">
            <div class="wrapper">
                <nav id="site-navigation" class="main-navigation" aria-label="<?php echo esc_attr__('Primary Menu', 'mavix-beat'); ?>">
                    <button type="button" class="menu-toggle" aria-label="<?php esc_attr_e( 'Toggle navigation', 'mavix-beat' ); ?>" aria-expanded="false" aria-controls="site-navigation">
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                        <span class="icon-bar"></span>
                    </button>

                    <?php
                    $mavix_beat_show_menu_button   = mavix_beat_get_option( 'mavix_beat_show_menu_button' );
                    $mavix_beat_menu_button_text   = mavix_beat_get_option( 'mavix_beat_menu_button_text' );
                    $mavix_beat_menu_button_url    = mavix_beat_get_option( 'mavix_beat_menu_button_url' );

                    $mavix_beat_custom_button = '';
                    if( $mavix_beat_show_menu_button ) {
                        $mavix_beat_custom_button .= '<div class="custom-menu-item"><a href="'. esc_url( $mavix_beat_menu_button_url ) .'" class="btn">'
                                . esc_html( $mavix_beat_menu_button_text ) .'</a>
                                </div>';
                    }

                    wp_nav_menu( array(
                        'theme_location' => 'primary',
                        'menu_id'        => 'primary-menu',
                        'menu_class'     => 'nav-menu',
                        'fallback_cb'    => 'mavix_beat_primary_navigation_fallback',
                    ) );
                    ?>

                    <?php echo wp_kses_post( $mavix_beat_custom_button ); ?>
                </nav><!-- #site-navigation -->
            </div><!-- .wrapper -->
        </div><!-- .header-bottom -->
<?php }
endif;
add_action( 'mavix_beat_action_header', 'mavix_beat_site_branding', 10 );

if ( ! function_exists( 'mavix_beat_footer_top_section' ) ) :

  /**
   * Top  Footer 
   *
   * @since 1.0.0
   */
  function mavix_beat_footer_top_section() {
      $mavix_beat_footer_sidebar_data = mavix_beat_footer_sidebar_class();
      $mavix_beat_footer_sidebar    = $mavix_beat_footer_sidebar_data['active_sidebar'];
      $mavix_beat_footer_class      = $mavix_beat_footer_sidebar_data['class'];
      if ( empty( $mavix_beat_footer_sidebar ) ) {
        return;
      }      ?>
      <div class="footer-widgets-area section-gap <?php echo esc_attr( $mavix_beat_footer_class ); ?>"> <!-- widget area starting from here -->
          <div class="wrapper">
            <?php
              for ( $i = 1; $i <= 4 ; $i++ ) {
                if ( is_active_sidebar( 'footer-' . $i ) ) {
                ?>
                  <div class="hentry">
                    <?php dynamic_sidebar( 'footer-' . $i ); ?>
                  </div>
                  <?php
                }
              }
            ?>
            </div>
          
      </div> <!-- widget area starting from here -->
    <?php
 }
endif;

add_action( 'mavix_beat_action_footer', 'mavix_beat_footer_top_section', 10 );

if ( ! function_exists( 'mavix_beat_footer_section' ) ) :

  /**
   * Footer copyright
   *
   * @since 1.0.0
   */
  function mavix_beat_footer_section() { ?>
    <div class="site-info">    
        <?php 
            $mavix_beat_copyright_footer = mavix_beat_get_option('copyright_text'); 
            if ( ! empty( $mavix_beat_copyright_footer ) ) {
                $mavix_beat_copyright_footer = wp_kses_data( $mavix_beat_copyright_footer );
            }
            // Powered by content.
            $mavix_beat_powered_by_text = sprintf( __( ' Theme Mavix Beat by %s', 'mavix-beat' ), '<a target="_blank" rel="designer" href="'. esc_url( 'https://creativthemes.com/' ) .'">Creativ Themes</a>' );
        ?>
        <div class="wrapper">
            <span class="copy-right"><?php echo esc_html($mavix_beat_copyright_footer);?><?php echo wp_kses_post($mavix_beat_powered_by_text); ?></span>
        </div><!-- .wrapper --> 
    </div> <!-- .site-info -->
    
  <?php }

endif;
add_action( 'mavix_beat_action_footer', 'mavix_beat_footer_section', 20 );

if ( ! function_exists( 'mavix_beat_footer_sidebar_class' ) ) :
  /**
   * Count the number of footer sidebars to enable dynamic classes for the footer
   *
   * @since mavix_beat 0.1
   */
  function mavix_beat_footer_sidebar_class() {
    $mavix_beat_data = array();
    $mavix_beat_active_sidebar = array();
      $mavix_beat_count = 0;

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

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

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

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

      $mavix_beat_class = '';

      switch ( $mavix_beat_count ) {
          case '1':
            $mavix_beat_class = 'col-1';
            break;
          case '2':
            $mavix_beat_class = 'col-2';
            break;
          case '3':
            $mavix_beat_class = 'col-3';
            break;
            case '4':
            $mavix_beat_class = 'col-4';
            break;
      }

    $mavix_beat_data['active_sidebar'] = $mavix_beat_active_sidebar;
    $mavix_beat_data['class']        = $mavix_beat_class;

      return $mavix_beat_data;
  }
endif;

if ( ! function_exists( 'mavix_beat_excerpt_length' ) ) :

  /**
   * Implement excerpt length.
   *
   * @since 1.0.0
   *
   * @param int $length The number of words.
   * @return int Excerpt length.
   */
  function mavix_beat_excerpt_length( $length ) {

    if ( is_admin() ) {
      return $length;
    }

    $mavix_beat_excerpt_length = mavix_beat_get_option( 'excerpt_length' );

    if ( absint( $mavix_beat_excerpt_length ) > 0 ) {
      $mavix_beat_length = absint( $mavix_beat_excerpt_length );
    }

    return $mavix_beat_length;
  }

endif;

add_filter( 'excerpt_length', 'mavix_beat_excerpt_length', 999 );

if( ! function_exists( 'mavix_beat_banner_header' ) ) :
    /**
     * Page Header
    */
    function mavix_beat_banner_header() { ?>

        <?php if( !is_front_page() || is_home() ) { ?>

            <div id="header-image">

                <?php
                $header_image = get_header_image();

                if ( $header_image ) {
                    the_custom_header_markup();
                } else {
                    ?>
                    <div id="wp-custom-header" class="wp-custom-header">
                        <img src="<?php echo esc_url( get_template_directory_uri() . '/assets/images/default-header.jpg' ); ?>" alt="Default Header">
                    </div><!-- #wp-custom-header -->
                    <?php
                }
                ?>

                <header class='page-header'>
                    <div class="wrapper">
                        <?php mavix_beat_banner_title();?>
                    </div><!-- .wrapper -->
                </header>

            </div><!-- #header-image -->

        <?php } ?>

        <?php
    }
endif;

add_action( 'mavix_beat_banner_header', 'mavix_beat_banner_header', 10 );

if( ! function_exists( 'mavix_beat_banner_title' ) ) :
/**
 * Page Header
*/
function mavix_beat_banner_title(){ 
    if ( ( is_front_page() && is_home() ) || is_home() ){ 
        $mavix_beat_your_latest_posts_title = mavix_beat_get_option( 'your_latest_posts_title' );?>
        <h2 class="page-title"><?php echo esc_html($mavix_beat_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 printf( esc_html__( 'Search Results for: %s', 'mavix-beat' ), '<span>' . get_search_query() . '</span>' ); ?></h2>
    <?php }
    
    if( is_404() ) {
        echo '<h2 class="page-title">' . esc_html__( 'Error 404', 'mavix-beat' ) . '</h2>';
    }
}
endif;

if ( ! function_exists( 'mavix_beat_posts_tags' ) ) :
    /**
     * Prints HTML with meta information for the current post-date/time and author.
     */
    function mavix_beat_posts_tags() {
        // Hide category and tag text for pages.
        if ( 'post' === get_post_type() && has_tag() ) { ?>
                <div class="tags-links">

                    <?php /* translators: used between list items, there is a space after the comma */
                    $mavix_beat_tags = get_the_tags();
                    if ( $mavix_beat_tags ) {

                        foreach ( $mavix_beat_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;