<?php
/**
 * Eisai template functions.
 */

if ( ! function_exists( 'eisai_no_js_class' ) ) {
	/**
	 * If we're missing JavaScript support, the HTML element will have a no-js class.
	 */
	function eisai_no_js_class() {
		?>
		<script>document.documentElement.className = document.documentElement.className.replace( 'no-js', 'js' );</script>
		<?php
	}
}

if ( ! function_exists( 'eisai_body_classes' ) ) {
	/**
	 * Add conditional body classes.
	 *
	 * @param array $classes Classes added to the body tag.
	 *
	 * @return array $classes Classes added to the body tag.
	 */
	function eisai_body_classes( $classes ) {

		global $post;

		// Check for post thumbnail.
		if ( is_singular() && has_post_thumbnail() ) {
			$classes[] = 'has-post-thumbnail';
		} elseif ( is_singular() ) {
			$classes[] = 'missing-post-thumbnail';
		}

		// Check if avatars are visible.
		$classes[] = get_option( 'show_avatars' ) ? 'show-avatars' : 'hide-avatars';

		return $classes;

	}
}

if ( ! function_exists( 'eisai_filter_primary_nav_menu_css_class' ) ) {
    /**
	 * Add 'uk-parent' class to primary navigation menu items that has children menus.
	 */
    function eisai_filter_primary_nav_menu_css_class( $classes, $item, $args, $depth ) {

        if ( 'primary' !== $args->theme_location ) {
            return $classes;
        }

        if ( in_array( 'menu-item-has-children', $classes ) ) {
            array_push( $classes, 'uk-parent' );
        }

        return $classes;

    }
}

if ( ! function_exists( 'eisai_filter_navbar_nav_menu_item_args' ) ) {
	/**
	 * Add UIkit chevron-down icon to navbar menu item with childrens.
	 */
	function eisai_filter_navbar_nav_menu_item_args( $args, $item, $depth ) {

        // Place chevron-down icon after a link element.
    	if ( 'navbar' == $args->theme_location ) {
    	    $args->link_after = ( $args->walker->has_children )
    			? '<span data-uk-icon="icon: chevron-down; ratio: .65" style="margin-left: 2px;"></span>'
    			: '';
    	}

        return $args;
    }
}

if ( ! function_exists( 'eisai_filter_social_menu_icons' ) ) {
    /**
    * Add icons and other attributes to social menu links.
    */
    function eisai_filter_social_menu_icons( $atts, $item, $args ) {

        // Add special attributes to social link menu items.
        if ( $args->theme_location == 'social' ) {
            $atts['target'] 		= '_blank';
    		$atts['rel'] 			= 'nofollow';
    		$atts['class'] 			= 'uk-icon-button';
			$atts['data-uk-icon'] 	= eisai_get_social_icon_id( $item->url );
        }

        return $atts;
    }
}

if ( ! function_exists( 'eisai_filter_the_archive_title' ) ) {
	/**
	 * Display avatar on Author's arhive page.
	 */
	function eisai_filter_the_archive_title( $title ) {

		if ( is_author() ) {

			$title = '';

			if ( true == get_option( 'show_avatars' ) ) {
				$title  = '<span class="avatar-wrapper">' . wp_kses_post( get_avatar( get_the_author_meta( 'ID' ) ) ) . '</span>';
			}

			$title .= '<span class="vcard">' . get_the_author() . '</span>';
		}

	    return $title;
	}
}

if ( ! function_exists( 'eisai_filter_comments_popup_link_attributes' ) ) {
	/**
	 *  Scroll to comments section when comments popup link is clicked.
	 */
	function eisai_filter_comments_popup_link_attributes( $attributes ) {
	    $attributes .= ' data-uk-scroll';

	    return $attributes;
	}
}

if ( ! function_exists( 'eisai_skip_link' ) ) {
    /**
     * Include a skip to content link at the top of the page so that users can bypass the menu.
     */
    function eisai_skip_link() {
		printf( '<a class="skip-link screen-reader-text" href="#site-content" title="%s">%s</a>', esc_attr__( 'Skip to the content', 'eisai' ), esc_html__( 'Skip to the content', 'eisai' ) );
    }
}

