Index: bp-core-widgets.php
===================================================================
--- bp-core-widgets.php	(revision 7287)
+++ bp-core-widgets.php	(working copy)
@@ -7,6 +7,9 @@
 	add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Members_Widget");') );
 	add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Whos_Online_Widget");') );
 	add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Recently_Active_Widget");') );
+	
+	if( bp_is_active('friends') )
+		add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Friends_Widget");') );
 }
 add_action( 'bp_register_widgets', 'bp_core_register_widgets' );
 
@@ -143,6 +146,144 @@
 	}
 }
 
+/*** MEMBER FRIENDS WIDGET *****************/
+
+class BP_Core_Friends_Widget extends WP_Widget {
+
+	function __construct() {
+		$widget_ops = array(
+			'description' => __( 'A dynamic list of recently active, popular, and newest Friends of loggedin/displayed member', 'buddypress' ),
+			'classname' => 'widget_bp_core_friends_widget buddypress widget',
+		);
+		parent::__construct( false, $name = _x( '(BuddyPress) Friends', '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( 'bp_core_widget_friends-js', BP_PLUGIN_URL . "bp-core/js/widget-friends{$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_friends_slug() );
+			$instance['title'] = sprintf( __( '%s friends', '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_friends_slug() );
+			$instance['title'] = __( 'My friends', 'buddypress' );
+
+		} else {
+			return;
+		}
+
+		if ( !$instance['friend_default'] )
+			$instance['friend_default'] = 'active';
+
+		$title = apply_filters( 'widget_title', $instance['title'] );
+
+		echo $before_widget;
+
+		$title = $instance['link_title'] ? '<a href="' . $link . '">' . $title . '</a>' : $title;
+
+		echo $before_title
+		   . $title
+		   . $after_title; ?>
+
+		<?php if ( bp_has_members( 'user_id=' . $user_id . '&type=' . $instance['friend_default'] . '&max=' . $instance['max_friends'] . '&populate_extras=1' ) ) : ?>
+			<div class="item-options" id="friends-list-options">
+				<a href="<?php bp_members_directory_permalink(); ?>" id="newest-friends" <?php if ( $instance['friend_default'] == 'newest' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></a>
+				| <a href="<?php bp_members_directory_permalink(); ?>" id="recently-active-friends" <?php if ( $instance['friend_default'] == 'active' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></a>
+				| <a href="<?php bp_members_directory_permalink(); ?>" id="popular-friends" <?php if ( $instance['friend_default'] == 'popular' ) : ?>class="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></a>
+			</div>
+
+			<ul id="friends-list" class="item-list">
+				<?php while ( bp_members() ) : bp_the_member(); ?>
+					<li class="vcard">
+						<div class="item-avatar">
+							<a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_avatar() ?></a>
+						</div>
+
+						<div class="item">
+							<div class="item-title fn"><a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_name() ?></a></div>
+							<div class="item-meta">
+								<span class="activity">
+								<?php
+									if ( 'newest' == $instance['friend_default'] )
+										bp_member_registered();
+									if ( 'active' == $instance['friend_default'] )
+										bp_member_last_active();
+									if ( 'popular' == $instance['friend_default'] )
+										bp_member_total_friend_count();
+								?>
+								</span>
+							</div>
+						</div>
+					</li>
+
+				<?php endwhile; ?>
+			</ul>
+			<?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?>
+			<input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo esc_attr( $instance['max_friends'] ); ?>" />
+
+		<?php else: ?>
+
+			<div class="widget-error">
+				<?php _e('Sorry, no members were found.', 'buddypress') ?>
+			</div>
+
+		<?php endif; ?>
+
+		<?php echo $after_widget; ?>
+	<?php
+	}
+
+	function update( $new_instance, $old_instance ) {
+		$instance = $old_instance;
+
+		$instance['max_friends']    = strip_tags( $new_instance['max_friends'] );
+		$instance['friend_default'] = strip_tags( $new_instance['friend_default'] );
+		$instance['link_title']	    = (bool)$new_instance['link_title'];
+
+		return $instance;
+	}
+
+	function form( $instance ) {
+		$defaults = array(
+			'max_friends' 	 => 5,
+			'friend_default' => 'active',
+			'link_title' 	 => false
+		);
+		$instance = wp_parse_args( (array) $instance, $defaults );
+
+		$max_friends 	= strip_tags( $instance['max_friends'] );
+		$friend_default = strip_tags( $instance['friend_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 Members directory', 'buddypress' ) ?></label></p>
+
+		<p><label for="bp-core-widget-friends-max"><?php _e('Max friends to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_friends' ); ?>" name="<?php echo $this->get_field_name( 'max_friends' ); ?>" type="text" value="<?php echo esc_attr( $max_friends ); ?>" style="width: 30%" /></label></p>
+
+		<p>
+			<label for="bp-core-widget-friends-default"><?php _e('Default friends to show:', 'buddypress'); ?>
+			<select name="<?php echo $this->get_field_name( 'friend_default' ) ?>">
+				<option value="newest" <?php if ( $friend_default == 'newest' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
+				<option value="active" <?php if ( $friend_default == 'active' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></option>
+				<option value="popular"  <?php if ( $friend_default == 'popular' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
+			</select>
+			</label>
+		</p>
+
+	<?php
+	}
+}
+
 /*** WHO'S ONLINE WIDGET *****************/
 
 class BP_Core_Whos_Online_Widget extends WP_Widget {
@@ -334,3 +475,58 @@
 }
 add_action( 'wp_ajax_widget_members', 'bp_core_ajax_widget_members' );
 add_action( 'wp_ajax_nopriv_widget_members', 'bp_core_ajax_widget_members' );
+
+function bp_core_ajax_widget_friends() {
+
+	global $bp;
+	check_ajax_referer( 'bp_core_widget_friends' );
+
+	switch ( $_POST['filter'] ) {
+		case 'newest-friends':
+			$type = 'newest';
+			break;
+
+		case 'recently-active-friends':
+			$type = 'active';
+			break;
+
+		case 'popular-friends':
+			$type = 'popular';
+			break;
+	}
+
+
+	$user_id = $bp->displayed_user->id;
+	if( !isset($user_id) || $user_id == 0 )
+		$user_id = $bp->loggedin_user->id;
+
+	if ( bp_has_members( 'user_id=' . $user_id . '&type=' . $type . '&per_page=' . $_POST['max-friends'] . '&max=' . $_POST['max-friends'] . '&populate_extras=1' ) ) : ?>
+		<?php echo '0[[SPLIT]]'; // return valid result. TODO: remove this. ?>
+		<?php while ( bp_members() ) : bp_the_member(); ?>
+			<li class="vcard">
+				<div class="item-avatar">
+					<a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
+				</div>
+
+				<div class="item">
+					<div class="item-title fn"><a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_name() ?></a></div>
+					<?php if ( 'active' == $type ) : ?>
+						<div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>
+					<?php elseif ( 'newest' == $type ) : ?>
+						<div class="item-meta"><span class="activity"><?php bp_member_registered() ?></span></div>
+					<?php elseif ( bp_is_active( 'friends' ) ) : ?>
+						<div class="item-meta"><span class="activity"><?php bp_member_total_friend_count() ?></span></div>
+					<?php endif; ?>
+				</div>
+			</li>
+		<?php endwhile; ?>
+
+	<?php else: ?>
+		<?php echo "-1[[SPLIT]]<li>"; ?>
+		<?php _e( 'There were no members found, please try another filter.', 'buddypress' ) ?>
+		<?php echo "</li>"; ?>
+	<?php endif;
+}
+add_action( 'wp_ajax_widget_friends', 'bp_core_ajax_widget_friends' );
+add_action( 'wp_ajax_nopriv_widget_friends', 'bp_core_ajax_widget_friends' );
+
