我的 LearnDash 插件配置为共享课程步骤,以便我可以在多个课程中使用相同的课程。但是,如果用户将某一特定课程中的课程标记为完成或不完整,则不会在所有课程中反映出来(默认设置)。我希望共享课程的完成状态的任何变化都会反映在包含该课程的所有课程中。
这是我用来实现预期结果的代码,但它总是会导致严重错误。
function sync_lesson_completion($lesson_id, $user_id, $course_id) {
// Get the course that contains the lesson
$course = get_post($course_id);
// Check if the course has shared steps enabled
$shared_steps_enabled = get_post_meta($course_id, '_sfwd-courses_sharedsteps_enabled', true);
// Check if shared steps are enabled and the lesson is shared
if ($shared_steps_enabled && !empty($lesson_id)) {
// Get all the courses where the lesson is shared
$shared_courses = learndash_get_courses_by_step($lesson_id, 'sfwd-lessons');
// Loop through the shared courses
foreach ($shared_courses as $shared_course_id) {
// Skip the current course
if ($shared_course_id == $course_id) {
continue;
}
// Mark the lesson as completed for the user in the shared course
learndash_update_user_activity($user_id, $lesson_id, 'sfwd-lessons', $shared_course_id, true);
}
}
}
// Hook into the lesson completion action
add_action('learndash_lesson_completed', 'sync_lesson_completion', 10, 3);
我做错了什么以及如何达到预期的结果?
learndash_get_courses_by_step 应该是 learndash_get_courses_for_step https://developers.learndash.com/function/learndash_get_courses_for_step/