Advertisement
mbis

LearnDash Nested Lesson URLs

May 5th, 2022
1,073
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.85 KB | None | 0 0
  1. /**
  2.  * Detect and parse the nested lesson URL
  3.  */
  4. function pm_detect_nested_lesson_url($uri_parts, $request_url, $endpoints) {
  5.     $lesson_cpt_slug = pm_get_lesson_rewrite_slug();
  6.    
  7.     if(!empty($uri_parts['uri']) && strpos("/{$uri_parts['uri']}/", $lesson_cpt_slug) !== false) {
  8.         preg_match('/([^\/]+)\/lessen\/([^\/]+)/', $uri_parts['uri'], $parts);
  9.        
  10.         if(!empty($parts[2])) {
  11.             $uri_parts['uri'] = $parts[2];
  12.             $uri_parts['course'] = $parts[1];
  13.         }
  14.     }
  15.    
  16.     return $uri_parts;
  17. }
  18. add_filter('permalink_manager_detect_uri', 'pm_detect_nested_lesson_url', 10, 3);
  19.  
  20. /**
  21.  * Fix lesson query if "Nested URLs" are enabled (add original course name to the query)
  22.  */
  23. function pm_query_nested_lesson_url($query, $old_query, $uri_parts, $pm_query, $content_type) {
  24.     if(!empty($query['post_type']) && $query['post_type'] == 'sfwd-lessons' && !empty($pm_query['id']) && function_exists('learndash_get_course_id')) {
  25.         $lesson_id = $pm_query['id'];
  26.        
  27.         if(!empty($uri_parts['course'])) {
  28.             $course_name = $uri_parts['course'];
  29.         } else {
  30.             $course_id = learndash_get_course_id($lesson_id);
  31.             $course_name = get_post_field('post_name', $course_id);
  32.         }
  33.        
  34.         if(!empty($course_name) && empty($query['sfwd-courses'])) {
  35.             $query['sfwd-courses'] = $course_name;
  36.         }
  37.     }
  38.    
  39.     return $query;
  40. }
  41. add_filter('permalink_manager_filter_query', 'pm_query_nested_lesson_url', 5, 5);
  42.  
  43. /**
  44.  * Get rewrite slug for 'lesson'
  45.  */
  46. function pm_get_lesson_rewrite_slug() {
  47.     if(function_exists('learndash_get_post_type_slug')) {
  48.         $lesson_cpt_slug = learndash_get_post_type_slug('lesson');
  49.         $lesson_cpt_obj = get_post_type_object($lesson_cpt_slug);
  50.         $lesson_cpt_slug = apply_filters('learndash_post_type_rewrite_slug', $lesson_cpt_obj->rewrite['slug'], $lesson_cpt_slug);  
  51.     } else {
  52.         $lesson_cpt_slug = 'lesson';
  53.     }
  54.    
  55.     return $lesson_cpt_slug;
  56. }
  57.  
  58. /**
  59.  * Add course URL to lesson URL if "Nested URLs" are enabled
  60.  */
  61. function pm_append_course_name($permalink, $post, $old_permalink) {
  62.     global $permalink_manager_uris;
  63.    
  64.     if(!empty($post->post_type) && $post->post_type == 'sfwd-lessons' && !empty($permalink_manager_uris[$post->ID])) {
  65.         $course_id = 0;
  66.  
  67.         if((isset($_GET['course_id'])) && (!empty($_GET['course_id']))) {
  68.             $course_id = absint($_GET['course_id']);
  69.         }
  70.         $course_id = apply_filters( 'learndash_post_link_course_id', $course_id, $old_permalink, $post);
  71.        
  72.         if(!empty($course_id)) {
  73.             $course_name = get_post_field('post_name', $course_id);
  74.             $lesson_cpt_slug = pm_get_lesson_rewrite_slug();
  75.             $lesson_old_uri = $permalink_manager_uris[$post->ID];
  76.            
  77.             $lesson_new_uri = sprintf('%s/%s/%s', $course_name, $lesson_cpt_slug, $lesson_old_uri);
  78.  
  79.             $permalink = str_replace($lesson_old_uri, $lesson_new_uri, $permalink);
  80.         }
  81.     }
  82.    
  83.     return $permalink;
  84. }
  85. add_filter('permalink_manager_filter_final_post_permalink', 'pm_append_course_name', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement