Skip to content

fix(course-structure): prune deleted lessons from outline saves - #8260

Draft
faisalahammad wants to merge 1 commit into
Automattic:trunkfrom
faisalahammad:fix/8183-deleted-lesson-blocks-outline-save
Draft

faisalahammad wants to merge 1 commit into
Automattic:trunkfrom
faisalahammad:fix/8183-deleted-lesson-blocks-outline-save

Conversation

@faisalahammad

@faisalahammad faisalahammad commented Sep 15, 2026 •

Copy link
Copy Markdown

Resolves #8183

Proposed Changes

  • A lesson deleted or trashed while it was still in the course outline no longer blocks saving the course structure. The permission callback skips lessons whose post no longer exists instead of returning 403, and sanitize_item() prunes them so the rest of the structure still saves. Lessons the user genuinely may not edit still return 403.
  • The save response is now treated as authoritative in the editor store, so the stale lesson block is dropped from the outline after a successful save. A load response stays non-authoritative, so an outline that has not been persisted yet is never wiped.
  • A failed course-structure fetch now shows an error notice, matching the quiz store behavior.

The 403 came from map_meta_cap(): current_user_can( 'edit_post', $id ) returns do_not_allow when get_post() is null, so a single deleted lesson id failed the whole request. A stale lesson nested in a module skipped the permission loop and failed later in sanitize_item() with sensei_course_structure_missing_lesson (400).

Screenshots

None, no visual changes.

Testing Instructions

  • Create a course with at least two lessons in the course outline.
  • Open the outline on the front end or in a second tab, then delete one of the lessons from the Lessons list.
  • Back in the course outline editor, drag a block or make any edit that triggers a save, and confirm the save succeeds (no 403) and the deleted lesson disappears from the outline.
  • Repeat with a lesson nested inside a module and confirm the save succeeds (no 400).
  • As a teacher, submit a structure containing a lesson belonging to another teacher and confirm it still returns 403.

New/Updated Hooks

None.

Deprecated Code

None.

Changelog entry

  • Automatically create a changelog entry from the details below.
Changelog Entry Details

Significance

  • Patch - Backward-compatible bug fixes

Type

  • Fixed - Fixes a bug

Message

Fix a deleted or trashed lesson blocking the course outline from being saved and lingering in the editor.

Note: the changelog/fix-deleted-lesson-blocks-course-save file is committed on this branch because it comes from a fork, so CI cannot push the entry.

Out of scope, worth a separate issue

  • Nested module lessons are never checked with current_user_can( 'edit_post' ) in can_user_save_structure(); the loop only walks top-level items. A module lesson another teacher owns can still be retitled and reassigned through save_module(). This gap is pre-existing and unchanged by this PR.
  • A deleted module term still returns sensei_course_structure_missing_module.
  • The outline is only reconciled on save, so a course whose only lesson was deleted outside the editor keeps showing it until the first save.

A lesson deleted or trashed while still in the course outline made the
course-structure save fail with 403 (or 400 when nested in a module)
because map_meta_cap denies edit_post for a missing post and
sanitize_item rejected the id. The stale block then lingered in the
editor.

Skip nonexistent lessons in the permission callback, prune them in
sanitize_item so the rest of the structure saves, and treat the save
response as authoritative so the editor drops the stale block.

Fixes Automattic#8183
@faisalahammad

Copy link
Copy Markdown
Author

Two GitHub restrictions on my account that maintainers may need to fix up:

  • The PR is stuck in draft state. Marking it ready for review returns 403 and creating it non-draft returns 404, since my account only has read/pull access on this repo. Please click "Ready for review" or merge as-is.
  • I could not assign a milestone either, for the same reason. This one should go to 4.26.4.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deleted lesson lingers in course outline and blocks course-structure save (403)

1 participant