Skip to content

Fix null activity objects causing PHP warnings#4919

Open
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/null-activity-object-guards
Open

Fix null activity objects causing PHP warnings#4919
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/null-activity-object-guards

Conversation

@GaryJones
Copy link
Copy Markdown

Problem

When activity rows are deleted from the database but their IDs are still
referenced (e.g. in cached query results), BP_Activity_Activity::get_activity_data()
adds false entries to the $activities array. This happens because
wp_cache_get() returns false for missing cache keys, and the result
is unconditionally appended to the array regardless of validity.

Downstream code then iterates over these null entries and attempts
property access, producing three PHP warnings:

PHP Warning: Attempt to read property "id" on null in .../class-bp-activity-activity.php on line 895
PHP Warning: Attempt to read property "mptt_right" on null in .../class-bp-activity-activity.php on line 1641
PHP Warning: Attempt to read property "component" on null in .../bp-friends-activity.php on line 221

Fix

  1. get_activity_data() — wrap the $activities[] = $activity
    assignment with if ( ! empty( $activity ) ) so that only valid
    objects are added to the return array. This is the root cause fix
    and prevents all three warnings.

  2. bp_friends_prefetch_activity_object_data() — add a defensive
    is_object() check to skip non-object entries, protecting against
    any code path that might pass invalid data into this function.

Testing

  1. Delete an activity from the bp_activity table without clearing
    the object cache or any referencing queries
  2. Load the activity feed — the three warnings above should no longer
    appear
  3. Normal activity feed behaviour is unaffected

When an activity ID exists in query results but the corresponding row
has been deleted from the database, wp_cache_get() returns false.
get_activity_data() unconditionally adds this falsy value to the
$activities array, causing PHP warnings when downstream code accesses
properties on null entries (->id, ->mptt_right, ->component).

Only add the activity to the return array when it is a valid object.
Add a defensive is_object() check in bp_friends_prefetch_activity_object_data()
as well.
@GaryJones GaryJones force-pushed the fix/null-activity-object-guards branch from 62f6c63 to e031f46 Compare March 20, 2026 16:35
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.

1 participant