Opened 8 years ago
Closed 8 years ago
#7149 closed defect (bug) (fixed)
Templates: locating empty array of template(s) errors undefined variable
Reported by: | Offereins | Owned by: | Offereins |
---|---|---|---|
Milestone: | 2.8 | Priority: | normal |
Severity: | normal | Version: | |
Component: | Core | Keywords: | has-patch commit |
Cc: | lmoffereins@… |
Description
In order to prevent loading a specific template part, in my case the 'activity/post-form' (a brilliant thought of mine to make the activity stream contain only actual site activity), I empty the array of templates to locate through filtering bp_get_template_part
.
Thus follows sending an empty array into bp_locate_template()
. However, here the array will be looped, creating new variables in the loop that do not exist outside of it. Beyond the loop these variables ($template_name
being the first) are then used, but since the loop was never run, the variable appears to be undefined.
What I expect is that the template loading dies silently when there appear to be no templates requested.
To fix this, I suggest to only run bp_locate_template()
when the list of templates is non-empty. Patch is attached.
Attachments (4)
Change History (14)
This ticket was mentioned in Slack in #buddypress by offereins. View the logs.
8 years ago
#4
@
8 years ago
- Keywords reporter-feedback removed
@DJPaul Thanks for confirming the bug. In reply to your suggestion: returning a false-y string is okay with me. Most return value evaluations of the function are loosely compared, but I've found the following strict-type checks for output of bp_locate_template()
:
bp-core/bp-core-template-loader.php
@ line 175 inbp_locate_template_asset()
bp-groups/classes/class-bp-group-extension.php
@ line 974 inBP_Group_Extension::setup_edit_hooks()
(compares empty string)
Furthermore, bp_locate_template()
constructs the return value for:
bp_get_template_part()
of which no value evaluations are found, but is widely usedbp_displayed_user_get_front_template()
which has a strict type check in:bp-members/bp-members-template.php
@ line 1379 inbp_displayed_user_front_template_part()
bp_groups_get_front_template()
which has a strict type check in:bp-groups/bp-groups-template.php
@ line 4189 inbp_groups_front_template_part()
My conclusion is that this propagates to somewhat more functionality than you'd originally guess.
#5
@
8 years ago
- Milestone changed from 2.7 to Future Release
Sorry, have to move this to future release.
This ticket was mentioned in Slack in #buddypress by offereins. View the logs.
8 years ago
#7
@
8 years ago
- Milestone changed from Future Release to 2.8
OK, let's just get this patch in for 2.8, @Offereins. We'll need to add the { }
brackets, but that's just a minor tweak.
Easily re-produced:
@Offereins Thanks for the report and patch. How do you feel about making this function return an empty string if a template can't be found? If so, let's put a
since
tag on the PHPDoc explaining the return type change.I know it returns a bool already, but I think that's just bad function design. An empty string is a false-y value, so it'd probably won't break anything unless someone's doing a type-strict check on the output of
bp_locate_template
. We/you would need to check the instances of this function in BuddyPress core to check if this breaks anything.