<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

/*
=================================================================================================================
fasto_widgets_init() - add widget areas
=================================================================================================================
*/
function fasto_widgets_init() {

	register_sidebar( array(
		'name'          => 'Sidebar',
		'id'            => 'sidebar',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2><span class="line"></span>',
	) );	
	
	register_sidebar( array(
		'name'          => 'Footer Sidebar 1',
		'id'            => 'footer-sidebar-1',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2><span class="line"></span>',
	) );	
	
	register_sidebar( array(
		'name'          => 'Footer Sidebar 2',
		'id'            => 'footer-sidebar-2',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2><span class="line"></span>',
	) );	
	
	register_sidebar( array(
		'name'          => 'Footer Sidebar 3',
		'id'            => 'footer-sidebar-3',
		'before_widget' => '<div id="%1$s" class="widget %2$s">',
		'after_widget'  => '</div>',
		'before_title'  => '<h2>',
		'after_title'   => '</h2><span class="line"></span>',
	) );

}
add_action( 'widgets_init', 'fasto_widgets_init' );


/*
=================================================================================================================
fasto_tag_cloud_size() - widget tags size
=================================================================================================================
*/
function fasto_tag_cloud_size($args) {
    $args['largest'] = 14; //largest tag
    $args['smallest'] = 14; //smallest tag
    $args['unit'] = 'px'; //tag font unit
    return $args;
}
add_filter( 'widget_tag_cloud_args', 'fasto_tag_cloud_size' );



/**
 * Custom walker class.
 */
class fasto_Walker_Nav_Menu extends Walker_Nav_Menu {
 
    /**
     * Starts the list before the elements are added.
     *
     * Adds classes to the unordered list sub-menus.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     */
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        // Depth-dependent classes.
        $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $classes = array(
            'sub-menu', 'primary-font'
        );
        $class_names = implode( ' ', $classes );
 
        // Build HTML for output.
        $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
    }
 
    /**
     * Start the element output.
     *
     * Adds main/sub-classes to the list items and links.
     *
     * @param string $output Passed by reference. Used to append additional content.
     * @param object $item   Menu item data object.
     * @param int    $depth  Depth of menu item. Used for padding.
     * @param array  $args   An array of arguments. @see wp_nav_menu()
     * @param int    $id     Current item ID.
     */
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
		$dropdown_arrow = '';
        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
 
        // Depth-dependent classes.
        $depth_classes = array();
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
 
        // Passed classes.
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
 
		if ( is_object( $args ) ){

			//dropdown
			if( $args->walker->has_children == true ){
			
				if( $depth === 0 ){					
					$dropdown_arrow = fasto_svg( 'arrow-drop-down-line' , false  ); # fasto_svg() - safely escaped in functions/theme.php
				}
				elseif( $depth > 0 ){
					if ( is_rtl() ){
						$dropdown_arrow = fasto_svg( 'arrow-drop-left-line' , false  ); # fasto_svg() - safely escaped in functions/theme.php
					}
					else{
						$dropdown_arrow = fasto_svg( 'arrow-drop-right-line' , false  ); # fasto_svg() - safely escaped in functions/theme.php
					}
				}				
			}



			// Build HTML.
			$output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
	 
			// Link attributes.
			$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
			$attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
			$attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
			$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
			$attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
	 
			// Build HTML output and pass through the proper filter.
			$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s%5$s</a>%6$s',
				$args->before,
				$attributes,
				$args->link_before,
				apply_filters( 'the_title', $item->title, $item->ID ),
				$dropdown_arrow,
				$args->after
			);
			$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
			
		}
    }
}


/**
 * Custom walker class for widget nav
 */
class fasto_Widget_Custom_Walker extends Walker_Nav_Menu {
 
