Skip to:
Content

BuddyPress.org

Ticket #2242: 2242.diff

File 2242.diff, 20.9 KB (added by imath, 11 years ago)
  • bp-themes/bp-default/groups/groups-loop.php

     
    3737
    3838        <?php while ( bp_groups() ) : bp_the_group(); ?>
    3939
    40                 <li>
     40                <li <?php bp_groups_group_class();?>>
    4141                        <div class="item-avatar">
    4242                                <a href="<?php bp_group_permalink(); ?>"><?php bp_group_avatar( 'type=thumb&width=50&height=50' ); ?></a>
    4343                        </div>
  • bp-themes/bp-default/_inc/css/default.css

     
    22082208        right: 0;
    22092209        text-align: right;
    22102210}
     2211ul.item-list li.sticky {
     2212        background:#fff9db;
     2213        padding:5px;
     2214}
     2215ul.item-list li.sticky div.action{
     2216        right:5px;
     2217}
    22112218ul.item-list li div.meta {
    22122219        color: #888;
    22132220        font-size: 11px;
     
    24342441.field-visibility-settings legend,
    24352442.field-visibility-settings-toggle {
    24362443        font-style: italic;
    2437 }
    2438  No newline at end of file
     2444}
  • bp-core/admin/bp-core-settings.php

     
    197197<?php
    198198}
    199199
     200/**
     201 * Allow Administrators to stick a group to front of Groups Directory
     202 *
     203 *
     204 * @uses checked() To display the checked attribute
     205 */
     206function bp_admin_setting_callback_group_sticky() {
     207?>
     208
     209        <input id="bp_allow_sticky_group" name="bp_allow_sticky_group" type="checkbox" value="1" <?php checked( bp_allow_sticky_group( false ) ); ?> />
     210        <label for="bp_allow_sticky_group"><?php _e( 'Enable sticky groups in Groups Directory', 'buddypress' ); ?></label>
     211        <p class="description"><?php _e( 'Administrators can set groups as sticky, regardless of this setting.', 'buddypress' ); ?></p>
     212
     213<?php
     214}
     215
     216/**
     217 * Sanitization for bp_allow_sticky_group setting
     218 *
     219 * In the UI, a checkbox asks whether you'd like to *enable* sticky groups. For
     220 * legacy reasons, the option that we store is 1 if these comments are *disabled*. So we use this
     221 * function to flip the boolean before saving the intval.
     222 */
     223function bp_admin_sanitize_callback_sticky_group( $value = false ) {
     224        return $value ? 0 : 1;
     225}
     226
    200227/** Forums Section ************************************************************/
    201228
    202229/**
     
    302329                        'bp_disable_blogforum_comments',
    303330                        'bp-disable-profile-sync',
    304331                        'bp_restrict_group_creation',
     332                        'bp_allow_sticky_group',
    305333                        'hide-loggedout-adminbar',
    306334                );
    307335
  • bp-core/bp-core-admin.php

     
    275275                        // Add the main section
    276276                        add_settings_section( 'bp_groups',        __( 'Groups Settings',  'buddypress' ), 'bp_admin_setting_callback_groups_section',   'buddypress'              );
    277277
    278                         // Allow subscriptions setting
     278                        // Allow group creation for non admin
    279279                        add_settings_field( 'bp_restrict_group_creation', __( 'Group Creation',   'buddypress' ), 'bp_admin_setting_callback_group_creation',   'buddypress', 'bp_groups' );
    280280                        register_setting  ( 'buddypress',         'bp_restrict_group_creation',   'intval'                                                                                );
     281
     282                        // Allow admin to stick groups to front of Groups Directory
     283                        add_settings_field( 'bp_allow_sticky_group', __( 'Sticky Groups',   'buddypress' ), 'bp_admin_setting_callback_group_sticky',   'buddypress', 'bp_groups' );
     284                        register_setting  ( 'buddypress',         'bp_allow_sticky_group',   'bp_admin_sanitize_callback_sticky_group'                                                                                );
    281285                }
    282286
    283287                /** Forums ************************************************************/
  • bp-core/bp-core-options.php

     
    458458}
    459459
    460460/**
     461 * can we stick groups?
     462 *
     463 *
     464 * @param bool $default Optional. Default value true
     465 *
     466 * @todo Move into groups component
     467 * @uses bp_get_option() To get the group creation
     468 * @return bool Allow sticky groups?
     469 */
     470function bp_allow_sticky_group( $default = false ) {
     471        return (bool) apply_filters( 'bp_allow_sticky_group', (bool) bp_get_option( 'bp_allow_sticky_group', $default ) );
     472}
     473
     474/**
    461475 * Have we migrated to using the WordPress Toolbar?
    462476 *
    463477 * @since BuddyPress (1.6)
  • bp-templates/bp-legacy/buddypress/groups/groups-loop.php

     
    3737
    3838        <?php while ( bp_groups() ) : bp_the_group(); ?>
    3939
    40                 <li>
     40                <li <?php bp_groups_group_class();?>>
    4141                        <div class="item-avatar">
    4242                                <a href="<?php bp_group_permalink(); ?>"><?php bp_group_avatar( 'type=thumb&width=50&height=50' ); ?></a>
    4343                        </div>
  • bp-templates/bp-legacy/css/buddypress.css

     
    11731173        right: 0;
    11741174        text-align: right;
    11751175}
     1176
     1177#buddypress ul.item-list li.sticky {
     1178        background:#fff9db;
     1179        padding:5px;
     1180}
     1181
     1182#buddypress ul.item-list li.sticky div.action{
     1183        right:5px;
     1184}
    11761185#buddypress ul.item-list li div.meta {
    11771186        color: #888;
    11781187        font-size: 80%;
  • bp-groups/admin/css/admin.css

     
    5555        white-space: nowrap;
    5656        padding-left: 15px;
    5757}
     58
     59body.toplevel_page_bp-groups table.groups tr.sticky {
     60        background:#fff9db;
     61}
  • bp-groups/admin/css/admin.min.css

     
    1 body.toplevel_page_bp-groups table.groups th#status,body.toplevel_page_bp-groups table.groups th#members{width:10%}body.toplevel_page_bp-groups table.groups th#last_active{width:15%}#bp-groups-edit-form input{outline:medium none;padding:3px 8px}#bp-groups-edit-form input#bp-groups-name{font-size:1.7em;width:100%}#bp-groups-edit-form input#bp-groups-new-members{width:100%;max-width:90%;border:0}.bp-groups-settings-section{margin-bottom:15px}.bp-groups-member-type{position:relative;padding-bottom:1.7em}.bp-groups-member-type>h4{margin-bottom:.5em}ul.bp-group-delete-list{list-style-type:disc;margin:4px 26px}.bp-group-admin-pagination{position:absolute;text-align:right;width:100%}.bp-group-admin-pagination.table-top{top:0}.bp-group-admin-pagination.table-bottom{bottom:0}.bp-group-admin-pagination-viewing{font-family:Georgia,"Times New Roman","Bitstream Charter",Times,serif;color:#777;font-size:12px;font-style:italic}.bp-group-admin-pagination-links{white-space:nowrap;padding-left:15px}
    2  No newline at end of file
     1body.toplevel_page_bp-groups table.groups th#status,body.toplevel_page_bp-groups table.groups th#members{width:10%}body.toplevel_page_bp-groups table.groups th#last_active{width:15%}#bp-groups-edit-form input{outline:medium none;padding:3px 8px}#bp-groups-edit-form input#bp-groups-name{font-size:1.7em;width:100%}#bp-groups-edit-form input#bp-groups-new-members{width:100%;max-width:90%;border:0}.bp-groups-settings-section{margin-bottom:15px}.bp-groups-member-type{position:relative;padding-bottom:1.7em}.bp-groups-member-type>h4{margin-bottom:.5em}ul.bp-group-delete-list{list-style-type:disc;margin:4px 26px}.bp-group-admin-pagination{position:absolute;text-align:right;width:100%}.bp-group-admin-pagination.table-top{top:0}.bp-group-admin-pagination.table-bottom{bottom:0}.bp-group-admin-pagination-viewing{font-family:Georgia,"Times New Roman","Bitstream Charter",Times,serif;color:#777;font-size:12px;font-style:italic}.bp-group-admin-pagination-links{white-space:nowrap;padding-left:15px}body.toplevel_page_bp-groups table.groups tr.sticky{background:#fff9db;}
  • bp-groups/bp-groups-admin.php

     
    215215                        $error = $group_id;
    216216                }
    217217
     218                // Stick group to front
     219                $sticky_group   = ( isset( $_POST['group-stick-group'] ) ) ? 1 : 0;
     220
     221                if( !empty( $sticky_group ) )
     222                        groups_update_groupmeta( $group_id, 'sticky', 1 );
     223                else
     224                        groups_delete_groupmeta( $group_id, 'sticky' );
     225
    218226                // Process new members
    219227                $user_names = array();
    220228
     
    663671                </div>
    664672        <?php endif; ?>
    665673
     674        <?php if ( groups_can_stick_group( $item ) ) : ?>
     675                <div class="bp-groups-settings-section" id="bp-groups-settings-section-sticky">
     676                        <label for="group-stick-group"><input type="checkbox" name="group-stick-group" id="group-stick-group" <?php checked( groups_get_groupmeta( $item->id, 'sticky' ) ); ?> /> <?php _e( 'Stick to front of Groups Directory', 'buddypress' ) ?><br />
     677                </div>
     678        <?php endif; ?>
     679
    666680        <div class="bp-groups-settings-section" id="bp-groups-settings-section-status">
    667681                <label for="group-status"><?php _e( 'Privacy', 'buddypress' ); ?></label>
    668682
     
    11931207        function single_row( $item = array() ) {
    11941208                static $row_class = '';
    11951209
     1210                $row_classes = array();
     1211
    11961212                if ( empty( $row_class ) ) {
    1197                         $row_class = ' class="alternate odd"';
     1213                        $row_classes[] = 'alternate odd';
    11981214                } else {
    1199                         $row_class = ' class="even"';
     1215                        $row_classes[] = 'even';
    12001216                }
    12011217
     1218                if( groups_get_groupmeta(  $item['id'], 'sticky' ) && bp_allow_sticky_group() )
     1219                        $row_classes[] = 'sticky';
     1220
     1221                $row_class = ' class="' .implode( ' ', $row_classes ) .'"';
     1222
    12021223                echo '<tr' . $row_class . ' id="group-' . esc_attr( $item['id'] ) . '" data-parent_id="' . esc_attr( $item['id'] ) . '" data-root_id="' . esc_attr( $item['id'] ) . '">';
    12031224                echo $this->single_row_columns( $item );
    12041225                echo '</tr>';
     1226
     1227                if( in_array( 'even', $row_classes ) )
     1228                        $row_class = '';
    12051229        }
    12061230
    12071231        /**
  • bp-groups/bp-groups-classes.php

     
    346346                        'populate_extras' => true,
    347347                        'exclude'         => false,
    348348                        'show_hidden'     => false,
     349                        'sticky'          => false,
    349350                );
    350351
    351352                $r = wp_parse_args( $args, $defaults );
    352353
    353354                $sql       = array();
    354355                $total_sql = array();
     356                $stickies = false;
    355357
    356358                $sql['select'] = "SELECT DISTINCT g.id, g.*, gm1.meta_value AS total_member_count, gm2.meta_value AS last_activity";
    357359                $sql['from']   = " FROM {$bp->groups->table_name_groupmeta} gm1, {$bp->groups->table_name_groupmeta} gm2,";
     
    429431                // Convert 'orderby' into the proper ORDER BY term
    430432                $orderby = self::convert_orderby_to_order_by_term( $orderby );
    431433
     434                // sticky groups
     435                if( !empty( $r['sticky'] ) ) {
     436                        $sper_page = $r['per_page'];
     437                        $spage = $r['page'];
     438
     439                        $stickies = self::get_stickies( $sql, $meta_query_sql, $r['per_page'], $order, $orderby );
     440                        $sticky_num = is_array( $stickies ) ? count( $stickies ) : false;
     441
     442                        if( !empty( $sticky_num ) ) {
     443                               
     444                                // We use the exclude arguments to avoid duplicate display of sticky groups
     445                                $exclude_stickies = array();
     446
     447                                foreach( $stickies as $stick ) {
     448                                        $exclude_stickies[] = $stick->id;
     449                                }
     450
     451                                // if exclude argument is set, merge it with exclude stickies one
     452                                $group_excluded = !empty( $exclude ) ? array_merge( $exclude_stickies, explode( ',', $exclude  ) ) : $exclude_stickies;
     453                                $group_excluded = implode( ',', $group_excluded );
     454
     455                                $sql['exclude'] = " AND g.id NOT IN ({$group_excluded})";
     456
     457                                if( $r['page'] <= 1 )
     458                                        $r['per_page'] -= $sticky_num;
     459                                else
     460                                        $r['page'] =  $r['page'] - ( $sticky_num / $r['per_page'] ) ;
     461                        }
     462
     463                }
     464
    432465                // Random order is a special case
    433466                if ( 'rand()' === $orderby ) {
    434467                        $sql[] = "ORDER BY rand()";
     
    436469                        $sql[] = "ORDER BY {$orderby} {$order}";
    437470                }
    438471
    439                 if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) ) {
    440                         $sql['pagination'] = $wpdb->prepare( "LIMIT %d, %d", intval( ( $r['page'] - 1 ) * $r['per_page']), intval( $r['per_page'] ) );
     472                /*
     473                If Number of stickies is equal to the per page argument, and we're on first page
     474                A new query with a null offset is not necessary ;)
     475                */
     476                if( empty( $sticky_num ) || $sticky_num < $sper_page || $spage > 1 ) {
     477
     478                        // we need to use the round function as for instance intval( (1.4 - 1) * 5 ) == 1 instead of 2 ?!
     479                        if ( ! empty( $r['per_page'] ) && ! empty( $r['page'] ) ) {
     480                                $sql['pagination'] = $wpdb->prepare( "LIMIT %d, %d", intval( round( ( $r['page'] - 1 ) * $r['per_page'] ) ), intval( $r['per_page'] ) );
     481                        }
     482
     483                        // Get paginated results
     484                        $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', join( ' ', (array) $sql ), $sql );
     485                        $paged_groups     = $wpdb->get_results( $paged_groups_sql );
     486
    441487                }
    442488
    443                 // Get paginated results
    444                 $paged_groups_sql = apply_filters( 'bp_groups_get_paged_groups_sql', join( ' ', (array) $sql ), $sql );
    445                 $paged_groups     = $wpdb->get_results( $paged_groups_sql );
    446 
    447489                $total_sql['select'] = "SELECT COUNT(DISTINCT g.id) FROM {$bp->groups->table_name} g, {$bp->groups->table_name_members} gm1, {$bp->groups->table_name_groupmeta} gm2";
    448490
    449491                if ( ! empty( $r['user_id'] ) ) {
     
    498540                $total_groups     = $wpdb->get_var( $total_groups_sql );
    499541
    500542                $group_ids = array();
     543
     544                // merging if we still have regular groups
     545                if( !empty( $sticky_num ) && $spage <= 1 )
     546                        $paged_groups = !empty( $paged_groups ) ? array_merge( $stickies, (array) $paged_groups ) : $stickies ;
     547
    501548                foreach ( (array) $paged_groups as $group ) {
    502549                        $group_ids[] = $group->id;
    503550                }
     
    517564        }
    518565
    519566        /**
    520          * Get the SQL for the 'meta_query' param in BP_Activity_Activity::get()
     567         * Get the SQL for the 'meta_query' param in BP_Groups_Group::get()
    521568         *
    522569         * We use WP_Meta_Query to do the heavy lifting of parsing the
    523570         * meta_query array and creating the necessary SQL clauses. However,
     
    577624        }
    578625
    579626        /**
     627         * Gets the stickies to add to the groups queried in BP_Groups_Group::get()
     628         *
     629         * @access protected
     630         *
     631         * @global $wpdb the WordPress DB API
     632         * @param  array $sticky_sql the sql array calculated by BP_Groups_Group::get()
     633         * @param  array $meta_query_sql the sql array of an eventual meta query
     634         * @param  integer $per_page the query offset
     635         * @param  string $order DESC or ASC ?
     636         * @param  string $orderby how to order the sticky query
     637         * @uses   buddypress() to get main instance of the plugin
     638         * @return array the sticky groups found
     639         */
     640        protected static function get_stickies( $sticky_sql = array(), $meta_query_sql = array(), $per_page = 0, $order = '', $orderby = '' ) {
     641                global $wpdb;
     642                $bp = buddypress();
     643
     644                if( empty( $sticky_sql ) || !is_array( $sticky_sql ) )
     645                        return false;
     646
     647                // When searching a group, stickies would be annoying..
     648                if ( ! empty($sticky_sql['search'] ) )
     649                        return false;
     650
     651                // In case of restricting the group query to limited groups, we don't need stickies
     652                if ( ! empty( $sticky_sql['include'] ) )
     653                        return false;
     654
     655                // We only play in Group Directory so a user id tells us we're no on groups directory
     656                if ( ! empty( $sticky_sql['members_from'] ) )
     657                        return false;
     658
     659                // hidden can't be sticky
     660                $sticky_sql['hidden'] = " AND g.status != 'hidden'";
     661
     662                // If a meta query, don't apply it on stickies
     663                if( !empty( $meta_query_sql['join'] ) )
     664                        $sticky_sql['from'] = str_replace( $meta_query_sql['join'], '', $sticky_sql['from'] );
     665
     666                if ( ! empty( $meta_query_sql['where'] ) )
     667                        unset( $sticky_sql['meta'] );
     668
     669                // Adding this will spread in global $groups_template ( $groups_template->group->sticky )
     670                $sticky_sql['select'] .= ", 1 as sticky";
     671
     672                // Adding the specific sticky part for the group query
     673                $sticky_sql['from'] .= "{$bp->groups->table_name_groupmeta} gms,";
     674                $sticky_sql['where'] .= " AND g.id = gms.group_id AND gms.meta_key = 'sticky'";
     675
     676                if( !empty( $orderby ) && !empty( $order ) )
     677                        $sticky_sql[] = "ORDER BY {$orderby} {$order}";
     678
     679                /* max is per_page, after it's no more a sticky group... */
     680                if ( ! empty( $per_page ) ) {
     681                        $sticky_sql['pagination'] = $wpdb->prepare( "LIMIT %d", intval( $per_page ) );
     682                }
     683
     684                // Plugin authors can modify the query if they need to
     685                $sticky_query = apply_filters( 'bp_groups_get_stickies_sql', join( ' ', (array) $sticky_sql ), $sticky_sql );
     686
     687                // returning the found stickies.
     688                return (array) $wpdb->get_results( $sticky_query );
     689        }
     690
     691        /**
    580692         * Convert the 'type' parameter to 'order' and 'orderby'
    581693         *
    582694         * @since BuddyPress (1.8)
  • bp-groups/bp-groups-functions.php

     
    465465                'per_page'        => 20,       // The number of results to return per page
    466466                'page'            => 1,        // The page to return if limiting per page
    467467                'populate_extras' => true,     // Fetch meta such as is_banned and is_member
     468                'sticky'          => false
    468469        );
    469470
    470471        $r = wp_parse_args( $args, $defaults );
     
    480481                'per_page'        => $r['per_page'],
    481482                'page'            => $r['page'],
    482483                'populate_extras' => $r['populate_extras'],
     484                'sticky'          => $r['sticky'],
    483485                'order'           => $r['order'],
    484                 'orderby'         => $r['orderby'],
     486                'orderby'         => $r['orderby']
    485487        ) );
    486488
    487489        return apply_filters_ref_array( 'groups_get_groups', array( &$groups, &$r ) );
     
    10781080add_action( 'wpmu_delete_user',  'groups_remove_data_for_user' );
    10791081add_action( 'delete_user',       'groups_remove_data_for_user' );
    10801082add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
     1083
     1084
     1085/*** Sticky group *****/
     1086
     1087/**
     1088 * Checks if a group can be sticky according to blog settings and group status
     1089 *
     1090 * @param  BP_Groups_Group $group the group object
     1091 * @uses   groups_get_current_group() if the argument is not defined to try to find current group
     1092 * @uses   bp_allow_sticky_group() to get blog settings for the stick to front group setting
     1093 * @return boolean true||false
     1094 */
     1095function groups_can_stick_group( $group = false ) {
     1096        if( empty( $group ) )
     1097                $group = groups_get_current_group();
     1098
     1099        if( $group->status != 'hidden' )
     1100                return bp_allow_sticky_group();
     1101        else
     1102                return false;
     1103
     1104}
  • bp-groups/bp-groups-template.php

     
    145145                        'exclude'         => false,
    146146                        'search_terms'    => '',
    147147                        'meta_query'      => false,
    148                         'populate_extras' => true
     148                        'populate_extras' => true,
     149                        'sticky'          => false,
    149150                );
    150151
    151152                $r = wp_parse_args( $args, $defaults );
     
    176177                                'include'         => $include,
    177178                                'exclude'         => $exclude,
    178179                                'populate_extras' => $populate_extras,
    179                                 'show_hidden'     => $show_hidden
     180                                'show_hidden'     => $show_hidden,
     181                                'sticky'          => $sticky
    180182                        ) );
    181183                }
    182184
     
    291293        $type    = '';
    292294        $user_id = 0;
    293295        $order   = '';
     296        $sticky  = false;
    294297
    295298        // User filtering
    296299        if ( bp_displayed_user_id() )
     
    342345                else
    343346                        $r['search_terms'] = false;
    344347        }
     348       
     349        if ( bp_is_groups_component() && !bp_current_action() && !bp_current_item() && empty( $r['search_terms'] ) )
     350                $sticky = bp_allow_sticky_group();
    345351
    346352        $groups_template = new BP_Groups_Template( array(
    347353                'type'            => $r['type'],
     
    358364                'meta_query'      => $r['meta_query'],
    359365                'include'         => $r['include'],
    360366                'exclude'         => $r['exclude'],
    361                 'populate_extras' => (bool) $r['populate_extras']
     367                'populate_extras' => (bool) $r['populate_extras'],
     368                'sticky'          => (bool) $sticky
    362369        ) );
    363370
    364371        return apply_filters( 'bp_has_groups', $groups_template->has_groups(), $groups_template, $r );
     
    407414        }
    408415
    409416/**
     417 * Output the row class of a group in the groups loop
     418 */
     419function bp_groups_group_class() {
     420        echo bp_get_groups_group_class();
     421}
     422        /**
     423         * Return the row class of a group in the groups loop
     424         *
     425         * @global BP_Groups_Template $groups_template
     426         * @uses   apply_filters() to let plugins add their classes
     427         * @return string Row class of the group
     428         */
     429        function bp_get_groups_group_class() {
     430                global $groups_template;
     431
     432                $classes = array();
     433
     434                if( !empty( $groups_template->group->sticky ) )
     435                        $classes[] = 'sticky';
     436
     437                $classes = apply_filters( 'bp_get_groups_class', $classes );
     438                $classes = array_merge( $classes, array() );
     439                $retval = 'class="' . join( ' ', $classes ) . '"';
     440                return $retval;
     441        }
     442
     443/**
    410444 * Output the row class of a group
    411445 *
    412446 * @since BuddyPress (1.7)