Skip to:
Content

BuddyPress.org

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

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

A patch for friends widget

  • bp-core-widgets.php

     
    77        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Members_Widget");') );
    88        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Whos_Online_Widget");') );
    99        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Recently_Active_Widget");') );
     10       
     11        if( bp_is_active('friends') )
     12                add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Friends_Widget");') );
    1013}
    1114add_action( 'bp_register_widgets', 'bp_core_register_widgets' );
    1215
     
    143146        }
    144147}
    145148
     149/*** MEMBER FRIENDS WIDGET *****************/
     150
     151class BP_Core_Friends_Widget extends WP_Widget {
     152
     153        function __construct() {
     154                $widget_ops = array(
     155                        'description' => __( 'A dynamic list of recently active, popular, and newest Friends of loggedin/displayed member', 'buddypress' ),
     156                        'classname' => 'widget_bp_core_friends_widget buddypress widget',
     157                );
     158                parent::__construct( false, $name = _x( '(BuddyPress) Friends', 'widget name', 'buddypress' ), $widget_ops );
     159
     160                if ( is_active_widget( false, false, $this->id_base ) && !is_admin() && !is_network_admin() ) {
     161                        $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
     162                        wp_enqueue_script( 'bp_core_widget_friends-js', BP_PLUGIN_URL . "bp-core/js/widget-friends{$min}.js", array( 'jquery' ), bp_get_version() );
     163                }
     164        }
     165
     166        function widget( $args, $instance ) {
     167
     168                global $bp;
     169                extract( $args );
     170
     171                if ( bp_displayed_user_domain() ) {
     172                        $user_id = $bp->displayed_user->id;
     173                        $link = str_replace( bp_displayed_user_domain(), bp_displayed_user_domain(), bp_get_friends_slug() );
     174                        $instance['title'] = sprintf( __( '%s friends', 'buddypress' ), bp_get_displayed_user_fullname() );
     175
     176                } elseif ( bp_loggedin_user_domain() ) {
     177                        $user_id = $bp->loggedin_user->id;
     178                        $link = trailingslashit( bp_loggedin_user_domain() . bp_get_friends_slug() );
     179                        $instance['title'] = __( 'My friends', 'buddypress' );
     180
     181                } else {
     182                        return;
     183                }
     184
     185                if ( !$instance['friend_default'] )
     186                        $instance['friend_default'] = 'active';
     187
     188                $title = apply_filters( 'widget_title', $instance['title'] );
     189
     190                echo $before_widget;
     191
     192                $title = $instance['link_title'] ? '<a href="' . $link . '">' . $title . '</a>' : $title;
     193
     194                echo $before_title
     195                   . $title
     196                   . $after_title; ?>
     197
     198                <?php if ( bp_has_members( 'user_id=' . $user_id . '&type=' . $instance['friend_default'] . '&max=' . $instance['max_friends'] . '&populate_extras=1' ) ) : ?>
     199                        <div class="item-options" id="friends-list-options">
     200                                <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>
     201                                | <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>
     202                                | <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>
     203                        </div>
     204
     205                        <ul id="friends-list" class="item-list">
     206                                <?php while ( bp_members() ) : bp_the_member(); ?>
     207                                        <li class="vcard">
     208                                                <div class="item-avatar">
     209                                                        <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_avatar() ?></a>
     210                                                </div>
     211
     212                                                <div class="item">
     213                                                        <div class="item-title fn"><a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_name() ?></a></div>
     214                                                        <div class="item-meta">
     215                                                                <span class="activity">
     216                                                                <?php
     217                                                                        if ( 'newest' == $instance['friend_default'] )
     218                                                                                bp_member_registered();
     219                                                                        if ( 'active' == $instance['friend_default'] )
     220                                                                                bp_member_last_active();
     221                                                                        if ( 'popular' == $instance['friend_default'] )
     222                                                                                bp_member_total_friend_count();
     223                                                                ?>
     224                                                                </span>
     225                                                        </div>
     226                                                </div>
     227                                        </li>
     228
     229                                <?php endwhile; ?>
     230                        </ul>
     231                        <?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?>
     232                        <input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo esc_attr( $instance['max_friends'] ); ?>" />
     233
     234                <?php else: ?>
     235
     236                        <div class="widget-error">
     237                                <?php _e('Sorry, no members were found.', 'buddypress') ?>
     238                        </div>
     239
     240                <?php endif; ?>
     241
     242                <?php echo $after_widget; ?>
     243        <?php
     244        }
     245
     246        function update( $new_instance, $old_instance ) {
     247                $instance = $old_instance;
     248
     249                $instance['max_friends']    = strip_tags( $new_instance['max_friends'] );
     250                $instance['friend_default'] = strip_tags( $new_instance['friend_default'] );
     251                $instance['link_title']     = (bool)$new_instance['link_title'];
     252
     253                return $instance;
     254        }
     255
     256        function form( $instance ) {
     257                $defaults = array(
     258                        'max_friends'    => 5,
     259                        'friend_default' => 'active',
     260                        'link_title'     => false
     261                );
     262                $instance = wp_parse_args( (array) $instance, $defaults );
     263
     264                $max_friends    = strip_tags( $instance['max_friends'] );
     265                $friend_default = strip_tags( $instance['friend_default'] );
     266                $link_title     = (bool)$instance['link_title'];
     267                ?>
     268
     269                <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>
     270
     271                <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>
     272
     273                <p>
     274                        <label for="bp-core-widget-friends-default"><?php _e('Default friends to show:', 'buddypress'); ?>
     275                        <select name="<?php echo $this->get_field_name( 'friend_default' ) ?>">
     276                                <option value="newest" <?php if ( $friend_default == 'newest' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Newest', 'buddypress' ) ?></option>
     277                                <option value="active" <?php if ( $friend_default == 'active' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Active', 'buddypress' ) ?></option>
     278                                <option value="popular"  <?php if ( $friend_default == 'popular' ) : ?>selected="selected"<?php endif; ?>><?php _e( 'Popular', 'buddypress' ) ?></option>
     279                        </select>
     280                        </label>
     281                </p>
     282
     283        <?php
     284        }
     285}
     286
    146287/*** WHO'S ONLINE WIDGET *****************/
    147288
    148289class BP_Core_Whos_Online_Widget extends WP_Widget {
     
    334475}
    335476add_action( 'wp_ajax_widget_members', 'bp_core_ajax_widget_members' );
    336477add_action( 'wp_ajax_nopriv_widget_members', 'bp_core_ajax_widget_members' );
     478
     479function bp_core_ajax_widget_friends() {
     480
     481        global $bp;
     482        check_ajax_referer( 'bp_core_widget_friends' );
     483
     484        switch ( $_POST['filter'] ) {
     485                case 'newest-friends':
     486                        $type = 'newest';
     487                        break;
     488
     489                case 'recently-active-friends':
     490                        $type = 'active';
     491                        break;
     492
     493                case 'popular-friends':
     494                        $type = 'popular';
     495                        break;
     496        }
     497
     498
     499        $user_id = $bp->displayed_user->id;
     500        if( !isset($user_id) || $user_id == 0 )
     501                $user_id = $bp->loggedin_user->id;
     502
     503        if ( bp_has_members( 'user_id=' . $user_id . '&type=' . $type . '&per_page=' . $_POST['max-friends'] . '&max=' . $_POST['max-friends'] . '&populate_extras=1' ) ) : ?>
     504                <?php echo '0[[SPLIT]]'; // return valid result. TODO: remove this. ?>
     505                <?php while ( bp_members() ) : bp_the_member(); ?>
     506                        <li class="vcard">
     507                                <div class="item-avatar">
     508                                        <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
     509                                </div>
     510
     511                                <div class="item">
     512                                        <div class="item-title fn"><a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name() ?>"><?php bp_member_name() ?></a></div>
     513                                        <?php if ( 'active' == $type ) : ?>
     514                                                <div class="item-meta"><span class="activity"><?php bp_member_last_active() ?></span></div>
     515                                        <?php elseif ( 'newest' == $type ) : ?>
     516                                                <div class="item-meta"><span class="activity"><?php bp_member_registered() ?></span></div>
     517                                        <?php elseif ( bp_is_active( 'friends' ) ) : ?>
     518                                                <div class="item-meta"><span class="activity"><?php bp_member_total_friend_count() ?></span></div>
     519                                        <?php endif; ?>
     520                                </div>
     521                        </li>
     522                <?php endwhile; ?>
     523
     524        <?php else: ?>
     525                <?php echo "-1[[SPLIT]]<li>"; ?>
     526                <?php _e( 'There were no members found, please try another filter.', 'buddypress' ) ?>
     527                <?php echo "</li>"; ?>
     528        <?php endif;
     529}
     530add_action( 'wp_ajax_widget_friends', 'bp_core_ajax_widget_friends' );
     531add_action( 'wp_ajax_nopriv_widget_friends', 'bp_core_ajax_widget_friends' );
     532