<?php
/**
 * Load files
 *
 * @package OMag
 */

/**
 * Implement the Custom Header feature.
 */
require get_template_directory() . '/inc/custom-header.php';

/**
 * Custom template tags for this theme.
 */
require get_template_directory() . '/inc/template-tags.php';

/**
 * Functions which enhance the theme by hooking into WordPress.
 */
require get_template_directory() . '/inc/template-functions.php';

/**
 * Include default theme options
 */
require_once get_template_directory() . '/inc/customizer/default.php';

/**
 * Customizer additions.
 */
require get_template_directory() . '/inc/customizer.php';

/**
 * Breadcrumbs
 */
require get_template_directory() . '/inc/breadcrumbs/breadcrumbs.php';

/**
 * Hook for social media
 */
require get_template_directory() . '/inc/hooks/social-media.php';

/**
 * Hook for slideshow
 */
require get_template_directory() . '/inc/hooks/slideshow.php';

/**
 * Hook for Featured News
 */
require get_template_directory() . '/inc/hooks/featured-news.php';

/**
 * Load widgets
 */
require get_template_directory() . '/inc/widgets/widgets.php';

/**
 * Common Hook
 */
require get_template_directory() . '/inc/hooks/common.php';

/**
 * Load Jetpack compatibility file.
 */
if ( defined( 'JETPACK__VERSION' ) ) {
    require get_template_directory() . '/inc/jetpack.php';
}



if ( is_admin() ) {

    /**
     * Load About
     */

    require_once get_template_directory() . '/inc/about-omag/class-about.php';
    require_once get_template_directory() . '/inc/about-omag/about.php';

}

/**
 * Function to check widget status
 */
 if ( ! function_exists( 'omag_widget_count' ) ) :

 function omag_widget_count( $sidebar_names ){

    $status = 0;

    foreach ($sidebar_names as $sidebar) {
      
        if( is_active_sidebar( $sidebar )){
            $status = $status+1;
        }
    }

    return $status;        
 }

endif;


/*Post Excerpt*/

if ( ! function_exists( 'omag_get_the_excerpt' ) ) :

	/**
	 * Returns post excerpt.
	 *
	 * @since 1.0.0
	 *
	 * @param int     $length      Excerpt length in words.
	 * @param WP_Post $post_object The post object.
	 * @return string Post excerpt.
	 */
	function omag_get_the_excerpt( $length = 0, $post_object = null ) {
		global $post;

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

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

		$source_content = $post_object->post_content;

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

		$source_content = strip_shortcodes( $source_content );
		$trimmed_content = wp_trim_words( $source_content, $length, '...' );
		return $trimmed_content;
	}

endif;

/**
 * Filter the except length to 20 words.
 *
 * @param int $length Excerpt length.
 * @return int (Maybe) modified excerpt length.
 */
function omag_excerpt_length( $length ) {
    return 20;
}

if ( ! is_admin() ) {
    add_filter( 'excerpt_length', 'omag_excerpt_length', 999 );
}

/**
 * Filter the excerpt "read more" string.
 *
 * @param string $more "Read more" excerpt string.
 * @return string (Maybe) modified "read more" excerpt string.
 */
function omag_excerpt_more( $more ) {
    return '...';
}
add_filter( 'excerpt_more', 'omag_excerpt_more' );


/**
 * Function for owlcarousel dynamic slideSpeed and autoplayTimeout
 */
function omag_load_owl_scripts() {

    $theme_options  = omag_theme_options();

    wp_localize_script( 'omag-custom', 'omag_script_vars', array(
        'autoPlaySpeed'     => absint($theme_options['slider_transition_duration']*1000),
        'autoplayTimeout'   => absint($theme_options['slider_transition_delay']*1000),
        )
    );
 
}
add_action('wp_enqueue_scripts', 'omag_load_owl_scripts');
