Advertisement
mbis

LearnDash Nested Lesson URLs #2

May 6th, 2022 (edited)
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.85 KB | None | 0 0
  1. /**
  2.  * Fix lesson query if "Nested URLs" are enabled (add original course name to the query)
  3.  */
  4. function pm_query_lesson_url($query, $old_query = '') {
  5.     global $wpdb, $wp, $pm_query;
  6.    
  7.     if(empty($wp->request)) {
  8.         return $query;
  9.     }
  10.    
  11.     $lesson_cpt_slug = pm_get_ldms_rewrite_slug('lesson');
  12.    
  13.     // A. Detect nested lesson URLs
  14.     if(strpos($wp->request, "/{$lesson_cpt_slug}/") !== false) {
  15.         preg_match("/([^\/]+)\/{$lesson_cpt_slug}\/([^\/]+)(?:.+?([^\/]+)$)?/", $wp->request, $parts);
  16.        
  17.         if(!empty($parts[2])) {
  18.             $query = array(
  19.                 'do_not_redirect' => 1,
  20.                 'sfwd-courses' => $parts[1],
  21.                 'sfwd-lessons' => $parts[2],
  22.                 'name' => $parts[2],
  23.                 'post_type' => 'sfwd-lessons'
  24.             );
  25.         }
  26.        
  27.         if(!empty($parts[3])) {
  28.             $topic_quiz = sanitize_title($parts[3]);
  29.             $post_type = $wpdb->get_var($wpdb->prepare("SELECT post_type FROM {$wpdb->posts} WHERE post_name = %s", $topic_quiz));
  30.            
  31.             if(!empty($post_type) && in_array($post_type, array('sfwd-topic', 'sfwd-quiz'))) {
  32.                 $query[$post_type] = $topic_quiz;
  33.                 $query['post_type'] = $post_type;
  34.             }
  35.         }
  36.     }
  37.     // B. Check if requested slug is used by any lesson/topic/quiz
  38.     else if(strpos($wp->request, "/") === false) {
  39.         $lesson = $wpdb->get_row($wpdb->prepare("
  40.             SELECT * FROM {$wpdb->posts}
  41.             WHERE post_name = %s
  42.             AND post_type IN ('sfwd-lessons', 'sfwd-topic', 'sfwd-quiz')
  43.             ",
  44.             basename($wp->request)
  45.         ));
  46.        
  47.         if(!empty($lesson->post_type)) {
  48.             $course_id = learndash_get_course_id($lesson->ID);
  49.             $course_name = get_post_field('post_name', $course_id);
  50.            
  51.             $query = array(
  52.                 $lesson->post_type => $lesson->post_name,
  53.                 'name' => $lesson->post_name,
  54.                 'post_type' => $lesson->post_type
  55.             );
  56.            
  57.             if(!empty($course_name)) {
  58.                 $query['sfwd-courses'] = $course_name;
  59.                 $query['do_not_redirect'] = 1;
  60.             } else {
  61.                 $query['do_not_redirect'] = 1;
  62.             }
  63.         }
  64.     }
  65.    
  66.     return $query;
  67. }
  68. add_filter('request', 'pm_query_lesson_url', 10, 2);
  69.  
  70. /**
  71.  * Get rewrite slug for 'lesson'
  72.  */
  73. function pm_get_ldms_rewrite_slug($post_type = '') {
  74.     if(function_exists('learndash_get_post_type_slug') && !empty($post_type)) {
  75.         $lesson_cpt_slug = learndash_get_post_type_slug('lesson');
  76.         $lesson_cpt_obj = get_post_type_object($lesson_cpt_slug);
  77.         $lesson_cpt_slug = apply_filters('learndash_post_type_rewrite_slug', $lesson_cpt_obj->rewrite['slug'], $lesson_cpt_slug);  
  78.     } else {
  79.         $lesson_cpt_slug = $post_type;
  80.     }
  81.    
  82.     return $lesson_cpt_slug;
  83. }
  84.  
  85. /**
  86.  * Add course URL to lesson URL if "Nested URLs" are enabled
  87.  */
  88. function pm_append_course_name($permalink, $post) {
  89.     if(empty($post->post_type)) {
  90.         return $permalink;
  91.     }
  92.    
  93.     // Get the dynamically generated course name
  94.     if(in_array($post->post_type, array('sfwd-lessons', 'sfwd-topic', 'sfwd-quiz')) && function_exists('learndash_get_course_id')) {
  95.         // Keep the old permalink in a separate variable
  96.         $old_permalink = $permalink;
  97.         $permalink_base = trim(get_option('home'), "/");
  98.        
  99.         $course_id = apply_filters('learndash_post_link_course_id', learndash_get_course_id($post->ID), $old_permalink, $post);
  100.        
  101.         if(!empty($course_id)) {
  102.             $course_name = get_post_field('post_name', $course_id);
  103.             $lesson_cpt_slug = pm_get_ldms_rewrite_slug('lesson');
  104.         }
  105.  
  106.         // A. Change 'Lesson' permalink
  107.         if($post->post_type == 'sfwd-lessons') {
  108.             $sfwd_lessons = get_post_meta($post->ID, '_sfwd-lessons', true);
  109.            
  110.             if(!empty($course_name) && empty($sfwd_lessons['sfwd-lessons_sample_lesson'])) {
  111.                 $new_permalink = sprintf('%s/%s/%s/%s', $permalink_base, $course_name, $lesson_cpt_slug, $post->post_name);
  112.             } else {
  113.                 $new_permalink = sprintf('%s/%s', $permalink_base, $post->post_name);  
  114.             }
  115.         }
  116.  
  117.         // B. Change 'Topic' & 'Quiz' permalink
  118.         if(in_array($post->post_type, array('sfwd-topic', 'sfwd-quiz')) && !empty($course_name) && class_exists('LearnDash_Settings_Section')) {
  119.             if(LearnDash_Settings_Section::get_section_setting('LearnDash_Settings_Courses_Builder', 'shared_steps') == 'yes' && function_exists('learndash_course_get_single_parent_step')) {
  120.                 $lesson_id = learndash_course_get_single_parent_step($course_id, $post->ID);
  121.             } else if(function_exists('learndash_get_lesson_id')) {
  122.                 $lesson_id = learndash_get_lesson_id($post->ID);
  123.             }
  124.            
  125.             $sfwd_lessons = get_post_meta($lesson_id, '_sfwd-lessons', true);
  126.  
  127.             if(!empty($lesson_id)) {
  128.                 $lesson_name = get_post_field('post_name', $lesson_id);
  129.                
  130.                 if(empty($sfwd_lessons['sfwd-lessons_sample_lesson'])) {
  131.                     $new_permalink = sprintf('%s/%s/%s/%s/%s', $permalink_base, $course_name, $lesson_cpt_slug, $lesson_name, $post->post_name);
  132.                 } else {
  133.                     $new_permalink = sprintf('%s/%s/%s', $permalink_base, $lesson_name, $post->post_name);
  134.                 }
  135.             }
  136.         }
  137.     }
  138.    
  139.     return (!empty($new_permalink)) ? user_trailingslashit($new_permalink) : $permalink;
  140. }
  141. add_filter('post_type_link', 'pm_append_course_name', 150, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement