<?php
/*
 * All Front-End Helper Functions
 * Author & Copyright:themexshaper
 * URL: http://themeforest.net/user/themexshaper
 */

/* Exclude category from blog */
if( ! function_exists( 'confin_excludeCat' ) ) {
  function confin_excludeCat($query) {
  	if ( $query->is_home ) {
  		$exclude_cat_ids = cs_get_option('theme_exclude_categories');
  		if($exclude_cat_ids) {
  			foreach( $exclude_cat_ids as $exclude_cat_id ) {
  				$exclude_from_blog[] = '-'. $exclude_cat_id;
  			}
  			$query->set('cat', implode(',', $exclude_from_blog));
  		}
  	}
  	return $query;
  }
  add_filter('pre_get_posts', 'confin_excludeCat');
}

/* Excerpt Length */
class ConfinExcerpt {

  // Default length (by WordPress)
  public static $length = 55;

  // Output: confin_excerpt('short');
  public static $types = array(
    'short' => 25,
    'regular' => 55,
    'long' => 100
  );

  /**
   * Sets the length for the excerpt,
   * then it adds the WP filter
   * And automatically calls the_excerpt();
   *
   * @param string $new_length
   * @return void
   * @author Baylor Rae'
   */
  public static function length($new_length = 55) {
    ConfinExcerpt::$length = $new_length;
    add_filter('excerpt_length', 'ConfinExcerpt::new_length');
    ConfinExcerpt::output();
  }

  // Tells WP the new length
  public static function new_length() {
    if( isset(ConfinExcerpt::$types[ConfinExcerpt::$length]) )
      return ConfinExcerpt::$types[ConfinExcerpt::$length];
    else
      return ConfinExcerpt::$length;
  }

  // Echoes out the excerpt
  public static function output() {
    the_excerpt();
  }

}

// Custom Excerpt Length
if( ! function_exists( 'confin_excerpt' ) ) {
  function confin_excerpt($length = 55) {
    ConfinExcerpt::length($length);
  }
}

if ( ! function_exists( 'confin_new_excerpt_more' ) ) {
  function confin_new_excerpt_more( $more ) {
    return ' ';
  }
  add_filter('excerpt_more', 'confin_new_excerpt_more');
}

/* Tag Cloud Widget - Remove Inline Font Size */
if( ! function_exists( 'confin_tag_cloud' ) ) {
  function confin_tag_cloud($tag_string){
    return preg_replace("/style='font-size:.+pt;'/", '', $tag_string);
  }
  add_filter('wp_generate_tag_cloud', 'confin_tag_cloud', 10, 3);
}

/* Password Form */
if( ! function_exists( 'confin_password_form' ) ) {
  function confin_password_form( $output ) {
    $output = str_replace( 'type="submit"', 'type="submit" class=""', $output );
    return $output;
  }
  add_filter('the_password_form' , 'confin_password_form');
}