    function start_lvl( &$output, $depth = 0, $args = array() ) {
        // Depth-dependent classes.
        $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' ); // code indent
        $display_depth = ( $depth + 1); // because it counts the first submenu as 0
        $classes = array(
            'sub-menu-widget'
        );
        $class_names = implode( ' ', $classes );
 
        // Build HTML for output.
        $output .= "\n" . $indent . '<ul class="' . $class_names . '">' . "\n";
    }
    function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
        global $wp_query;
        $indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' ); // code indent
 
        // Depth-dependent classes.
        $depth_classes = array( );
        $depth_class_names = esc_attr( implode( ' ', $depth_classes ) );
 
        // Passed classes.
        $classes = empty( $item->classes ) ? array() : (array) $item->classes;
        $class_names = esc_attr( implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) ) );
 
		if ( is_object( $args ) ){

			// Build HTML.
			$output .= $indent . '<li id="nav-menu-item-'. $item->ID . '" class="' . $depth_class_names . ' ' . $class_names . '">';
	 
			// Link attributes.
			$attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
			$attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
			$attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
			$attributes .= ! empty( $item->url )        ? ' href="'   . esc_attr( $item->url        ) .'"' : '';
			$attributes .= ' class="menu-link ' . ( $depth > 0 ? 'sub-menu-link' : 'main-menu-link' ) . '"';
	 
			// Build HTML output and pass through the proper filter.
			$item_output = sprintf( '%1$s<a%2$s>%3$s%4$s</a>%5$s',
				$args->before,
				$attributes,
				$args->link_before,
				apply_filters( 'the_title', $item->title, $item->ID ),
				$args->after
			);
			$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
			
		}
    }
}

function fasto_custom_walker_widget( $args ) {
	$args['menu_class'] = 'widget-nav-menu';
    return array_merge( $args, array(
        'walker' => new fasto_Widget_Custom_Walker(),
    ) );
}
add_filter( 'widget_nav_menu_args', 'fasto_custom_walker_widget' );

/*
=================================================================================================================
fasto_pagination() - custom pagination
=================================================================================================================
*/
function fasto_pagination() {
 
    if( is_singular() )
        return;
 
    global $wp_query;
 
    /** Stop execution if there's only 1 page */
    if( $wp_query->max_num_pages <= 1 )
        return;
 
    $paged = get_query_var( 'paged' ) ? absint( get_query_var( 'paged' ) ) : 1;
    $max   = intval( $wp_query->max_num_pages );
 
    /** Add current page to the array */
    if ( $paged >= 1 )
        $links[] = $paged;
 
    /** Add the pages around the current page to the array */
    if ( $paged >= 3 ) {
        $links[] = $paged - 1;
        $links[] = $paged - 2;
    }
 
    if ( ( $paged + 2 ) <= $max ) {
        $links[] = $paged + 2;
        $links[] = $paged + 1;
    }
 
    echo '<div class="pagination"><ul>' . "\n";
 
    /** Previous Post Link */
    if ( get_previous_posts_link() )
        printf( '' . "\n", get_previous_posts_link() );
 
    /** Link to first page, plus ellipses if necessary */
    if ( ! in_array( 1, $links ) ) {
        $class = 1 == $paged ? ' class="active"' : '';
 
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( 1 ) ), '1' );
 
        if ( ! in_array( 2, $links ) )
            echo '<li><a>...</a></li>';
    }
 
    /** Link to current page, plus 2 pages in either direction if necessary */
    sort( $links );
    foreach ( (array) $links as $link ) {
        $class = $paged == $link ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $link ) ), $link );
    }
 
    /** Link to last page, plus ellipses if necessary */
    if ( ! in_array( $max, $links ) ) {
        if ( ! in_array( $max - 1, $links ) )
            echo '<li><a>...</a></li>' . "\n";
 
        $class = $paged == $max ? ' class="active"' : '';
        printf( '<li%s><a href="%s">%s</a></li>' . "\n", $class, esc_url( get_pagenum_link( $max ) ), $max );
    }
 
    /** Next Post Link */
    if ( get_next_posts_link() )
        printf( '' . "\n", get_next_posts_link() );
 
    echo '</ul></div>' . "\n";
 
}


