Permalinks


Source

File: classes/Permalinks.php

class Permalinks {

	public function __construct() {
		add_filter( 'query_vars', [ $this, 'register_query_vars' ] );
		add_action( 'generate_rewrite_rules', [ $this, 'add_rewrite_rules' ] );
		add_filter( 'post_type_link', [ $this, 'course_content_permalink' ], 1, 2 );
		add_action( 'dozent_option_save_after', [ $this, 'flush_rewrite' ] );
	}

	/**
	 * Register query vars
	 *
	 *
	 * @since DozentLMS 1.0.0
	 *
	 *
	 * @param $vars
	 *
	 * @return array
	 */

	public function register_query_vars( $vars ) {
		$vars = array_merge( $vars, dozent_register_query_vars() );

		return $vars;
	}

	public function add_rewrite_rules( $wp_rewrite ) {
		$course_base_slug     = dozent_course_base_slug();
		$lecture_base_slug    = dozent_lecture_base_slug();
		$quiz_base_slug       = dozent_quiz_base_slug();
		$assignment_base_slug = dozent_assignment_base_slug();

		$new_rules = [
			//Lesson Permalink
			$course_base_slug . "/(.+?)/{$lecture_base_slug}/(.+?)/?$"    => "index.php?post_type=dozent_lecture&name=" . $wp_rewrite->preg_index( 2 ),
			//Quiz Permalink
			$course_base_slug . "/(.+?)/{$quiz_base_slug}/(.+?)/?$"       => "index.php?post_type=dozent_quiz&name=" . $wp_rewrite->preg_index( 2 ),
			//Assignments URL
			$course_base_slug
			. "/(.+?)/{$assignment_base_slug}/(.+?)/?$" => "index.php?post_type=dozent_assignment&name=" . $wp_rewrite->preg_index( 2 ),

			//Private Video URL
			"video-url/(.+?)/?$" => "index.php?post_type=dozent_lecture&lecture_video=true&name=" . $wp_rewrite->preg_index( 1 ),
			//Student Public Profile URL
			"profile/(.+?)/(.+?)/?$" => "index.php?dozent_profile_username=" . $wp_rewrite->preg_index( 1 ) . "&profile_sub_page=" . $wp_rewrite->preg_index( 2 ),
			"profile/(.+?)/?$" => "index.php?dozent_profile_username=" . $wp_rewrite->preg_index( 1 ),
		];

		//Dashboard URL
		$dashboard_permalink_groups = dozent_dashboard_permalinks();

		foreach ( $dashboard_permalink_groups as $dashboard_pages ) {
			foreach ( $dashboard_pages as $dashboard_page ) {
				$page_id = dozent_array_get( 'id', $dashboard_page );

				$new_rules["(.+?)/{$page_id}/?$"] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&dozent_dashboard_page=' . $page_id;

				//Sub Page of dashboard sub page
				//regext = ([^/]*)
				$new_rules["(.+?)/{$page_id}/(.+?)/?$"] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&dozent_dashboard_page=' . $page_id . '&dozent_dashboard_sub_page=' . $wp_rewrite->preg_index( 2 );
			}
		}

		$wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
	}


	function course_content_permalink( $post_link, $post ) {
		$post = get_post( $post );

		global $wpdb;

		$course_base_slug  = dozent_course_base_slug();
		$lecture_base_slug = dozent_lecture_base_slug();
		$quiz_base_slug    = dozent_quiz_base_slug();

		$course_post_name = 'sample-course';


		$course_id = (int) dozent_get_course_id_by_content( $post->ID );

		if ( $course_id ) {
			$course_slug = $wpdb->get_var( "select post_name from {$wpdb->posts} where ID = {$course_id}" );

			if ( $course_slug ) {
				$course_post_name = $course_slug;
			}
		}

		if ( is_object( $post ) && $post->post_type == 'dozent_lecture' ) {
			return home_url( "/{$course_base_slug}/{$course_post_name}/{$lecture_base_slug}/" . $post->post_name . '/' );

		} else if ( is_object( $post ) && $post->post_type === 'dozent_quiz' ) {

			return home_url( "/{$course_base_slug}/{$course_post_name}/{$quiz_base_slug}/" . $post->post_name . '/' );
		}

		return $post_link;
	}

	public function flush_rewrite() {
		flush_rewrite_rules();
	}


}

Methods