/* Widget Layouts */
if ( ! function_exists( 'confin_footer_widgets' ) ) {
  function confin_footer_widgets() {

    $output = '';
    $footer_widget_layout = cs_get_option('footer_widget_layout');

    if( $footer_widget_layout ) {

      switch ( $footer_widget_layout ) {
        case 1: $widget = array('piece' => 1, 'class' => 'col-lg-12'); break;
        case 2: $widget = array('piece' => 2, 'class' => 'col-lg-6'); break;
        case 3: $widget = array('piece' => 3, 'class' => 'col-lg-4'); break;
        case 4: $widget = array('piece' => 4, 'class' => 'col-lg-3 col-md-3 col-sm-6'); break;
        case 5: $widget = array('piece' => 3, 'class' => 'col-lg-3', 'layout' => 'col-lg-6', 'queue' => 1); break;
        case 6: $widget = array('piece' => 3, 'class' => 'col-lg-3', 'layout' => 'col-lg-6', 'queue' => 2); break;
        case 7: $widget = array('piece' => 3, 'class' => 'col-lg-3', 'layout' => 'col-lg-6', 'queue' => 3); break;
        case 8: $widget = array('piece' => 4, 'class' => 'col-lg-2', 'layout' => 'col-lg-6', 'queue' => 1); break;
        case 9: $widget = array('piece' => 4, 'class' => 'col-lg-2', 'layout' => 'col-lg-6', 'queue' => 4); break;
        default : $widget = array('piece' => 4, 'class' => 'col-lg-3'); break;
      }

      for( $i = 1; $i < $widget["piece"]+1; $i++ ) {

        $widget_class = ( isset( $widget["queue"] ) && $widget["queue"] == $i ) ? $widget["layout"] : $widget["class"];

        if (is_active_sidebar('footer-'. $i)) {
          if ($footer_widget_layout == 10 && $i == 1 ) {
            $output .= '<div class="col col-lg-4 col-md-3 col-sm-6">';
          } elseif( $footer_widget_layout == 10 && $i == 2 ){
            $output .= '<div class="col col-lg-2 col-md-3 col-sm-6">';
          } elseif( $footer_widget_layout == 10 && ($i == 3 || $i == 4) ){
            $output .= '<div class="col col-lg-3 col-md-3 col-sm-6">';
          } else {
            $output .= '<div class="'. $widget_class .'">';
          }
          ob_start();
            dynamic_sidebar( 'footer-'. $i );
          $output .= ob_get_clean();
          $output .= '</div>';

        }

      }
    }

    return $output;

  }
}


/* WP Link Pages */
if ( ! function_exists( 'confin_wp_link_pages' ) ) {
  function confin_wp_link_pages() {
    $defaults = array(
      'before'           => '<div class="wp-link-pages">' . esc_html__( 'Pages:', 'confin' ),
      'after'            => '</div>',
      'link_before'      => '<span>',
      'link_after'       => '</span>',
      'next_or_number'   => 'number',
      'separator'        => ' ',
      'pagelink'         => '%',
      'echo'             => 1
    );
    wp_link_pages( $defaults );
  }
}

/* Metas */
if ( ! function_exists( 'confin_post_metas' ) ) {
  function confin_post_metas() {
  $metas_hide = (array) cs_get_option( 'theme_metas_hide' );
  ?>
  <div class="bp-top-meta">
    <?php
    if ( !in_array( 'category', $metas_hide ) ) { // Category Hide
      if ( get_post_type() === 'post') {
        $category_list = get_the_category_list( ' ' );
        if ( $category_list ) {
          echo '<div class="bp-cat">'. $category_list .' </div>';
        }
      }
    } // Category Hides
    if ( !in_array( 'date', $metas_hide ) ) { // Date Hide
    ?>
    <div class="bp-date">
      <span><?php echo get_the_date('M d, Y'); ?></span>
    </div>
    <?php } // Date Hides
    if ( !in_array( 'author', $metas_hide ) ) { // Author Hide
    ?>
    <div class="bp-author">
      <?php
      printf(
        '<span>'. esc_html__('by','confin') .' <a href="%1$s" rel="author">%2$s</a></span>',
        esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ),
        get_the_author()
      );
      ?>
    </div>
    <?php } ?>
  </div>
  <?php
  }
}

