130 | 130 | $this->topic_count = apply_filters_ref_array( 'bp_forums_template_topic_count', array( $this->topic_count, &$this->topics, $type, $forum_id, $per_page, $max, $no_stickies ) ); |
131 | 131 | $this->total_topic_count = apply_filters_ref_array( 'bp_forums_template_total_topic_count', array( $this->total_topic_count, $this->topic_count, &$this->topics, $type, $forum_id, $per_page, $max, $no_stickies ) ); |
132 | 132 | |
270 | | $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms ); |
| 248 | // Fetch the stickies |
| 249 | $forum_stickies = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, 0, 0, $max, 'sticky', $search_terms ); |
| 250 | // Try merging the stickies |
| 251 | if ( $forum_stickies->has_topics() ) { |
| 252 | // If stickies are for current $page |
| 253 | $page_start_num = ( $page - 1 ) * $per_page; |
| 254 | $page_end_num = $page * $per_page; |
| 255 | if ( $forum_stickies->topic_count > $page_start_num && $forum_stickies->topic_count < $page_end_num ) { |
| 256 | // Fetch the non-sticky topics |
| 257 | $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'normal', $search_terms ); |
| 258 | // Get the exact number of normal topics to complement the $per_page size |
| 259 | $forum_template->topics = array_slice( $forum_template->topics, 0, $per_page - $forum_stickies->topic_count ); |
| 260 | // Slice the stickies array for current page size |
| 261 | $forum_stickies->topics = array_slice( $forum_stickies->topics, $page_start_num, $page_end_num ); |
| 262 | // Merge the two, reverse stickies first, than pop on top of the normals |
| 263 | foreach ( array_reverse( $forum_stickies->topics ) as $sticky ) |
| 264 | array_unshift( $forum_template->topics, $sticky ); |
| 265 | } else { |
| 266 | // Recalculate the $per_page excluding the stickies |
| 267 | $per_page -= $forum_stickies->topic_count; |
| 268 | // Fetch the non-sticky topics |
| 269 | $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'normal', $search_terms ); |
| 270 | // Recalculate the new index of topics |
| 271 | $forum_template->pag_num += $forum_stickies->topic_count; |
| 272 | } |
| 273 | } else |
| 274 | // Fetch the non-sticky topics if no stickies were found |
| 275 | $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'normal', $search_terms ); |
| 276 | |