<?php
/**
 * Custom functions that act independently of the theme templates.
 *
 * Eventually, some of the functionality here could be replaced by core features.
 *
 * @package TriFold
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function trifold_body_classes( $classes ) {

	$classes[] = 'stretched no-transition';
	// Adds a class of layout content
	$classes[] = trifold_is_get_layout_content();

	// Adds a class of group-blog to blogs with more than 1 published author.
	if ( is_multi_author() ) {
		$classes[] = 'group-blog';
	}

	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	return $classes;
}
add_filter( 'body_class', 'trifold_body_classes' );

/**
 * Filter the navigational markup.
 * @since 1.0.0
 */
function trifold_navigation_markup(){
	$template = '<nav class="navigation %1$s clearfix" role="navigation">
	              	<h2 class="screen-reader-text">%2$s</h2>
                	<div class="nav-links">%3$s</div>
       			</nav>';
	return $template;
}
add_filter( 'navigation_markup_template', 'trifold_navigation_markup' );

/**
 * Hooks search form
 * @param  string $form
 * @return string
 */
function trifold_search_form( $form ) {
	$form = '<form role="search" method="get" class="search-form" action="' . home_url( '/' ) . '" >
	<label class="screen-reader-text" for="s">' . esc_attr_x( 'Search for:', 'label', 'trifold' ) . '</label>
	<input type="text" value="' . trim( get_search_query() ) . '" name="s" class="field" placeholder="'. esc_attr_x( 'Search...', 'label', 'trifold' ) .'"/>
	<button type="submit" name="submit" class="icon-search3 submit" value="'. esc_attr_x( 'Search', 'submit button', 'trifold' ) .'" /></button>
	</form>';

	return $form;
}

add_filter( 'get_search_form', 'trifold_search_form' );

if ( ! function_exists( 'trifold_filter_attr_custom_logo' ) ) :
/**
 * Filtering attribute custom logo
 * @param  string $attr
 * @return mixed      
 */
function trifold_filter_attr_custom_logo( $html ) {
	$html = str_replace( '<a', '<a class="custom-logo-link standard-logo"', $html );

	return $html;
}
endif;
add_filter( 'get_custom_logo', 'trifold_filter_attr_custom_logo' );

if ( ! function_exists( 'trifold_filter_attr_header' ) ) :
/**
 * Filtering attribute header
 * @param $attr
 * @return array
 */
function trifold_filter_attr_header( $attr, $transparent = '' ) {
    
    $media_type = get_theme_mod( 'intro-frontpage-media-type', trifold_get_default( 'intro-frontpage-media-type' ) );
    // if the slider off so the template and class transparent not shown up
    if( ( is_front_page() && get_option( 'show_on_front' ) != 'page' ) && in_array( $media_type, array( 'static', 'slider' ) ) ) {
		$transparent = 'transparent-header';
	}

	$attr['class'] = esc_attr( implode( ' ', array( 'site-header', $transparent ) ) );

	return $attr;
}

endif;

add_filter( 'trifold_attr_header', 'trifold_filter_attr_header' );

if ( ! function_exists( 'trifold_filter_attr_menu' ) ) :
/**
 * Filtering attribute menu
 * @param $attr
 * @return array
 */
function trifold_filter_attr_menu( $attr ) {
	$attr['class'] = 'menu';
	$attr['id'] = 'primary-menu';

	return $attr;
}

endif;

add_filter( 'trifold_attr_menu', 'trifold_filter_attr_menu' );

if ( ! function_exists( 'trifold_filter_attr_branding' ) ) :
/**
 * Filtering attribute branding
 * @param $attr
 * @return array
 */
function trifold_filter_attr_branding( $attr ) {
	$attr['id'] = 'logo';

	return $attr;
}

endif;

add_filter( 'trifold_attr_branding', 'trifold_filter_attr_branding' );

if ( ! function_exists( 'trifold_filter_attr_sidebar' ) ) :
/**
 * Filtering attribute sidebar
 * @param $attr
 * @return array
 */
function trifold_filter_attr_sidebar( $attr ) {
	$attr['class'] = esc_attr( implode( ' ', array( 'sidebar', 'nobottommargin', 'clearfix' ) ) );

	return $attr;
}

endif;

add_filter( 'trifold_attr_sidebar', 'trifold_filter_attr_sidebar', 5, 2 );
