<?php

if ( !defined( 'ABSPATH' ) ) {
	exit;
}

class OVATEAM_templates_loader {
	
	/**
	 * The Constructor
	 */
	public function __construct() {
		add_filter( 'template_include', array( $this, 'template_loader' ) );
	}

	public function template_loader( $template ) {

		$post_type = isset($_REQUEST['post_type'] ) ? esc_html( $_REQUEST['post_type'] ) : get_post_type();

		$email_from_element = isset( $_REQUEST['email_from_element'] ) ? esc_html( $_REQUEST['email_from_element'] ) : '';

		if( $email_from_element === 'send_mail' ){

			if( send_mail_element( $_REQUEST ) ){
				$thank_page = isset( $_REQUEST['thank_page'] ) ? get_page_link( $_REQUEST['thank_page'] )  : home_url('/');
				wp_redirect( $thank_page );
			}else{
				$error_page = isset( $_REQUEST['error_page'] ) ? get_page_link( $_REQUEST['error_page'] )  : home_url('/');
				wp_redirect( $error_page );
			}
			exit();
		}


		if( is_tax( 'cat_team' ) ||  get_query_var( 'cat_team' ) != '' ){

			$paged = get_query_var('paged') ? get_query_var('paged') : '1';
			
			query_posts( '&cat_team='.get_query_var( 'cat_team' ).'&paged=' . $paged );
			ovateam_get_template( 'archive-team.php' );
			return false;
		}


		// Is Team Post Type
		if(  $post_type == 'team' ){

			if ( is_post_type_archive( 'team' ) ) { 

				ovateam_get_template( 'archive-team.php' );
				return false;

			} else if ( is_single() ) {

				ovateam_get_template( 'single-team.php' );
				return false;

			}
		}


		if ( $post_type !== 'team' ){
			return $template;
		}
	}
}

new OVATEAM_templates_loader();