if ( ! function_exists( 'eisai_primary_navigation_toggle' ) ) {
    /**
     * Display primary navigation toggle button.
     */
    function eisai_primary_navigation_toggle() {
		if ( has_nav_menu( 'primary' ) ) {
			?>
			<div class="uk-navbar-item">
				<button id="primary-navigation-toggle" class="uk-button uk-button-text" data-uk-toggle="target: #primary-navigation" data-uk-icon="eisai-menu">
					<span class="screen-reader-text"><?php esc_html_e( 'Menu', 'eisai' ); ?></span>
				</button>
			</div>
			<?php
		}
	}
}

if ( ! function_exists( 'eisai_primary_navigation' ) ) {
    /**
     * Output primary navigation markup.
     */
    function eisai_primary_navigation() {
    	get_template_part( 'partials/primary-navigation' );
    }
}

if ( ! function_exists( 'eisai_navbar_navigation' ) ) {
    /**
     * Output navbar navigation menu markup.
     */
    function eisai_navbar_navigation() {
    	get_template_part( 'partials/navbar-navigation' );
    }
}

if ( ! function_exists( 'eisai_site_branding' ) ) {
    /**
     * Display site branding.
     */
    function eisai_site_branding() {
		if ( has_custom_logo() ) {
			if ( function_exists( 'the_custom_logo' ) ) {
				if ( is_home() ) {
					echo '<h1>';
						the_custom_logo();
					echo '</h1>';
				} else {
					the_custom_logo();
				}
			}
		} else {
			if ( is_home() ) {
				?>
				<h1>
					<a class="uk-logo" href="<?php echo esc_url( home_url() ); ?>" rel="home">
						<?php bloginfo( 'name' ); ?>
					</a>
				</h1>
				<?php
			} else {
				?>
				<a class="uk-logo" href="<?php echo esc_url( home_url() ); ?>" rel="home">
					<?php bloginfo( 'name' ); ?>
				</a>
				<?php
			}
		}
    }
}

if ( ! function_exists( 'eisai_search_toggle' ) ) {
    /**
     * Display search toggle button.
     */
    function eisai_search_toggle() {
		?>
		<div class="uk-navbar-item">
			<button id="search-modal-toggle" class="uk-button uk-button-text" data-uk-toggle="target: #search-modal" data-uk-icon="eisai-search">
				<span class="screen-reader-text"><?php esc_html_e( 'Search', 'eisai' ); ?></span>
			</button>
		</div>
		<?php
	}
}

if ( ! function_exists( 'eisai_search_modal' ) ) {
    /**
     * Output search modal markup.
     */
    function eisai_search_modal() {
    	get_template_part( 'partials/search-modal' );
    }
}

if ( ! function_exists( 'eisai_archive_header' ) ) {
    /**
     * Output header section on blog page, search, archives, taxonimies, etc.
     */
    function eisai_archive_header() {

		$archive_title    = '';
		$archive_description = '';

		if ( is_search() ) {

			global $wp_query;

			$archive_title = sprintf(
				'%1$s %2$s',
				__( 'Search:', 'eisai' ),
				'<span class="search-query">' . get_search_query() . '</span>'
			);

			if ( $wp_query->found_posts ) {

				if ( 1 === (int) $wp_query->found_posts ) {
					$archive_description = __( 'We found single result for your search:', 'eisai');
				} else {
					$archive_description = __( 'Here are the results we found on your request:', 'eisai' );
				}

			} else {
				$archive_description = __( 'We could not find any results for your search. You can give it another try through the search form below.', 'eisai' );
			}

		} elseif ( is_archive() ) {

			$archive_title		 = get_the_archive_title();
			$archive_description = get_the_archive_description();

		} elseif ( is_home() ) {

			$archive_title		 = get_the_title( get_option( 'page_for_posts', true ) );
			$archive_description = get_bloginfo( 'description' );

		}

		if ( $archive_title || $archive_description ) {
			?>
			<header class="archive-header uk-section <?php echo ( $archive_description ) ? '' : 'uk-margin'; //phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- static output ?>" data-uk-scrollspy="cls: uk-animation-fade" style="visibility: hidden;">

				<div class="uk-container">

					<?php

					/**
					 * Check whether the 'show_categories_on_blog_page' is activated in the Customizer. Default is false.
					 */
					$show_categories = get_theme_mod( 'show_categories_on_blog_page', false );

					if ( true === $show_categories && ! is_search() ) {

						if ( is_home() || is_category() ) {

							eisai_archive_categories( $archive_title );

						} else {

							if ( $archive_title ) {
								?>
								<h1 class="archive-title">
									<?php echo wp_kses_post( $archive_title ); ?>
								</h1> <!-- .archive-title -->
								<?php
							}

						}

					}

					if ( false === $show_categories || is_search() ) {
						if ( $archive_title ) {
							?>
							<h1 class="archive-title">
								<?php echo wp_kses_post( $archive_title ); ?>
							</h1> <!-- .archive-title -->
							<?php
						}
					}

					if ( $archive_description ) {
						?>
						<div class="archive-description uk-margin">
							<?php echo wp_kses_post( wpautop( $archive_description ) ); ?>
						</div> <!-- .archive-description -->
						<?php
					}

					?>

				</div>

			</header> <!-- .archive-header -->
			<?php
		}
    }
}

