<?php
/**
 * Shortcode For Testimonials Post Type
 * @package Symple Testimonials
 * @since 1.0
 * @author AJ Clarke : http://sympleplorer.com
 * @copyright Copyright (c) 2012, AJ Clarke
 * @link http://sympleplorer.com
 * @License: GNU General Public License version 3.0
 * @License URI: http://www.gnu.org/licenses/gpl-3.0.html
 */

if(! function_exists( 'symple_testimonials_shortcode' ) ) :

	function symple_testimonials_shortcode($atts, $content = null) {
		
		// Define shortcode params
		extract(shortcode_atts(array(
				'post_type' => 'testimonials',
				'count' => '-1',
				'order' => 'DESC',
				'excerpt' => 'false',
				'orderby' => 'date',
				'title' => 'true',
				'category_id' => '',
				'filter_content' => 'false',
				'no_found_rows' => true,
				'offset' => 0,
				'filter' => ''
				), $atts));
		
		// Give testimonials a unique name
		$unique_classname = 'testimonials-'. rand(1, 100);
		
		// Start Tax Query
		$tax_query = NULL;
		if( $category_id !=='' ) {
			$tax_query = array(
				array(
					'taxonomy' => 'testimonials_category',
					'field' => 'id',
					'terms' => $category_id
				),
			);
		}
		
		// The Query
		$wpex_testimonials_query = new WP_Query(
			array(
				'post_type' => $post_type,
				'posts_per_page' => $count,
				'excerpt' => 'false',
				'offset' => $offset,
				'order' => $order,
				'orderby' => $orderby,
				'tax_query' => $tax_query,
				'filter_content' => $filter_content,
				'no_found_rows' => true
			)
		);

		$output = '';

		//Output posts
		if( $wpex_testimonials_query->posts ) :
		
		// Load Testimonials Slider JS
		wp_enqueue_script('testimonials-shortcode');
		
			// Main wrapper div
			$output .= '<div class="symple-testimonials flexslider flex-loading"><ul class="slides clearfix">';
		
			// Loop through posts
			foreach ( $wpex_testimonials_query->posts as $post ) :
			
				// Post vars/meta
				$postid = $post->ID;
				$post_title = get_the_title($postid);
				$post_excerpt = $post->post_excerpt;
				$post_content = $post->post_content;
				$url = get_permalink($postid);
				$thumb = wp_get_attachment_url( get_post_thumbnail_id($postid) );
				$thumb = aq_resize( $thumb, 80, 80, true );
				$testimonial_by = get_post_meta( $postid, 'wpex_testimonial_by', true );
				$testimonial_url = get_post_meta( $postid, 'wpex_testimonial_url', true );
						
				// Testimonials Post Output
				$output .= '<li>';
				
					$output .= '<div class="symple-testimonials-slide">';
						
						// Image
						if ( $thumb ) {
							$output .= '<img src="'. $thumb .'" class="symple-testimonials-thumb" alt="'. $testimonial_by .'" />';
						}
	
						$output .= '<div class="symple-testimonials-content">';
						
							$output .= '<header>';
								// Title
								if ( $title == 'true' ) {
									$output .= '<div class="symple-testimonials-title">'. $post_title .'</div>';
								}
								
								if ( $testimonial_url ) {
									$output .= '<div class="symple-testimonials-url"><a href="'. $testimonial_url .'" title="'. $testimonial_by .'">'. $testimonial_url .'</a></div>';
								}
							$output .= '</header>';
						
							// Excerpt
							if ( $excerpt == 'true' ) {
								
								if ( $post_excerpt ) {
									$output .= $post_excerpt;
								} else {
									$output .= wp_trim_words( strip_shortcodes( $post_content ), 18);
									$output .= '<a href="'. $url .'" title="'. __('read more','wpex') .'">'. __('read more','wpex') .'&rarr;</a>';
								}
							} else {
							// Content
								$output .= apply_filters('the_content',$post_content);
							}
						
						$output .= '</div>';
						
						if ( $testimonial_by ) {
							$output .= '<div class="symple-testimonials-by"><span class="icon-user"></span>'. $testimonial_by .'</div>';
						}
					
					$output .= '</div>';
														
				// Close main wrap	
				$output .= '</li>';
			
			// End foreach loop
			endforeach;
			
			// Close main wrap
			$output .= '</ul></div><!-- END Symple Testimonials -->';
			
		endif; // End $wpex_testimonials_query->posts check
		
		
		// Set things back to normal
		$wpex_testimonials_query = NULL;
		wp_reset_postdata();


		// Return data
		return $output; 
		
	}
	add_shortcode("symple_testimonials", "symple_testimonials_shortcode");
	
	
	
	/*
	 * Add Editor Button
	 * @since v1.1
	*/
	
	//Creating TinyMCE buttons
	function symple_testimonials_editor_button() {
	   if ( current_user_can('edit_posts') &&  current_user_can('edit_pages') ) {
		 if (get_user_option('rich_editing')) {
			add_filter('mce_external_plugins', 'symple_testimonials_add_custom');
			add_filter('mce_buttons', 'symple_testimonials_register_button');
		 }
	   }
	} 
	add_action('init', 'symple_testimonials_editor_button');
	
	function symple_testimonials_add_custom($plugin_array) {
		   $plugin_array['symple_testimonials'] = get_template_directory_uri() .'/functions/shortcodes/mce/testimonials-mce-button.js';
		   return $plugin_array;
	}
	
	function symple_testimonials_register_button($buttons) {
	   array_push($buttons, "symple_testimonials");
	   return $buttons;
	} 
	

	
endif; // end symple_testimonials_shortcode check
