<?php
/**
 * @package Eroshiksavp theme
 *
 *
*/

  if ( ! function_exists( 'eroshiksavp_setup' ) ) {
    function eroshiksavp_setup(){
      add_theme_support( 'title-tag' );
      add_theme_support('post-thumbnails');
      /* Activate HTML5 features */
      add_theme_support( 'html5', array( 'comment-list', 'search-form', 'gallery', 'caption' ) );
      add_theme_support('automatic-feed-links');
      $defaults = array(
      	'default-color'          => '#5a5560'
      );
      add_theme_support( 'custom-background',$defaults);

      $args = array(
          'default-image'      => esc_url(get_template_directory_uri() . '/images/header_bg_1200.png'),
          'default-text-color' => '000',
          'width'              => 1000,
          'height'             => 250,
          'flex-width'         => true,
          'flex-height'        => true
      );
      add_theme_support( 'custom-header', $args );


      $defaults = array(
          'flex-height' => true,
          'flex-width'  => true

      );
      add_theme_support( 'custom-logo', $defaults );
      register_nav_menu('header_menu', __('Header navigation menu','eroshiksavp'));

      $formats=array('aside','gallery', 'link', 'image', 'quote','status', 'video', 'audio', 'chat');
      add_theme_support('post-formats',$formats);

      add_theme_support( 'customize-selective-refresh-widgets' );

    }

  }
add_action( 'after_setup_theme', 'eroshiksavp_setup' );

     //sidebar
  if ( ! function_exists( 'eroshik_savp_blog_sidebar_init' ) ) {
    function eroshik_savp_blog_sidebar_init() {

  	register_sidebar(
  		array(
  			'name' =>  __('Blog sidebar','eroshiksavp'),
  			'id' => 'eroshik-blog-sidebar',
  			'description' => __('Dynamic right blog sidebar','eroshiksavp'),
  			'before_widget' => '<section id="%1$s" class="eroshik-widget %2$s">',
  			'after_widget' => '</section>',
  			'before_title' => '<h3 class="eroshik-widget-title">',
  			'after_title' => '</h3>'
  		)
  	);

    }
    add_action( 'widgets_init', 'eroshik_savp_blog_sidebar_init' );
  }

if ( ! function_exists( 'eroshik_savp_header_sidebar_init' ) ) {
function eroshik_savp_header_sidebar_init() {
  register_sidebar( array(
  'name' => __('Header Widget','eroshiksavp'),
  'id' => 'eroshik-header-widget',
  'description' => __('Dynamic header section','eroshiksavp'),
  'before_widget' => '<section id="%1$s" class="eroshik-widget-header %2$s">',
  'after_widget' => '</section>',
  'before_title' => '<h3 class="eroshik-widget-title">',
  'after_title' => '</h3>',
  ) );

  }
  add_action( 'widgets_init', 'eroshik_savp_header_sidebar_init' );
}

    //widget page_top
    if ( ! function_exists( 'eroshik_savp_page_top_init' ) ) {
    function eroshik_savp_page_top_init() {

    register_sidebar(
      array(
        'name' => __('Top page section','eroshiksavp'),
        'id' => 'eroshik-page-top',
        'description' => __('Dynamic top page section','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-page-top %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-page-top-widget-title">',
        'after_title' => '</h3>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_page_top_init' );
  }


    //footer first widget area
    if ( ! function_exists( 'eroshik_savp_footer_first_init' ) ) {
    function eroshik_savp_footer_first_init() {

    register_sidebar(
      array(
        'name' => __('Footer first column','eroshiksavp'),
        'id' => 'eroshik-footer-first',
        'description' => __('Dynamic footer first section','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-footer-first %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-footer-first-widget-title">',
        'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_footer_first_init' );
  }

    //footer second widget area
  if ( ! function_exists( 'eroshik_savp_footer_second_init' ) ) {
    function eroshik_savp_footer_second_init() {

    register_sidebar(
      array(
        'name' => __('Footer second column','eroshiksavp'),
        'id' => 'eroshik-footer-second',
        'description' => __('Dynamic footer second section','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-footer-second %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-footer-second-widget-title">',
        'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_footer_second_init' );
  }



    //footer third widget area
  if ( ! function_exists( 'eroshik_savp_footer_third_init' ) ) {
    function eroshik_savp_footer_third_init() {
    register_sidebar(
      array(
        'name' => __('Footer third column','eroshiksavp'),
        'id' => 'eroshik-footer-third',
        'description' => __('Dynamic footer third section','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-footer-third %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-footer-third-widget-title">',
        'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_footer_third_init' );
  }


    //footer fourth widget area
    if ( ! function_exists( 'eroshik_savp_footer_fourth_init' ) ) {
    function eroshik_savp_footer_fourth_init() {
    register_sidebar(
      array(
        'name' => __('Footer fourth column','eroshiksavp'),
        'id' => 'eroshik-footer-fourth',
        'description' => __('Dynamic footer fourth section','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-footer-fourth %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-footer-fourth-widget-title">',
        'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_footer_fourth_init' );
  }


    //page sidebar
    if ( ! function_exists( 'eroshik_savp_page_sidebar_init' ) ) {
    function eroshik_savp_page_sidebar_init() {
    register_sidebar(
      array(
        'name' => __('Page Sidebar','eroshiksavp'),
        'id' => 'eroshik-page-sidebar',
        'description' => __('Dynamic page sidebar','eroshiksavp'),
        'before_widget' => '<section id="%1$s" class="eroshik-widget-page-sidebar %2$s">',
        'after_widget' => '</section>',
        'before_title' => '<h3 class="eroshik-page-sidebar-widget-title">',
        'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
      )
    );

    }
    add_action( 'widgets_init', 'eroshik_savp_page_sidebar_init' );
  }


  //archive sidebar
  if ( ! function_exists( 'eroshik_savp_archive_sidebar_init' ) ) {
  function eroshik_savp_archive_sidebar_init() {
  register_sidebar(
    array(
      'name' => __('Archive Sidebar','eroshiksavp'),
      'id' => 'eroshik-archive-sidebar',
      'description' => __('Dynamic archive sidebar','eroshiksavp'),
      'before_widget' => '<section id="%1$s" class="eroshik-widget-archive-sidebar %2$s">',
      'after_widget' => '</section>',
      'before_title' => '<h3 class="eroshik-archive-sidebar-widget-title">',
      'after_title' => '</h3><div class="seperator"><div class="seperator_line"></div></div>'
    )
  );

  }
  add_action( 'widgets_init', 'eroshik_savp_archive_sidebar_init' );
}








     //posts formats
    if ( ! function_exists( 'eroshik_savp_posted_meta' ) ) {
     function eroshik_savp_posted_meta(){
       $posted_on=human_time_diff(get_the_time('U'), current_time('timestamp'));
       $category=get_the_category();
       $seperator=', ';
       $output='';
       $i=1;
       if(!empty($category)){
         foreach ($category as $cat){
           if($i>1){ $output.=$seperator;};
           $output.= '<a href="'.esc_url(get_category_link($cat->term_id)).'" alt="' .sprintf( esc_attr__('View all posts in%s','eroshiksavp'), $cat->name).'">'. esc_html($cat->name).'</a>';
           $i++;
         }
       }

       echo  '<span class="posted_on"> '.__('Posted','eroshiksavp').'<a href="'.esc_url(get_permalink()).'"> '.esc_html($posted_on).'</a> '.__('ago','eroshiksavp').' / </span><span class="posted_in">'.$output.'</span>';
     }
   }
  if ( ! function_exists( 'eroshik_savp_posted_footer' ) ) {
     function eroshik_savp_posted_footer(){
       $comments_number=get_comments_number();

       if(comments_open()){

         //get comments links
         if($comments_number==0){
           $comments=__('No comments','eroshiksavp');
         }else if($comments_number>1){
           $comments=$comments_number.' '.__('Comments','eroshiksavp');
         }else{
           $comments=__('One comment','eroshiksavp');
         }
        $comments='<a  href="'.esc_url(get_comments_link()).'"><span class="comment_icon"><i class="fa fa-comments"></i></span>'.$comments.'</a>';
       }else{
         $comments=__('Comments are closed','eroshiksavp');
       }
       echo '<div class="footer_container"><div class="row"><div class="col-xs-12 col-sm-6">'.wp_kses_post(get_the_tag_list('<div class="tags_list"><span class="tag_icon"><i class="fa fa-tags"></i></span>', ' ', '</div>')).'</div><div class="col-xs-12 col-sm-6 text-right">'.$comments.'</div></div></div>';
     }
   }

if ( ! function_exists( 'eroshik_savp_get_attachment' ) ) {
     function eroshik_savp_get_attachment($num=1){
       $output = '';
      if (has_post_thumbnail() && $num == 1){
          $output = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));
        }else {
          $attachments = get_posts(array(
              'post_parent' => get_the_ID(),
              'post_type' => 'attachment',
              'posts_per_page' => $num,
              'post_status' => 'inherit'
          ));

      if ($attachments && $num == 1){
              foreach ($attachments as $attachment){
                  $output = wp_get_attachment_url($attachment->ID);
                }
      }else if($attachments && $num > 1){
              $output = $attachments;
      }
      wp_reset_postdata();
    }

    return $output;
  }
  }


if ( ! function_exists( 'eroshik_savp_get_embedded_media' ) ) {
  function eroshik_savp_get_embedded_media( $type = array() ){
    $output="";
    $content = do_shortcode( apply_filters( 'the_content', get_the_content() ) );
    $embed = get_media_embedded_in_content( $content, $type );


      if( in_array( 'audio' , $type) ){
        $output = str_replace( '?visual=true', '?visual=false', $embed[0] );
    }else{
      if(isset($embed[0])){
        $output = $embed[0];
      }

      }

      return $output;
  }
}

if ( ! function_exists( 'eroshik_savp_get_bs_slides' ) ) {
function eroshik_savp_get_bs_slides( $attachments ){

	$output = array();
	$count = count($attachments)-1;

	for( $i = 0; $i <= $count; $i++ ){

		$active = ( $i == 0 ? ' active' : '' );

		$n = ( $i == $count ? 0 : $i+1 );
		$nextImg = wp_get_attachment_thumb_url( $attachments[$n]->ID );
		$p = ( $i == 0 ? $count : $i-1 );
		$prevImg = wp_get_attachment_thumb_url( $attachments[$p]->ID );

		$output[$i] = array(
			'class'		=> $active,
			'url'		=> wp_get_attachment_url( $attachments[$i]->ID ),
			'next_img'	=> $nextImg,
			'prev_img'	=> $prevImg,
			'caption'	=> $attachments[$i]->post_excerpt
		);

	}

	return $output;

}
}

if ( ! function_exists( 'eroshik_savp_grab_url' ) ) {
function eroshik_savp_grab_url() {
	if( ! preg_match( '/<a\s[^>]*?href=[\'"](.+?)[\'"]/i', get_the_content(), $links ) ){
		return false;
	}
	return esc_url_raw( $links[1] );
}
}
if ( ! function_exists( 'eroshik_savp_grab_current_url' ) ) {
function eroshik_savp_grab_current_url(){
  $secure="";

  if(!isset($_SERVER['HTTPS'])){
    $secure='http://';
  }else{
    $secure='https://';
  }

  $secure=esc_url($secure);
  if(isset($_SERVER['HTTP_HOST']) && isset($_SERVER['REQUEST_URI'])){
  $referer=$secure.esc_url(wp_unslash($_SERVER['HTTP_HOST']));
  $archive_url= $referer.esc_url(wp_unslash($_SERVER['REQUEST_URI']));
  return $archive_url;
  }
}
}

if ( ! function_exists( 'eroshik_savp_post_navigation' ) ) {
function eroshik_savp_post_navigation(){

	$nav = '<div class="row next-prev-link-single">';

	$prev = get_previous_post_link( '<div class="post-link-nav"><span class="fa fa-chevron-left" aria-hidden="true"></span> %link</div>', '%title' );
	$nav .= '<div class="col-xs-12 col-sm-6">' . $prev . '</div>';

	$next = get_next_post_link( '<div class="post-link-nav">%link <span class="fa fa-chevron-right" aria-hidden="true"></span></div>', '%title' );
	$nav .= '<div class="col-xs-12 col-sm-6 text-right">' . $next . '</div>';

	$nav .= '</div>';

	echo  $nav;

}
}


if ( ! function_exists( 'eroshik_savp_get_post_navigation' ) ) {
function eroshik_savp_get_post_navigation(){

	if( get_comment_pages_count() > 1 && get_theme_mod( 'page_comments' ) ){

    get_template_part('inc/templates/eroshik-savp-comment-nav');

	}

}
}


if ( ! isset( $content_width ) ) {
	$content_width = 400;
}


if(function_exists('eroshik_savp_add_editor_styles')) {
function eroshik_savp_add_editor_styles() {
    add_editor_style( 'custom-editor-style.css' );
}
add_action( 'admin_init', 'eroshik_savp_add_editor_styles' );
}

?>
