Skip to:
Content

BuddyPress.org

Ticket #5008: bp-groups-widgets.php.patch

File bp-groups-widgets.php.patch, 8.8 KB (added by megainfo, 12 years ago)

A patch for groups widget

  • bp-groups-widgets.php

     
    1313/* Register widgets for groups component */
    1414function groups_register_widgets() {
    1515        add_action('widgets_init', create_function('', 'return register_widget("BP_Groups_Widget");') );
     16        add_action('widgets_init', create_function('', 'return register_widget("BP_Member_Groups_Widget");') );
    1617}
    1718add_action( 'bp_register_widgets', 'groups_register_widgets' );
    1819
     
    203204}
    204205add_action( 'wp_ajax_widget_groups_list',        'groups_ajax_widget_groups_list' );
    205206add_action( 'wp_ajax_nopriv_widget_groups_list', 'groups_ajax_widget_groups_list' );
     207
     208class BP_Member_Groups_Widget extends WP_Widget {
     209
     210        function __construct() {
     211                $widget_ops = array(
     212                        'description' => __( 'A dynamic list of recently active, popular, and newest groups of a loggedin/displayed member', 'buddypress' ),
     213                        'classname' => 'widget_bp_member_groups_widget buddypress widget',
     214                );
     215                parent::__construct( false, _x( '(BuddyPress) My Groups', 'widget name', 'buddypress' ), $widget_ops );
     216
     217                if ( is_active_widget( false, false, $this->id_base ) && !is_admin() && !is_network_admin() ) {
     218                        $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
     219                        wp_enqueue_script( 'groups_widget_member_groups_list-js', BP_PLUGIN_URL . "bp-groups/js/widget-member-groups{$min}.js", array( 'jquery' ), bp_get_version() );
     220                }
     221        }
     222
     223        function widget( $args, $instance ) {
     224
     225                global $bp;
     226                extract( $args );
     227
     228                if ( bp_displayed_user_domain() ) {
     229                        $user_id = $bp->displayed_user->id;
     230                        $link = str_replace( bp_displayed_user_domain(), bp_displayed_user_domain(), bp_get_groups_slug() );
     231                        $instance['title'] = sprintf( __( '%s Groups', 'buddypress' ), bp_get_displayed_user_fullname() );
     232
     233                } elseif ( bp_loggedin_user_domain() ) {
     234                        $user_id = $bp->loggedin_user->id;
     235                        $link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
     236                        $instance['title'] = __( 'My Groups', 'buddypress' );
     237
     238                } else {
     239                        return;
     240                }
     241
     242                if ( empty( $instance['member_group_default'] ) )
     243                        $instance['member_group_default'] = 'popular';
     244
     245                $title = apply_filters( 'widget_title', $instance['title'] );
     246
     247                echo $before_widget;
     248
     249                $title = !empty( $instance['link_title'] ) ? '<a href="' . $link . '">' . $title . '</a>' : $title;
     250
     251                echo $before_title . $title . $after_title; ?>
     252
     253                <?php if ( bp_has_groups( 'user_id=' . $user_id . '&type=' . $instance['member_group_default'] . '&max=' . $instance['max_member_groups'] ) ) : ?>
     254                        <div class="item-options" id="member-groups-list-options">
     255                                <a href="<?php bp_groups_directory_permalink(); ?>" id="newest-groups"<?php if ( $instance['member_group_default'] == 'newest' ) : ?> class="selected"<?php endif; ?>><?php _e("Newest", 'buddypress') ?></a> |
     256                                <a href="<?php bp_groups_directory_permalink(); ?>" id="recently-active-groups"<?php if ( $instance['member_group_default'] == 'active' ) : ?> class="selected"<?php endif; ?>><?php _e("Active", 'buddypress') ?></a> |
     257                                <a href="<?php bp_groups_directory_permalink(); ?>" id="popular-groups" <?php if ( $instance['member_group_default'] == 'popular' ) : ?> class="selected"<?php endif; ?>><?php _e("Popular", 'buddypress') ?></a>
     258                        </div>
     259
     260                        <ul id="member-groups-list" class="item-list">
     261                                <?php while ( bp_groups() ) : bp_the_group(); ?>
     262                                        <li>
     263                                                <div class="item-avatar">
     264                                                        <a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a>
     265                                                </div>
     266
     267                                                <div class="item">
     268                                                        <div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
     269                                                        <div class="item-meta">
     270                                                                <span class="activity">
     271                                                                <?php
     272                                                                        if ( 'newest' == $instance['member_group_default'] )
     273                                                                                printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
     274                                                                        if ( 'active' == $instance['member_group_default'] )
     275                                                                                printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
     276                                                                        else if ( 'popular' == $instance['member_group_default'] )
     277                                                                                bp_group_member_count();
     278                                                                ?>
     279                                                                </span>
     280                                                        </div>
     281                                                </div>
     282                                        </li>
     283
     284                                <?php endwhile; ?>
     285                        </ul>
     286                        <?php wp_nonce_field( 'groups_widget_member_groups_list', '_wpnonce-member-groups' ); ?>
     287                        <input type="hidden" name="member_groups_widget_max" id="member_groups_widget_max" value="<?php echo esc_attr( $instance['max_member_groups'] ); ?>" />
     288
     289                <?php else: ?>
     290
     291                        <div class="widget-error">
     292                                <?php _e('There are no groups to display.', 'buddypress') ?>
     293                        </div>
     294
     295                <?php endif; ?>
     296
     297                <?php echo $after_widget; ?>
     298        <?php
     299        }
     300
     301        function update( $new_instance, $old_instance ) {
     302                $instance = $old_instance;
     303
     304                $instance['max_member_groups']    = strip_tags( $new_instance['max_member_groups'] );
     305                $instance['member_group_default'] = strip_tags( $new_instance['member_group_default'] );
     306                $instance['link_title']    = (bool)$new_instance['link_title'];
     307
     308                return $instance;
     309        }
     310
     311        function form( $instance ) {
     312                $defaults = array(
     313                        'max_member_groups'    => 5,
     314                        'member_group_default' => 'active',
     315                        'link_title'    => false
     316                );
     317                $instance = wp_parse_args( (array) $instance, $defaults );
     318       
     319                $max_member_groups    = strip_tags( $instance['max_member_groups'] );
     320                $member_group_default = strip_tags( $instance['member_group_default'] );
     321                $link_title    = (bool)$instance['link_title'];
     322                ?>
     323
     324                <p><label for="<?php echo $this->get_field_name('link_title') ?>"><input type="checkbox" name="<?php echo $this->get_field_name('link_title') ?>" value="1" <?php checked( $link_title ) ?> /> <?php _e( 'Link widget title to Groups directory', 'buddypress' ) ?></label></p>
     325
     326                <p><label for="bp-groups-widget-member-groups-max"><?php _e('Max groups to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_member_groups' ); ?>" name="<?php echo $this->get_field_name( 'max_member_groups' ); ?>" type="text" value="<?php echo esc_attr( $max_member_groups ); ?>" style="width: 30%" /></label></p>
     327
     328                <p>
     329                        <label for="bp-groups-widget-member-groups-default"><?php _e('Default groups to show:', 'buddypress'); ?>
     330                        <select name="<?php echo $this->get_field_name( 'member_group_default' ); ?>">
     331                                <option value="newest" <?php if ( $member_group_default == 'newest' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
     332                                <option value="active" <?php if ( $member_group_default == 'active' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></option>
     333                                <option value="popular"  <?php if ( $member_group_default == 'popular' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
     334                        </select>
     335                        </label>
     336                </p>
     337        <?php
     338        }
     339}
     340
     341function groups_ajax_widget_member_groups_list() {
     342
     343        check_ajax_referer('groups_widget_member_groups_list');
     344
     345        switch ( $_POST['filter'] ) {
     346                case 'newest-groups':
     347                        $type = 'newest';
     348                break;
     349                case 'recently-active-groups':
     350                        $type = 'active';
     351                break;
     352                case 'popular-groups':
     353                        $type = 'popular';
     354                break;
     355        }
     356
     357        if ( bp_has_groups( 'type=' . $type . '&per_page=' . $_POST['max_member_groups'] . '&max=' . $_POST['max_member_groups'] ) ) : ?>
     358                <?php echo "0[[SPLIT]]"; ?>
     359                <?php while ( bp_groups() ) : bp_the_group(); ?>
     360                        <li>
     361                                <div class="item-avatar">
     362                                        <a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a>
     363                                </div>
     364
     365                                <div class="item">
     366                                        <div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
     367                                        <div class="item-meta">
     368                                                <span class="activity">
     369                                                        <?php
     370                                                        if ( 'newest-groups' == $_POST['filter'] ) {
     371                                                                printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
     372                                                        } else if ( 'recently-active-groups' == $_POST['filter'] ) {
     373                                                                printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
     374                                                        } else if ( 'popular-groups' == $_POST['filter'] ) {
     375                                                                bp_group_member_count();
     376                                                        }
     377                                                        ?>
     378                                                </span>
     379                                        </div>
     380                                </div>
     381                        </li>
     382                <?php endwhile; ?>
     383
     384                <?php wp_nonce_field( 'groups_widget_member_groups_list', '_wpnonce-member-groups' ); ?>
     385                <input type="hidden" name="member_groups_widget_max" id="member_groups_widget_max" value="<?php echo esc_attr( $_POST['max_member_groups'] ); ?>" />
     386
     387        <?php else: ?>
     388
     389                <?php echo "-1[[SPLIT]]<li>" . __("No groups matched the current filter.", 'buddypress'); ?>
     390
     391        <?php endif;
     392
     393}
     394add_action( 'wp_ajax_widget_member_groups_list',        'groups_ajax_widget_member_groups_list' );
     395add_action( 'wp_ajax_nopriv_widget_member_groups_list', 'groups_ajax_widget_member_groups_list' );
     396 No newline at end of file