You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

LearnDash作业审核通过后自动完成对应主题的实现方法

LearnDash作业审核通过自动标记学习内容完成方案

问题原因

现有代码无法正确调用完成函数的核心原因有两个:

  • 没有从作业元数据中读取关联的topic_id字段,sfwd-assignment类型文章默认会存储关联的课程、课时、主题ID,分别对应元键course_id、lesson_id、topic_id
  • 不清楚learndash_process_mark_complete的参数规则:该函数第三个参数接收需要标记完成的学习内容ID,兼容课时(lesson)和主题(topic)两种类型,不需要额外区分内容类型;如果作业挂载在主题下传topic ID,直接挂载在课时下传lesson ID即可。

函数正确传参格式:
learndash_process_mark_complete( int $student_user_id, int $course_id, int $step_post_id )

修正后代码

add_action('acf/save_post', 'assignment_custom_acf_save', 5);

function assignment_custom_acf_save($post_id) {
    if (get_post_type($post_id) !== 'sfwd-assignment') {
        return;
    }

    $assignment = get_post($post_id);
    $assignment_meta = get_post_meta($assignment->ID);
    
    // 读取关联学习内容ID
    $student_id = (int)$assignment->post_author;
    $course_id = (int)($assignment_meta['course_id'][0] ?? 0);
    $lesson_id = (int)($assignment_meta['lesson_id'][0] ?? 0);
    $topic_id = (int)($assignment_meta['topic_id'][0] ?? 0);
    // 自动识别需要标记完成的节点:有主题用主题ID,无主题用课时ID
    $target_step_id = $topic_id ?: $lesson_id;

    // 读取ACF配置
    $new_status = (int)get_field('custom_approval_status', $post_id);
    $teachers_comment = get_field('field_628fdef373411', $post_id);
    $course_title = get_the_title($course_id);
    $step_title = get_the_title($target_step_id);
    $step_link = get_permalink($target_step_id);
    $course_link = get_permalink($course_id);

    if ($new_status === 1) {
        // 驳回逻辑
        delete_post_meta($post_id, 'approval_status');
        learndash_process_mark_incomplete($student_id, $course_id, $target_step_id);
        send_email_after_assignment_rejection($post_id, $teachers_comment);

        $rejection_message = sprintf(
            'Thanks for your submission for <b><a href="%s">%s</a></b> in the <b><a href="%s">%s</a></b>.<br><br>Unfortunately, it didn\'t meet the requirements for this assignment.<br>%s<br><br>Let\'s give it another go!',
            esc_url($step_link),
            esc_html($step_title),
            esc_url($course_link),
            esc_html($course_title),
            !empty($teachers_comment) ? 'This is the comment to your work: <b>' . esc_html($teachers_comment) . '</b>' : ''
        );
        send_new_message($student_id, $rejection_message);
    } elseif ($new_status === 2) {
        // 审核通过逻辑
        update_post_meta($post_id, 'approval_status', 1);
        // 自动标记对应节点完成,无需学生手动点击按钮
        learndash_process_mark_complete($student_id, $course_id, $target_step_id);
        send_email_after_assignment_approval($post_id, $teachers_comment);

        $approval_message = sprintf(
            'Great job on your assignment! <br>%sYour submission for <b><a href="%s">%s</a></b> in the <b><a href="%s">%s</a></b> is now approved. <br>Thanks for spending the time to get that in.',
            !empty($teachers_comment) ? '<b>' . esc_html($teachers_comment) . '</b><br>' : '',
            esc_url($step_link),
            esc_html($step_title),
            esc_url($course_link),
            esc_html($course_title)
        );
        send_new_message($student_id, $approval_message);
    }
}

优化点说明

  • 新增非空判断,避免元数据缺失引发PHP报错
  • 替换直接读取$_POST的逻辑,改用ACF原生函数取值,兼容性更好
  • 同步修正驳回场景的标记逻辑,解决之前写死传lesson ID导致挂载在topic下的作业驳回后状态不更新的问题
  • 增加输出转义,规避前端XSS风险
  • 自动适配作业挂载在lesson/topic两种场景,不需要手动配置ID

内容的提问来源于stack exchange,提问作者Avinish Kr. Singh Rajawat

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.27 13:27:18