<?php
/**
 * Functions which enhance the theme by hooking into WordPress
 *
 * @package Edify
 */

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function edify_body_classes( $classes ) {
	// Adds a class of hfeed to non-singular pages.
	if ( ! is_singular() ) {
		$classes[] = 'hfeed';
	}

	// Adds a class of no-sidebar when there is no sidebar present.
	if ( ! is_active_sidebar( 'blog-sidebar' ) ) {
		$classes[] = 'no-sidebar';
	}

	// Add class if sidebar is used.
	if ( is_active_sidebar( 'blog-sidebar' ) ) {
		$classes[] = 'has-sidebar';
	}

	if( class_exists( 'WooCommerce' )){
		// Add class if sidebar is used.
		if ( ! is_page() || is_shop() && is_active_sidebar( 'shop-sidebar' ) ) {
			$classes[] = 'has-sidebar';
		}

	}
	
	

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

/**
 * Add a pingback url auto-discovery header for single posts, pages, or attachments.
 */
function edify_pingback_header() {
	if ( is_singular() && pings_open() ) {
		printf( '<link rel="pingback" href="%s">', esc_url( get_bloginfo( 'pingback_url' ) ) );
	}
}
add_action( 'wp_head', 'edify_pingback_header' );



// Edify post pagination

function edify_post_pagination(){
	echo '<nav class="blog-page-navigation">';
	the_posts_pagination( array(
		'mid_size' => 2,
		'prev_text' => '<i class="fa fa-angle-double-left"></i>',
		'next_text' => '<i class="fa fa-angle-double-right"></i>',
	) );
	echo '</nav>';
}


// wp kses
function edify_wp_kses($val){
	return wp_kses($val, array(
	
	'p' => array(),
	'span' => array('class' => array(),'id' => array()),
	'div' => array(),
	'strong' => array(),
	'em' => array(),
	'b' => array('class' => array(),'id' => array()),
	'br' => array(),
	'h1' => array(),
	'h2' => array(),
	'h3' => array(),
	'h4' => array(),
	'h5' => array(),
	'h6' => array(),
	'i'=> array('class' => array(),'id' => array()),
	'div'=> array('class' => array(),'id' => array()),
	'ul'=> array('class' => array(),'id' => array()),
	'li'=> array('class' => array(),'id' => array()),
	'a'=> array('href' => array(),'target' => array()),
	'iframe'=> array('src' => array(),'height' => array(),'width' => array()),
	
	), '');
}

// modify search widget
function edify_my_search_form( $form ) {
	$form = '
			
		<form method="get" class="search-form" action="' . esc_url(home_url( '/' )) . '" >
			<input type="text" value="' . esc_attr(get_search_query()) . '" name="s" id="s" class="form-control" placeholder="' . esc_attr__('Search' , 'edify') .'">
			<button type="submit" class="search-submit"><i class="fa fa-search"></i></button>
		</form>
			
		
        ';
	return $form;
}
add_filter( 'get_search_form', 'edify_my_search_form' );

// Header search widget
function edify_header_search_form() {
?>
	
		<form action="<?php echo esc_url(home_url( '/' )); ?>">
			<input type="text" class="form-control" name="s" id="s" placeholder="<?php echo esc_attr__('Search for anything' , 'edify'); ?>">
			<button type="submit" class="search-submit"><i class="far fa-search"></i></button>
	   </form>
<?php
}


// edify_comment 
if ( !function_exists( 'edify_comment' ) ) {
    function edify_comment( $comment, $args, $depth ) {
        $GLOBAL['comment'] = $comment;
        extract( $args, EXTR_SKIP );
        $args['reply_text'] = 'Reply';
        $replayClass = 'comment-depth-' . esc_attr( $depth );
        ?>
            <li id="comment-<?php comment_ID();?>">
                <div class="comments-box postbox__comment-box d-sm-flex align-items-start">
                    <div class="postbox__comment-info">
                        <div class="postbox__comment-avater comments-avatar">
                        <?php print get_avatar( $comment, 102, null, null, [ 'class' => [] ] );?>
                        </div>
                    </div>
                    
                    <div class="postbox__comment-text ">
                        <div class="postbox__comment-name">
                            <span class="post-meta"><?php comment_time( get_option( 'date_format' ) );?></span>
                            <h5><?php print get_comment_author_link();?></h5>
                        </div>
                            <?php comment_text();?>
                        <div class="postbox__comment-reply">
                            <?php comment_reply_link( array_merge( $args, [ 'depth' => $depth, 'max_depth' => $args['max_depth'] ] ) );?>
                        </div>
                    </div>
                </div>
        <?php
    }
}

/*
 * Set post views count using post meta
 */
function edify_setPostViews($postID) {
    $countKey = 'post_views_count';
    $count = get_post_meta($postID, $countKey, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $countKey);
        add_post_meta($postID, $countKey, '0');
    }else{
        $count++;
        update_post_meta($postID, $countKey, $count);
    }
}

/* Header Course Search */

function edify_header_course_search(){ ?>

	<div class="page_search_box">
		<div class="search_close">
			<i class="fa fa-times"></i>
		</div>

		<form action="<?php echo esc_url(home_url('/'));?>" class="border-bottom">
			<input type="text" class="border-0" name="s" placeholder="<?php echo esc_attr__('Search Courses...' , 'edify');?>">
			<input type="hidden" name="post_type" value="courses" />
			<button type="submit"><i class="fa fa-search"></i></button>
		</form>
		
	</div>	

<?php }



/*=============================
Single pots navigation
==============================*/

function edify_post_navigation() { 
	
	?>
	
	
	<div class="post-navigation-area">
		<div class="container p-0">
			<div class="row">
			<?php 
				$prev_post = get_previous_post();
				if(!empty( $prev_post ) ) :
				?>
				<div class="col-lg-6">
						<div class="nextprev-post-wrapper  prev-post">
							<span><?php echo esc_html__('Previous Post','edify'); ?></span>
							<a href="<?php echo get_the_permalink($prev_post);?>">
								<h5><?php echo get_the_title($prev_post );?></h5>
							</a>
						</div>
				</div>
		<?php endif ?>
		
		
		<?php 
				$next_post = get_next_post();
				if(!empty($next_post)) :
				?>
				<div class="col-lg-6">
				
					<div class="nextprev-post-wrapper  next-post">
						<span><?php echo esc_html__('Next Post','edify'); ?></span>
						<a href="<?php echo get_the_permalink($next_post);?>">
							<h5><?php echo get_the_title($next_post );?></h5>
						</a>
					</div>
				
				</div>
				
					<?php endif ?>
			</div>
		</div>
	</div>
	
	<?php 
	}
	
