Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Detect and parse the nested lesson URL
- */
- function pm_detect_nested_lesson_url($uri_parts, $request_url, $endpoints) {
- $lesson_cpt_slug = pm_get_lesson_rewrite_slug();
- if(!empty($uri_parts['uri']) && strpos("/{$uri_parts['uri']}/", $lesson_cpt_slug) !== false) {
- preg_match('/([^\/]+)\/lessen\/([^\/]+)/', $uri_parts['uri'], $parts);
- if(!empty($parts[2])) {
- $uri_parts['uri'] = $parts[2];
- $uri_parts['course'] = $parts[1];
- }
- }
- return $uri_parts;
- }
- add_filter('permalink_manager_detect_uri', 'pm_detect_nested_lesson_url', 10, 3);
- /**
- * Fix lesson query if "Nested URLs" are enabled (add original course name to the query)
- */
- function pm_query_nested_lesson_url($query, $old_query, $uri_parts, $pm_query, $content_type) {
- if(!empty($query['post_type']) && $query['post_type'] == 'sfwd-lessons' && !empty($pm_query['id']) && function_exists('learndash_get_course_id')) {
- $lesson_id = $pm_query['id'];
- if(!empty($uri_parts['course'])) {
- $course_name = $uri_parts['course'];
- } else {
- $course_id = learndash_get_course_id($lesson_id);
- $course_name = get_post_field('post_name', $course_id);
- }
- if(!empty($course_name) && empty($query['sfwd-courses'])) {
- $query['sfwd-courses'] = $course_name;
- }
- }
- return $query;
- }
- add_filter('permalink_manager_filter_query', 'pm_query_nested_lesson_url', 5, 5);
- /**
- * Get rewrite slug for 'lesson'
- */
- function pm_get_lesson_rewrite_slug() {
- if(function_exists('learndash_get_post_type_slug')) {
- $lesson_cpt_slug = learndash_get_post_type_slug('lesson');
- $lesson_cpt_obj = get_post_type_object($lesson_cpt_slug);
- $lesson_cpt_slug = apply_filters('learndash_post_type_rewrite_slug', $lesson_cpt_obj->rewrite['slug'], $lesson_cpt_slug);
- } else {
- $lesson_cpt_slug = 'lesson';
- }
- return $lesson_cpt_slug;
- }
- /**
- * Add course URL to lesson URL if "Nested URLs" are enabled
- */
- function pm_append_course_name($permalink, $post, $old_permalink) {
- global $permalink_manager_uris;
- if(!empty($post->post_type) && $post->post_type == 'sfwd-lessons' && !empty($permalink_manager_uris[$post->ID])) {
- $course_id = 0;
- if((isset($_GET['course_id'])) && (!empty($_GET['course_id']))) {
- $course_id = absint($_GET['course_id']);
- }
- $course_id = apply_filters( 'learndash_post_link_course_id', $course_id, $old_permalink, $post);
- if(!empty($course_id)) {
- $course_name = get_post_field('post_name', $course_id);
- $lesson_cpt_slug = pm_get_lesson_rewrite_slug();
- $lesson_old_uri = $permalink_manager_uris[$post->ID];
- $lesson_new_uri = sprintf('%s/%s/%s', $course_name, $lesson_cpt_slug, $lesson_old_uri);
- $permalink = str_replace($lesson_old_uri, $lesson_new_uri, $permalink);
- }
- }
- return $permalink;
- }
- add_filter('permalink_manager_filter_final_post_permalink', 'pm_append_course_name', 10, 3);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement