Template


Source

File: classes/Template.php

class Template {

    public function __construct(){
    	add_action( 'pre_get_posts', [$this, 'course_query_archive'], 99 );
        
        /**
         * Should Load Template Override
         * Integration for specially for oxygen builder
         * If we found false of below filter, then we will not use this file
         */

        $template_override = apply_filters('dozent_should_template_override', true);
        if ( ! $template_override){
            return;
        }

        add_filter( 'template_include', [$this, 'course_archive_template'], 99 );
        add_filter( 'template_include', [$this, 'single_course_template'], 99 );
        add_filter( 'template_include', [$this, 'single_curriculum_template'], 99 );
	    add_filter( 'template_include', [$this, 'stream_video'], 99 );
	    add_filter( 'template_include', [$this, 'public_profile'], 99 );
	    add_filter( 'pre_get_document_title', [$this, 'public_profile_title']);
	    
	    add_filter( 'the_content', [$this, 'page_to_template']);

	    add_filter( 'template_include', [$this, 'dozent_dashboard'], 99 );
        add_filter( 'dozent_load_dashboard_page', [$this, 'dozent_load_dashboard_page'], 99, 2 );
	    add_action( 'template_redirect', [ $this, 'logged_in_user_in_signup_page' ] );


    }

    public function course_archive_template( $template ){

    	if ( is_dozent_course_archive() ){
		    $template = dozent_get_template_path('archive-course');
		    return $template;
	    }

    	return $template;
    }


