#3190 closed defect (bug) (fixed)
Undefined $args[0] in meta_map filter for the topics and replies
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | 2.6 | Priority: | normal |
Severity: | normal | Version: | |
Component: | API - Roles/Capabilities | Keywords: | good-first-bug has-patch |
Cc: | contato@…, chriscct7@… |
Description
Over the years, a lot of bbPress users have apparently opened tickets on WordPress.org support forums for various plugins that were "breaking" bbPress due to a set of undefined offset notices in the capabilities parts of bbPress. I got one of these today on one of my plugins:
Notice: Undefined offset: 0 in /bbpress/includes/topics/capabilities.php on line 80
Notice: Undefined offset: 0 in /bbpress/includes/replies/capabilities.php on line 62
Above both of these lines, bbPress just needs to add the following (a patch I'd submit but I literally just uninstalled both sublime and tortoiseSVN):
if ( empty( $args[0] ) ) {
return $caps;
}
This will prevent the attempted access of $args[0], which is normally the array of mapped meta caps for custom post types, but in the case of another plugin registering a non-CPT tied meta-cap on a non-post type (example: a settings screen), $args will be an empty array, which bbPress doesn't check for before trying to access, resulting in the above errors.
Attachments (2)
Change History (13)
#2
@
7 years ago
- Keywords good-first-bug added; good-first-patch removed
- Milestone changed from Future Release to 2.6
#4
@
7 years ago
- Cc contato@… added
- Keywords has-patch added; needs-patch removed
I didn't add to the Forum post type because I'm assuming this bug doesn't happen there. But maybe it is better to double check over there as well.
What do you guys think?
#5
@
7 years ago
- Keywords needs-patch added; has-patch removed
Hi there,
The code should be:
if ( empty( $args[0] ) ) { return $caps; }
empty() in PHP is the equivalent of ! isset($var) || $var == false
so there's no need to do both if ( ! isset( $args[0] ) || empty( $args[0] ) ) {
it can just be if ( empty( $args[0] ) ) {
In the case that $args[0] is not set, it should return $caps, and thus not proceed further, because in the case of the included patch, execution will continue and $args[0] will still be undefined and will throw the same error.
Therefore the patch above just needs all of the changes of
if ( ! isset( $args[0] ) || empty( $args[0] ) ) { $caps = array(); }
swapped out with
if ( empty( $args[0] ) ) { return $caps; }
Alternatively, instead of new lines, the lines containing
$_post = get_post( $args[0] );
could be modified into
$_post = ! empty( $args[0] ) ? get_post( $args[0] ) : false;
Thanks @chriscct7 :+1: