<?php

class Intense_Post_Fields extends Intense_Shortcode {

	function __construct() {
        parent::__construct();

		global $intense_visions_options;

		$this->title = __( 'Post Fields', 'intense' );
		$this->description = __( 'The post fields shortcode is meant to be used in conjunction with the templates custom post type. When creating a layout template, you can use the post fields shortcode to select which fields you want to show, in what places. This allows for easy creation of your own templates, you have full control.', 'intense' );
		$this->category = __( 'Posts', 'intense' );
		$this->show_preview = false;
		$this->show_preset = false;	
	}

	function shortcode( $atts, $content = null ) {
		global $intense_visions_options, $post;
		extract( shortcode_atts( $this->get_shortcode_defaults( null, $atts ), $atts ) );

		$returnString = '';

		if ( $excerpt_length != '' && is_numeric( $excerpt_length ) ) {
			$original_content = $post->post_content;
			$original_content = preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $original_content );

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

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

			$content = apply_filters( 'the_content', $content );
			$content = str_replace( ']]>', ']]&gt;', $content );
		} else {
			$content = $post->post_content;
		}

		if ( $field === '' ) {
			return '';
		}

		if ( $field === 'title' ) {
			return the_title_attribute( 'echo=0' );
		}

		if ( $field === 'permalink' ) {
			return get_permalink();
		}

		if ( $field === 'content' ) {
			return do_shortcode( $content );
		}

		if ( $field === 'featured_images' ) {
			return intense_get_post_thumbnails( ( $size === '' ? 'full' : $size ) );
		}

		if ( $field === 'featured_color' ) {
			return get_field( 'intense_' . $field ); 
		}

		if ( strpos( $field, '_category' ) !== false || $field === 'book_author' || $field === 'book_publisher' || $field === 'job_type' || $field === 'movie_genre' || $field === 'movie_cast' || $field === 'movie_director' || $field === 'portfolio_skills' || $field === 'project_skills' || $field === 'recipes_ingredients' || $field === 'recipes_cuisines' || $field === 'recipes_courses' || $field === 'recipes_skill_levels' || $field === 'team_position' || $field === 'team_skills' ) {
			$taxonomy = 'intense_' . $field;

			if ( $field === 'portfolio_category' || $field === 'portfolio_skills' ) {
				$taxonomy = $field;
			}

			$categories = wp_get_post_terms( get_the_ID(), $taxonomy, array("fields" => "all") );
			
			if ( $categories ) {
				$category_list = array();

				foreach ( $categories as $category ) {
					$category_list[] = $category->name;
				}

				return join( ', ', $category_list );
			}
		} elseif ( strpos( $field, 'portfolio' ) !== false ) {
		 	if ( $field === 'portfolio_subtitle' ) {
		 		return get_field( 'intense_' . $field );
		 	} elseif ( $field === 'portfolio_date' ) {
		 		return date("M d, Y", strtotime( get_field( 'intense_' . $field ) ) );
		 	} else {
		 		return get_field( str_replace( 'portfolio', 'intense', $field ) );
		 	}
		} elseif ( substr( $field, 0, 7 ) === "project" ) {
		 	return get_field( 'intense_' . $field );
		} elseif ( strpos( $field, '_date' ) !== false ) {
			return date("M d, Y", strtotime( get_field( 'intense_' . $field ) ) );
		} elseif ( strpos( $field, '_image' ) !== false || strpos( $field, '_logo' ) !== false || strpos( $field, '_photo' ) !== false ) {
			$image = get_field( 'intense_' . str_replace( 'team_', '', $field ) );			
		
			return intense_run_shortcode( 'intense_image', array( 
					'image' => $image['id'],
					'size' => ( $size === '' ? 'full' : $size )
				) );
		} elseif ( strpos( $field, '_audio' ) !== false ) {
			return intense_run_shortcode( 'intense_audio', array( 
		          'url' => get_field( 'intense_' . $field )
		        ) );
		// } elseif ( $field === 'team_member_facebook' || $field === 'team_member_googleplus' || $field === 'team_member_twitter' || $field === 'team_member_dribbble' || $field === 'team_member_linkedin' ) {
		// 	return intense_run_shortcode( 'intense_social_icon', array(
		// 			'type' => str_replace( 'team_member_', '', str_replace( 'plus', '-plus', $field ) ),
		// 			'link' => get_field( 'intense_' . str_replace( 'team_', '', $field ) ),
		// 			'link_target' => '_blank',
		// 			'color' => 'primary',
		// 			'size' => '20'
		// 		) );
		// } elseif ( $field === 'team_member_custom_social_icon' ) {
		// 	return intense_run_shortcode( 'intense_social_icon', array(
		// 			'mode' => 'custom',
		// 			'image' => get_field( 'intense_' . str_replace( 'team_', '', $field ) )['id'],
		// 			'link' => get_field( 'intense_member_custom_social_link' ),
		// 			'link_target' => '_blank'
		// 		) );
		} elseif ( $field == 'wooproduct_product' ) {
			return do_shortcode('[product id="' . $post->ID . '"]');
		} elseif ( $field == 'wooproduct_productpage' ) {
			return do_shortcode('[product_page id="' . $post->ID . '"]');
		} elseif ( $field == 'wooproduct_addtocart' ) {
			return do_shortcode('[add_to_cart id="' . $post->ID . '"]');
		} elseif ( $field == 'wooproduct_addtocart' ) {
			return do_shortcode('[add_to_cart_url id="' . $post->ID . '"]');
		} else {
			return get_field( 'intense_' . str_replace( 'team_', '', $field ) );
		}
	}
}
