CourseGate


Source

File: classes/CourseGate.php

class CourseGate {

	public function __construct() {

		add_dozent_action( 'enter_course', [ $this, 'enter_course' ] );
		add_filter( 'dozent_video_start_stream', [ $this, 'video_streaming_permission' ], 10, 2 );
	}


	public function enter_course() {
		dozent_checking_nonce();

		$course_id = (int) dozent_input_text( 'course_id' );

		/**
		 * Main course gate action hook, it will fire most first when any user try to enter / buy course
		 *
		 * @since DozentLMS 1.0.0
		 *
		 * @param  int  $course_id  Course ID
		 */

		do_action( 'dozent_course_gate_before', $course_id );

		$course    = dozent_get_course( $course_id );
		$start_uri = $course->start_uri();

		//If Public, send him to direct course start page
		if ( $course->is_public() ) {
			dozent_redirect( $start_uri );
		}

		/**
		 * Filter the auto enrol decision
		 *
		 * @since DozentLMS 1.0.0
		 */

		$auto_enrol = apply_filters( 'dozent_course_auto_enrol_after_login', true );

		if ( ! is_user_logged_in() ) {

			$login_redirect_query_arg = [
				'course_id'              => $course_id,
				'redirected_after_login' => 1,
			];

			if ( $auto_enrol ) {
				$login_redirect_query_arg['auto_enrol'] = 1;
			}

			/**
			 * Filter the query arg after successfully login and redirect course URL
			 */

			$login_redirect_query_arg = (array) apply_filters( 'login_redirect_to_course_query_arg',
				$login_redirect_query_arg );

			if ( dozent_count( $login_redirect_query_arg ) ) {
				$start_uri = add_query_arg( $login_redirect_query_arg, $start_uri );
			}

			dozent_redirect( wp_login_url( $start_uri ) );
		}

		//If course is protected sent directory to course start page
		if ( $course->is_protected() ) {
			dozent_redirect( $start_uri );
		}

		$user = wp_get_current_user();

		if ( dozent_course_is_enrolled( $course, $user, true ) ) {

			dozent_redirect( $start_uri );

		} else{

			if ( $course->is_free() ) {
				$enrol_data = dozent_enrol_course( $course, $user );
				dozent_redirect( $start_uri );
			}

			if ( $course->is_closed() ) {
				//Close product required integration in order to complete enrolment

				/**
				 * Action hook to process course gate with integration
				 *
				 * @since DozentLMS 1.0.0
				 *
				 * @param object $course
				 */

				do_action( 'dozent_course_type_closed_gate', $course );

			}

		}

		do_action( 'dozent_course_gate_after', $course_id );
	}

	/**
	 * Check course has access for specific user
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 * @see video_streaming_permission();
	 *
	 * @param bool $bool True or False
	 * @param int $post_id Course ID
	 *
	 * @return bool
	 */
	public function video_streaming_permission( $bool, $post_id ){
		return dozent_has_course_access( $post_id );
	}


}

Methods