<?php

/**
 * Functions related to working with posts
 */

/**
 * Gets the post featured images urls
 *
 * @param string  $size              size of image
 * @param boolean $only_first        return only the first featured image
 * @param boolean $use_missing_image return missing image url if no image is found
 * @return array                     list of featured image urls
 */
function intense_get_post_thumbnail_src( $size, $only_first = false, $use_missing_image = false ) {
	global $intense_visions_options;

	$thumbnail_ids = intense_get_post_thumbnail_ids();
	$thumbnail_src = null;

	foreach ( $thumbnail_ids as $id ) {
		$photo = Intense_Photo_Source::get_wordpress_photo( $id, $size );		
		$thumbnail_src[] = $photo['url'];
	}

	if ( !isset( $thumbnail_src ) ) {
		$thumbnail_src[] = $intense_visions_options['intense_missing_image']['url'];
	}

	return $thumbnail_src;
}

/**
 * Gets a list of attachemnt ids for post featured images
 *
 * @return array list of attachemnt ids for post featured images
 */
function intense_get_post_thumbnail_ids( $postid = null, $custom_post_field_key = null ) {
	global $post, $intense_active_page, $product, $intense_visions_options;	

	if ( isset( $postid ) && get_post_status( $postid ) === FALSE ) {
		if ( isset( $intense_active_page ) ) {
			$thumbnail_post = $intense_active_page;
		} else {
			$thumbnail_post = $post;
		}
	} else {
		if ( isset( $postid ) ) {
			$thumbnail_post = get_post( $postid );
		} else {
			if ( isset( $intense_active_page ) ) {
				$thumbnail_post = $intense_active_page;
			} else {
				$thumbnail_post = $post;
			}
		}
	}

	$ids = array();

	//Custom Post Type image
	if ( isset( $custom_post_field_key ) ) {
		$image = get_field( $custom_post_field_key, $thumbnail_post->ID );

		if ( !empty( $image ) )	{
			$ids[] = $image['id'];
		}
	}
	
	$ids[] = get_post_thumbnail_id( $thumbnail_post->ID );
	$hasGallery = get_field('intense_featured_gallery', $thumbnail_post->ID, false);
	$gallery = '';

	if ( $hasGallery ) {
		$gallery = array_filter( get_field('intense_featured_gallery', $thumbnail_post->ID, false) );
	}	

	if ( !empty( $gallery ) ) {
		$ids = array_merge($ids, $gallery);
	}

	//WooCommerce gallery
	if ( isset( $product ) && method_exists( $product, 'get_gallery_attachment_ids' ) ) {
		$ids = array_merge( $ids, $product->get_gallery_attachment_ids() );
	}

	$ids = array_filter( $ids );

	return array_values( $ids );
}

/**
 * Gets html used to show featured images/videos for a post
 *
 * @param string  $size                 the size of the images
 * @param string  $attributes           not used
 * @param boolean $show_only_first      show only the first image/video
 * @param boolean $show_missing_image   show missing image if no images found
 * @param integer $shadow               shadow to show
 * @param string  $hover_effect_type    hover effect type (effeckt or subtle)
 * @param string  $hover_effect         hover effect to show
 * @param string  $hover_effect_color   color of hover effect
 * @param int     $hover_effect_opacity opacity of hover effect
 * @param string  $image_border_radius  border radius for iamges
 * @param boolean $show_video           show featured videos
 * @param boolean $show_audio           show featured audio
 * @param         $thumbnail_post 
 * @return string                       html used to show post featured images/videos
 */
function intense_get_post_thumbnails( $size, $attributes = null, $show_only_first = false, $show_missing_image = false, $shadow = 0, $hover_effect_type = null, $hover_effect = null, $hover_effect_color = null, $hover_effect_opacity = null, $image_border_radius = null, $show_video = true, $show_audio = true, $thumbnail_post = null, $hide_first = false ) {

	$output = intense_run_shortcode( 'intense_post_media', array( 
			'size' => $size,
			'show_only_first' => $show_only_first, 
			'show_missing_image' => $show_missing_image, 
			'shadow' => $shadow, 
			'hover_effect_type' => $hover_effect_type,
			'effeckt' => $hover_effect,
			'effeckt_color' => $hover_effect_color,
			'effeckt_opacity' => $hover_effect_opacity,
			'border_radius' => $image_border_radius,
			'show_video' => $show_video,
			'show_audio' => $show_audio,
			'is_slider' => true,
			'hide_first' => $hide_first
		) );

	return $output;
}

if ( ! function_exists( 'intense_return_posted_on' ) ) :

	function intense_get_post_metadata( $post_id, $date_format = null ) {
		if ( empty( $post_id ) ) {
			$post_id = get_the_ID();	
		}

		$post_author_id = get_post_field( 'post_author', $post_id );

		$author_bio = intense_get_author_bio( $post_author_id );
		
		$categories = null;
		$comment_count = get_comments_number( $post_id );

		if ( intense_categorized_blog() ) {
			$categories = wp_get_object_terms( $post_id, 'category', array( 'fields' => 'ids' ) );
		} 

		$tags = wp_get_object_terms( $post_id, 'post_tag', array( 'fields' => 'ids' ) );

		return array(
			'date' => get_the_date( $date_format, $post_id ),
			'time' => get_the_time( null, $post_id ),
			'datetime' => get_the_date( 'c', $post_id ),
			'permalink' => get_permalink( $post_id ),
			'categories' => count( $categories ) > 0 ? $categories : null, 
			'tags' => count( $tags ) > 0 ? $tags : null, 
			'comment_count' => $comment_count,
			'comment_text' => _n( 'comment', 'comments', $comment_count, 'intense' ),
			'author_id' => $post_author_id,
			'author' => get_the_author_meta( 'display_name', $post_author_id ),
			'author_url' => get_author_posts_url( $post_author_id ),
			'author_source' => $author_bio['source'],
			'author_name' => $author_bio['name'],
			'author_template' => isset( $author_bio['template'] ) ? $author_bio['template'] : null,
			'author_markup' => $author_bio['markup'],
			'post_id' => $post_id,
		);
	}

	/**
	 * Returns string with meta information for the current post-date/time and author.
	 */
	function intense_return_posted_on() {
		global $intense_visions_options;

		if ( is_rtl() ) return intense_return_posted_on_rtl();

		$read_more = "";
		$output = "";

		$output .= intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_date_icon']['source'], 'type' => $intense_visions_options['intense_date_icon']['type'] ) ) . " <a href='" . esc_url( get_permalink() ) . "' title='" . esc_attr( get_the_time() ) . "' rel='bookmark'><time class='entry-date' datetime='" . esc_attr( get_the_date( 'c' ) ) . "'>" . esc_html( get_the_date() ) . "</time></a>";

		if ( intense_categorized_blog() ) {
			$categories_list = get_the_category_list( __( ', ', 'intense' ) );
			$categories_list = " " . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_category_icon']['source'], 'type' => $intense_visions_options['intense_category_icon']['type'] ) ) . " " . $categories_list;
		}

		$comment_count = " " . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_comment_icon']['source'], 'type' => $intense_visions_options['intense_comment_icon']['type'] ) ) . " <a href='" . get_permalink() . "#comments'>" . get_comments_number() . " " . __( "comments", "intense" ) . "</a>";

		if ( !is_author() ) {
			$author = " " . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_user_icon']['source'], 'type' => $intense_visions_options['intense_user_icon']['type'] ) ) . " <span class='byline'> " . __( " by ", "intense" ) . " <span class='author vcard'><a class='url fn n' href='" . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . "' title='" . __( "View all posts by", "intense" ) . " " .  get_the_author() . "' rel='author'>" . get_the_author() . "</a></span></span>";
		} else {
			$author = "";
		}

		$rand = rand();
		$comment = " " . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_comment_icon']['source'], 'type' => $intense_visions_options['intense_comment_icon']['type'] ) ) . " " . get_comments_number() . "  " . __( "comments", "intense" );
		$comment_count = intense_run_shortcode( 'intense_lightbox', array(
			        	'type' => 'colorbox',
			        	'content_type' => 'html',
			        	'html_element' => 'intense_comments_' . $rand
			        ), $comment );

		$comments = get_comments(array(
			'post_id' => get_the_ID(),
			'status' => 'approve' //Change this to the type of comments to be displayed
		));

		//Display the list of comments
		$list_comments = wp_list_comments(array(
			'echo' => false,
			'reverse_top_level' => false //Show the latest comments at the top of the list
		), $comments);

		$output .= $comment_count;
		if ( isset( $categories_list ) ) $output .= $categories_list;
		$output .= $author;

		$output .= '<div id="intense_comments_' . $rand . '" style="height:600px; width:800px; padding:20px;">' . $list_comments . '</div>';

		return $output;
	}

