<?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( 'sidebar-1' ) ) {
		$classes[] = 'no-sidebar';
	}

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

	if( class_exists( 'WooCommerce' )){
		// Add class if sidebar is used.
		if ( ! is_page() || is_shop() && is_active_sidebar( 'shop-sidebar-1' ) ) {
			$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
}


// comment list modify

function edify_comments($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>

<li <?php comment_class(); ?> id="comment-<?php comment_ID() ?>">

	<div class="comment-box">
		<div class="comment-avatar">
			<?php echo get_avatar( $comment, 95 ); ?>
		</div>
		<div class="comment-info">
			<h5><?php comment_author_link() ?></h5>
			<span><?php echo esc_html(get_comment_date('F j, Y')); ?></span>
			<?php if ($comment->comment_approved == '0') : ?>
			<p><em><?php esc_html_e('Your comment is awaiting moderation.','edify'); ?></em></p>
			<?php endif; ?>			
			<?php comment_text(); ?>	
		</div>
	</div>
								
</li>


<?php } 

// comment box title change
add_filter( 'comment_form_defaults', 'edify_remove_comment_form_allowed_tags' );
function edify_remove_comment_form_allowed_tags( $defaults ) {

	$defaults['comment_notes_after'] = '';
	$defaults['comment_notes_before'] = '';
	return $defaults;

}

function edify_comment_reform ($arg) {

$arg['title_reply'] = esc_html__('Write your comment Here','edify');
$arg['comment_field'] = '<div class="row"><div class="form-group col-lg-12"><textarea id="comment" class="comment_field form-control" name="comment" cols="30" rows="6" placeholder="'. esc_attr__("Comment", "edify").'" aria-required="true"></textarea></div></div>';


return $arg;

}
add_filter('comment_form_defaults','edify_comment_reform');

// comment form modify

function edify_modify_comment_form_fields($fields){
	$commenter = wp_get_current_commenter();
	$req	   = get_option( 'require_name_email' );

	$fields['author'] = '<div class="row"><div class="form-group col-lg-6"><input type="text" name="author" id="author" value="'. esc_attr( $commenter['comment_author'] ) .'" placeholder="'. esc_attr__("Your Name *", "edify").'" size="22" tabindex="1"'. ( $req ? 'aria-required="true"' : '' ).' class="input-name form-control" /></div>';

	$fields['email'] = '<div class="form-group col-lg-6"><input type="text" name="email" id="email" value="'. esc_attr( $commenter['comment_author_email'] ) .'" placeholder="'.esc_attr__("Your Email *", "edify").'" size="22" tabindex="2"'. ( $req ? 'aria-required="true"' : '' ).' class="input-email form-control"  /></div>';
	
	$fields['url'] = '<div class="form-group col-lg-12"><input type="text" name="url" id="url" value="'. esc_attr( $commenter['comment_author_url'] ) .'" placeholder="'. esc_attr__("Website", "edify").'" size="22" tabindex="2"'. ( $req ? 'aria-required="false"' : '' ).' class="input-url form-control"  /></div></div>';

	return $fields;
}
add_filter('comment_form_default_fields','edify_modify_comment_form_fields');

function edify_move_comment_field_to_bottom( $fields ) {
	$comment_field = $fields['comment'];
	unset( $fields['comment'] );
	$fields['comment'] = $comment_field;
	return $fields;
}
add_filter( 'comment_form_fields', 'edify_move_comment_field_to_bottom' );


/*
 * 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">
				<div class="col-lg-6">
					<?php 
						$prev_post = get_previous_post();
						if(!empty( $prev_post ) ) :
						?>
						<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>
					<?php endif ?>
				</div>
	
				<div class="col-lg-6">
					<?php 
					$next_post = get_next_post();
					if(!empty($next_post)) :
					?>
						<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>
	
					<?php endif ?>
				</div>
			</div>
		</div>
	</div>
	
	<?php 
	}
	
