Index: bp-groups-widgets.php
===================================================================
--- bp-groups-widgets.php	(revision 7287)
+++ bp-groups-widgets.php	(working copy)
@@ -13,6 +13,7 @@
 /* Register widgets for groups component */
 function groups_register_widgets() {
 	add_action('widgets_init', create_function('', 'return register_widget("BP_Groups_Widget");') );
+	add_action('widgets_init', create_function('', 'return register_widget("BP_Member_Groups_Widget");') );
 }
 add_action( 'bp_register_widgets', 'groups_register_widgets' );
 
@@ -203,3 +204,192 @@
 }
 add_action( 'wp_ajax_widget_groups_list',        'groups_ajax_widget_groups_list' );
 add_action( 'wp_ajax_nopriv_widget_groups_list', 'groups_ajax_widget_groups_list' );
+
+class BP_Member_Groups_Widget extends WP_Widget {
+
+	function __construct() {
+		$widget_ops = array(
+			'description' => __( 'A dynamic list of recently active, popular, and newest groups of a loggedin/displayed member', 'buddypress' ),
+			'classname' => 'widget_bp_member_groups_widget buddypress widget',
+		);
+		parent::__construct( false, _x( '(BuddyPress) My Groups', 'widget name', 'buddypress' ), $widget_ops );
+
+		if ( is_active_widget( false, false, $this->id_base ) && !is_admin() && !is_network_admin() ) {
+			$min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
+			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() );
+		}
+	}
+
+	function widget( $args, $instance ) {
+
+		global $bp;
+		extract( $args );
+
+		if ( bp_displayed_user_domain() ) {
+			$user_id = $bp->displayed_user->id;
+			$link = str_replace( bp_displayed_user_domain(), bp_displayed_user_domain(), bp_get_groups_slug() );
+			$instance['title'] = sprintf( __( '%s Groups', 'buddypress' ), bp_get_displayed_user_fullname() );
+
+		} elseif ( bp_loggedin_user_domain() ) {
+			$user_id = $bp->loggedin_user->id;
+			$link = trailingslashit( bp_loggedin_user_domain() . bp_get_groups_slug() );
+			$instance['title'] = __( 'My Groups', 'buddypress' );
+
+		} else {
+			return;
+		}
+
+		if ( empty( $instance['member_group_default'] ) )
+			$instance['member_group_default'] = 'popular';
+
+		$title = apply_filters( 'widget_title', $instance['title'] );
+
+		echo $before_widget;
+
+		$title = !empty( $instance['link_title'] ) ? '<a href="' . $link . '">' . $title . '</a>' : $title;
+
+		echo $before_title . $title . $after_title; ?>
+
+		<?php if ( bp_has_groups( 'user_id=' . $user_id . '&type=' . $instance['member_group_default'] . '&max=' . $instance['max_member_groups'] ) ) : ?>
+			<div class="item-options" id="member-groups-list-options">
+				<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> |
+				<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> |
+				<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>
+			</div>
+
+			<ul id="member-groups-list" class="item-list">
+				<?php while ( bp_groups() ) : bp_the_group(); ?>
+					<li>
+						<div class="item-avatar">
+							<a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a>
+						</div>
+
+						<div class="item">
+							<div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
+							<div class="item-meta">
+								<span class="activity">
+								<?php
+									if ( 'newest' == $instance['member_group_default'] )
+										printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
+									if ( 'active' == $instance['member_group_default'] )
+										printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
+									else if ( 'popular' == $instance['member_group_default'] )
+										bp_group_member_count();
+								?>
+								</span>
+							</div>
+						</div>
+					</li>
+
+				<?php endwhile; ?>
+			</ul>
+			<?php wp_nonce_field( 'groups_widget_member_groups_list', '_wpnonce-member-groups' ); ?>
+			<input type="hidden" name="member_groups_widget_max" id="member_groups_widget_max" value="<?php echo esc_attr( $instance['max_member_groups'] ); ?>" />
+
+		<?php else: ?>
+
+			<div class="widget-error">
+				<?php _e('There are no groups to display.', 'buddypress') ?>
+			</div>
+
+		<?php endif; ?>
+
+		<?php echo $after_widget; ?>
+	<?php
+	}
+
+	function update( $new_instance, $old_instance ) {
+		$instance = $old_instance;
+
+		$instance['max_member_groups']    = strip_tags( $new_instance['max_member_groups'] );
+		$instance['member_group_default'] = strip_tags( $new_instance['member_group_default'] );
+		$instance['link_title']    = (bool)$new_instance['link_title'];
+
+		return $instance;
+	}
+
+	function form( $instance ) {
+		$defaults = array(
+			'max_member_groups'    => 5,
+			'member_group_default' => 'active',
+			'link_title'    => false
+		);
+		$instance = wp_parse_args( (array) $instance, $defaults );
+	
+		$max_member_groups    = strip_tags( $instance['max_member_groups'] );
+		$member_group_default = strip_tags( $instance['member_group_default'] );
+		$link_title    = (bool)$instance['link_title'];
+		?>
+
+		<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>
+
+		<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>
+
+		<p>
+			<label for="bp-groups-widget-member-groups-default"><?php _e('Default groups to show:', 'buddypress'); ?>
+			<select name="<?php echo $this->get_field_name( 'member_group_default' ); ?>">
+				<option value="newest" <?php if ( $member_group_default == 'newest' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
+				<option value="active" <?php if ( $member_group_default == 'active' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></option>
+				<option value="popular"  <?php if ( $member_group_default == 'popular' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
+			</select>
+			</label>
+		</p>
+	<?php
+	}
+}
+
+function groups_ajax_widget_member_groups_list() {
+
+	check_ajax_referer('groups_widget_member_groups_list');
+
+	switch ( $_POST['filter'] ) {
+		case 'newest-groups':
+			$type = 'newest';
+		break;
+		case 'recently-active-groups':
+			$type = 'active';
+		break;
+		case 'popular-groups':
+			$type = 'popular';
+		break;
+	}
+
+	if ( bp_has_groups( 'type=' . $type . '&per_page=' . $_POST['max_member_groups'] . '&max=' . $_POST['max_member_groups'] ) ) : ?>
+		<?php echo "0[[SPLIT]]"; ?>
+		<?php while ( bp_groups() ) : bp_the_group(); ?>
+			<li>
+				<div class="item-avatar">
+					<a href="<?php bp_group_permalink() ?>"><?php bp_group_avatar_thumb() ?></a>
+				</div>
+
+				<div class="item">
+					<div class="item-title"><a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_name() ?></a></div>
+					<div class="item-meta">
+						<span class="activity">
+							<?php
+							if ( 'newest-groups' == $_POST['filter'] ) {
+								printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() );
+							} else if ( 'recently-active-groups' == $_POST['filter'] ) {
+								printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() );
+							} else if ( 'popular-groups' == $_POST['filter'] ) {
+								bp_group_member_count();
+							}
+							?>
+						</span>
+					</div>
+				</div>
+			</li>
+		<?php endwhile; ?>
+
+		<?php wp_nonce_field( 'groups_widget_member_groups_list', '_wpnonce-member-groups' ); ?>
+		<input type="hidden" name="member_groups_widget_max" id="member_groups_widget_max" value="<?php echo esc_attr( $_POST['max_member_groups'] ); ?>" />
+
+	<?php else: ?>
+
+		<?php echo "-1[[SPLIT]]<li>" . __("No groups matched the current filter.", 'buddypress'); ?>
+
+	<?php endif;
+
+}
+add_action( 'wp_ajax_widget_member_groups_list',        'groups_ajax_widget_member_groups_list' );
+add_action( 'wp_ajax_nopriv_widget_member_groups_list', 'groups_ajax_widget_member_groups_list' );
\ No newline at end of file
