Skip to:
Content

Opened 6 years ago

Closed 6 years ago

Last modified 5 years ago

#934 closed defect (fixed)

recursive statics not reset in bb_get_forums_hierarchical() when called multiple times on single page

Reported by: zappoman Owned by:
Milestone: 1.0 Priority: normal
Severity: normal Version: 1.0-rc-2
Component: Front-end Keywords: 0.8.x, 1.0-beta, 1.0-alpha, dropdown
Cc:

Description

The recursive function bb_get_forums_hierarchical() fails to produce proper results if it is called more than once on a page. An use case example is a home page that shows all forums and includes a post-form at the bottom that includes the bb_new_topic_forum_dropdown() forum drop down list.

The bug relates to the fact that the recursion is controlled by the static _leaves variable, which starts out as false on the first call, and then is reset to contain the current leaves of the recursive calls. This is all well and good for the first top level call, but when the first call fully unwinds the recursion, the static variable is left set to an empty array, instead of being set to false... as a result, all subsequent calls will return with an empty array.

One fix to this is to implement a top level wrapper, which is called as the public api to bb_get_forums_hierarchical() which resets the control variable to false, and calls the (pseudo) private function. Here is a diff which fixes this problem.

Index: bb-includes/functions.php
===================================================================
--- bb-includes/functions.php	(revision 1651)
+++ bb-includes/functions.php	(working copy)
@@ -122,9 +122,15 @@
 }
 
 /* Forums */
+function bb_get_forums_hierarchical( $root = 0, $depth = 0, $leaves = false, $_recursed = false ) {
+	global $_leaves;
+	$_leaves = false;
+	return _bb_get_forums_hierarchical($root,$depth,$leaves,$_recursed);
+}
 
-function bb_get_forums_hierarchical( $root = 0, $depth = 0, $leaves = false, $_recursed = false ) {
-	static $_leaves = false;
+function _bb_get_forums_hierarchical( $root = 0, $depth = 0, $leaves = false, $_recursed = false ) {
+	global $_leaves;
+
 	$root = (int) $root;
 
 	if ( false === $_leaves )
@@ -141,7 +147,7 @@
 		if ( $root == $leaf->forum_parent ) {
 			$new_root = (int) $leaf->forum_id;
 			unset($_leaves[$l]);
-			$branch[$new_root] = 1 == $depth ? true : bb_get_forums_hierarchical( $new_root, $depth - 1, false, true );
+			$branch[$new_root] = 1 == $depth ? true : _bb_get_forums_hierarchical( $new_root, $depth - 1, false, true );
 			reset($_leaves);
 		}
 	}

Change History (2)

comment:1 sambauers6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [1657]) Reset $_leaves to false if not recursed bb_get_forums_hierarchical(), fixes #934

comment:2 sambauers5 years ago

  • Milestone set to 1.0
Note: See TracTickets for help on using tickets.