PluginProbe
Tutor LMS – eLearning and online course solution / 1.3.5
Tutor LMS – eLearning and online course solution v1.3.5
4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 4.0.0 3.9.15 3.9.14 3.9.13 3.9.12 3.9.11 trunk 1.0.0 1.0.0-alpha 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 All 191 releases
tutor / classes / Rewrite_Rules.php

Rewrite_Rules.php in Tutor LMS – eLearning and online course solution 1.3.5, at classes/Rewrite_Rules.php

127 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace TUTOR;
3
4 if ( ! defined( 'ABSPATH' ) )
5 exit;
6
7 class Rewrite_Rules extends Tutor_Base {
8
9
10 public function __construct() {
11 parent::__construct();
12
13 add_filter( 'query_vars', array($this, 'tutor_register_query_vars') );
14 add_action('generate_rewrite_rules', array($this, 'add_rewrite_rules'));
15
16 //Lesson Permalink
17 add_filter('post_type_link', array($this, 'change_lesson_single_url'), 1, 2);
18 }
19
20 public function tutor_register_query_vars( $vars ) {
21 $vars[] = 'course_subpage';
22 $vars[] = 'lesson_video';
23 $vars[] = 'tutor_dashboard_page';
24 $vars[] = 'tutor_dashboard_sub_page';
25
26 $enable_public_profile = tutor_utils()->get_option('enable_public_profile');
27 if ($enable_public_profile){
28 $vars[] = 'tutor_student_username';
29 $vars[] = 'profile_sub_page';
30 }
31
32 return $vars;
33 }
34
35 public function add_rewrite_rules($wp_rewrite){
36 $new_rules = array(
37 //Lesson Permalink
38 $this->course_post_type."/(.+?)/{$this->lesson_base_permalink}/(.+?)/?$" => "index.php?post_type={$this->lesson_post_type}&name=".$wp_rewrite->preg_index(2),
39 //Quiz Permalink
40 $this->course_post_type."/(.+?)/tutor_quiz/(.+?)/?$" => "index.php?post_type=tutor_quiz&name=".$wp_rewrite->preg_index(2),
41 //Assignments URL
42 $this->course_post_type."/(.+?)/assignments/(.+?)/?$" => "index.php?post_type=tutor_assignments&name=".$wp_rewrite->preg_index(2),
43
44 //Private Video URL
45 "video-url/(.+?)/?$" => "index.php?post_type={$this->lesson_post_type}&lesson_video=true&name=". $wp_rewrite->preg_index(1),
46 //Student Public Profile URL
47 "profile/(.+?)/(.+?)/?$" => "index.php?tutor_student_username=". $wp_rewrite->preg_index(1)."&profile_sub_page=".$wp_rewrite->preg_index(2),
48 "profile/(.+?)/?$" => "index.php?tutor_student_username=". $wp_rewrite->preg_index(1),
49 );
50
51 //Nav Items
52 $course_nav_items = tutor_utils()->course_sub_pages();
53 //$course_nav_items = array_keys($course_nav_items);
54
55 if (is_array($course_nav_items) && count($course_nav_items)){
56 foreach ($course_nav_items as $nav_key => $nav_item){
57 $new_rules[$this->course_post_type."/(.+?)/{$nav_key}/?$"] ="index.php?post_type={$this->course_post_type}&name=".$wp_rewrite->preg_index(1).'&course_subpage='.$nav_key;
58 }
59 }
60
61 //Student Dashboard URL
62 $dashboard_pages = tutor_utils()->tutor_dashboard_pages();
63 foreach ($dashboard_pages as $dashboard_key => $dashboard_page){
64 $new_rules["(.+?)/{$dashboard_key}/?$"] ='index.php?pagename='.$wp_rewrite->preg_index(1).'&tutor_dashboard_page=' .$dashboard_key;
65
66 //Sub Page of dashboard sub page
67 //regext = ([^/]*)
68 $new_rules["(.+?)/{$dashboard_key}/(.+?)/?$"] ='index.php?pagename='.$wp_rewrite->preg_index(1).'&tutor_dashboard_page=' .$dashboard_key.'&tutor_dashboard_sub_page='.$wp_rewrite->preg_index(2);
69 }
70
71 $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
72 }
73
74 /**
75 * @param $post_link
76 * @param int $id
77 *
78 * @return string
79 *
80 * Change the lesson permalink
81 */
82 function change_lesson_single_url($post_link, $id=0){
83 $post = get_post($id);
84
85 global $wpdb;
86
87 $course_base_slug = 'sample-course';
88
89 if( is_object($post) && $post->post_type == $this->lesson_post_type){
90 //Lesson Permalink
91 $course_id = get_post_meta($post->ID, '_tutor_course_id_for_lesson', true);
92
93 if ($course_id){
94 $course = $wpdb->get_row("select {$wpdb->posts}.post_name from {$wpdb->posts} where ID = {$course_id} ");
95 if ($course){
96 $course_base_slug = $course->post_name;
97 }
98 return home_url("/{$this->course_post_type}/{$course_base_slug}/{$this->lesson_base_permalink}/". $post->post_name.'/');
99 }else{
100 return home_url("/{$this->course_post_type}/sample-course/{$this->lesson_base_permalink}/". $post->post_name.'/');
101 }
102 }elseif (is_object($post) && $post->post_type === 'tutor_quiz'){
103 //Quiz Permalink
104 $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} ");
105 if ($course){
106 //Checking if this topic
107 if ($course->post_type !== $this->course_post_type){
108 $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} ");
109 }
110 //Checking if this lesson
111 if (isset($course->post_type) && $course->post_type !== $this->course_post_type){
112 $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} ");
113 }
114
115 $course_post_name = isset($course->post_name) ? $course->post_name : 'sample-course';
116 return home_url("/{$this->course_post_type}/{$course_post_name}/tutor_quiz/{$post->post_name}/");
117 }else{
118 return home_url("/{$this->course_post_type}/sample-course/tutor_quiz/{$post->post_name}/");
119 }
120 }
121 return $post_link;
122 }
123
124 }
125
126
127