if ( ! function_exists( 'eisai_loop_wrapper_start' ) ) {
    /**
     * Output loop container and grid wrapper.
     */
    function eisai_loop_wrapper_start() {
		?>
		<div class="uk-container uk-container-xlarge">
			<div class="loop uk-grid uk-grid-large uk-child-width-1-2@m" data-uk-grid>
			<?php
    }
}

if ( ! function_exists( 'eisai_loop_wrapper_end' ) ) {
    /**
     * Output loop container and grid wrapper closing tags.
     */
    function eisai_loop_wrapper_end() {
			?>
			</div> <!-- .loop -->
		</div> <!-- .uk-container -->
		<?php
    }
}

function eisai_footer_sidebars() {
	get_template_part( 'partials/footer-sidebars' );
}

if ( ! function_exists( 'eisai_copyright' ) ) {
    /**
     * Display copyright text.
     */
    function eisai_copyright() {
		?>
		<div class="copyright uk-flex uk-flex-middle uk-flex-center uk-flex-left@l uk-height-1-1">
			<?php echo sprintf( '&copy; %d,&#8194;<a class="uk-link-text" href="%s" rel="home">%s</a>', esc_html( date_i18n( date('Y') ) ), esc_url( home_url( '/' ) ), esc_html( get_bloginfo( 'name' ) ) ); ?>
		</div>
		<?php
    }
}

if ( ! function_exists( 'eisai_footer_social_menu' ) ) {
    /**
     * Display social icons menu.
     */
    function eisai_footer_social_menu() {
		if ( has_nav_menu( 'social' ) ) :
        ?>
		<div class="uk-navbar uk-navbar-container uk-navbar-transparent">
			<div class="uk-navbar-center">
				<div class="uk-navbar-item">
		            <nav class="footer-social-menu uk-width-auto@m uk-navbar-container uk-navbar-transparent" role="navigation" aria-label="<?php _e( 'Social Links Menu', 'eisai' ); ?>" data-uk-navbar>
		                <?php wp_nav_menu( array(
		                    'theme_location' => 'social',
		                    'container' => false,
		                    'menu_class' => 'uk-navbar-nav uk-navbar-center',
		                    'before' => '<div class="uk-display-inline-block">',
		                    'after' => '</div>',
		                    'link_before' => '<span class="screen-reader-text">',
		                    'link_after' => '</span>',
		                ) ); ?>
		            </nav>
				</div>
			</div>
		</div>
        <?php
        endif;
    }
}

if ( ! function_exists( 'eisai_theme_developer' ) ) {
    /**
     * Dispaly link to a developers website.
     */
    function eisai_theme_developer() {
    	?>
		<div class="uk-flex uk-flex-middle uk-flex-center uk-flex-right@l uk-height-1-1">
			<?php if ( '' !== get_privacy_policy_url() ) : ?>
				<a class="uk-link-text" href="<?php echo esc_url( get_privacy_policy_url(), 'eisai' ); ?>">
					<?php esc_html_e( 'Privacy Policy', 'eisai' ); ?>
				</a>
			<?php endif; ?>
			&#8195;
			<a class="uk-link-text" href="<?php echo esc_url( __( 'https://webminimalism.com/wordpress-themes/', 'eisai' ) ); ?>" rel="dofollow">
				<?php esc_html_e( 'Eisai theme by Ivan Fonin', 'eisai' ); ?>
			</a>
		</div>
		<?php
    }
}
