Fix fatal error when quiz's parent lesson has been deleted - #3192
Open
thisismyurl wants to merge 3 commits into
Open
Fix fatal error when quiz's parent lesson has been deleted#3192thisismyurl wants to merge 3 commits into
thisismyurl wants to merge 3 commits into
Conversation
When a quiz's parent lesson has been permanently deleted, two access functions can trigger a fatal error by calling methods on a null object: - llms_is_post_restricted_by_prerequisite(): After llms_get_post( $lesson_id ) returns false, the next line calls $lesson->get_course() which produces a fatal error. Added a null check that returns false (unrestricted). - llms_is_post_restricted_by_time_period(): Same scenario. Also fixes a copy-paste error: the guard was if ( ! $lesson_id ) placed after llms_get_post(), but $lesson_id is already truthy at that point.
4 tasks
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Prevents fatal errors when determining quiz access restrictions if the quiz’s parent lesson has been deleted.
Changes:
- Add a null-guard in
llms_is_post_restricted_by_prerequisite()to avoid callingget_course()on a missing lesson. - Fix a copy/paste guard bug in
llms_is_post_restricted_by_time_period()by checking the resolved lesson object instead of the already-truthy$lesson_id. - Add a changelog entry documenting the fix.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| includes/functions/llms.functions.access.php | Adds/fixes guards to prevent fatals when a related lesson is missing. |
| .changelogs/fix-orphaned-quiz-fatal-error.yml | Documents the patch fix and rationale. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Comment on lines
397
to
+400
| $lesson = llms_get_post( $lesson_id ); | ||
| if ( ! $lesson ) { | ||
| return false; | ||
| } |
Comment on lines
+482
to
484
| if ( ! $lesson ) { | ||
| return false; | ||
| } |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
TL;DR: Delete a lesson and the quiz that used to live in it will fatal-error anyone who tries to access it — two access-check functions call methods on the
falsethatllms_get_post()returns when the lesson is gone. This patches both with a null guard. One of the two also had a copy-paste bug where the guard was checking the wrong variable. AI helped me trace the call stack and verify both fixes are load-bearing.Description
When a quiz's parent lesson has been permanently deleted, two functions in
llms.functions.access.phpcan trigger a fatal PHP error.llms_is_post_restricted_by_prerequisite()After the
llms_quizswitch case resolves$lesson_idand callsllms_get_post( $lesson_id ), the return value can befalseif the lesson no longer exists. The immediately following call to$lesson->get_course()then produces a fatal error on thefalsevalue.llms_is_post_restricted_by_time_period()Same scenario. Additionally, the original guard is
if ( ! $lesson_id )placed after thellms_get_post()call — but$lesson_idis already guaranteed truthy at that point (we returnedfalseearlier if it wasn't). The guard was checking the wrong variable. Changed toif ( ! $lesson )so it actually checks thellms_get_post()return value.Both are fixed by adding a null check on the
llms_get_post()result and returningfalse(unrestricted) when the lesson no longer exists.No upstream issue exists for this bug.
How has this been tested?
The fix was verified by code analysis: removing either null check causes the next method call on a
falsevalue to produce a fatal error (Call to a member function get_course() on bool).Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist:
(full disclosure: AI helped me identify the issue and verify my work)