-
-
Save maciejbis/a9a2dac2c576cfde95c5d42dd3771949 to your computer and use it in GitHub Desktop.
LearnDash Partial Support for Permalink Manager
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Fix lesson query if "Nested URLs" are enabled (add original course name to the query) | |
*/ | |
function pm_query_lesson_url( $query, $old_query = '' ) { | |
global $wpdb, $wp, $pm_query; | |
if ( empty( $wp->request ) ) { | |
return $query; | |
} | |
$lesson_cpt_slug = pm_get_ldms_rewrite_slug( 'lesson' ); | |
// A. Detect nested lesson URLs | |
if ( strpos( $wp->request, "/{$lesson_cpt_slug}/" ) !== false ) { | |
preg_match( "/([^\/]+)\/{$lesson_cpt_slug}\/([^\/]+)(?:.+?([^\/]+)$)?/", $wp->request, $parts ); | |
if ( ! empty( $parts[2] ) ) { | |
$query = array( | |
'do_not_redirect' => 1, | |
'sfwd-courses' => $parts[1], | |
'sfwd-lessons' => $parts[2], | |
'name' => $parts[2], | |
'post_type' => 'sfwd-lessons' | |
); | |
} | |
if ( ! empty( $parts[3] ) ) { | |
$topic_quiz = sanitize_title( $parts[3] ); | |
$post_type = $wpdb->get_var( $wpdb->prepare( "SELECT post_type FROM {$wpdb->posts} WHERE post_name = %s", $topic_quiz ) ); | |
if ( ! empty( $post_type ) && in_array( $post_type, array( 'sfwd-topic', 'sfwd-quiz' ) ) ) { | |
$query[ $post_type ] = $topic_quiz; | |
$query['post_type'] = $post_type; | |
} | |
} | |
} // B. Check if requested slug is used by any lesson/topic/quiz | |
else if ( strpos( $wp->request, "/" ) === false ) { | |
$lesson = $wpdb->get_row( $wpdb->prepare( " | |
SELECT * FROM {$wpdb->posts} | |
WHERE post_name = %s | |
AND post_type IN ('sfwd-lessons', 'sfwd-topic', 'sfwd-quiz') | |
", basename( $wp->request ) ) ); | |
if ( ! empty( $lesson->post_type ) ) { | |
$course_id = learndash_get_course_id( $lesson->ID ); | |
$course_name = get_post_field( 'post_name', $course_id ); | |
$query = array( | |
$lesson->post_type => $lesson->post_name, | |
'name' => $lesson->post_name, | |
'post_type' => $lesson->post_type | |
); | |
if ( ! empty( $course_name ) ) { | |
$query['sfwd-courses'] = $course_name; | |
$query['do_not_redirect'] = 1; | |
} else { | |
$query['do_not_redirect'] = 1; | |
} | |
} | |
} | |
return $query; | |
} | |
add_filter( 'request', 'pm_query_lesson_url', 10, 2 ); | |
/** | |
* Get rewrite slug for 'lesson' | |
*/ | |
function pm_get_ldms_rewrite_slug( $post_type = '' ) { | |
if ( function_exists( 'learndash_get_post_type_slug' ) && ! empty( $post_type ) ) { | |
$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 = $post_type; | |
} | |
return $lesson_cpt_slug; | |
} | |
/** | |
* Add course URL to lesson URL if "Nested URLs" are enabled | |
*/ | |
function pm_append_course_name( $permalink, $post ) { | |
if ( empty( $post->post_type ) ) { | |
return $permalink; | |
} | |
// Get the dynamically generated course name | |
if ( in_array( $post->post_type, array( 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz' ) ) && function_exists( 'learndash_get_course_id' ) ) { | |
// Keep the old permalink in a separate variable | |
$old_permalink = $permalink; | |
$permalink_base = trim( get_option( 'home' ), "/" ); | |
$course_id = apply_filters( 'learndash_post_link_course_id', learndash_get_course_id( $post->ID ), $old_permalink, $post ); | |
if ( ! empty( $course_id ) ) { | |
$course_name = get_post_field( 'post_name', $course_id ); | |
$lesson_cpt_slug = pm_get_ldms_rewrite_slug( 'lesson' ); | |
} | |
// A. Change 'Lesson' permalink | |
if ( $post->post_type == 'sfwd-lessons' ) { | |
$sfwd_lessons = get_post_meta( $post->ID, '_sfwd-lessons', true ); | |
if ( ! empty( $course_name ) && empty( $sfwd_lessons['sfwd-lessons_sample_lesson'] ) ) { | |
$new_permalink = sprintf( '%s/%s/%s/%s', $permalink_base, $course_name, $lesson_cpt_slug, $post->post_name ); | |
} else { | |
$new_permalink = sprintf( '%s/%s', $permalink_base, $post->post_name ); | |
} | |
} | |
// B. Change 'Topic' & 'Quiz' permalink | |
if ( in_array( $post->post_type, array( 'sfwd-topic', 'sfwd-quiz' ) ) && ! empty( $course_name ) && class_exists( 'LearnDash_Settings_Section' ) ) { | |
if ( LearnDash_Settings_Section::get_section_setting( 'LearnDash_Settings_Courses_Builder', 'shared_steps' ) == 'yes' && function_exists( 'learndash_course_get_single_parent_step' ) ) { | |
$lesson_id = learndash_course_get_single_parent_step( $course_id, $post->ID ); | |
} else if ( function_exists( 'learndash_get_lesson_id' ) ) { | |
$lesson_id = learndash_get_lesson_id( $post->ID ); | |
} | |
$sfwd_lessons = get_post_meta( $lesson_id, '_sfwd-lessons', true ); | |
if ( ! empty( $lesson_id ) ) { | |
$lesson_name = get_post_field( 'post_name', $lesson_id ); | |
if ( empty( $sfwd_lessons['sfwd-lessons_sample_lesson'] ) ) { | |
$new_permalink = sprintf( '%s/%s/%s/%s/%s', $permalink_base, $course_name, $lesson_cpt_slug, $lesson_name, $post->post_name ); | |
} else { | |
$new_permalink = sprintf( '%s/%s/%s', $permalink_base, $lesson_name, $post->post_name ); | |
} | |
} | |
} | |
} | |
return ( ! empty( $new_permalink ) ) ? user_trailingslashit( $new_permalink ) : $permalink; | |
} | |
add_filter( 'post_type_link', 'pm_append_course_name', 150, 3 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment