query_vars['accessnow_slms_resolve'] ?? ''; if ( ! $resolve ) { return; } $course_slug = $wp->query_vars['accessnow_slms_course_slug'] ?? ''; $unit_slug = $wp->query_vars['accessnow_slms_unit_slug'] ?? ''; if ( ! $course_slug || ! $unit_slug ) { return; } // Find course $course = self::get_post_by_slug( $course_slug, 'slms_course' ); if ( ! $course ) { return; } // Find unit by slug + parent course $unit = self::get_child_by_slug( $unit_slug, 'slms_unit', '_slms_parent_course', $course->ID ); if ( ! $unit ) { return; } if ( 'lesson' === $resolve ) { $lesson_slug = $wp->query_vars['accessnow_slms_lesson_slug'] ?? ''; if ( ! $lesson_slug ) { return; } $lesson = self::get_child_by_slug( $lesson_slug, 'slms_lesson', '_slms_parent_unit', $unit->ID ); if ( ! $lesson ) { return; } $wp->query_vars = array( 'p' => $lesson->ID, 'post_type' => 'slms_lesson', ); } elseif ( 'quiz' === $resolve ) { $lesson_slug = $wp->query_vars['accessnow_slms_lesson_slug'] ?? ''; $quiz_slug = $wp->query_vars['accessnow_slms_quiz_slug'] ?? ''; if ( ! $lesson_slug || ! $quiz_slug ) { return; } $lesson = self::get_child_by_slug( $lesson_slug, 'slms_lesson', '_slms_parent_unit', $unit->ID ); if ( ! $lesson ) { return; } $quiz = self::get_child_by_slug( $quiz_slug, 'slms_quiz', '_slms_parent_lesson', $lesson->ID ); if ( ! $quiz ) { return; } $wp->query_vars = array( 'p' => $quiz->ID, 'post_type' => 'slms_quiz', ); } } /* ─── Permalink Filters ──────────────────────────────────── */ public static function lesson_permalink( $post_link, $post ) { if ( 'slms_lesson' !== $post->post_type ) { return $post_link; } $ancestors = self::get_lesson_ancestors( $post->ID ); if ( ! $ancestors ) { return $post_link; } return home_url( sprintf( '/courses/%s/%s/%s/', $ancestors['course_slug'], $ancestors['unit_slug'], $post->post_name ) ); } public static function quiz_permalink( $post_link, $post ) { if ( 'slms_quiz' !== $post->post_type ) { return $post_link; } $ancestors = self::get_quiz_ancestors( $post->ID ); if ( ! $ancestors ) { return $post_link; } return home_url( sprintf( '/courses/%s/%s/%s/%s/', $ancestors['course_slug'], $ancestors['unit_slug'], $ancestors['lesson_slug'], $post->post_name ) ); } /* ─── Legacy URL Redirects ───────────────────────────────── */ public static function legacy_redirects() { if ( ! is_singular( array( 'slms_lesson', 'slms_quiz' ) ) ) { return; } // Path relative to the site root, so subdirectory installs match too. $uri = isset( $_SERVER['REQUEST_URI'] ) ? esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) : ''; $path = (string) wp_parse_url( $uri, PHP_URL_PATH ); $home = (string) wp_parse_url( home_url( '/' ), PHP_URL_PATH ); if ( '' !== $home && '/' !== $home && 0 === strpos( $path, $home ) ) { $path = '/' . substr( $path, strlen( $home ) ); } if ( ( is_singular( 'slms_lesson' ) && preg_match( '#^/lessons/#', $path ) ) || ( is_singular( 'slms_quiz' ) && preg_match( '#^/quizzes/#', $path ) ) ) { wp_safe_redirect( get_permalink(), 301 ); exit; } } /* ─── Helper Methods ─────────────────────────────────────── */ private static function get_post_by_slug( $slug, $post_type ) { $posts = get_posts( array( 'post_type' => $post_type, 'name' => $slug, 'post_status' => array( 'publish', 'draft', 'private' ), 'posts_per_page' => 1, ) ); return ! empty( $posts ) ? $posts[0] : null; } private static function get_child_by_slug( $slug, $post_type, $parent_meta_key, $parent_id ) { $posts = get_posts( array( 'post_type' => $post_type, 'name' => $slug, 'post_status' => array( 'publish', 'draft', 'private' ), 'posts_per_page' => 1, 'meta_key' => $parent_meta_key, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- course structure is stored as post meta. 'meta_value' => $parent_id, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_value -- course structure is stored as post meta. ) ); return ! empty( $posts ) ? $posts[0] : null; } private static function get_lesson_ancestors( $lesson_id ) { $unit_id = get_post_meta( $lesson_id, '_slms_parent_unit', true ); if ( ! $unit_id ) { return null; } $unit = get_post( $unit_id ); if ( ! $unit ) { return null; } $course_id = get_post_meta( $unit_id, '_slms_parent_course', true ); if ( ! $course_id ) { return null; } $course = get_post( $course_id ); if ( ! $course ) { return null; } return array( 'course_slug' => $course->post_name, 'unit_slug' => $unit->post_name, ); } private static function get_quiz_ancestors( $quiz_id ) { $lesson_id = get_post_meta( $quiz_id, '_slms_parent_lesson', true ); if ( ! $lesson_id ) { return null; } $lesson = get_post( $lesson_id ); if ( ! $lesson ) { return null; } $ancestors = self::get_lesson_ancestors( $lesson_id ); if ( ! $ancestors ) { return null; } $ancestors['lesson_slug'] = $lesson->post_name; return $ancestors; } }