<?php
/***
* Special Thanks To Devin Price
* This file is a modified of the original plugin found @https://github.com/devinsays/faq-post-type - Special Thanks!
***/

if ( ! class_exists( 'Symple_FAQ_Post_Type' ) ) :
class Symple_FAQ_Post_Type {

	function __construct() {

		// Adds the faq post type and taxonomies
		add_action( 'init', array( &$this, 'faq_init' ), 0 );

		// Thumbnail support for faq posts
		add_theme_support( 'post-thumbnails', array( 'faq' ) );

		// Adds columns in the admin view for thumbnail and taxonomies
		add_filter( 'manage_edit-faq_columns', array( &$this, 'faq_edit_columns' ) );
		add_action( 'manage_faq_posts_custom_column', array( &$this, 'faq_column_display' ), 10, 2 );

		// Allows filtering of posts by taxonomy in the admin view
		add_action( 'restrict_manage_posts', array( &$this, 'faq_add_taxonomy_filters' ) );
		
	}
	

	function faq_init() {

		/**
		 * Enable the FAQ custom post type
		 * http://codex.wordpress.org/Function_Reference/register_post_type
		 */

		$labels = array(
			'name'					=> __( 'FAQ', 'wpex' ),
			'singular_name'		=> __( 'FAQ Item', 'wpex' ),
			'add_new'				=> __( 'Add New Item', 'wpex' ),
			'add_new_item'			=> __( 'Add New FAQ Item', 'wpex' ),
			'edit_item'			=> __( 'Edit FAQ Item', 'wpex' ),
			'new_item'				=> __( 'Add New FAQ Item', 'wpex' ),
			'view_item'			=> __( 'View Item', 'wpex' ),
			'search_items'			=> __( 'Search FAQ', 'wpex' ),
			'not_found'			=> __( 'No faq items found', 'wpex' ),
			'not_found_in_trash'	=> __( 'No faq items found in trash', 'wpex' )
		);
		
		$args = array(
	    	'labels'			=> $labels,
	    	'public'			=> true,
			'supports'			=> array( 'title', 'editor', 'excerpt', 'custom-fields', 'revisions' ),
			'capability_type'	=> 'post',
			'rewrite'			=> array("slug"	=> "faq"), // Permalinks format
			'has_archive'		=> true,
			'menu_icon'		=> 'dashicons-editor-help',
		); 
		
		$args = apply_filters('symple_faq_args', $args);
		
		register_post_type( 'faq', $args );

		/**
		 * Register a taxonomy for FAQ Tags
		 * http://codex.wordpress.org/Function_Reference/register_taxonomy
		 */

		$taxonomy_faq_tag_labels = array(
			'name'							=> __( 'FAQ Tags', 'wpex' ),
			'singular_name'				=> __( 'FAQ Tag', 'wpex' ),
			'search_items'					=> __( 'Search FAQ Tags', 'wpex' ),
			'popular_items'				=> __( 'Popular FAQ Tags', 'wpex' ),
			'all_items'					=> __( 'All FAQ Tags', 'wpex' ),
			'parent_item'					=> __( 'Parent FAQ Tag', 'wpex' ),
			'parent_item_colon'			=> __( 'Parent FAQ Tag:', 'wpex' ),
			'edit_item'					=> __( 'Edit FAQ Tag', 'wpex' ),
			'update_item'					=> __( 'Update FAQ Tag', 'wpex' ),
			'add_new_item'					=> __( 'Add New FAQ Tag', 'wpex' ),
			'new_item_name'				=> __( 'New FAQ Tag Name', 'wpex' ),
			'separate_items_with_commas'	=> __( 'Separate faq tags with commas', 'wpex' ),
			'add_or_remove_items'			=> __( 'Add or remove faq tags', 'wpex' ),
			'choose_from_most_used'		=> __( 'Choose from the most used faq tags', 'wpex' ),
			'menu_name'					=> __( 'FAQ Tags', 'wpex' )
		);

		$taxonomy_faq_tag_args = array(
			'labels'				=> $taxonomy_faq_tag_labels,
			'public'				=> true,
			'show_in_nav_menus'	=> true,
			'show_ui'				=> true,
			'show_tagcloud'		=> true,
			'hierarchical'			=> false,
			'rewrite'				=> array( 'slug'	=> 'faq-tag' ),
			'query_var'			=> true
		);

		$taxonomy_faq_tag_args = apply_filters('symple_taxonomy_faq_tag_args', $taxonomy_faq_tag_args);
		
		register_taxonomy( 'faq_tag', array( 'faq' ), $taxonomy_faq_tag_args );

		/**
		 * Register a taxonomy for FAQ Categories
		 * http://codex.wordpress.org/Function_Reference/register_taxonomy
		 */

	    $taxonomy_faq_category_labels = array(
			'name'							=> __( 'FAQ Categories', 'wpex' ),
			'singular_name'				=> __( 'FAQ Category', 'wpex' ),
			'search_items'					=> __( 'Search FAQ Categories', 'wpex' ),
			'popular_items'				=> __( 'Popular FAQ Categories', 'wpex' ),
			'all_items'					=> __( 'All FAQ Categories', 'wpex' ),
			'parent_item'					=> __( 'Parent FAQ Category', 'wpex' ),
			'parent_item_colon'			=> __( 'Parent FAQ Category:', 'wpex' ),
			'edit_item'					=> __( 'Edit FAQ Category', 'wpex' ),
			'update_item'					=> __( 'Update FAQ Category', 'wpex' ),
			'add_new_item'					=> __( 'Add New FAQ Category', 'wpex' ),
			'new_item_name'				=> __( 'New FAQ Category Name', 'wpex' ),
			'separate_items_with_commas'	=> __( 'Separate faq categories with commas', 'wpex' ),
			'add_or_remove_items'			=> __( 'Add or remove faq categories', 'wpex' ),
			'choose_from_most_used'		=> __( 'Choose from the most used faq categories', 'wpex' ),
			'menu_name'					=> __( 'FAQ Categories', 'wpex' ),
	    );

	    $taxonomy_faq_category_args = array(
			'labels'				=> $taxonomy_faq_category_labels,
			'public'				=> true,
			'show_in_nav_menus'	=> true,
			'show_ui'				=> true,
			'show_tagcloud'		=> true,
			'hierarchical'			=> true,
			'rewrite'				=> array( 'slug'	=> 'faq-category' ),
			'query_var'			=> true
	    );

		$taxonomy_faq_category_args = apply_filters('symple_taxonomy_faq_category_args', $taxonomy_faq_category_args);
		
	    register_taxonomy( 'faq_category', array( 'faq' ), $taxonomy_faq_category_args );

	}

