Skip to content

Add null safety checks in forums code#4901

Open
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/forums-null-safety
Open

Add null safety checks in forums code#4901
GaryJones wants to merge 1 commit intobuddyboss:releasefrom
GaryJones:fix/forums-null-safety

Conversation

@GaryJones
Copy link
Copy Markdown

Problem

Three places in the forums code lack null safety checks, causing PHP warnings, deprecation notices, or potential fatal errors on PHP 8.1+.

1. Null post object access in forum group detection

In bp-forums/functions.php, get_post( $forum_id ) can return null when the forum post does not exist (e.g. it was deleted or the ID is invalid). The code immediately accesses $current_post->post_parent without checking, causing a fatal error.

Fix: Add $current_post && before accessing the property.

2. REST reply permission check logic error

In class-bp-rest-reply-endpoint.php, the update_item_permissions_check method calls $this->get_item_permissions_check() which may return a WP_Error. The code then unconditionally compares the reply author, even when $retval is already an error. This can cause unexpected behaviour or mask the original permission error.

Fix: Add true === $retval && to only evaluate the author check when the prior permission check passed.

3. Null passed to sanitize_title() for REST topic tag

In class-bp-rest-topics-endpoint.php, $request->get_param( 'tag' ) returns null when the parameter is not provided. Passing null to sanitize_title() triggers a deprecation warning on PHP 8.1+.

Fix: Add ?? '' null-coalescing fallback.

Test plan

  • Access a forum where the forum post has been deleted — should not fatal
  • Make an unauthenticated REST API request to update a reply — should return a proper permission error
  • Make a REST API request to list topics without a tag parameter on PHP 8.1+ — no deprecation warning

Three small fixes to prevent PHP warnings and errors:

1. Null-check get_post() result before accessing post_parent in
   forum group detection (bp-forums/functions.php).

2. Guard REST reply update permission check so the author comparison
   only runs when $retval is already true, preventing errors when
   the prior permission check failed (class-bp-rest-reply-endpoint.php).

3. Add null-coalescing fallback for the tag REST parameter before
   passing to sanitize_title(), preventing a PHP 8.1+ deprecation
   warning (class-bp-rest-topics-endpoint.php).
@chetansatasiya
Copy link
Copy Markdown
Contributor

@GaryJones Thanks for the PR and it will be added in our upcoming release.

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.

2 participants