<?php
function Lioit_doctitle() {
		$site_name = get_bloginfo('name');
	    $separator = '|';
	        	
	    if ( is_single() ) {
	      $content = single_post_title('', FALSE);
	    }
	    elseif ( is_home() || is_front_page() ) { 
	      $content = get_bloginfo('description');
	    }
	    elseif ( is_page() ) { 
	      $content = single_post_title('', FALSE); 
	    }
	    elseif ( is_search() ) { 
	      $content = __('Search Results for:', 'Lioit'); 
	      $content .= ' ' . esc_html(stripslashes(get_search_query()));
	    }
	    elseif ( is_category() ) {
	      $content = __('Category Archives:', 'Lioit');
	      $content .= ' ' . single_cat_title("", false);;
	    }
	    elseif ( is_tag() ) { 
	      $content = __('Tag Archives:', 'Lioit');
	      $content .= ' ' . Lioit_tag_query();
	    }
	    elseif ( is_404() ) { 
	      $content = __('Not Found', 'Lioit'); 
	    }
	    else { 
	      $content = get_bloginfo('description');
	    }
	
	    if (get_query_var('paged')) {
	      $content .= ' ' .$separator. ' ';
	      $content .= 'Page';
	      $content .= ' ';
	      $content .= get_query_var('paged');
	    }
	
	    if($content) {
	      if ( is_home() || is_front_page() ) {
	          $elements = array(
	            'site_name' => $site_name,
	            'separator' => $separator,
	            'content' => $content
	          );
	      }
	      else {
	          $elements = array(
	            'content' => $content
	          );
	      }  
	    } else {
	      $elements = array(
	        'site_name' => $site_name
	      );
	    }
	
	    // Filters should return an array
	    $elements = apply_filters('Lioit_doctitle', $elements);
		
	    // But if they don't, it won't try to implode
	    if(is_array($elements)) {
	      $doctitle = implode(' ', $elements);
	    }
	    else {
	      $doctitle = $elements;
	    }
	    
	    $doctitle = "\t" . "<title>" . $doctitle . "</title>" . "\n\n";
	    
	    echo $doctitle;
	} // end Lioit_doctitle

// Creates the content-type section
function Lioit_create_contenttype() {
    $content  = "\t";
    $content .= "<meta http-equiv=\"Content-Type\" content=\"";
    $content .= get_bloginfo('html_type'); 
    $content .= "; charset=";
    $content .= get_bloginfo('charset');
    $content .= "\" />";
    $content .= "\n\n";
    echo apply_filters('Lioit_create_contenttype', $content);
} // end Lioit_create_contenttype

// The master switch for SEO functions
function Lioit_seo() {
		$content = TRUE;
		return apply_filters('Lioit_seo', $content);
}

// Creates the canonical URL
function Lioit_canonical_url() {
		if (Lioit_seo()) {
    		if ( is_singular() ) {
        		$canonical_url = "\t";
        		$canonical_url .= '<link rel="canonical" href="' . get_permalink() . '" />';
        		$canonical_url .= "\n\n";        
        		echo apply_filters('Lioit_canonical_url', $canonical_url);
				}
    }
} // end Lioit_canonical_url


// switch use of Lioit_the_excerpt() - default: ON
function Lioit_use_excerpt() {
    $display = TRUE;
    $display = apply_filters('Lioit_use_excerpt', $display);
    return $display;
} // end Lioit_use_excerpt


// switch use of Lioit_the_excerpt() - default: OFF
function Lioit_use_autoexcerpt() {
    $display = FALSE;
    $display = apply_filters('Lioit_use_autoexcerpt', $display);
    return $display;
} // end Lioit_use_autoexcerpt

function Lioit_the_excerpt($deprecated = '') {
	global $post;
	$output = '';
	$output = strip_tags($post->post_excerpt);
	$output = str_replace('"', '\'', $output);
	if ( post_password_required($post) ) {
		$output = __('There is no excerpt because this is a protected post.');
		return $output;
	}

	return $output;
	
}

// Creates the meta-tag description
function Lioit_create_description() {
		$content = '';
		if (Lioit_seo()) {
    		if (is_single() || is_page() ) {
      		  if ( have_posts() ) {
          		  while ( have_posts() ) {
            		    the_post();
										if (Lioit_the_excerpt() == "") {
                    		if (Lioit_use_autoexcerpt()) {
                        		$content ="\t";
														$content .= "<meta name=\"description\" content=\"";
                        		$content .= Lioit_excerpt_rss();
                        		$content .= "\" />";
                        		$content .= "\n\n";
                    		}
                		} else {
                    		if (Lioit_use_excerpt()) {
                        		$content ="\t";
                        		$content .= "<meta name=\"description\" content=\"";
                        		$content .= Lioit_the_excerpt();
                        		$content .= "\" />";
                        		$content .= "\n\n";
                    		}
                		}
            		}
        		}
    		} elseif ( is_home() || is_front_page() ) {
        		$content ="\t";
        		$content .= "<meta name=\"description\" content=\"";
        		$content .= get_bloginfo('description');
        		$content .= "\" />";
        		$content .= "\n\n";
    		}
    		echo apply_filters ('Lioit_create_description', $content);
		}
} // end Lioit_create_description


// meta-tag description is switchable using a filter
function Lioit_show_description() {
    $display = TRUE;
    $display = apply_filters('Lioit_show_description', $display);
    if ($display) {
        Lioit_create_description();
    }
} // end Lioit_show_description


// create meta-tag robots
function Lioit_create_robots() {
        global $paged;
		if (Lioit_seo()) {
    		$content = "\t";
    		if((is_home() && ($paged < 2 )) || is_front_page() || is_single() || is_page() || is_attachment()) {
				$content .= "<meta name=\"robots\" content=\"index,follow\" />";
    		} elseif (is_search()) {
        		$content .= "<meta name=\"robots\" content=\"noindex,nofollow\" />";
    		} else {	
        		$content .= "<meta name=\"robots\" content=\"noindex,follow\" />";
    		}
    		$content .= "\n\n";
    		if (get_option('blog_public')) {
    				echo apply_filters('Lioit_create_robots', $content);
    		}
		}
} // end Lioit_create_robots


// meta-tag robots is switchable using a filter
function Lioit_show_robots() {
    $display = TRUE;
    $display = apply_filters('Lioit_show_robots', $display);
    if ($display) {
        Lioit_create_robots();
    }
} // end Lioit_show_robots

// creates link to style.css
function Lioit_create_stylesheet() {
    $content = "\t";
    $content .= "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
    $content .= get_bloginfo('stylesheet_url');
    $content .= "\" />";
    $content .= "\n\n";
    echo apply_filters('Lioit_create_stylesheet', $content);
}

//short Text
function limitLength($text,$length=100)
{
         // Change to the number of characters you want to display
          $chars_limit = $length;
          $chars_text = strlen($text);
          $text = $text." ";
           $text = substr($text,0,$chars_limit);
           $text = substr($text,0,strrpos($text,' '));
           // If the text has more characters that your limit,
           //add ... so the user knows the text is actually longer
           if ($chars_text > $chars_limit)
           {
              $text = $text."...";
           }
         return $text;
   }


/*
Plugin Name: Related Posts by Taxonomy
Plugin URI: http://pippinspages.com/related-posts-by-taxonomy
Description: Adds a related posts section to your posts that displays those related by custom taxonomies
Version: 1.0
Author: Pippin Williamson
Author URI: http://pippinspages.com
*/

function tax_related_posts($taxonomy = '') {
	
	global $post;
	
	if($taxonomy == '') { $taxonomy = 'post_tag'; }
	
	$tags = wp_get_post_terms($post->ID, $taxonomy);

	if ($tags) {
		$first_tag 	= $tags[0]->term_id;
		$second_tag = $tags[1]->term_id;
		$third_tag 	= $tags[2]->term_id;
		$args = array(
			'post_type' => get_post_type($post->ID),
			'posts_per_page' => 3,
			'orderby' => 'rand',
			'post__not_in' => array($post->ID),
			'tax_query' => array(
				'relation' => 'OR',
				array(
					'taxonomy' => $taxonomy,
					'terms' => $second_tag,
					'field' => 'id',
					'operator' => 'IN',
				),
				array(
					'taxonomy' => $taxonomy,
					'terms' => $first_tag,
					'field' => 'id',
					'operator' => 'IN',
				),
				array(
					'taxonomy' => $taxonomy,
					'terms' => $third_tag,
					'field' => 'id',
					'operator' => 'IN',
				)
			)
		);
		$related = get_posts($args);
		$i = 0;
		if( $related ) {
			global $post;
			$temp_post = $post;
				foreach($related as $post) : setup_postdata($post);
				$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' ); 
						$content .= '<li><a href="' . get_permalink() . '" title="' . get_the_title() . '"><img src="' . Lioit_SCRIPTS .'/timthumb.php?src='. $image[0] .'&amp;h=120&amp;w=170&amp;zc=1" alt=""></a>
						<a href="'. get_permalink() .'">'. limitLength(get_the_title(), $lenght=60) .'</a></li>';
				endforeach;
			$post = $temp_post;
		}
	}

	return '<div class="related_posts"><ul>'. $content . '</ul></div>';
}

?>