/* Author Info */
if ( ! function_exists( 'confin_author_info' ) ) {
  function confin_author_info() {

    if (get_the_author_meta( 'url' )) {
      $author_url = get_author_posts_url( get_the_author_meta( 'ID' ) );
      $website_url = get_the_author_meta( 'url' );
      $target = 'target="_blank"';
    } else {
      $author_url = get_author_posts_url( get_the_author_meta( 'ID' ) );
      $website_url = get_author_posts_url( get_the_author_meta( 'ID' ) );
      $target = '';
    }

    // variables
    $author_text = cs_get_option('author_text');
    $author_text = $author_text ? $author_text : esc_html__( 'Author', 'confin' );
    $author_content = get_the_author_meta( 'description' );
    $facebook = get_the_author_meta( 'facebook' );
    $twitter = get_the_author_meta( 'twitter' );
    $instagram = get_the_author_meta( 'instagram' );
    $pinterest = get_the_author_meta( 'pinterest' );
if ($author_content) {
?>
<div class="author-box">
    <div class="author-avatar">
        <a href="<?php the_permalink(); ?>" target="_blank"><?php echo get_avatar( get_the_author_meta( 'ID' ), 125 ); ?></a>
    </div>
    <div class="author-content">
         <a href="<?php echo esc_url($author_url); ?>" class="author-name"><?php echo get_the_author_meta('first_name').' '.get_the_author_meta('last_name'); ?></a>
        <p><?php echo get_the_author_meta( 'description' ); ?></p>
        <?php if( $facebook || $twitter || $instagram || $pinterest ) { ?>
        <div class="socials">
            <ul class="social-lnk">
                <li>
                  <?php if($twitter) { ?>
                  <a href="<?php echo esc_url( $twitter ); ?>"><i class="ti-twitter"></i></a>
                  <?php } ?>
               </li>
                <li>
                  <?php if($facebook) { ?>
                    <a href="<?php echo esc_url( $facebook ); ?>"><i class="ti-facebook"></i></a>
                  <?php } ?>
                </li>
                <li>
                  <?php if($instagram) { ?>
                    <a href="<?php echo esc_attr( $instagram ); ?>"><i class="ti-instagram"></i></a>
                  <?php } ?>
                </li>
                <li>
                 <?php if($pinterest) { ?>
                  <a href="<?php echo esc_url( $pinterest ); ?>"><i class="ti-pinterest"></i></a>
                 <?php } ?>
                </li>
            </ul>
        </div>
      <?php } ?>
    </div>
</div>

<?php
} // if $author_content
  }
}

/* ==============================================
   Custom Comment Area Modification
=============================================== */
if ( ! function_exists( 'confin_comment_modification' ) ) {
  function confin_comment_modification($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment;
    extract($args, EXTR_SKIP);
    if ( 'div' == $args['style'] ) {
      $tag = 'div';
      $add_below = 'comment';
    } else {
      $tag = 'li';
      $add_below = 'div-comment';
    }
    $comment_class = empty( $args['has_children'] ) ? '' : 'parent';
  ?>

  <<?php echo esc_attr($tag); ?> <?php comment_class('item ' . $comment_class .' ' ); ?> id="comment-<?php comment_ID() ?>">
    <?php if ( 'div' != $args['style'] ) : ?>
   <article>
    <div id="div-comment-<?php comment_ID() ?>" class="confin-comment">
    <?php endif; ?>
    <div class="comment-theme">
        <div class="comment-image">
          <?php if ( $args['avatar_size'] != 0 ) {
            echo get_avatar( $comment, 80 );
          } ?>
        </div>
    </div>
    <div class="comment-main-area">
      <div class="comments-meta">
        <h4><?php printf( '%s', get_comment_author() ); ?></h4>
         <span class="comments-date"><?php echo 'says '. get_comment_date(' F d, Y'); echo ' at '. get_comment_time(); ?></span>
      </div>
      <?php if ( $comment->comment_approved == '0' ) : ?>
      <em class="comment-awaiting-moderation"><?php echo esc_html__( 'Your comment is awaiting moderation.', 'confin' ); ?></em>
      <?php endif; ?>
      <div class="comment-content">
        <?php comment_text(); ?>
        <div class="comments-reply">
          <?php
            comment_reply_link( array_merge( $args, array(
            'reply_text' => '<span class="comment-reply-link icofont icofont-reply-all">'.esc_html__( 'Reply', 'confin' ).'</span>',
            'before' => '',
            'class'  => '',
            'depth' => $depth,
            'max_depth' => $args['max_depth']
            ) ) );
          ?>
        </div>
      </div>
    </div>
  <?php if ( 'div' != $args['style'] ) : ?>
  </div>
  </article>
  <?php endif;
  }
}

/* Comments Form - Textarea next to Normal Fields */
if( ! function_exists( 'confin_move_comment_field' ) ) {
  add_filter( 'comment_form_fields', 'confin_move_comment_field' );
  function confin_move_comment_field( $fields ) {
    $comment_field = $fields['comment'];
    unset( $fields['comment'] );
    $fields['comment'] = $comment_field;
    return $fields;
  }
}