/**
 * Right to left display of post metadata
 *
 * @return string html for psot metadata
 */
function intense_return_posted_on_rtl() {
	global $intense_visions_options;

	$read_more = "";
	$output = "";

	if ( intense_categorized_blog() ) {
		$categories_list = "<span style='padding: 0 3px; display: inline-block;'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_category_icon']['source'], 'type' => $intense_visions_options['intense_category_icon']['type'] ) ) . " " . get_the_category_list( __( ' ,', 'intense' ) ) . "</span>";
	}

	$comment_count = "<span style='padding: 0 3px; display: inline-block;'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_comment_icon']['source'], 'type' => $intense_visions_options['intense_comment_icon']['type'] ) ) . " <a href='" . get_permalink() . "#comments'>" . get_comments_number() . " " . __( "comments", "intense" ) . "</a></span>";

	if ( !is_author() ) {
		$author =  "<span style='padding: 0 3px; display: inline-block;'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_user_icon']['source'], 'type' => $intense_visions_options['intense_user_icon']['type'] ) ) . "<span class='author vcard'><a class='url fn n' href='" . esc_url( get_author_posts_url( get_the_author_meta( 'ID' ) ) ) . "' title='" . __( "View all posts by", "intense" ) . " " .  get_the_author() . "' rel='author'>" . get_the_author() . "</a></span></span>";
	} else {
		$author = "";
	}

	$output .= "<span style='padding: 0 0 0 3px; display: inline-block;'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_date_icon']['source'], 'type' => $intense_visions_options['intense_date_icon']['type'] ) ). " <a href='" . esc_url( get_permalink() ) . "' title='" . esc_attr( get_the_time() ) . "' rel='bookmark'><time class='entry-date' datetime='" . esc_attr( get_the_date( 'c' ) ) . "'>" . esc_html( get_the_date() ) . "</time></a></span>";

	$output .= $comment_count;
	if ( isset( $categories_list ) ) $output .= $categories_list;
	$output .= $author;

	return $output;
}
endif;

/**
 * Returns navigation to next/previous pages when applicable
 *
 * @param string  $nav_id client id of navigation
 * @param integer $range  how many pages of navigation
 * @return  string the html for the navigation
 */
function intense_return_content_nav( $nav_id, $range = 2, $query = null ) {
	ob_start();

	intense_content_nav( $nav_id, $range, $query );

	$output = ob_get_contents();

	ob_end_clean();

	return $output;
}

/**
 * Displays navigation to next/previous pages when applicable
 *
 * @param string  $nav_id client id of navigation
 * @param integer $range  how many pages of navigation
 * @return  string the html for the navigation
 */
