<?php

/**
 * Registers 'team' post type 
 *
 * This function registers the 'team' posts
 * type which is used for the meet the team posts.
 *
 * @function team_post_type()
 *
 * @uses register_post_type()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.3.9 
 *
 */
function team_post_type() {
	
	$labels = array(
		'name' 				 => _x( 'Team', 'post type general name' ),
		'singular_name' 	 => _x( 'Team', 'post type singular name' ),
		'menu_name' 		 => __( 'Team Profiles' ),
		'add_new' 			 => _x( 'Add New', 'team item' ),
		'add_new_item' 		 => __( 'Add New Profile' ),
		'edit_item' 		 => __( 'Edit Profile' ),
		'new_item' 			 => __( 'New Profile' ),
		'view_item' 		 => __( 'View Profile' ),
		'search_items' 		 => __( 'Search Profiles' ),
		'not_found' 		 =>  __( 'No Profiles Found' ),
		'not_found_in_trash' => __( 'No Profiles Found in Trash' ),
		'parent_item_colon'  => '',
	);
	register_post_type( 'team' , array(
		'labels' 	  => $labels,
		'public' 	  => true,
		'has_archive' => false,
		'menu_icon'   => 'dashicons-groups',
		'rewrite' 	  => true,
		'supports' 	  => array( 'title', 'editor', 'thumbnail' ),
		'show_in_rest' => true,
	) );
}
add_action( 'init', 'team_post_type', 0 );



/**
 * Registers 'departments' taxonomy for 'team' post type 
 *
 * This function registers the 'departments' taxonomy
 * for the 'team' posts type which is used for the
 * meet the team posts.
 *
 * @function team_taxonomy()
 *
 * @uses register_taxonomy()
 *
 * @param N/A
 *
 * @return N/A
 *
 * @since 1.0.0 
 *
 */
function team_taxonomy() {
	
	$labels = array(
		'name' 				=> _x( 'Departments', 'taxonomy general name' ),
		'singular_name' 	=> _x( 'Department', 'taxonomy singular name' ),
		'search_items' 		=> __( 'Search Department' ),
		'all_items' 		=> __( 'All Departments' ),
		'parent_item' 		=> __( 'Parent Department' ),
		'parent_item_colon' => __( 'Parent Department:' ),
		'edit_item' 		=> __( 'Edit Department' ),
		'update_item' 		=> __( 'Update Department' ),
		'add_new_item' 		=> __( 'Add New Department' ),
		'new_item_name' 	=> __( 'New Department Name' ),
	);
	register_taxonomy( 'department', 'team', array(
		'labels' 			=> $labels,
		'public' 			=> true,
		'show_ui' 			=> true,
		'show_in_nav_menus' => true,
		'hierarchical' 		=> true,
		'query_var' 		=> true,
		'rewrite' 			=> false,
	) );
}
add_action( 'init', 'team_taxonomy', 0 );



/**
 * Returns 'team' post type 
 *
 * This function returns the 'team' posts type which
 * is used for the meet the team posts. The posts are
 * returned using the team template located in the
 * WordPress Theme.
 *
 * @function add_meet_the_team()
 *
 * @uses WP_Query::the_post(), get_template_part()
 *
 * @param N/A
 *
 * @return (string) The 'team' posts
 *
 * @since 1.0.0 
 *
 */
function add_meet_the_team() {
	
    $query = new WP_Query( array(
		'post_type' 	 => 'team',
        'post_status' 	 => 'publish',
		'posts_per_page' => -1,
		'meta_key' 		 => 'order-by',
        'orderby' 		 => 'meta_value_num',
        'order' 		 => 'ASC',
    ) );
	$string = '';
	
	if ( $query->have_posts() ) {
		
		while ( $query->have_posts() ) {
			
			$query->the_post();
			
			ob_start();
			
			get_template_part( 'templates/meet-the-team' );
			
			$string .= ob_get_clean();
		}
	}
	wp_reset_postdata();
	return $string;
}
add_shortcode( 'meet_the_team', 'add_meet_the_team' );