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

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

if ( ! function_exists( 'basho_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 basho_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';

       // Check if footer background color is different then body background color, we'll add some padding to the content via CSS in this case.
       $site_bg = ( get_theme_mod( 'basho_site_background_color' ) ) ? sanitize_hex_color( get_theme_mod( 'basho_site_background_color' ) ) : '#ffffff';
       $footer_bg = ( get_theme_mod( 'basho_site_footer_background_color' ) ) ? sanitize_hex_color( get_theme_mod( 'basho_site_footer_background_color' ) ) : '#ffffff';

       if ( $footer_bg !== $site_bg ) {
           $classes[] = 'site-footer-has-background';
       }

       // Adding sticky-header class to the body element to apply CSS scroll-padding-top rule.
       $header_is_sticky = get_theme_mod( 'basho_sticky_header', false );

       if ( $header_is_sticky ) {
           $classes[] = 'sticky-header';
       }

       return $classes;

   }
}

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

        // Skip the rest if it is not a main menu.
        if ( 'offcanvas' !== $args->theme_location ) {
            return $classes;
        }

        // For the main menu add 'uk-parent' class to elements that has children items.
        if ( in_array( 'menu-item-has-children', $classes ) ) {
            array_push( $classes, 'uk-parent' );
        }

        return $classes;

    }
}

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

        // Place chevron-down icon after a link element.
    	if ( 'main' == $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( 'basho_filter_social_menu_icons' ) ) {
    /**
     * Add icons and other attributes to social menu links.
     */
    function basho_filter_social_menu_icons( $atts, $item, $args ) {

        // Check if item url has a certain string and add UIkit icon attribute to link if so.
        if ( $args->theme_location == 'social' ) {
            $atts['target'] 		= '_blank';
    		$atts['rel'] 			= 'nofollow';
    		$atts['class'] 			= 'uk-icon-button';
			$atts['data-uk-icon'] 	= basho_get_social_icon_id( $item->url );
        }

        return $atts;

    }
}

if ( ! function_exists( 'basho_filter_the_archive_title' ) ) {
    /**
     * Add UIkit classes and icons to archive titles.
     */
    function basho_filter_the_archive_title( $title ) {

    	if ( is_tag() ) {
            // I like to use '#' symbol before hashtag. That's why I added this filter.
            $prefix = apply_filters( 'basho_archive_tag_title_prefix', '' );

            $title = single_tag_title( $prefix, false );
        }

    	if ( is_author() ) {
            $title = '<span>' . get_the_author() . '</span>';
        }

    	return $title;

    }
}

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

	    return $attributes;
	}
}

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

if ( ! function_exists( 'basho_offcanvas_navigation_toggle' ) ) {
    /**
     * Display offcanvas navigation toggle button if the WooCommerce plugin is activated.
     */
    function basho_offcanvas_navigation_toggle() {

        if ( has_nav_menu( 'offcanvas' ) && basho_is_woocommerce_activated() ) {
			?>
			<div class="uk-navbar-item">
				<button id="offcanvas-navigation-toggle" class="uk-button uk-button-text" data-uk-toggle="target: #offcanvas-navigation" data-uk-icon="icon: basho-menu; ratio: .9">
					<span class="screen-reader-text"><?php esc_html_e( 'Menu', 'basho' ); ?></span>
				</button>
			</div>
			<?php
		}

	}
}

if ( ! function_exists( 'basho_header_logotype' ) ) {
    /**
     * Display logotype.
     */
    function basho_header_logotype() {

        echo '<div class="uk-navbar-item site-logo">';

            if ( has_custom_logo() ) {
                the_custom_logo();
            } else {
                ?>
                <a class="uk-logo" href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home">
                    <?php bloginfo( 'name' ); ?>
                </a>
                <?php
            }

        echo '</div>';

    }
}

if ( ! function_exists( 'basho_filter_get_custom_logo' ) ) {
    /**
     * Filter custom logo markup.
     */
    function basho_filter_get_custom_logo( $html ) {

        $logo_id = get_theme_mod( 'custom_logo' );

    	if ( ! $logo_id ) {
    		return $html;
    	}

    	$logo = wp_get_attachment_image_src( $logo_id, 'full' );

    	if ( $logo ) {
    		// For clarity.
    		$logo_width  = esc_attr( $logo[1] );
    		$logo_height = esc_attr( $logo[2] );

    		// If the basho_retina_logo logo setting is active, reduce the width/height by half.
    		if ( get_theme_mod( 'basho_retina_logo', false ) ) {
    			$logo_width  = floor( $logo_width / 2 );
    			$logo_height = floor( $logo_height / 2 );

    			$search = array(
    				'/width=\"\d+\"/iU',
    				'/height=\"\d+\"/iU',
    			);

    			$replace = array(
    				"width=\"{$logo_width}\"",
    				"height=\"{$logo_height}\"",
    			);

    			// Add a style attribute with the height, or append the height to the style attribute if the style attribute already exists.
    			if ( strpos( $html, ' style=' ) === false ) {
    				$search[]  = '/(src=)/';
    				$replace[] = "style=\"height: {$logo_height}px;\" src=";
    			} else {
    				$search[]  = '/(style="[^"]*)/';
    				$replace[] = "$1 height: {$logo_height}px;";
    			}

    			$html = preg_replace( $search, $replace, $html );

    		}
    	}

    	return $html;

    }
}