	public function course_query_archive($query){

		$courses_per_page = dozent_courses_per_page();
		$search_query = get_search_query();

		if ( is_dozent_course_archive_page() ) {
			$paged        = dozent_current_page();

			query_posts( [
				'post_type'      => 'dozent_course',
				'paged'          => $paged,
				's'              => $search_query,
				'posts_per_page' => $courses_per_page
			] );
		}

		if ( is_dozent_course_archive() ){

			$query->set('posts_per_page', $courses_per_page);

			$sort_by = dozent_input_text( 'sort_by' );
			if ( ! $sort_by ){
				$sort_by = 'relevance';
			}

			switch ($sort_by){

				case 'most-reviewed':
					$query->set('orderby', 'meta_value_num');
					$query->set('meta_key', '_rating_count');
					$query->set('order', 'desc');
					break;

				case 'highest-rated':
					$query->set('orderby', 'meta_value_num');
					$query->set('meta_key', '_rating_avg');
					$query->set('order', 'desc');
					break;
				case 'newest-first':
				case 'relevance':
					$query->set('orderby', 'ID');
					$query->set('order', 'desc');
					break;
				case 'oldest-first':
					$query->set('orderby', 'ID');
					$query->set('order', 'asc');
					break;
				case 'course-title-az':
					$query->set('orderby', 'post_title');
					$query->set('order', 'asc');
					break;
				case 'course-title-za':
					$query->set('orderby', 'post_title');
					$query->set('order', 'desc');
					break;
			}
		}

		/**
		 * Filter by level
		 */
		$levels = dozent_input_array_field( 'level' );

		if ( dozent_count( $levels ) && ! in_array( 'all', $levels ) ) {

			$level_query[] = [
				'key'     => '_difficulty_level',
				'value'   => $levels,
				'compare' => 'IN',
			];

			$query->set( 'meta_query', $level_query );
		}

		//Filter by Rating
		$rating = dozent_input_text( 'rating' );
		if ( $rating ) {
			$level_query[] = [
				'key'     => '_rating_avg',
				'value'   => $rating,
				'compare' => '>=',
			];
			$query->set( 'meta_query', $level_query );
		}
		
		//Filter by Video Duration
		$video_duration = dozent_input_text( 'video_duration' );

		if ( $video_duration ){
			
			if ($video_duration === '0_2'){
				$durationStart = 0;
				$durationEnd = (60 * 60 * 3) - 1; //02:59:59

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '3_5'){
				$durationStart = (60 * 60 * 3) ;
				$durationEnd = (60 * 60 * 6) -1;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '6_10'){
				$durationStart = (60 * 60 * 6) ;
				$durationEnd = (60 * 60 * 11) -1;


				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '11_20'){
				$durationStart = (60 * 60 * 11) ;
				$durationEnd = (60 * 60 * 21) -1;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => array( $durationStart, $durationEnd ),
					'type'    => 'numeric',
					'compare' => 'BETWEEN',
				];
				$query->set( 'meta_query', $level_query );

			}elseif ($video_duration === '21'){
				$durationStart = (60 * 60 * 21) ;

				$level_query[] = [
					'key'     => '_total_video_time',
					'value'   => $durationStart,
					'compare' => '>=',
				];
				$query->set( 'meta_query', $level_query );

			}

		}

	}

	/**
     * Load Single Course Template
     */
    public function single_course_template($template){

    	if (is_dozent_course() && ! dozent_has_single_template_in_theme( 'dozent_course' ) ){
            wp_reset_query();

            $template = dozent_get_template_path( 'single-course' );

            return apply_filters('dozent_single_course_template', $template);
        }
        return $template;
    }


    public function single_curriculum_template( $template ) {

	    if ( is_dozent_single_lecture() || is_dozent_single_quiz() || is_dozent_single_assignment() ) {
		    $page_id = get_the_ID();

		    do_action( 'dozent_curriculum_load_before', $template );

		    setup_postdata( $page_id );

		    $template = dozent_get_template_path( 'single-curriculum' );

		    wp_reset_postdata();

		    return apply_filters( 'dozent_curriculum_template', $template );
	    }

	    return $template;
    }


    public function page_to_template( $content ) {

	    //Instructor SignUp Page

	    if ( is_dozent_user_signup_page() ) {
		    $shortcode = new Shortcode();
		    return $shortcode->user_signup();
	    }

	    return $content;
    }

    public function dozent_dashboard( $template ){

        if ( is_dozent_dashboard() ) {
        	global $wp_query;

	        $dashboard_page = dozent_array_get('dozent_dashboard_page', $wp_query->query_vars);

	        do_action( 'dozent_dashboard_template_before_'.$dashboard_page );

	        $dashboard_pages = dozent_dashboard_permalinks();
	        $dashboard_page_item = dozent_array_get($dashboard_page, $dashboard_pages);

	        if ( $dashboard_page && dozent_array_get( 'guest_item', $dashboard_page_item ) ){
		        $template = dozent_get_template_path( "partial.{$dashboard_page}" );
	        }else{
		        $template = dozent_get_template_path( 'dashboard' );
	        }
        }

        return $template;
    }


	function stream_video( $template ) {
		if ( is_dozent_single_lecture() && get_query_var( 'lecture_video' ) === 'true' ) {
			/**
			 * Filter to default start video stream behaviour
			 *
			 * @param bool $bool Default false
			 * @param int $lectureID Lecture ID
			 */

			$should_start = apply_filters( 'dozent_video_start_stream', false, get_the_ID() );
			if ( $should_start ) {
				$video = dozent_get_video();
				if ( $video ) {
					$stream = new VideoStream( dozent_array_get( 'path', $video ) );
					$stream->start();
				}
				exit();
			}

			exit( __( 'Video is protected', 'dozent' ) );
		}

		return $template;
	}

	/**
	 * @param $template_html
	 * @param $template
	 *
	 * @return mixed|void
	 *
	 * Check dashboard page if proper permission exists
	 */

    public function dozent_load_dashboard_page( $template_html, $template ){
        $url_segment = dozent_get_dashboard_url_segment();

        if ( $url_segment ) {
        	$has_permission = dozent_has_instructor_permission_dashboard_permalink( $url_segment );

        	if ( ! $has_permission ){
		        $template_html = dozent_load_template( 'global/permission-denied', [], false );
	        }
        }

        return $template_html;
    }

	public function public_profile($template){
    	if ( is_dozent_profile_page() ){
		    $template = dozent_get_template_path( 'profile' );
	    }

		return $template;
	}

	public function public_profile_title() {
		$username = get_query_var( 'dozent_profile_username' );
		if ( ! empty( $username ) ) {
			$user = get_user_by( 'slug', $username );

			if ( $user ) {
				return sprintf( "%s's Profile page ", $user->display_name );
			}
		}

		return '';
	}

	public function logged_in_user_in_signup_page(){
    	if ( is_dozent_user_signup_page() ){
    		dozent_redirect( dozent_dashboard_uri() );
	    }
	}

}

Methods