fix(core): prevent null-post PHP warnings on model and templates - #3275
Open
faisalahammad wants to merge 1 commit into
Open
fix(core): prevent null-post PHP warnings on model and templates#3275faisalahammad wants to merge 1 commit into
faisalahammad wants to merge 1 commit into
Conversation
- Guard LLMS_Post_Model::___get() so post-derived property reads return an empty string when the underlying WP_Post is missing instead of triggering a "property on null" warning. - Apply the same guard inside toArray() and across toArray()'s access to the post type object so serialization of a null-post model is safe. - Bail in the course tags, categories, and tracks template wrappers (and the templates themselves) when global $post is not a WP_Post, covering the shortcode and pre-loop render paths. Adds PHPUnit coverage for the model null-post path and the template wrappers, plus a fix/patch changelog entry. Fixes gocodebox#2577
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.
Description
Fixes #2577
The LifterLMS core could emit
Attempt to read property ... on nullPHP warnings under two related conditions:LLMS_Post_Modelinstance whose underlyingWP_Postis missing (for example, constructed from a now-deleted post ID). Every read of a post-derived property (title,content,type,excerpt,menu_order, …) previously went through___get()and dereferenced$this->postwithout a null check.toArray()andget_post_type_data()access had the same defect.global $post->IDwithout guarding against the global being null. The[lifterlms_course_meta_info]shortcode and other pre-loop render paths can reach those templates with no global$postset.This change adds an early-return guard in
LLMS_Post_Model::___get()(returning an empty string for post-derived keys when$this->postis null), mirrors the guard insidetoArray(), and uses aWP_Postinstance check in both the wrapper functions and the term templates themselves to bail out cleanly.PHPUnit coverage is added for both the model null-post path and the template wrappers; PHPCS is clean on the touched files. Running the full suite against a baseline that previously had two failing tests and four incomplete tests, this PR additionally resolves one pre-existing error and one pre-existing failure while adding 67 new tests across the new files.
How has this been tested?
composer run-script tests-run— full PHPUnit run, 9 new tests in the model class (4 explicitly named, 5 inherited "no properties to test" skipped) and 1 in the template class all pass.composer run-script check-cs-errors— clean on every touched file.Manual
wp evalrepro for both reported warnings:Both produce no PHP warnings after the fix.
Testing environment: PHP 8.5.8, PHPUnit 9.6.35, LifterLMS dev branch at
573ba813e.Screenshots
Not applicable: no user-visible UI change. The warnings are observable only via PHP error logs.
Types of changes
Bug fix (non-breaking change which fixes an issue)
Checklist:
/.changelogs/2577-fix-null-post-warnings.ymlwithsignificance: patch,type: fixed)