<?php
namespace fuel;
/**
 * Taxonomy
 *
 * @author Kyeong Jun Lee<eric@fuelmultimedia.ca>
 *
 * Usage:
 *
 * require get_template_directory() . '/inc/taxo.php';
 * $_mytaxonomy = new \fuel\Taxonomy('test','tests','text-domain',array('post','products'),$arg);
 *
 *
 */
class Taxonomy
{
    /**
     * @var {string} $single      - singular taxonomy name
     * @var {string} $plural      - plural taxonomy name
     * @var {string} $text_domain - text domain
     * @var {array()} $post_types - applied post types
     * @var {array()} $arg        - options for the taxonomy
     */
    public $single;
    public $plural;
    public $text_domain;
    public $post_types;
    public $arg;

    /**
     * @param {string} $s      - singular taxonomy name
     * @param {string} $p      - plural taxonomy name
     * @param {string} $td     - text domain
     * @param {array()} $post_types - applied post types Optional
     * @param {array()} $arg   - options for the taxonomy Optional
     */
    public function __construct($s, $p, $td,$post_types = array('page','post'), $arg = array() ) {
        // Make it sure to have 'name' as plural
        $this->single       = $s;
        $this->plural       = $p;
        $this->text_domain  = $td;
        // $built_in_post_types = array('post','page','attachment','revision','nav_menu_item');
        $this->post_types   = $post_types;
        $this->arg          = $arg;

        $this->addAction();
    }

    function addAction() {
        //Add Taxonomy
        add_action( 'init', array( $this, 'custom_taxonomy'), 0 );
    }
    // Register Custom Taxonomy
    function custom_taxonomy() {
        /**
         * A local variable for singular post type name
         * @var {string}
         */
        $_single = $this->single;
        /**
         * A local variable for plural post type name
         * @var {string}
         */
        $_plural = $this->plural;
        /**
         * A local variable for text domain of post type name
         * @var {string}
         */
        $text_domain = $this->text_domain;
        /**
         * Regular expression to repalce spaces with '_'
         * @var {pattern}
         */
        $find_space = '/\s/';
        /**
         * generated post type name with all the blanks replaced with '_'
         * @var {string}
         */
        $_taxo_type = strtolower( preg_replace( $find_space, '_',$this->plural ) );
        /**
         * Reassign arguments to be used locally
         * @var {array()}
         */
        $_arg = $this->arg;
        /**
         * Reassign post types to be used locally
         * @var {array()}
         */
        $_post_types = $this->post_types;

        $labels = array(
            'name'                  => _x( $_plural, 'Taxonomy General Name', $text_domain ),
            'singular_name'         => _x( $_single, 'Taxonomy Singular Name', $text_domain ),
            'menu_name'             => __( $_plural, $text_domain ),
            'all_items'             => __( 'All '.$_plural, $text_domain ),
            'parent_item'                => __( 'Parent '.$_single, $text_domain ),
            'parent_item_colon'          => __( 'Parent '.$_single.':', $text_domain ),
            'new_item_name'              => __( 'New '.$_single.' Name', $text_domain ),
            'add_new_item'               => __( 'Add New '.$_single, $text_domain ),
            'edit_item'                  => __( 'Edit '.$_single, $text_domain ),
            'update_item'                => __( 'Update '.$_single, $text_domain ),
            'view_item'                  => __( 'View '.$_single, $text_domain ),
            'separate_items_with_commas' => __( 'Separate '.$_plural.' with commas', $text_domain ),
            'add_or_remove_items'        => __( 'Add or remove '.$_plural, $text_domain ),
            'choose_from_most_used'      => __( 'Choose from the most used', $text_domain ),
            'popular_items'              => __( 'Popular '.$_plural, $text_domain ),
            'search_items'               => __( 'Search '.$_plural, $text_domain ),
            'not_found'                  => __( 'Not Found', $text_domain ),
            'no_terms'                   => __( 'No '.$_plural, $text_domain ),
            'items_list'                 => __( $_plural.' list', $text_domain ),
            'items_list_navigation'      => __( $_plural.' list navigation', $text_domain ),
        );
    $args = array(
        'labels'                     => $labels,
        'hierarchical'               => isset($_arg['hierarchical']) ? $_arg['hierarchical'] :  true,
        'public'                     => isset($_arg['public']) ? $_arg['public'] : true,
        'show_ui'                    => isset($_arg['show_ui']) ? $_arg['show_ui'] : true,
        'show_admin_column'          => isset($_arg['show_admin_column']) ? $_arg['show_admin_column'] : true,
        'show_in_nav_menus'          => isset($_arg['show_in_nav_menus']) ? $_arg['show_in_nav_menus'] : true,
        'show_tagcloud'              => isset($_arg['show_tagcloud']) ? $_arg['show_tagcloud'] : false,
    );
    register_taxonomy( $_taxo_type , $_post_types , $args );
    }
}