Skip to content

fix(core): prevent null-post PHP warnings on model and templates - #3275

Open
faisalahammad wants to merge 1 commit into
gocodebox:devfrom
faisalahammad:fix/2577-null-post-warnings
Open

fix(core): prevent null-post PHP warnings on model and templates#3275
faisalahammad wants to merge 1 commit into
gocodebox:devfrom
faisalahammad:fix/2577-null-post-warnings

Conversation

@faisalahammad

Copy link
Copy Markdown
Contributor

Description

Fixes #2577

The LifterLMS core could emit Attempt to read property ... on null PHP warnings under two related conditions:

  1. An LLMS_Post_Model instance whose underlying WP_Post is 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->post without a null check. toArray() and get_post_type_data() access had the same defect.
  2. The course tags, categories, and tracks templates, which read global $post->ID without 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 $post set.

This change adds an early-return guard in LLMS_Post_Model::___get() (returning an empty string for post-derived keys when $this->post is null), mirrors the guard inside toArray(), and uses a WP_Post instance 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 eval repro for both reported warnings:

    wp eval '
    $course = new LLMS_Course( 999999 );
    echo $course->get( "title" );
    print_r( $course->toArray() );
    '
    
    wp eval '
    global $post;
    $post = null;
    do_action( "lifterlms_single_course_after_summary" );
    '
    

    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:

  • This PR requires and contains at least one changelog file. (/.changelogs/2577-fix-null-post-warnings.yml with significance: patch, type: fixed)
  • My code has been tested.
  • My code passes all existing automated tests.
  • My code follows the LifterLMS Coding & Documentation Standards.

- 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
@faisalahammad
faisalahammad requested a review from brianhogg as a code owner July 24, 2026 18:18
@brianhogg brianhogg moved this to Awaiting Review in Development Jul 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Awaiting Review

Development

Successfully merging this pull request may close these issues.

2 participants