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

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

	// Add class for global layout.
	$sidebar_layout = online_news_get_option('layout_options'); 
	$sidebar_layout = apply_filters( 'online_news_filter_theme_global_layout', $sidebar_layout );
	$classes[] = 'global-layout-' . esc_attr( $sidebar_layout );

	$enable_home_section     = online_news_get_option('enable_home_section');

    if(  'true' == $enable_home_section ){
         $classes[] = 'home-content';
    }

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

/**
 * Add a pingback url auto-discovery header for singularly identifiable articles.
 */
function online_news_pingback_header() {
	if ( is_singular() && pings_open() ) {
		echo '<link rel="pingback" href="', esc_url( get_bloginfo( 'pingback_url' ) ), '">';
	}
}
add_action( 'wp_head', 'online_news_pingback_header' );

if ( ! function_exists( 'online_news_the_excerpt' ) ) :

	/**
	 * Generate excerpt.
	 *
	 * @since 1.0.0
	 *
	 * @param int     $length Excerpt length in words.
	 * @param WP_Post $post_obj WP_Post instance (Optional).
	 * @return string Excerpt.
	 */
	function online_news_the_excerpt( $length = 0, $post_obj = null ) {

		global $post;

		if ( is_null( $post_obj ) ) {
			$post_obj = $post;
		}

		$length = absint( $length );

		if ( 0 === $length ) {
			return;
		}

		$source_content = $post_obj->post_content;

		if ( ! empty( $post_obj->post_excerpt ) ) {
			$source_content = $post_obj->post_excerpt;
		}

		$source_content = preg_replace( '`\[[^\]]*\]`', '', $source_content );
		$trimmed_content = wp_trim_words( $source_content, $length, '&hellip;' );
		return $trimmed_content;

	}

endif;

if ( ! function_exists( 'online_news_add_sidebar' ) ) :

	/**
	 * Add sidebar.
	 *
	 * @since 1.0.0
	 */
	function online_news_add_sidebar() {

		$sidebar_layout = online_news_get_option('layout_options'); 
		$sidebar_layout = apply_filters( 'online_news_filter_theme_global_layout', $sidebar_layout );

		// Include sidebar.
		if ( 'no-sidebar' !== $sidebar_layout ) {
			get_sidebar();
		}

	}
endif;
add_action( 'online_news_action_sidebar', 'online_news_add_sidebar' );
