Skip to:
Content

BuddyPress.org

Ticket #3059: 3059.2.diff

File 3059.2.diff, 13.0 KB (added by boonebgorges, 12 years ago)
  • bp-forums/bp-forums-functions.php

    function bp_forums_get_forum_topics( $args = '' ) { 
    8080                'user_id'       => false,
    8181                'page'          => 1,
    8282                'per_page'      => 15,
     83                'offset'        => false,
     84                'number'        => false,
    8385                'exclude'       => false,
    8486                'show_stickies' => 'all',
    8587                'filter'        => false // if $type = tag then filter is the tag name, otherwise it's terms to search on.
    function bp_forums_get_forum_topics( $args = '' ) { 
    9193        if ( class_exists( 'BB_Query' ) ) {
    9294                switch ( $type ) {
    9395                        case 'newest':
    94                                 $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'number' => $per_page, 'exclude' => $exclude, 'topic_title' => $filter, 'sticky' => $show_stickies ), 'get_latest_topics' );
     96                                $query = new BB_Query( 'topic', array( 'forum_id' => $forum_id, 'topic_author_id' => $user_id, 'per_page' => $per_page, 'page' => $page, 'number' => $per_page, 'exclude' => $exclude, 'topic_title' => $filter, 'sticky' => $show_stickies, 'offset' => $offset, 'number' => $number ), 'get_latest_topics' );
    9597                                $topics =& $query->results;
    9698                                break;
    9799
  • bp-forums/bp-forums-template.php

    class BP_Forums_Template_Forum { 
    6868        var $sort_by;
    6969        var $order;
    7070
    71         function BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms ) {
    72                 $this->__construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms );
     71        function BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms, $offset = false, $number = false ) {
     72                $this->__construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms, $offset, $number );
    7373        }
    7474
    75         function __construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms ) {       
     75        function __construct( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms, $offset = false, $number = false ) {     
    7676                global $bp;
    7777
    78                 $this->pag_page     = isset( $_REQUEST['p'] ) ? intval( $_REQUEST['p'] ) : $page;
    79                 $this->pag_num      = isset( $_REQUEST['n'] ) ? intval( $_REQUEST['n'] ) : $per_page;
     78                $this->pag_page     = $page;
     79                $this->pag_num      = $per_page;
    8080                $this->type         = $type;
    8181                $this->search_terms = $search_terms;
    8282                $this->forum_id     = $forum_id;
     83                $this->offset       = $offset;
     84                $this->number       = $number;
    8385
    8486                switch ( $type ) {
    85                         case 'newest': default:
    86                                 $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'forum_id' => $forum_id, 'filter' => $search_terms, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies ) );
     87                        case 'newest': default:                         
     88                                $this->topics = bp_forums_get_forum_topics( array( 'user_id' => $user_id, 'forum_id' => $forum_id, 'filter' => $search_terms, 'page' => $this->pag_page, 'per_page' => $this->pag_num, 'show_stickies' => $no_stickies, 'offset' => $offset, 'number' => $number ) );
    8789                                break;
    8890
    8991                        case 'popular':
    class BP_Forums_Template_Forum { 
    111113                        } else if ( !empty( $bp->groups->current_group ) ) {
    112114                                $topic_count = (int)groups_total_public_forum_topic_count( $type );
    113115                        } else {
    114                                 $topic_count = count( $this->topics );
     116                                // For forum directories, get a true count
     117                                $status = is_super_admin() ? 'all' : 'public'; // todo: member-of
     118                                $topic_count = (int)groups_total_forum_topic_count( $status );
    115119                        }
    116120
    117121                        if ( !$max || $max >= $topic_count ) {
    class BP_Forums_Template_Forum { 
    119123                        } else {
    120124                                $this->total_topic_count = (int)$max;
    121125                        }
    122 
     126                       
    123127                        if ( $max ) {
    124128                                if ( $max >= count($this->topics) ) {
    125129                                        $this->topic_count = count( $this->topics );
    class BP_Forums_Template_Forum { 
    134138                $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 ) );
    135139                $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 ) );
    136140
    137                 if ( !$no_stickies ) {
    138                         $stickies = array();
    139                         $standard = array();
    140 
    141                         // Place stickies at the top - not sure why bbPress doesn't do this?
    142                         foreach( (array)$this->topics as $topic ) {
    143                                 if ( isset( $topic->topic_sticky ) && 1 == (int)$topic->topic_sticky ) {
    144                                         $stickies[] = $topic;
    145                                 } else {
    146                                         $standard[] = $topic;
    147                                 }
    148                         }
    149 
    150                         $this->topics = array_merge( (array)$stickies, (array)$standard );
    151                 }
    152 
    153141                // Fetch extra information for topics, so we don't have to query inside the loop
    154142                $this->topics = bp_forums_get_topic_extras( $this->topics );
    155143
    function bp_has_forum_topics( $args = '' ) { 
    224212        $user_id      = 0;
    225213        $forum_id     = false;
    226214        $search_terms = false;
    227         $no_stickies  = 'all';
    228215
    229216        // User filtering
    230217        if ( !empty( $bp->displayed_user->id ) )
    function bp_has_forum_topics( $args = '' ) { 
    254241        // If $_GET['fs'] is set, let's auto populate the search_terms var
    255242        if ( bp_is_directory() && !empty( $_GET['fs'] ) )
    256243                $search_terms = $_GET['fs'];
    257 
    258         // Show stickies on a group forum
    259         if ( isset( $bp->groups->current_group ) )
    260                 $no_stickies = null;
     244       
     245        // Get the pagination arguments from $_REQUEST
     246        $page     = isset( $_REQUEST['p'] ) ? intval( $_REQUEST['p'] ) : 1;
     247        $per_page = isset( $_REQUEST['n'] ) ? intval( $_REQUEST['n'] ) : 20;
    261248
    262249        $defaults = array(
    263250                'type'         => $type,
    264251                'forum_id'     => $forum_id,
    265252                'user_id'      => $user_id,
    266                 'page'         => 1,
    267                 'per_page'     => 20,
     253                'page'         => $page,
     254                'per_page'     => $per_page,
    268255                'max'          => false,
    269                 'no_stickies'  => $no_stickies,
     256                'number'       => false,
     257                'offset'       => false,
    270258                'search_terms' => $search_terms
    271259        );
    272260
    function bp_has_forum_topics( $args = '' ) { 
    279267                $search_terms = $bp->action_variables[0];
    280268                $type = 'tags';
    281269        }
    282 
    283         $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, $no_stickies, $search_terms );
     270       
     271        // Fetch the stickies
     272        $stickies_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, 0, 0, $max, 'sticky', $search_terms );
     273       
     274        // If stickies are found, try merging them
     275        if ( $stickies_template->has_topics() ) {
     276       
     277                // If stickies are for current $page           
     278                $page_start_num = ( ( $page - 1 ) * $per_page ) + 1;
     279                $page_end_num   = $page * $per_page <= $stickies_template->total_topic_count ? $page * $per_page : $stickies_template->total_topic_count;
     280               
     281                // Calculate the number of sticky topics that will be shown on this page
     282                if ( $stickies_template->topic_count < $page_start_num ) {
     283                        $this_page_stickies = 0;
     284                } else {                       
     285                        $this_page_stickies = $stickies_template->topic_count - $per_page * floor( $stickies_template->topic_count / $per_page ) * ( $page - 1 ); // Total stickies minus sticky count through this page
     286                       
     287                        // $this_page_stickies cannot be more than $per_page or less than 0
     288                        if ( $this_page_stickies > $per_page )
     289                                $this_page_stickies = $per_page;
     290                        else if ( $this_page_stickies < 0 )
     291                                $this_page_stickies = 0;
     292                }
     293               
     294                // Calculate the total number of topics that will be shown on this page
     295                $this_page_topics = $stickies_template->total_topic_count >= ( $page * $per_page ) ? $per_page : $page_end_num - ( $page_start_num - 1 );
     296               
     297                // If the number of stickies to be shown is less than $per_page, fetch some non-
     298                // stickies to fill in the rest
     299                if ( $this_page_stickies < $this_page_topics ) {
     300                        // How many non-stickies do we need?
     301                        $non_sticky_number = $this_page_topics - $this_page_stickies;
     302                       
     303                        // Calculate the non-sticky offset                     
     304                        // How many non-stickies on all pages up to this point?
     305                        $non_sticky_total = $page_end_num - $stickies_template->topic_count;
     306                       
     307                        // The offset is the number of total non-stickies, less the number to be
     308                        // shown on this page
     309                        $non_sticky_offset = $non_sticky_total - $non_sticky_number;
     310                       
     311                        // Fetch the non-stickies
     312                        $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, 1, $per_page, $max, 'no', $search_terms, $non_sticky_offset, $non_sticky_number );
     313                       
     314                        // If there are stickies to merge on this page, do it now
     315                        if ( $this_page_stickies ) {
     316                                // Correct the topic_count
     317                                $forum_template->topic_count += (int)$this_page_stickies;
     318                               
     319                                // Figure out which stickies need to be included
     320                                $this_page_sticky_topics = array_slice( $stickies_template->topics, 0 - $this_page_stickies );
     321                               
     322                                // Merge these topics into the forum template
     323                                $forum_template->topics = array_merge( $this_page_sticky_topics, (array)$forum_template->topics );
     324                        }
     325                } else {
     326                        // This page has no non-stickies
     327                        $forum_template = $stickies_template;
     328                       
     329                        // Adjust the topic count and trim the topics
     330                        $forum_template->topic_count = $this_page_stickies;
     331                        $forum_template->topics      = array_slice( $forum_template->topics, $page - 1 );               
     332                       
     333                }
     334                               
     335                // Because we're using a manual offset and number for the topic query, we
     336                // must set the page number manually, and recalculate the pagination links
     337                $forum_template->pag_num     = $per_page;
     338                $forum_template->pag_page    = $page;
     339               
     340                $forum_template->pag_links = paginate_links( array(
     341                        'base'      => add_query_arg( array( 'p' => '%#%', 'n' => $forum_template->pag_num ) ),
     342                        'format'    => '',
     343                        'total'     => ceil( (int)$forum_template->total_topic_count / (int)$forum_template->pag_num ),
     344                        'current'   => $forum_template->pag_page,
     345                        'prev_text' => '&larr;',
     346                        'next_text' => '&rarr;',
     347                        'mid_size'  => 1
     348                ) );
     349               
     350        } else {
     351                // Fetch the non-sticky topics if no stickies were found
     352                $forum_template = new BP_Forums_Template_Forum( $type, $forum_id, $user_id, $page, $per_page, $max, 'all', $search_terms );
     353        }
     354               
    284355        return apply_filters( 'bp_has_topics', $forum_template->has_topics(), $forum_template );
    285356}
    286357
    function bp_forum_topic_count( $user_id = 0 ) { 
    12801351        function bp_get_forum_topic_count( $user_id = 0 ) {
    12811352                return apply_filters( 'bp_get_forum_topic_count', bp_forums_total_topic_count( $user_id ) );
    12821353        }
    1283 ?>
    1284  No newline at end of file
     1354?>
  • bp-groups/bp-groups-classes.php

    Class BP_Groups_Group { 
    595595
    596596                return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(id) FROM {$bp->groups->table_name_members} WHERE group_id = %d AND is_confirmed = 1 AND is_banned = 0", $group_id ) );
    597597        }
     598       
     599       
     600        /**
     601         * Get a total count of all topics of a given status, across groups/forums
     602         *
     603         * @package BuddyPress
     604         * @since 1.3
     605         *
     606         * @param str $status 'public', 'private', 'hidden', 'all' Which group types to count
     607         * @return int The topic count
     608         */
     609        function get_global_topic_count( $status = 'public' ) {
     610                global $bbdb, $wpdb, $bp;
     611               
     612                switch ( $status ) {
     613                        case 'all' :
     614                                $status_sql = '';
     615                                break;
     616                               
     617                        case 'hidden' :
     618                                $status_sql = $wpdb->prepare( "AND g.status = 'hidden'" );
     619                                break;
     620                               
     621                        case 'private' :
     622                                $status_sql = $wpdb->prepare( "AND g.status = 'private'" );
     623                                break;
     624                               
     625                        case 'public' :
     626                        default :
     627                                $status_sql = $wpdb->prepare( "AND g.status = 'public'" );
     628                                break;
     629                }
     630               
     631                return $wpdb->get_var( "SELECT COUNT(t.topic_id) FROM {$bbdb->topics} AS t INNER JOIN {$bp->groups->table_name_groupmeta} AS gm ON t.forum_id = gm.meta_value INNER JOIN {$bp->groups->table_name} AS g ON gm.group_id = g.id WHERE gm.meta_key = 'forum_id' {$status_sql} AND t.topic_status = '0' AND t.topic_sticky != '2' " );
     632        }
    598633}
    599634
    600635Class BP_Groups_Member {
  • bp-groups/bp-groups-forums.php

    function groups_delete_group_forum_post( $post_id, $topic_id ) { 
    244244        return false;
    245245}
    246246
    247 
    248247function groups_total_public_forum_topic_count( $type = 'newest' ) {
    249248        return apply_filters( 'groups_total_public_forum_topic_count', BP_Groups_Group::get_global_forum_topic_count( $type ) );
    250249}
    251250
     251/**
     252 * Get a total count of all topics of a given status, across groups/forums
     253 *
     254 * @package BuddyPress
     255 * @since 1.3
     256 *
     257 * @param str $status 'public', 'private', 'hidden', 'all' Which group types to count
     258 * @return int The topic count
     259 */
     260function groups_total_forum_topic_count( $status = 'public' ) {
     261        return apply_filters( 'groups_total_forum_topic_count', BP_Groups_Group::get_global_topic_count( $status ) );
     262}
     263
    252264?>