	/**
	 * Add Columns to FAQ Edit Screen
	 * http://wptheming.com/2010/07/column-edit-pages/
	 */

	function faq_edit_columns( $columns ) {
		$columns['faq_category'] = __( 'Category', 'wpex' );
		$columns['faq_tag'] = __( 'Tags', 'wpex' );		return $columns;
	}

	function faq_column_display( $column, $post_id ) {

		// Code from: http://wpengineer.com/display-post-thumbnail-post-page-overview

		switch ( $column ) {


			// Display the faq tags in the column view
			case "faq_category":

			if ( $category_list = get_the_term_list( $post_id, 'faq_category', '', ', ', '' ) ) {
				echo $category_list;
			} else {
				echo __('None', 'wpex');
			}
			break;	

			// Display the faq tags in the column view
			case "faq_tag":

			if ( $tag_list = get_the_term_list( $post_id, 'faq_tag', '', ', ', '' ) ) {
				echo $tag_list;
			} else {
				echo __('None', 'wpex');
			}
			break;			
		}
	}

	/**
	 * Adds taxonomy filters to the faq admin page
	 * Code artfully lifed from http://pippinsplugins.com
	 */

	function faq_add_taxonomy_filters() {
		global $typenow;

		// An array of all the taxonomyies you want to display. Use the taxonomy name or slug
		$taxonomies = array( 'faq_category', 'faq_tag' );

		// must set this to the post type you want the filter(s) displayed on
		if ( $typenow == 'faq' ) {

			foreach ( $taxonomies as $tax_slug ) {
				$current_tax_slug = isset( $_GET[$tax_slug] ) ? $_GET[$tax_slug] : false;
				$tax_obj = get_taxonomy( $tax_slug );
				$tax_name = $tax_obj->labels->name;
				$terms = get_terms($tax_slug);
				if ( count( $terms ) > 0) {
					echo "<select name='$tax_slug' id='$tax_slug' class='postform'>";
					echo "<option value=''>$tax_name</option>";
					foreach ( $terms as $term ) {
						echo '<option value=' . $term->slug, $current_tax_slug == $term->slug ? ' selected="selected"' : '','>' . $term->name .' (' . $term->count .')</option>';
					}
					echo "</select>";
				}
			}
		}
	}

	/**
	 * Add FAQ count to "Right Now" Dashboard Widget
	 */

	function add_faq_counts() {
	        if ( ! post_type_exists( 'faq' ) ) {
	             return;
	        }

	        $num_posts = wp_count_posts( 'faq' );
	        $num = number_format_i18n( $num_posts->publish );
	        $text = _n( 'FAQ', 'FAQs', intval($num_posts->publish) );
	        if ( current_user_can( 'edit_posts' ) ) {
	            $num = "<a href='edit.php?post_type=faq'>$num</a>";
	            $text = "<a href='edit.php?post_type=faq'>$text</a>";
	        }
	        echo '<td class="first b b-faq">' . $num . '</td>';
	        echo '<td class="t faq">' . $text . '</td>';
	        echo '</tr>';

	        if ($num_posts->pending > 0) {
	            $num = number_format_i18n( $num_posts->pending );
	            $text = _n( 'FAQ Pending', 'FAQs Pending', intval($num_posts->pending) );
	            if ( current_user_can( 'edit_posts' ) ) {
	                $num = "<a href='edit.php?post_status=pending&post_type=faq'>$num</a>";
	                $text = "<a href='edit.php?post_status=pending&post_type=faq'>$text</a>";
	            }
	            echo '<td class="first b b-faq">' . $num . '</td>';
	            echo '<td class="t faq">' . $text . '</td>';

	            echo '</tr>';
	        }
	}

}

new Symple_FAQ_Post_Type;

endif;