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

/**
 * Adds custom classes to the array of body classes.
 *
 * @param array $classes Classes for the body element.
 * @return array
 */
function vastnews_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';
	}

	if ( is_home() ) {
		$blog_sidebar = get_theme_mod( 'vastnews_blog_sidebar_position' , 'no-sidebar' );
		$classes[] = esc_attr( $blog_sidebar );
	}
	
	if( is_single() ) {
	  $post_sidebar = get_theme_mod(  'vastnews_post_sidebar_position','right-sidebar' );
		$classes[] = esc_attr( $post_sidebar );
	}

	if( is_page() ) {
	  $page_sidebar = get_theme_mod( 'vastnews_page_sidebar_position', 'right-sidebar' );
		$classes[] = esc_attr( $page_sidebar );
	}

	if( is_archive() || is_search() || is_404() ) {
		$archive_sidebar = get_theme_mod( 'vastnews_archive_sidebar_position' , 'right-sidebar' );
		$classes[] = esc_attr( $archive_sidebar );
	}

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

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


/**
 * Excerpt length and read more
*/
if ( ! function_exists( 'vastnews_excerpt_length' ) ) :
	
	function vastnews_excerpt_length( $length ) {
		if ( is_admin() ) {
			return $length;
		}
		return get_theme_mod( 'vastnews_excerpt_length', 20 );
	}
endif;
add_filter( 'excerpt_length', 'vastnews_excerpt_length', 999 );


function vastnews_excerpt_more( $more ) {
    return '......';
}
add_filter( 'excerpt_more', 'vastnews_excerpt_more' );


function  vastnews_get_post_cat_choices()  {
	$choices = array( '' => esc_html__( '--Select--', 'vastnews' ) );
	$cats    = get_categories();
	foreach ( $cats as $cat ) {
		$choices[ $cat->term_id ] = $cat->name;
	}
	return $choices;
}

add_filter( 'get_post_cat_choices', 'vastnews_get_post_cat_choices' );