<?php 

add_theme_support( 'post-thumbnails' );

add_theme_support( 'menus' );
register_nav_menus( array(
		'primary' => __( 'Primary Menu', 'gliemandgliem' ),
		'footer'  => __( 'Footer Menu', 'gliemandgliem' ),
	) );

/* remove [...] from default excerpt */
function new_excerpt_more( $more ) {
	return '...';
}
add_filter('excerpt_more', 'new_excerpt_more');
/* Custom excerpt lengths */
function excerpt($limit) {
  $excerpt = explode(' ', get_the_excerpt(), $limit);
  if (count($excerpt)>=$limit) {
    array_pop($excerpt);
    $excerpt = implode(" ",$excerpt).'...';
  } else {
    $excerpt = implode(" ",$excerpt);
  }
  $excerpt = preg_replace('`[[^]]*]`','',$excerpt);
  return $excerpt;
}
function content($limit) {
  $content = explode(' ', get_the_content(), $limit);
  if (count($content)>=$limit) {
    array_pop($content);
    $content = implode(" ",$content).'...';
  } else {
    $content = implode(" ",$content);
  }
  $content = preg_replace('/[.+]/','', $content);
  $content = apply_filters('the_content', $content);
  $content = str_replace(']]>', ']]&gt;', $content);
  return $content;
}
/* post view count */
function wpb_set_post_views($postID) {
    $count_key = 'wpb_post_views_count';
    $count = get_post_meta($postID, $count_key, true);
    if($count==''){
        $count = 0;
        delete_post_meta($postID, $count_key);
        add_post_meta($postID, $count_key, '0');
    }else{
        $count++;
        update_post_meta($postID, $count_key, $count);
    }
}
//To keep the count accurate, lets get rid of prefetching
remove_action('wp_head', 'rel_canonical'); 
remove_action('wp_head', 'rsd_link'); 
remove_action('wp_head', 'wp_generator'); 
remove_action('wp_head', 'feed_links', 2); 
remove_action('wp_head', 'index_rel_link'); 
remove_action('wp_head', 'wlwmanifest_link'); 
remove_action('wp_head', 'feed_links_extra', 3); 
remove_action('wp_head', 'start_post_rel_link', 10, 0); 
remove_action('wp_head', 'parent_post_rel_link', 10, 0); 
remove_action('wp_head', 'adjacent_posts_rel_link_wp_head', 10, 0); 
/* post thumbnail caption */
function the_post_thumbnail_caption() {
  global $post;
  $thumbnail_id    = get_post_thumbnail_id($post->ID);
  $thumbnail_image = get_posts(array('p' => $thumbnail_id, 'post_type' => 'attachment'));
  if ($thumbnail_image && isset($thumbnail_image[0])) {
    echo '<span>'.$thumbnail_image[0]->post_excerpt.'</span>';
  }
}
/* Enqueue scripts and styles */
function gliemandgliem_theme_scripts() {
	//wp_deregister_script( 'jquery' );
	wp_enqueue_style( 'gliemandgliem-theme-style', get_stylesheet_uri() );
}

add_action( 'wp_enqueue_scripts', 'gliemandgliem_theme_scripts' );

add_action( 'wp_head', function() {
	$html = '
	 <!--[if lt IE 9]>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7/html5shiv.js"></script>
	<![endif]-->';
	echo $html;
} );
/* Remove url field from comments form */
function alter_comment_form_fields($fields){
    $fields['url'] = '';  //removes website field
    return $fields;
}
add_filter('comment_form_default_fields','alter_comment_form_fields');

/* Fix for WP SEO Local Plugin */
function locations_cpt($args){
	if (count($args)){
		$args['rewrite'] = array( 'slug' => 'locations' , 'with_front' => false);
	}
	return $args;
}
add_filter('wpseo_local_cpt_args', 'locations_cpt' );
/* H1 title */
function h1_title($id=false, $echo=true){
	global $post;
	$title = "";
	if ( function_exists( 'get_field' ) ) {
		if ($id)
		$title = get_field('h1_title', $id)? get_field('h1_title', $id) : get_the_title($id);
		else 
			$title = get_field('h1_title', $post->ID)? get_field('h1_title', $post->ID) : get_the_title($post->ID);
		if ($title != ""){
			if ($echo)
				echo $title;
			else 
				return $title;	
		}else{
			return false;
		}
	}else{
		echo get_the_title($post->ID);
	}
	
	
}
function add_image_class($class){
	$class .= ' attachment';
	return $class;
}
add_filter('get_image_tag_class','add_image_class');
function templateUrl_func( $atts,  $content = null ) {
	extract( shortcode_atts( array(
		'directory' => '',
	), $atts ) );
	return get_template_directory_uri().'/'.$directory.'/'.$content;
}
add_shortcode( 'templateUrl', 'templateUrl_func' );
//remove_filter( 'the_content', 'wpautop' );
add_theme_support( 'html5', array(
	'search-form', 'comment-form', 'comment-list', 'gallery', 'caption'
) );

function gliemandgliem_widgets_init() {

	register_sidebar( array(
		'name'          => __( 'Copyrights', 'gliemandgliem' ),
		'id'            => 'copyrights-1',
		'description'   => __( 'Add Copyrights.', 'gliemandgliem' ),
		'before_widget' => '',
		'after_widget'  => '',
		'before_title'  => '',
		'after_title'   => '',
	) );
	
}
add_action( 'widgets_init', 'gliemandgliem_widgets_init' );

// Register Custom Navigation Walker
require_once('wp_bootstrap_navwalker.php');