/* Title Area */
if ( ! function_exists( 'confin_title_area' ) ) {
  function confin_title_area() {

    global $post, $wp_query;
    // Get post meta in all type of WP pages
    $confin_id    = ( isset( $post ) ) ? $post->ID : 0;
    $confin_id    = ( is_home() ) ? get_option( 'page_for_posts' ) : $confin_id;
    $confin_meta  = get_post_meta( $confin_id, 'page_type_metabox', true );
    if ($confin_meta && (!is_archive())) {
      $custom_title = $confin_meta['page_custom_title'];
      if ($custom_title) {
        $custom_title = $custom_title;
      } elseif(post_type_archive_title()) {
        post_type_archive_title();
      } else {
        $custom_title = '';
      }
    } else { $custom_title = ''; }

    if ( is_home() && is_front_page() ) {
      echo esc_html__( 'Blog', 'confin' );
    } elseif ( is_home() && !is_front_page() ) {
      single_post_title();
    } elseif ( is_search() ) {
      printf( esc_html__( 'Search Results for %s', 'confin' ), '<span>' . get_search_query() . '</span>' );
    } elseif ( is_category() || is_tax() ){
      single_cat_title();
    } elseif ( is_tag() ){
      single_tag_title( esc_html__('Posts Tagged: ', 'confin') );
    } elseif ( is_archive() ){
      if ( is_day() ) {
        printf( wp_kses( __( 'Archive for <span>%s</span>', 'confin' ), array( 'span' => array() ) ), get_the_date());
      } elseif ( is_month() ) {
        printf( wp_kses( __( 'Archive for <span>%s</span>', 'confin' ), array( 'span' => array() )), get_the_date( 'F, Y' ));
      } elseif ( is_year() ) {
        printf( wp_kses( __( 'Archive for <span>%s</span>', 'confin' ), array( 'span' => array() ) ), get_the_date( 'Y' ));
      } elseif ( is_author() ) {
        printf( wp_kses( __( 'Posts by: <span>%s</span>', 'confin' ), array( 'span' => array() ) ), get_the_author_meta( 'display_name', $wp_query->post->post_author ));
      } elseif ( is_post_type_archive() ) {
        post_type_archive_title();
      } else {
        esc_html_e( 'Archives', 'confin' );
      }
    } elseif( ( class_exists( 'WooCommerce' ) ) && ( is_product() ) ) {
       the_title();
    } elseif( is_singular( 'project' ) ) {
      the_title();
    } elseif( is_singular( 'service' ) ) {
       the_title();
    }  elseif( is_404() ) {
      esc_html_e('404 Error', 'confin');
    } elseif( $custom_title ) {
      echo esc_attr($custom_title);
    } else {
      if (is_single()) {
        the_title();
      } else {
        the_title();
      }
    }
  }
}

/**
 * Pagination Function
 */
if ( ! function_exists( 'confin_paging_nav' ) ) {
  function confin_paging_nav() {
    if ( function_exists('wp_pagenavi')) {
      wp_pagenavi();
    } else {
      $older_post = cs_get_option('older_post');
      $newer_post = cs_get_option('newer_post');
      $older_post = $older_post ? $older_post : wp_kses_post( '<i class="ti-arrow-left"></i>', 'confin' );
      $newer_post = $newer_post ? $newer_post : wp_kses_post( '  <i class="ti-arrow-right"></i>', 'confin' );
      global $wp_query;
      $big = 999999999;
      if($wp_query->max_num_pages == '1' ) {} else {echo '';}
      echo paginate_links( array(
        'base' => str_replace( $big, '%#%', get_pagenum_link( $big ) ),
        'format' => '?paged=%#%',
        'prev_text' => $older_post,
        'next_text' => $newer_post,
        'current' => max( 1, get_query_var('paged') ),
        'total' => $wp_query->max_num_pages,
        'type' => 'list'
      ));
      if($wp_query->max_num_pages == '1' ) {} else {echo '';}
    }
  }
}

