Skip to:
Content

bbPress.org

Changeset 4818


Ignore:
Timestamp:
03/21/2013 03:56:08 PM (13 years ago)
Author:
johnjamesjacoby
Message:

Improve logic in topics loop, so that super-stickies are always above forum-stickies. Props alex-ye. Fixes #2260.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/topics/template-tags.php

    r4814 r4818  
    162162        // Get super stickies and stickies in this forum
    163163        $stickies = bbp_get_super_stickies();
    164         $stickies = !empty( $r['post_parent'] ) ? array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) ) : $stickies;
     164
     165        // Get stickies for current forum
     166        if ( !empty( $r['post_parent'] ) ) {
     167            $stickies = array_merge( $stickies, bbp_get_stickies( $r['post_parent'] ) );
     168        }
     169
     170        // Remove any duplicate stickies
    165171        $stickies = array_unique( $stickies );
    166172
     
    168174        if ( is_array( $stickies ) && !empty( $stickies ) ) {
    169175
    170             // Setup the number of stickies and reset offset to 0
    171             $num_topics    = count( $bbp->topic_query->posts );
    172             $sticky_offset = 0;
     176            // Start the offset at -1 so first sticky is at correct 0 offset
     177            $sticky_offset = -1;
    173178
    174179            // Loop over topics and relocate stickies to the front.
    175             for ( $i = 0; $i < $num_topics; $i++ ) {
    176                 if ( in_array( $bbp->topic_query->posts[$i]->ID, $stickies ) ) {
    177                     $sticky = $bbp->topic_query->posts[$i];
     180            foreach ( $stickies as $sticky_index => $sticky_ID ) {
     181
     182                // Get the post offset from the posts array
     183                $post_offsets = wp_filter_object_list( $bbp->topic_query->posts, array( 'ID' => $sticky_ID ), 'OR', 'ID' );
     184
     185                // Continue if no post offsets
     186                if ( empty( $post_offsets ) ) {
     187                    continue;
     188                }
     189
     190                // Loop over posts in current query and splice them into position
     191                foreach ( array_keys( $post_offsets ) as $post_offset ) {
     192                    $sticky_offset++;
     193
     194                    $sticky = $bbp->topic_query->posts[$post_offset];
    178195
    179196                    // Remove sticky from current position
    180                     array_splice( $bbp->topic_query->posts, $i, 1 );
     197                    array_splice( $bbp->topic_query->posts, $post_offset, 1 );
    181198
    182199                    // Move to front, after other stickies
    183200                    array_splice( $bbp->topic_query->posts, $sticky_offset, 0, array( $sticky ) );
    184201
    185                     // Increment the sticky offset.  The next sticky will be placed at this offset.
    186                     $sticky_offset++;
    187 
    188                     // Remove post from sticky posts array
    189                     $offset = array_search( $sticky->ID, $stickies );
    190 
    191202                    // Cleanup
    192                     unset( $stickies[$offset] );
    193                     unset( $sticky            );
     203                    unset( $stickies[$sticky_index] );
     204                    unset( $sticky );
    194205                }
     206
     207                // Cleanup
     208                unset( $post_offsets );
    195209            }
     210
     211            // Cleanup
     212            unset( $sticky_offset );
    196213
    197214            // If any posts have been excluded specifically, Ignore those that are sticky.
     
    213230                );
    214231
     232                // Cleanup
     233                unset( $stickies );
     234
    215235                // What are the default allowed statuses (based on user caps)
    216236                if ( bbp_get_view_all() ) {
     
    229249                    $sticky_count = count( $sticky_posts );
    230250
    231                     // Loop through stickies and add them to beginning of array
    232                     foreach ( $sticky_posts as $sticky )
    233                         $topics[] = $sticky;
    234 
    235                     // Loop through topics and add them to end of array
    236                     foreach ( $bbp->topic_query->posts as $topic )
    237                         $topics[] = $topic;
     251                    // Merge the stickies topics with the query topics .
     252                    $bbp->topic_query->posts       = array_merge( $sticky_posts, $bbp->topic_query->posts );
    238253
    239254                    // Adjust loop and counts for new sticky positions
    240                     $bbp->topic_query->posts       = $topics;
    241255                    $bbp->topic_query->found_posts = (int) $bbp->topic_query->found_posts + (int) $sticky_count;
    242256                    $bbp->topic_query->post_count  = (int) $bbp->topic_query->post_count  + (int) $sticky_count;
    243257
    244258                    // Cleanup
    245                     unset( $topics       );
    246                     unset( $stickies     );
    247259                    unset( $sticky_posts );
    248260                }
     
    23122324     */
    23132325    function bbp_get_topic_edit_link( $args = '' ) {
    2314        
     2326
    23152327        // Parse arguments against default values
    23162328        $r = bbp_parse_args( $args, array(
Note: See TracChangeset for help on using the changeset viewer.