| @@ -1,109 +1,78 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * Manage rewrite rules for Tutor | |
| 4 | - * | |
| 5 | - * @package Tutor\RewriteRules | |
| 6 | - * @author Themeum <support@themeum.com> | |
| 7 | - * @link https://themeum.com | |
| 8 | - * @since 2.0.0 | |
| 9 | - */ | |
| 10 | 2 | |
| 11 | 3 | namespace TUTOR; |
| 12 | 4 | |
| 13 | -if ( ! defined( 'ABSPATH' ) ) { | |
| 5 | +if (!defined('ABSPATH')) | |
| 14 | 6 | exit; |
| 15 | -} | |
| 16 | 7 | |
| 17 | -/** | |
| 18 | - * Generate and manage rewrite rules | |
| 19 | - * | |
| 20 | - * @since 2.0.0 | |
| 21 | - */ | |
| 22 | 8 | class Rewrite_Rules extends Tutor_Base { |
| 23 | 9 | |
| 24 | - /** | |
| 25 | - * Register hooks | |
| 26 | - * | |
| 27 | - * @since 1.0.0 | |
| 28 | - */ | |
| 29 | 10 | public function __construct() { |
| 30 | 11 | parent::__construct(); |
| 31 | 12 | |
| 32 | - add_filter( 'query_vars', array( $this, 'tutor_register_query_vars' ) ); | |
| 33 | - add_action( 'generate_rewrite_rules', array( $this, 'add_rewrite_rules' ) ); | |
| 13 | + add_filter('query_vars', array($this, 'tutor_register_query_vars')); | |
| 14 | + add_action('generate_rewrite_rules', array($this, 'add_rewrite_rules')); | |
| 34 | 15 | |
| 35 | - // Lesson Permalink. | |
| 36 | - add_filter( 'post_type_link', array( $this, 'change_lesson_single_url' ), 1, 2 ); | |
| 16 | + //Lesson Permalink | |
| 17 | + add_filter('post_type_link', array($this, 'change_lesson_single_url'), 1, 2); | |
| 37 | 18 | } |
| 38 | 19 | |
| 39 | - /** | |
| 40 | - * Prepare query vars | |
| 41 | - * | |
| 42 | - * @since 1.0.0 | |
| 43 | - * | |
| 44 | - * @param sting $vars url structure. | |
| 45 | - * | |
| 46 | - * @return array | |
| 47 | - */ | |
| 48 | - public function tutor_register_query_vars( $vars ) { | |
| 20 | + public function tutor_register_query_vars($vars) { | |
| 49 | 21 | $vars[] = 'course_subpage'; |
| 50 | 22 | $vars[] = 'lesson_video'; |
| 51 | 23 | $vars[] = 'tutor_dashboard_page'; |
| 52 | 24 | $vars[] = 'tutor_dashboard_sub_page'; |
| 53 | 25 | |
| 54 | - /** | |
| 55 | - * If public_profile_layout is not private then | |
| 56 | - * add rewrite rules | |
| 57 | - * | |
| 58 | - * @since v2.0.0 | |
| 59 | - */ | |
| 60 | - $vars[] = 'tutor_profile_username'; | |
| 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 | + | |
| 61 | 32 | return $vars; |
| 62 | 33 | } |
| 63 | 34 | |
| 64 | - /** | |
| 65 | - * Tutor rewrite rules | |
| 66 | - * | |
| 67 | - * @since 1.0.0 | |
| 68 | - * @since 2.5.0 Param typecast added. | |
| 69 | - * Dynamic course permalink support added. | |
| 70 | - * | |
| 71 | - * @param \WP_Rewrite $wp_rewrite get the rewrite rule. | |
| 72 | - * | |
| 73 | - * @return void | |
| 74 | - */ | |
| 75 | - public function add_rewrite_rules( \WP_Rewrite $wp_rewrite ) { | |
| 35 | + public function add_rewrite_rules($wp_rewrite) { | |
| 76 | 36 | $new_rules = array( |
| 77 | - // Lesson Permalink. | |
| 78 | - $this->course_base_permalink . "/(.+?)/{$this->lesson_base_permalink}/(.+?)/?$" => "index.php?post_type={$this->lesson_post_type}&name=" . $wp_rewrite->preg_index( 2 ), | |
| 79 | - // Quiz Permalink. | |
| 80 | - $this->course_base_permalink . "/(.+?)/{$this->quiz_base_permalink}/(.+?)/?$" => 'index.php?post_type=tutor_quiz&name=' . $wp_rewrite->preg_index( 2 ), | |
| 81 | - // Assignments URL. | |
| 82 | - $this->course_base_permalink . "/(.+?)/{$this->assignment_base_permalink}/(.+?)/?$" => 'index.php?post_type=tutor_assignments&name=' . $wp_rewrite->preg_index( 2 ), | |
| 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 | + //Zoom Meeting | |
| 44 | + $this->course_post_type . "/(.+?)/zoom-meeting/(.+?)/?$" => "index.php?post_type=tutor_zoom_meeting&name=" . $wp_rewrite->preg_index(2), | |
| 83 | 45 | |
| 84 | - // Zoom Lessons. | |
| 85 | - $this->course_base_permalink . '/(.+?)/zoom-lessons/(.+?)/?$' => 'index.php?post_type=tutor_zoom_meeting&name=' . $wp_rewrite->preg_index( 2 ), | |
| 86 | - // Meet Lessons. | |
| 87 | - $this->course_base_permalink . '/(.+?)/meet-lessons/(.+?)/?$' => 'index.php?post_type=tutor-google-meet&name=' . $wp_rewrite->preg_index( 2 ), | |
| 46 | + //Private Video URL | |
| 47 | + "video-url/(.+?)/?$" => "index.php?post_type={$this->lesson_post_type}&lesson_video=true&name=" . $wp_rewrite->preg_index(1), | |
| 48 | + //Student Public Profile URL | |
| 49 | + "profile/(.+?)/(.+?)/?$" => "index.php?tutor_student_username=" . $wp_rewrite->preg_index(1) . "&profile_sub_page=" . $wp_rewrite->preg_index(2), | |
| 50 | + "profile/(.+?)/?$" => "index.php?tutor_student_username=" . $wp_rewrite->preg_index(1), | |
| 51 | + ); | |
| 88 | 52 | |
| 89 | - // Private Video URL. | |
| 90 | - 'video-url/(.+?)/?$' => "index.php?post_type={$this->lesson_post_type}&lesson_video=true&name=" . $wp_rewrite->preg_index( 1 ), | |
| 91 | - // Student Public Profile URL. | |
| 92 | - 'profile/(.+?)/?$' => 'index.php?tutor_profile_username=' . $wp_rewrite->preg_index( 1 ), | |
| 93 | - ); | |
| 53 | + //Nav Items | |
| 54 | + $course_nav_items = tutor_utils()->course_sub_pages(); | |
| 55 | + $course_nav_items = apply_filters('tutor_course/single/enrolled/nav_items_rewrite', $course_nav_items); | |
| 56 | + //$course_nav_items = array_keys($course_nav_items); | |
| 94 | 57 | |
| 95 | - // Student Dashboard URL. | |
| 96 | - $dashboard_pages = tutor_utils()->tutor_dashboard_permalinks(); | |
| 97 | - $dashboard_page_id = (int) tutor_utils()->get_option( 'tutor_dashboard_page_id' ); | |
| 98 | - $dashboard_page_slug = get_post_field( 'post_name', $dashboard_page_id ); | |
| 58 | + if (is_array($course_nav_items) && count($course_nav_items)) { | |
| 59 | + foreach ($course_nav_items as $nav_key => $nav_item) { | |
| 60 | + $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; | |
| 61 | + } | |
| 62 | + } | |
| 99 | 63 | |
| 100 | - foreach ( $dashboard_pages as $dashboard_key => $dashboard_page ) { | |
| 101 | - $new_rules[ "({$dashboard_page_slug})/{$dashboard_key}/?$" ] = 'index.php?pagename=' . $wp_rewrite->preg_index( 1 ) . '&tutor_dashboard_page=' . $dashboard_key; | |
| 64 | + //Student Dashboard URL | |
| 65 | + $dashboard_pages = tutor_utils()->tutor_dashboard_permalinks(); | |
| 66 | + $dashboard_page_id = (int) tutor_utils()->get_option('tutor_dashboard_page_id'); | |
| 67 | + $dashboard_page_slug = get_post_field('post_name', $dashboard_page_id); | |
| 102 | 68 | |
| 103 | - // Sub Page of dashboard sub page. | |
| 104 | - // regext = ([^/]*). | |
| 105 | - $new_rules[ "({$dashboard_page_slug})/{$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 | + foreach ($dashboard_pages as $dashboard_key => $dashboard_page) { | |
| 70 | + $new_rules["({$dashboard_page_slug})/{$dashboard_key}/?$"] = 'index.php?pagename=' . $wp_rewrite->preg_index(1) . '&tutor_dashboard_page=' . $dashboard_key; | |
| 71 | + | |
| 72 | + //Sub Page of dashboard sub page | |
| 73 | + //regext = ([^/]*) | |
| 74 | + $new_rules["({$dashboard_page_slug})/{$dashboard_key}/(.+?)/?$"] = 'index.php?pagename=' . $wp_rewrite->preg_index(1) . '&tutor_dashboard_page=' . $dashboard_key . '&tutor_dashboard_sub_page=' . $wp_rewrite->preg_index(2); | |
| 106 | 75 | } |
| 107 | 76 | |
| 108 | 77 | $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; |
| 109 | 78 | } |
| @@ -108,49 +77,52 @@ | ||
| 108 | 77 | $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; |
| 109 | 78 | } |
| 110 | 79 | |
| 111 | 80 | /** |
| 112 | - * Change the lesson permalink | |
| 81 | + * @param $post_link | |
| 82 | + * @param int $id | |
| 113 | 83 | * |
| 114 | - * @param string $post_link post link. | |
| 115 | - * @param int $id post id. | |
| 84 | + * @return string | |
| 116 | 85 | * |
| 117 | - * @return string | |
| 86 | + * Change the lesson permalink | |
| 118 | 87 | */ |
| 119 | - public function change_lesson_single_url( $post_link, $id = 0 ) { | |
| 120 | - $post = get_post( $id ); | |
| 88 | + function change_lesson_single_url($post_link, $id = 0) { | |
| 89 | + $post = get_post($id); | |
| 90 | + | |
| 91 | + global $wpdb; | |
| 92 | + | |
| 121 | 93 | $course_base_slug = 'sample-course'; |
| 122 | 94 | |
| 123 | - if ( is_object( $post ) && $post->post_type == $this->lesson_post_type ) { | |
| 124 | - // Lesson Permalink. | |
| 125 | - $course_id = tutor_utils()->get_course_id_by( 'lesson', $post->ID ); | |
| 95 | + if (is_object($post) && $post->post_type == $this->lesson_post_type) { | |
| 96 | + //Lesson Permalink | |
| 97 | + $course_id = get_post_meta($post->ID, '_tutor_course_id_for_lesson', true); | |
| 126 | 98 | |
| 127 | - if ( $course_id ) { | |
| 128 | - $course = get_post( $course_id ); | |
| 129 | - if ( $course ) { | |
| 99 | + if ($course_id) { | |
| 100 | + $course = $wpdb->get_row("select {$wpdb->posts}.post_name from {$wpdb->posts} where ID = {$course_id} "); | |
| 101 | + if ($course) { | |
| 130 | 102 | $course_base_slug = $course->post_name; |
| 131 | 103 | } |
| 132 | - return home_url( "/{$this->course_base_permalink}/{$course_base_slug}/{$this->lesson_base_permalink}/" . $post->post_name . '/' ); | |
| 104 | + return home_url("/{$this->course_post_type}/{$course_base_slug}/{$this->lesson_base_permalink}/" . $post->post_name . '/'); | |
| 133 | 105 | } else { |
| 134 | - return home_url( "/{$this->course_base_permalink}/sample-course/{$this->lesson_base_permalink}/" . $post->post_name . '/' ); | |
| 106 | + return home_url("/{$this->course_post_type}/sample-course/{$this->lesson_base_permalink}/" . $post->post_name . '/'); | |
| 135 | 107 | } |
| 136 | - } elseif ( is_object( $post ) && 'tutor_quiz' === $post->post_type ) { | |
| 137 | - // Quiz Permalink. | |
| 138 | - $course = get_post( $post->post_parent ); | |
| 139 | - if ( $course ) { | |
| 140 | - // Checking if this topic. | |
| 141 | - if ( $course->post_type !== $this->course_post_type ) { | |
| 142 | - $course = get_post( $course->post_parent ); | |
| 108 | + } elseif (is_object($post) && $post->post_type === 'tutor_quiz') { | |
| 109 | + //Quiz Permalink | |
| 110 | + $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$post->post_parent} "); | |
| 111 | + if ($course) { | |
| 112 | + //Checking if this topic | |
| 113 | + if ($course->post_type !== $this->course_post_type) { | |
| 114 | + $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} "); | |
| 143 | 115 | } |
| 144 | - // Checking if this lesson. | |
| 145 | - if ( isset( $course->post_type ) && $course->post_type !== $this->course_post_type ) { | |
| 146 | - $course = get_post( $course->post_parent ); | |
| 116 | + //Checking if this lesson | |
| 117 | + if (isset($course->post_type) && $course->post_type !== $this->course_post_type) { | |
| 118 | + $course = $wpdb->get_row("select ID, post_name, post_type, post_parent from {$wpdb->posts} where ID = {$course->post_parent} "); | |
| 147 | 119 | } |
| 148 | 120 | |
| 149 | - $course_post_name = isset( $course->post_name ) ? $course->post_name : 'sample-course'; | |
| 150 | - return home_url( "/{$this->course_base_permalink}/{$course_post_name}/{$this->quiz_base_permalink}/{$post->post_name}/" ); | |
| 121 | + $course_post_name = isset($course->post_name) ? $course->post_name : 'sample-course'; | |
| 122 | + return home_url("/{$this->course_post_type}/{$course_post_name}/tutor_quiz/{$post->post_name}/"); | |
| 151 | 123 | } else { |
| 152 | - return home_url( "/{$this->course_base_permalink}/sample-course/{$this->quiz_base_permalink}/{$post->post_name}/" ); | |
| 124 | + return home_url("/{$this->course_post_type}/sample-course/tutor_quiz/{$post->post_name}/"); | |
| 153 | 125 | } |
| 154 | 126 | } |
| 155 | 127 | return $post_link; |
| 156 | 128 | } |