// blog pagination
if ( !function_exists('confin_paginate_links') ) {
  function confin_paginate_links( $echo = true ) {
    global $wp_query;
    $big = 999999999; // need an unlikely integer
    $pages = paginate_links( array(
      'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
      'format' => '?paged=%#%',
      'current' => max( 1, get_query_var('paged') ),
      'total' => $wp_query->max_num_pages,
      'type'  => 'array',
      'prev_next'   => true,
      'prev_text'    => wp_kses_post( __( '<i class="fa fa-angle-left" aria-hidden="true"></i>', 'confin' ) ),
      'next_text'    => wp_kses_post( __( '<i class="fa fa-angle-right" aria-hidden="true"></i>', 'confin' ) ),
      )
    );
    if( is_array( $pages ) ) {
      $paged = ( get_query_var('paged') == 0 ) ? 1 : get_query_var('paged');
      $pagination = '<div class="row"><nav class="col-lg-12 irs-pagination text-center"><ul class="pagination">';
      foreach ( $pages as $page ) {
        $pagination .= "<li>$page</li>";
      }
      $pagination .= '</ul></nav></div>';
      if ( $echo ) {
        echo wp_kses_post( $pagination );
      } else {
        return $pagination;
      }
    }
  }
}
// Pingback Head
function confin_pingback_header() {
  if ( is_singular() && pings_open() ) {
    printf( '<link rel="pingback" href="%s">' . "\n", get_bloginfo( 'pingback_url' ) );
  }
}
add_action( 'wp_head', 'confin_pingback_header' );


// checking video id
function confin_video_format_data( $url ){
  global $post;
  $parsed = parse_url($url);
  if ( strpos( $parsed['host'], 'vimeo' ) !== false ) {
    $url = $url;
    if (preg_match('%^https?:\/\/(?:www\.|player\.)?vimeo.com\/(?:channels\/(?:\w+\/)?|groups\/([^\/]*)\/videos\/|album\/(\d+)\/video\/|video\/|)(\d+)(?:$|\/|\?)(?:[?]?.*)$%im', $url, $regs)) {
        $id = $regs[3];
    }
    return 'https://player.vimeo.com/video/'.$id.'?title=0&byline=0&portrait=0&badge=0&autoplay=0&title=0';
  } else {
    preg_match(
        '/[\\?\\&]v=([^\\?\\&]+)/',
        $url,
        $matches
      );
      $id = $matches[1];

    return 'https://www.youtube.com/embed/'.$id.'?&autoplay=0&showinfo=0';
  }
}



/**
 * SINGLE PAGINATION
 */
if(!function_exists('confin_single_pagination')) {
  function confin_single_pagination() {
    $next_post = get_next_post( '', false);
    $prev_post = get_previous_post( '', false);
    $newer_post = cs_get_option('newer_post');
    $newer_post = ( $newer_post ) ? $newer_post : esc_html__('Next Article', 'confin');
    $older_post = cs_get_option('older_post');
    $older_post = ( $older_post ) ? $older_post : esc_html__('Previous Article', 'confin');
    if (empty($next_post) || empty($prev_post)) {
      $style = 'border: none; width:100%;';
      $class = 'single';
    } else {
      $style = '';
      $class = '';
    }
    if ( !empty( $prev_post ) || !empty( $next_post ) ) { ?>
      <div class="more-posts clearfix">
        <?php if (!empty( $prev_post )): ?>
          <div class="previous-post <?php echo esc_attr($class); ?>" style="<?php echo esc_attr( $style ); ?>">
            <a href="<?php echo esc_url( get_permalink( $prev_post->ID ) ); ?>">
              <span class="post-control-link"><?php echo esc_html($older_post); ?>: </span>
              <span class="post-name"><?php echo esc_html($prev_post->post_title); ?></span>
            </a>
          </div>
          <?php endif; ?>
          <?php  if (!empty( $next_post )): ?>
          <div class="next-post <?php echo esc_attr($class); ?>" style="<?php echo esc_attr( $style ); ?>">
            <a href="<?php echo esc_url( get_permalink( $next_post->ID ) ); ?>">
              <span class="post-control-link"><?php echo esc_html($newer_post); ?></span>
              <span class="post-name"><?php echo esc_html($next_post->post_title); ?></span>
            </a>
          </div>
        <?php endif; ?>
      </div>
    <?php
    }
  }
}