if ( ! function_exists( 'basho_header_navbar' ) ) {
    /**
     * Display main navigation.
     */
    function basho_header_navbar() {

        if ( has_nav_menu( 'main' ) ) {
            ?>
            <div class="uk-navbar-item">
                <?php
                wp_nav_menu( array(
                    'theme_location' => 'main',
                    'container' => false,
                    'menu_class' => 'uk-navbar-nav uk-visible@m',
                    'walker' => new Basho_Walker_Navbar_Navigation(),
                ) );
                ?>
            </div>
            <?php
        }

    }
}

if ( ! function_exists( 'basho_header_search_form' ) ) {
    /**
     * Display search form or search toggle button in the site header. Or completely hide search from.
     */
    function basho_header_search_form() {

        /**
    	 * Check whether the 'basho_show_search_form' option is 'on' in the Customizer.
         */
    	$show_search_form_in_header = get_theme_mod( 'basho_show_search_form', true );

    	if ( true === $show_search_form_in_header ) {
            ?>

            <div class="uk-navbar-item uk-width-1-1 uk-visible@s">
                <?php get_search_form(); ?>
            </div>

            <?php
        }

    }
}

if ( ! function_exists( 'basho_header_search_modal_toggle' ) ) {
    /**
     * Display search modal toggle button.
     */
    function basho_header_search_modal_toggle() {

        /**
    	 * Check whether the 'basho_show_search_form' option is 'on' in the Customizer.
         */
    	$show_search_form_in_header = get_theme_mod( 'basho_show_search_form', true );

        if ( ! $show_search_form_in_header ) {
            ?>

            <div class="uk-navbar-item uk-padding-remove-left">
                <button class="uk-button uk-button-link" id="search-modal-toggle" data-uk-toggle="target: #search-modal" data-uk-icon="icon: basho-search; ratio: .85">
                    <span class="screen-reader-text"><?php esc_html_e( 'Search', 'basho' ); ?></span>
                </button>
            </div>

            <?php
        }

    }
}

