Skip to:
Content

BuddyPress.org

Ticket #3059: 3059.diff

File 3059.diff, 4.1 KB (added by sushkov, 14 years ago)
  • bp-forums/bp-forums-template.php

     
    130130                $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 ) );
    131131                $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 ) );
    132132
    133                 if ( !$no_stickies ) {
    134                         $stickies = array();
    135                         $standard = array();
    136 
    137                         // Place stickies at the top - not sure why bbPress doesn't do this?
    138                         foreach( (array)$this->topics as $topic ) {
    139                                 if ( isset( $topic->topic_sticky ) && 1 == (int)$topic->topic_sticky ) {
    140                                         $stickies[] = $topic;
    141                                 } else {
    142                                         $standard[] = $topic;
    143                                 }
    144                         }
    145 
    146                         $this->topics = array_merge( (array)$stickies, (array)$standard );
    147                 }
    148 
    149133                // Fetch extra information for topics, so we don't have to query inside the loop
    150134                $this->topics = bp_forums_get_topic_extras( $this->topics );
    151135
     
    220204        $user_id      = 0;
    221205        $forum_id     = false;
    222206        $search_terms = false;
    223         $no_stickies  = 'all';
    224207
    225208        // User filtering
    226209        if ( !empty( $bp->displayed_user->id ) )
     
    242225        if ( bp_is_directory() && !empty( $_GET['fs'] ) )
    243226                $search_terms = $_GET['fs'];
    244227
    245         // Show stickies on a group forum
    246         if ( isset( $bp->groups->current_group ) )
    247                 $no_stickies = null;
    248 
    249228        $defaults = array(
    250229                'type'         => $type,
    251230                'forum_id'     => $forum_id,
     
    253232                'page'         => 1,
    254233                'per_page'     => 20,
    255234                'max'          => false,
    256                 'no_stickies'  => $no_stickies,
    257235                'search_terms' => $search_terms
    258236        );
    259237
     
    267245                $type = 'tags';
    268246        }
    269247
    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               
    271277        return apply_filters( 'bp_has_topics', $forum_template->has_topics(), $forum_template );
    272278}
    273279
     
    12631269        function bp_get_forum_topic_count( $user_id = 0 ) {
    12641270                return apply_filters( 'bp_get_forum_topic_count', bp_forums_total_topic_count( $user_id ) );
    12651271        }
    1266 ?>
    1267  No newline at end of file
     1272?>