<?php
/**
 * Shortcode For Faq Post Type
 * @package Symple Portfolio
 * @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
 */

// Only show shortcode if faq post type plugin is active
if( class_exists( 'Symple_FAQ_Post_Type' ) ) :


	/*
	 * Shortcode
	 * @since v1.1
	*/

	if(! function_exists( 'symple_faq_shortcode' ) ) :

		function symple_faq_shortcode($atts, $content = null) {
			
			// Define shortcode params
			extract(shortcode_atts(array(
					'count' => '-1',
					'order' => 'DESC',
					'orderby' => 'date',
					'filter' => '',
					'read_more_text' => __('read more','wpex'),
					'category_id' => '',
					'tag_id' => '',
					'pagination' => 'false',
					'filter_content' => 'false',
					'offset' => 0
				), $atts));
			
			
			//Give faq a unique name
			$unique_classname = 'faq-'. rand(1, 100);
			
			
			// Start Tax Query
			$tax_query = '';
			if( $category_id !=='' && $tag_id =='' ) {
				$tax_query = array(
					array(
						'taxonomy' => 'faq_category',
						'field' => 'id',
						'terms' => $category_id
					),
				);
			} elseif( $tag_id !=='' && $category_id =='' ) {
				
				$tax_query = array(
					array(
						'taxonomy' => 'faq_tag',
						'field' => 'id',
						'terms' => $tag_id,
						'operator' => 'IN'
					)
				);
			} elseif( $category_id !=='' && $tag_id !=='' ) {
				
				$tax_query = array(
					'relation' => 'AND',
					array(
						'taxonomy' => 'faq_category',
						'field' => 'id',
						'terms' => $category_id
					),
					array(
						'taxonomy' => 'faq_tag',
						'field' => 'id',
						'terms' => $tag_id,
						'operator' => 'IN'
					)
				);
			} else {
				$tax_query = '';
			}
			
			// Pagination var
			if( $pagination == 'true' ) {
				global $paged;
				$paged = get_query_var('paged') ? get_query_var('paged') : 1;
			} else {
				$paged = NULL;
			}
			
			// The Query
			$wpex_faq_query = new WP_Query(
				array(
					'post_type' => 'faq',
					'posts_per_page' => $count,
					'offset' => $offset,
					'order' => $order,
					'orderby' => $orderby,
					'tax_query' => $tax_query,
					'filter_content' => $filter_content,
					'paged' => $paged
				)
			);
	
			$output = '';
	
			//Output posts
			if( $wpex_faq_query->posts ) :
			
			
				// Enque Javascript for the faq toggles
				wp_enqueue_script('faq-toggles');
	
				//Filter
				$filter_class = $faq_parent = NULL;
				if( $filter == 'false' ) $filter = NULL;
				if( $filter == 'true' ) $filter = 'category';
				if( $filter == 'category' || $filter == 'tag' ) {
					
					//Category Filter
					if ( $filter == 'category' )$faq_parent = $category_id;
					
					//Tag filter
					if ( $filter == 'tag' ) $faq_parent = $tag_id;
					
					//get faq categories
					$terms = get_terms('faq_'.$filter.'', array( 'hide_empty' => '1', 'child_of' => $faq_parent  ) );
					
					//show filter if categories exist
					if( $terms ) {
					
						// Output filter JS into the footer like a WP Jedi Master
						$output .= "
							<script type='text/javascript'>
							jQuery(function($){
							$('.faq-filter a').click(function() {
									var ourClass = $(this).data('filter');
									$('.faq-filter li a').removeClass('active');
									$(this).addClass('active');
									if(ourClass == 'all') {
									  $('.". $unique_classname ."').children('.faq-entry').show();
									}
									else {
										$('.". $unique_classname ."').children('article:not(.' + ourClass + ')').hide();
										$('.". $unique_classname ."').children('article.' + ourClass).show();
									}
									return false;
								});
							});
							</script>";
	
						// Display filter links
						$output .= '<ul class="faq-filter clearfix">';
							$output .= '<li><a href="#" class="active" data-filter="all"><span>'. __('All', 'wpex') .'</span></a></li>';
							foreach ($terms as $term ) :
								$output .= '<li><a href="#" data-filter="faq-'. $term->slug .'"><span>'. $term->name .'</span></a></li>';
							endforeach;
						$output .= '</ul>';
					
					} // end $terms check
					
				} // end filter check
			
			
				// Main wrapper div
				$output .= '<div class="faq-wrap '. $unique_classname .' clearfix">';
			
				// Loop through posts
				$count=0;
				foreach ( $wpex_faq_query->posts as $post ) : setup_postdata( $post );
				$count++;
				
					// Post vars
					$postid = $post->ID;
					$post_title = get_the_title($postid);
					
					// Get post terms
					if ( $filter !== '' ) {
						$cats = get_the_terms( $postid, 'faq_category' );
						$tags = get_the_terms( $postid, 'faq_tag' );
					} else { $cats = $tags = NULL; }
			
					// Portfolio Post Output
					// Services Post Output
					$output .= '<article class="faq-entry entry-'. $postid .' clearfix';
						if($cats) foreach($cats as $cat) $output .= ' faq-'. $cat->slug;
						if($tags) foreach($tags as $tag) $output .= ' faq-'. $tag->slug;
					$output .= '">';
					
					$output .= '<header class="faq-trigger"><h2><span class="icon-plus-sign-alt"></span>'. $post_title .'</h2></header>';

						//Content
							$output .= '<div class="faq-entry-content clearfix">';
								if ( !empty($post->post_excerpt) ) {
									$output .= apply_filters('the_content', $post->post_excerpt) ;
								} else {
									$output .= apply_filters('the_content', get_the_content() );
								}
							$output .= ' </div>';
					
					$output .= '</article>';
				
				// End foreach loop
				endforeach;
				
				// Close main wrap
				$output .= '</div>';
				
				// Paginate Posts
				if( $pagination == 'true' ) {
					$total = $wpex_faq_query->max_num_pages;
					$big = 999999999; // need an unlikely integer
					if( $total > 1 )  {
						 if( !$current_page = get_query_var('paged') )
							 $current_page = 1;
						 if( get_option('permalink_structure') ) {
							 $format = 'page/%#%/';
						 } else {
							 $format = '&paged=%#%';
						 }
						 $output .= paginate_links(array(
							'base' => str_replace( $big, '%#%', esc_url( get_pagenum_link( $big ) ) ),
							'format' => $format,
							'current' => max( 1, get_query_var('paged') ),
							'total' => $total,
							'mid_size'	=> 2,
							'type' => 'list',
							'prev_text' => '&laquo;',
							'next_text' => '&raquo;',
						 ));
					}
				}
				
			
			endif; // End $wpex_faq_query->posts check
			
			
			// Set things back to normal
			$wpex_faq_query = NULL;
			wp_reset_postdata();
	
	
			// Return data
			return $output; 
			
		}
		add_shortcode("symple_faq", "symple_faq_shortcode");
		
		
		
		/*
		 * Add Editor Button
		 * @since v1.1
		*/
		
		//Creating TinyMCE buttons
		function symple_faq_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_faq_add_custom');
				add_filter('mce_buttons', 'symple_faq_register_button');
			 }
		   }
		} 
		add_action('init', 'symple_faq_editor_button');
		
		function symple_faq_add_custom($plugin_array) {
			   $plugin_array['symple_faq'] = get_template_directory_uri() .'/functions/shortcodes/mce/faq-mce-button.js';
			   return $plugin_array;
		}
		
		function symple_faq_register_button($buttons) {
		   array_push($buttons, "symple_faq");
		   return $buttons;
		} 
		
	
		
	endif; // end symple_faq_shortcode check

	
endif; // end Symple_Portfolio_Post_Type check