if ( ! function_exists( 'basho_offcanvas_navigation_toggle_no_woocommerce' ) ) {
    /**
     * Display offcanvas navigation toggle button if the WooCommerce plugin is NOT activated.
     */
    function basho_offcanvas_navigation_toggle_no_woocommerce() {

        if ( has_nav_menu( 'offcanvas' ) && ( ! basho_is_woocommerce_activated() ) ) {
			?>
			<div class="uk-navbar-item">
				<button id="offcanvas-navigation-toggle" class="uk-button uk-button-text" data-uk-toggle="target: #offcanvas-navigation" data-uk-icon="icon: basho-menu; ratio: .9">
					<span class="screen-reader-text"><?php esc_html_e( 'Menu', 'basho' ); ?></span>
				</button>
			</div>
			<?php
		}

	}
}

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

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

		if ( is_search() ) {

			global $wp_query;

			$archive_title = sprintf(
				'%1$s %2$s',
				__( 'Search:', 'basho' ),
				'<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:', 'basho');
				} else {
					$archive_description = __( 'Here are the results we found on your request:', 'basho' );
				}

			} else {
				$archive_description = __( 'We could not find any results for your search. Maybe you can give it another try.', 'basho' );
			}

		} elseif ( is_archive() ) {

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

		} elseif ( is_home() ) {

			$archive_title		 = ( get_option( 'page_for_posts', true ) ) ? get_the_title( get_option( 'page_for_posts', true ) ) : __( 'Blog', 'basho' );
			$archive_description = get_bloginfo( 'description' );

		}

		if ( $archive_title || $archive_description ) {

            $archive_header_classnames = ( $archive_description ) ? ' has-description' : ' no-description';
            $archive_header_classnames .= ( get_theme_mod( 'basho_pro_show_blog_categories', false ) ) ? ' categories-shown' : ' categories-hidden';

			?>
			<header class="archive-header uk-section uk-section-xsmall<?php echo esc_attr( $archive_header_classnames ); ?>">
				<div class="uk-container<?php echo esc_attr( basho_get_container_width_class( 'archive-header' ) ); ?>">

					<?php

                    if ( $archive_title ) {
                        ?>
                        <h1 class="archive-title">
                            <?php

                            // Display author's avatar on the author archive page.
                            if ( is_author() ) {

                                $author_avatar = get_avatar( get_the_author_meta( 'ID' ), 160 );

                                if ( $author_avatar ) {
                                    ?>
                                    <div class="author-avatar">
                                        <?php echo wp_kses_post( $author_avatar ); ?>
                                    </div>
                                    <?php
                                 }

                             }

                             // Display archive title.
                             echo wp_kses_post( $archive_title );

                             ?>
                        </h1> <!-- .archive-title -->
                        <?php
                    }

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

					?>

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

if ( ! function_exists( 'basho_blog_categories' ) ) {
    /**
     * Show categories before loop (blog posts).
     */
    function basho_blog_categories() {

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

        if ( false === $show_blog_categories || is_search() ) {
            return;
        }

        ?>

        <div class="uk-container uk-section-xsmall uk-padding-remove-top<?php echo esc_attr( basho_get_container_width_class( 'loop' ) ); ?>">
            <?php basho_show_blog_categories(); ?>
        </div>

		<?php

    }
}

if ( ! function_exists( 'basho_loop_wrapper_start' ) ) {
    /**
     * Output loop container and grid wrapper.
     */
    function basho_loop_wrapper_start() {

        // Get grid classes based on Customizer settings.
        $blog_grid_classes  = basho_get_blog_grid_columns_classes();

		?>
		<div class="uk-container<?php echo esc_attr( basho_get_container_width_class( 'loop' ) ); ?>">
			<div class="loop uk-grid<?php echo esc_attr( $blog_grid_classes ); ?>" data-uk-grid="<?php echo esc_attr( apply_filters( 'basho_blog_grid_settings', '' ) ); ?>">
			<?php
    }
}

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

if ( ! function_exists( 'basho_footer_sidebars' ) ) {
    /**
     * Output footer sidebars section.
     */
    function basho_footer_sidebars() {
        get_template_part( 'partials/footer-sidebars' );
    }
}

if ( ! function_exists( 'basho_print_footer_left_section' ) ) {
    /**
     * Print footer left section.
     */
    function basho_print_footer_left_section() {
        ?>
        <div class="copyright uk-flex-last uk-flex-first@m  uk-text-center uk-text-left@m uk-text-small">
            <?php
            echo esc_html( date_i18n(
                /* translators: Copyright date format, see https://secure.php.net/date */
                _x( 'Y', 'copyright date format', 'basho' )
            ) );
            ?>
            &copy;
            <a href="<?php echo esc_url( home_url( '/' ) ); ?>" rel="home"><?php echo bloginfo( 'name' ); ?></a><?php esc_html_e( ', works on ', 'basho' ) ?>
            <a href="<?php echo esc_url( __( 'https://wordpress.org/', 'basho' ) ); ?>" target="_blank" class="uk-text-small">WordPress</a>
        </div>
        <?php
    }
}

if ( ! function_exists( 'basho_print_footer_center_section' ) ) {
    /**
     * Print footer center section.
     */
    function basho_print_footer_center_section() {

        if ( has_nav_menu( 'social' ) ) {
        ?>
            <nav class="footer-social-menu uk-width-auto@m uk-navbar-container uk-navbar-transparent" role="navigation" aria-label="<?php esc_attr_e( 'Social Links Menu', 'basho' ); ?>" 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 tm-padding-xsmall">',
                    'after' => '</div>',
                    'link_before' => '<span class="screen-reader-text">',
                    'link_after' => '</span>',
                ) ); ?>
            </nav>
        <?php
        }

    }
}

if ( ! function_exists( 'basho_print_footer_right_section' ) ) {
    /**
     * Print footer right section.
     */
    function basho_print_footer_right_section() {
        ?>
        <div class="designer uk-text-center uk-text-right@m uk-text-small">
            <?php printf( '%s <a href="%s" target="_blank">%s</a>', esc_html( 'Basho theme by', 'basho' ), esc_url( 'https://webminimalism.com/' ), esc_html( 'Ivan Fonin', 'basho' ) ); ?>
        </div>
        <?php
    }
}

if ( ! function_exists( 'basho_offcanvas_navigation' ) ) {
    /**
     * Output offcanvas navigation markup.
     */
    function basho_offcanvas_navigation() {

        if ( has_nav_menu( 'offcanvas' ) ) {
        	get_template_part( 'partials/offcanvas-navigation' );
        }

    }
}

if ( ! function_exists( 'basho_search_modal' ) ) {
    /**
     * Output search modal markup.
     */
    function basho_search_modal() {

        /**
    	 * Check whether the 'basho_show_search_form' option is 'on' in the Customizer.
         */
    	$show_search_form_in_header = get_theme_mod( 'basho_show_search_form', true );

        if ( ! $show_search_form_in_header ) {
            get_template_part( 'partials/search-modal' );
        }

    }
}