function intense_content_nav( $nav_id, $range = 2, $query = null ) {
	global $wp_query, $post, $paged, $pages, $intense_visions_options;

	if ( !isset( $range ) ) $range = 2;
	if ( !isset( $query ) ) $query = $wp_query;

	$showitems = ( $range * 2 ) + 1;

	if ( empty( $paged ) ) $paged = 1;

	// Don't print empty markup on single pages if there's nowhere to navigate.
	if ( is_single() ) {
		$previous = ( is_attachment() ) ? get_post( $post->post_parent ) : get_adjacent_post( false, '', true );
		$next = get_adjacent_post( false, '', false );

		if ( ! $next && ! $previous )
			return;
	}

	// Don't print empty markup in archives if there's only one page.
	if ( $query->max_num_pages < 2 && ( is_home() || is_archive() || is_search() ) )
		return;

	$nav_class = ( is_single() ) ? 'navigation-post' : 'navigation-paging';

?>
  <nav role="navigation" id="<?php echo esc_attr( $nav_id ); ?>" class="<?php echo $nav_class; ?>">

  <?php if ( is_single() ) : // navigation links for single posts ?>
    <ul class="pager">
    <?php previous_post_link( '<li class="previous">%link</li>', '<span class="meta-nav">' . _x( intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_previous_icon']['source'], 'type' => $intense_visions_options['intense_previous_icon']['type'] ) ), 'Previous post link', 'intense' ) . '</span> Previous' ); ?>
    <?php next_post_link( '<li class="next">%link</li>', 'Next <span class="meta-nav">' . _x( intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_next_icon']['source'], 'type' => $intense_visions_options['intense_next_icon']['type'] ) ), 'Next post link', 'intense' ) . '</span>' ); ?>
    </ul>
  <?php elseif ( $query->max_num_pages > 1 ) : // navigation links for home, archive, and search pages ?>
        <div class='intense pagination pagination-right pagination-mini clearfix'><ul>
        <?php
		if ( get_previous_posts_link() ) echo "<li><a class='pagination-prev' href='".get_pagenum_link( $paged - 1 )."'><span class='page-prev'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_previous_icon']['source'], 'type' => $intense_visions_options['intense_previous_icon']['type']) ) . "</span> ".__( 'Previous', 'intense' )."</a></li>";

		for ( $i=1; $i <= $query->max_num_pages; $i++ ) {
		if ( 1 != $pages &&( !( $i >= $paged+$range+1 || $i <= $paged-$range-1 ) || $pages <= $showitems ) ) {
			echo "<li class='" . ( ( $paged == $i ) ? "active" : "" ) .  "'><a href='" . get_pagenum_link( $i ) . "' >".$i."</a></li>";
		}
	}

	if ( get_next_posts_link() ) echo "<li><a class='pagination-next' href='".get_pagenum_link( $paged + 1 )."'>".__( 'Next', 'intense' )." <span class='page-next'>" . intense_run_shortcode( 'intense_icon', array( 'source' => $intense_visions_options['intense_next_icon']['source'], 'type' => $intense_visions_options['intense_next_icon']['type'] ) ) . "</span></a></li>";
?>
        </ul></div>
  <?php endif; ?>

  </nav><!-- #<?php echo esc_html( $nav_id ); ?> -->
  <?php
}

/**
 * Returns true if a blog has more than 1 category
 */
function intense_categorized_blog() {
	if ( false === ( $all_the_cool_cats = get_transient( 'all_the_cool_cats' ) ) ) {
		// Create an array of all the categories that are attached to posts
		$all_the_cool_cats = get_categories( array(
				'hide_empty' => 1,
			) );

		// Count the number of categories that are attached to the posts
		$all_the_cool_cats = count( $all_the_cool_cats );

		set_transient( 'all_the_cool_cats', $all_the_cool_cats );
	}

	if ( '1' != $all_the_cool_cats ) {
		// This blog has more than 1 category so intense_categorized_blog should return true
		return true;
	} else {
		// This blog has only 1 category so intense_categorized_blog should return false
		return false;
	}
}

/**
 * Flush out the transients used in intense_categorized_blog
 */
function intense_category_transient_flusher() {
	// Like, beat it. Dig?
	delete_transient( 'all_the_cool_cats' );
}
add_action( 'edit_category', 'intense_category_transient_flusher' );
add_action( 'save_post', 'intense_category_transient_flusher' );

/**
 * Post excerpt
 *
 * @param int     $limit number of characters to show
 * @return string        excerpt text
 */
function intense_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;
}

/**
 * Post content
 *
 * @param int     $limit number of characters to show
 * @return string        content text
 */
function intense_content( $limit ) {
	$original_content = get_the_content();
	$original_content = preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $original_content );

	$content = explode( ' ', $original_content, $limit );

	if ( count( $content ) >= $limit ) {
		array_pop( $content );
		$content = implode( " ", $content ).'...';
	} else {
		$content = implode( " ", $content );
	}

	$content = apply_filters( 'the_content', $content );
	$content = str_replace( ']]>', ']]&gt;', $content );

	return $content;
}

/**
 * Determine Post content to be shown in templates
 *
 * @param  object     	$intense_post_type
 * @param  string     	$template_content
 * @param  string     	$template_content_length
 * @param  string     	$default_content_length - this is used if no template_content_length is provided
 * @return string       content text
 */
function intense_template_content( $intense_post_type, $template_content, $template_content_length, $default_content_length ) {
	$temp_content_length = intense_coalesce( $template_content_length, $default_content_length );

	if ( $template_content == '' || $template_content == 'excerpt' ) {
		if ( is_object( $intense_post_type ) ) {			
			$content = $intense_post_type->get_excerpt( $temp_content_length );
		} else {			
			$content = intense_excerpt( $temp_content_length );
		}
	} elseif ( $template_content == 'content' ) {
		if ( is_object( $intense_post_type ) ) {
			$content = $intense_post_type->get_content( $temp_content_length );
		} else {
			$content = intense_content( $temp_content_length );
		}
	} elseif ( $template_content == 'full_post' ) {		
		$content = get_the_content();

	} else {
		if ( is_object( $intense_post_type ) ) {
			$content = $intense_post_type->get_excerpt( $temp_content_length );
		} else {
			$content = intense_excerpt( $temp_content_length );
		}
	}

	$content = apply_filters( 'the_content', $content );

	return $content;
}

/**
 * Displays navigation to next/previous pages when applicable
 *
 * @param string $object_list  array of objects
 * @param string $object_title text to be shown in the link
 * @return string of taxonomy links that are comma separated
 */
function intense_get_taxonomy_list( $object_list, $object_title ) {
	$count = 0;
	$content = '';

	foreach ($object_list as $object_item ) {
		if ( $count == 0 ) {
			$content = '<a href="' . get_term_link( $object_item ) . '">' . $object_item->$object_title . '</a>';
		} else {
			$content .= ', <a href="' . get_term_link( $object_item ) . '">' . $object_item->$object_title . '</a>';
		}
		$count++;
	}

	return $content;
}

/**
 * Walker class for wp_list_pages function calls
 * The walker will run a template for each individual item
 * The template and template_path are sent in args
 */
class Intense_List_Pages_Walker extends Walker_page {
	function start_el( &$output, $object, $depth = 0, $args = array(), $current_object_id = 0 ) {
		global $intense_active_page;

		$intense_active_page = $object;

		if ( $depth )
			$indent = str_repeat( "\t", $depth );
		else
			$indent = '';

		extract( $args, EXTR_SKIP );

		$css_class = array( 'page_item', 'page-item-'.$intense_active_page->ID );

		if ( !empty( $current_page ) ) {
			$_current_page = get_page( $current_page );
			//     _get_post_ancestors($_current_page);
			if ( isset( $_current_page->ancestors ) && in_array( $intense_active_page->ID, (array) $_current_page->ancestors ) )
				$css_class[] = 'current_page_ancestor';
			if ( $intense_active_page->ID == $current_page )
				$css_class[] = 'current_page_item';
			elseif ( $_current_page && $intense_active_page->ID == $_current_page->post_parent )
				$css_class[] = 'current_page_parent';
		} elseif ( $intense_active_page->ID == get_option( 'page_for_posts' ) ) {
			$css_class[] = 'current_page_parent';
		}

		$css_class = implode( ' ', apply_filters( 'page_css_class', $css_class, $intense_active_page, $depth, $args, isset( $current_page ) ? $current_page : null ) );

		$page_content = intense_get_template( array(
			$template_path . $template . '.php',
			$template_path . 'link.php',
		));		

		$output .= $indent . '<li class="' . $css_class . '">' . $page_content . '</li>';

		if ( !empty( $show_date ) ) {
			if ( 'modified' == $show_date )
				$time = $intense_active_page->post_modified;
			else
				$time = $intense_active_page->post_date;

			$output .= " " . mysql2date( $date_format, $time );
		}
	}
}

/**
 * Modified from get_avatar function defined
 * at http://core.trac.wordpress.org/browser/tags/3.5.2/wp-includes/pluggable.php?order=name
 */
function intense_get_avatar_image_url( $id_or_email, $size = '96', $default = '', $alt = false ) {
	if ( ! get_option( 'show_avatars' ) )
		return false;

	if ( false === $alt )
		$safe_alt = '';
	else
		$safe_alt = esc_attr( $alt );

	if ( !is_numeric( $size ) )
		$size = '96';

	$email = '';
	if ( is_numeric( $id_or_email ) ) {
		$id = (int) $id_or_email;
		$user = get_userdata( $id );
		if ( $user )
			$email = $user->user_email;
	} elseif ( is_object( $id_or_email ) ) {
		// No avatar for pingbacks or trackbacks
		$allowed_comment_types = apply_filters( 'get_avatar_comment_types', array( 'comment' ) );
		if ( ! empty( $id_or_email->comment_type ) && ! in_array( $id_or_email->comment_type, (array) $allowed_comment_types ) )
			return false;

		if ( !empty( $id_or_email->user_id ) ) {
			$id = (int) $id_or_email->user_id;
			$user = get_userdata( $id );
			if ( $user )
				$email = $user->user_email;
		} elseif ( !empty( $id_or_email->comment_author_email ) ) {
			$email = $id_or_email->comment_author_email;
		}
	} else {
		$email = $id_or_email;
	}

	if ( empty( $default ) ) {
		$avatar_default = get_option( 'avatar_default' );
		if ( empty( $avatar_default ) )
			$default = 'mystery';
		else
			$default = $avatar_default;
	}

	if ( !empty( $email ) )
		$email_hash = md5( strtolower( trim( $email ) ) );

	if ( is_ssl() ) {
		$host = 'https://secure.gravatar.com';
	} else {
		if ( !empty( $email ) )
			$host = sprintf( "http://%d.gravatar.com", ( hexdec( $email_hash[0] ) % 2 ) );
		else
			$host = 'http://0.gravatar.com';
	}

	if ( 'mystery' == $default )
		$default = "$host/avatar/ad516503a11cd5ca435acc9bb6523536?s={$size}"; // ad516503a11cd5ca435acc9bb6523536 == md5('unknown@gravatar.com')
	elseif ( 'blank' == $default )
		$default = $email ? 'blank' : includes_url( 'images/blank.gif' );
	elseif ( !empty( $email ) && 'gravatar_default' == $default )
		$default = '';
	elseif ( 'gravatar_default' == $default )
		$default = "$host/avatar/?s={$size}";
	elseif ( empty( $email ) )
		$default = "$host/avatar/?d=$default&amp;s={$size}";
	elseif ( strpos( $default, 'http://' ) === 0 )
		$default = add_query_arg( 's', $size, $default );

	if ( !empty( $email ) ) {
		$out = "$host/avatar/";
		$out .= $email_hash;
		$out .= '?s='.$size;
		$out .= '&amp;d=' . urlencode( $default );

		$rating = get_option( 'avatar_rating' );
		if ( !empty( $rating ) )
			$out .= "&amp;r={$rating}";

		return $out;
	} else {
		return $default;
	}
}

function intense_get_author_bio( $author_id, $author_template = null, $image_size = 'square150', $border_radius = '50%' ) {
	if ( false === ( $bio = wp_cache_get( 'intense_author_bio_' . $author_id ) ) ) {
		global $intense_visions_options, $wp_query, $post, $intense_custom_post;

		$bio = array();

		$bio['display_name'] = get_the_author_meta( 'display_name', $author_id );		

		// try to find a team member that has the author's username
		$args = array(
			'numberposts' => 1,
			'post_type' => 'intense_team',
			'meta_key' => 'intense_member_username',
			'meta_value' => get_the_author_meta( 'user_login', $author_id )
		);

		$saved_post = $post;
		$saved_query = $wp_query;
		$saved_custom_post = $intense_custom_post;		
			
		// get results
		$team_member_query = new WP_Query( $args );

		if( $team_member_query->have_posts() && !empty( $intense_visions_options['intense_active_cpt']['intense_cpt_team'] ) && $intense_visions_options['intense_active_cpt']['intense_cpt_team'] ) {
			while ( $team_member_query->have_posts() ) : $team_member_query->the_post();			
				$bio['source'] = 'custom_post_type';
				$bio['name'] = get_the_title( $post->ID );
				$bio['image'] = get_field( 'intense_member_photo' );
				$bio['title'] = get_field( 'intense_member_title' );
				$bio['role'] = get_field( 'intense_member_role' );
				$bio['excerpt'] =  intense_template_content( 'post', 'excerpt', '', 100 );
				$bio['content'] = intense_template_content( 'post', 'full_post', '', 100 );
				$bio['facebook'] = get_field( 'intense_member_facebook' );
				$bio['twitter'] = get_field( 'intense_member_twitter' );
				$bio['googleplus'] = get_field( 'intense_member_googleplus' );
				$bio['dribbble'] = get_field( 'intense_member_dribbble' );
				$bio['linkedin'] = get_field( 'intense_member_linkedin' );
				$bio['website'] = get_field( 'intense_member_website' ); 

				$photo = Intense_Photo_Source::get_wordpress_photo( $bio['image']['id'], $image_size );			
				$bio['image'] = $photo['url'];

				$template = get_field( 'intense_member_author_template' );

				if ( empty( $template ) ) $template = $author_template;
				if ( empty( $template ) ) $template = 'basic';

				$bio['markup'] = intense_run_shortcode( 'intense_team', array(
					'taxonomy' => 'post_tag',
					'template' => '/author/' . $template,
					'include_post_ids' => $post->ID,
					'posts_per_page' => '1',
					'post_count' => '1',
					'show_filter' => '0',
					'border_radius' => $border_radius
				));
			endwhile;
		} else {
			$bio['source'] = 'wordpress';
			$bio['name'] = get_the_author_meta( 'first_name', $author_id ) . ' ' . get_the_author_meta( 'last_name', $author_id );		
			if ( !empty( $bio['name'] ) ) $bio['name'] = trim( $bio['name'] );
			if ( empty( $bio['name'] ) ) $bio['name'] = get_the_author_meta( 'display_name', $author_id );
			if ( empty( $bio['name'] ) ) $bio['name'] = get_the_author();
			$bio['image'] = intense_get_avatar_image_url( $author_id, 400 );
			$bio['title'] = get_the_author_meta( "worktitle", $author_id );
			//$bio['role'] = get_field( 'intense_member_role' );
			//$bio['excerpt'] = ;
			$bio['content'] = get_the_author_meta( 'description', $author_id );
			$bio['facebook'] = get_the_author_meta( "facebook", $author_id );
			$bio['twitter'] = get_the_author_meta( "twitter", $author_id );
			$bio['googleplus'] = get_the_author_meta( "googleplus", $author_id );
			$bio['dribbble'] = get_the_author_meta( "dribbble", $author_id );
			$bio['linkedin'] = get_the_author_meta( "linkedin", $author_id );
			$bio['website'] = get_the_author_meta( "website", $author_id );

			$bio['markup'] = intense_run_shortcode( 'intense_person', array(
				'imageurl' => $bio['image'], 
				'imageposition' => "left",
				'size' => $image_size, 
				'name' => $bio['name'],
				'border_radius' => $border_radius							
			), get_the_author_meta('description', $author_id ) );
		}

		$post = $saved_post;
		$wp_query = $saved_query;
		$intense_custom_post = $saved_custom_post;

		setup_postdata( $post );
    	wp_cache_set( 'intense_author_bio_' . $author_id, $bio, 60 );
    }

	return $bio;
}