/*
=================================================================================================================
fasto_comment() - Template for comments and pingbacks.
=================================================================================================================
*/
if ( ! function_exists( 'fasto_comment' ) ) :
function fasto_comment( $comment, $args, $depth ) {
	$GLOBALS['comment'] = $comment;
	switch ( $comment->comment_type ) :
		case 'pingback' :
		case 'trackback' :
	?>
	<li class="post pingback">
		<p><?php esc_html_e( 'Pingback:', 'fasto' ); ?> <?php comment_author_link(); ?><?php edit_comment_link( esc_html__( 'Edit', 'fasto' ), '<span class="edit-link">', '</span>' ); ?></p>
	<?php
			break;
		default :
	?>
	<!-- comment level 1 -->
	<div class="comment-container"><!-- comment-container -->
		<div class="comment-author-img"><!-- comment-author-img -->
			<?php echo get_avatar( $comment, 80 ); ?>
		</div><!--/ comment-author-img -->
		<div class="comment-holder"><!-- comment-holder -->
			<div class="comment-details-holder"><!-- comment-details-holder -->
				<div class="comment-author"><h6><?php comment_author_link(); ?></h6></div>
				<div class="comment-date"><?php comment_date("d M Y"); ?></div>
				<div class="the-comment"><?php comment_text(); ?></div>
				<div class="comment-reply secondary-font"><?php comment_reply_link( array( 'depth' => $depth, 'max_depth' => $args['max_depth'],'reply_text'=>esc_attr__( 'Reply','fasto' ) ) ); ?></div>
			</div><!--/ comment-details-holder -->
		</div><!--/ comment-holder -->
		
	</div><!--/ comment-container -->
	<?php
			break;
	endswitch;
}
endif;

/*
=================================================================================================================
fasto_excerpt() - Get excerpt with custom length
=================================================================================================================
*/
function fasto_excerpt( $charlength , $post_ID ) {
	$excerpt = get_the_excerpt( $post_ID );
	$charlength++;

	if ( mb_strlen( $excerpt ) > $charlength ) {
		$subex = mb_substr( $excerpt, 0, $charlength - 5 );
		$exwords = explode( ' ', $subex );
		$excut = - ( mb_strlen( $exwords[ count( $exwords ) - 1 ] ) );
		if ( $excut < 0 ) {
			return esc_attr( mb_substr( $subex, 0, $excut ) );
		} else {
			return esc_attr( $subex );
		}
	} else {
		return esc_attr( $excerpt );
	}
}

/*
=================================================================================================================
fasto_plain_tags() - Get tags as plain text separeated by commas
=================================================================================================================
*/
function fasto_plain_tags( $post_ID = null ){ 
	$tags = get_the_tags( $post_ID );
    if ( $tags ) {
		if ( is_array ( $tags ) ){
			foreach ( $tags as $tag ){
				$arr_tags[] =  $tag->name;
			}
			return implode( ", ", $arr_tags ); 
		}
    }
	return false;
}


/*
=================================================================================================================
fasto_the_loop() - Custom theme loop
=================================================================================================================
*/
function fasto_the_loop(){
	$counter = 0;
	if( have_posts() ) {
		while( have_posts() ) {
		the_post();
		
		
			if (  $counter != 0 && ( $counter % fasto_get_grid( true ) == 0 ) ){
				echo '<div class="main-separator"></div>';
			}
			
			if (  $counter != 0 && ( $counter % 2 == 0 ) ){
				echo '<div class="main-separator tablet"></div>';
			}
			
			if (  $counter != 0 && ( $counter % 1 == 0 ) ){
				echo '<div class="main-separator mobile"></div>';
			}
			
			get_template_part( 'templates/post' ); 		
		
			$counter++;
		}
		fasto_pagination();
	}//end if have_posts
}