Skip to:
Content

BuddyPress.org


Ignore:
Timestamp:
07/15/2009 12:47:21 AM (17 years ago)
Author:
apeatling
Message:

Updating BuddyPress widgets to support new widget API.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/1.0/bp-groups/bp-groups-widgets.php

    r1520 r1602  
    33/* Register widgets for groups component */
    44function groups_register_widgets() {
    5         global $current_blog;
    6        
    7         /* Site welcome widget */
    8         wp_register_sidebar_widget( 'buddypress-groups', __( 'Groups', 'buddypress' ), 'groups_widget_groups_list' );
    9         wp_register_widget_control( 'buddypress-groups', __( 'Groups', 'buddypress' ), 'groups_widget_groups_list_control' );
    10        
    11         /* Include the javascript needed for activated widgets only */
    12         if ( is_active_widget( 'groups_widget_groups_list' ) ) {
    13                 wp_enqueue_script( 'groups_widget_groups_list-js', BP_PLUGIN_URL . '/bp-groups/js/widget-groups.js', array('jquery', 'jquery-livequery-pack') );               
    14                 wp_enqueue_style( 'groups_widget_members-css', BP_PLUGIN_URL . '/bp-groups/css/widget-groups.css' );           
    15         }
     5        add_action('widgets_init', create_function('', 'return register_widget("BP_Groups_Widget");') );       
    166}
    177add_action( 'plugins_loaded', 'groups_register_widgets' );
     
    2010/*** GROUPS WIDGET *****************/
    2111
    22 function groups_widget_groups_list($args) {
    23         global $current_blog, $bp;
    24        
    25     extract($args);
    26         $options = get_blog_option( $current_blog->blog_id, 'groups_widget_groups_list' );
    27 ?>
    28         <?php echo $before_widget; ?>
    29         <?php echo $before_title
    30                 . $widget_name
    31                 . $after_title; ?>
    32 
    33         <?php
    34         if ( empty( $options['max_groups'] ) || !$options['max_groups'] )
    35                 $options['max_groups'] = 5;
    36                
    37         if ( !$groups = wp_cache_get( 'popular_groups', 'bp' ) ) {
    38                 $groups = groups_get_popular( $options['max_groups'], 1 );
    39                 wp_cache_set( 'popular_groups', $groups, 'bp' );
    40         }
    41         ?>
    42 
    43         <?php if ( $groups['groups'] ) : ?>
    44                 <div class="item-options" id="groups-list-options">
    45                         <img id="ajax-loader-groups" src="<?php echo $bp->groups->image_base ?>/ajax-loader.gif" height="7" alt="<?php _e( 'Loading', 'buddypress' ) ?>" style="display: none;" />
    46                         <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="newest-groups"><?php _e("Newest", 'buddypress') ?></a> |
    47                         <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="recently-active-groups"><?php _e("Active", 'buddypress') ?></a> |
    48                         <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="popular-groups" class="selected"><?php _e("Popular", 'buddypress') ?></a>
    49                 </div>
    50                 <ul id="groups-list" class="item-list">
    51                         <?php foreach ( $groups['groups'] as $group_id ) : ?>
    52                                 <?php
    53                                 if ( !$group = wp_cache_get( 'groups_group_nouserdata_' . $group_id->group_id, 'bp' ) ) {
    54                                         $group = new BP_Groups_Group( $group_id->group_id, false, false );
    55                                         wp_cache_set( 'groups_group_nouserdata_' . $group_id->group_id, $group, 'bp' );
    56                                 }       
    57                                 ?>
    58                                 <li>
    59                                         <div class="item-avatar">
    60                                                 <a href="<?php echo bp_get_group_permalink( $group ) ?>" title="<?php echo bp_get_group_name( $group ) ?>"><?php echo bp_get_group_avatar_thumb( $group ); ?></a>
    61                                         </div>
    62 
    63                                         <div class="item">
    64                                                 <div class="item-title"><a href="<?php echo bp_get_group_permalink( $group ) ?>" title="<?php echo bp_get_group_name( $group ) ?>"><?php echo bp_get_group_name( $group ) ?></a></div>
    65                                                 <div class="item-meta">
    66                                                 <span class="activity">
    67                                                         <?php
    68                                                         if ( 1 == $group->total_member_count )
    69                                                                 echo $group->total_member_count . __(' member', 'buddypress');
    70                                                         else
    71                                                                 echo $group->total_member_count . __(' members', 'buddypress');
    72                                                         ?>
    73                                                 </span></div>
    74                                         </div>
    75                                 </li>
    76                                 <?php $counter++; ?>   
    77                         <?php endforeach; ?>
    78                 </ul>
    79                
    80                 <?php
    81                 if ( function_exists('wp_nonce_field') )
    82                         wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' );
    83                 ?>
    84                
    85                 <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo attribute_escape( $options['max_groups'] ); ?>" />
    86                
    87         <?php else: ?>
    88                 <div class="widget-error">
    89                         <?php _e('There are no groups to display.', 'buddypress') ?>
    90                 </div>
    91         <?php endif; ?>
    92        
    93         <?php echo $after_widget; ?>
    94 <?php
    95 }
    96 
    97 function groups_widget_groups_list_control() {
    98         global $current_blog;
    99        
    100         $options = $newoptions = get_blog_option( $current_blog->blog_id, 'groups_widget_groups_list');
    101 
    102         if ( $_POST['groups-widget-groups-list-submit'] ) {
    103                 $newoptions['max_groups'] = strip_tags( stripslashes( $_POST['groups-widget-groups-list-max'] ) );
    104         }
    105        
    106         if ( $options != $newoptions ) {
    107                 $options = $newoptions;
    108                 update_blog_option( $current_blog->blog_id, 'groups_widget_groups_list', $options );
     12class BP_Groups_Widget extends WP_Widget {
     13        function bp_groups_widget() {
     14                parent::WP_Widget( false, $name = 'Groups' );
     15                wp_enqueue_script( 'groups_widget_groups_list-js', BP_PLUGIN_URL . '/bp-groups/js/widget-groups.js', array('jquery', 'jquery-livequery-pack') );               
     16                wp_enqueue_style( 'groups_widget_members-css', BP_PLUGIN_URL . '/bp-groups/css/widget-groups.css' );           
    10917        }
    11018
     19        function widget($args, $instance) {
     20                global $bp;
     21               
     22            extract( $args );
     23               
     24                echo $before_widget;
     25                echo $before_title
     26                   . $widget_name
     27                   . $after_title; ?>
     28       
     29                <?php
     30                if ( empty( $instance['max_groups'] ) || !$instance['max_groups'] )
     31                        $instance['max_groups'] = 5; ?>
     32               
     33                <?php if ( bp_has_site_groups( 'type=popular&max=' . $instance['max_groups'] ) ) : ?>
     34                        <div class="item-options" id="groups-list-options">
     35                                <img id="ajax-loader-groups" src="<?php echo $bp->groups->image_base ?>/ajax-loader.gif" height="7" alt="<?php _e( 'Loading', 'buddypress' ) ?>" style="display: none;" />
     36                                <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="newest-groups"><?php _e("Newest", 'buddypress') ?></a> |
     37                                <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="recently-active-groups"><?php _e("Active", 'buddypress') ?></a> |
     38                                <a href="<?php echo site_url() . '/' . $bp->groups->slug ?>" id="popular-groups" class="selected"><?php _e("Popular", 'buddypress') ?></a>
     39                        </div>
     40                       
     41                        <ul id="groups-list" class="item-list">
     42                                <?php while ( bp_site_groups() ) : bp_the_site_group(); ?>
     43                                        <li>
     44                                                <div class="item-avatar">
     45                                                        <a href="<?php bp_the_site_group_link() ?>"><?php bp_the_site_group_avatar_thumb() ?></a>
     46                                                </div>
     47
     48                                                <div class="item">
     49                                                        <div class="item-title"><a href="<?php bp_the_site_group_link() ?>" title="<?php bp_the_site_group_name() ?>"><?php bp_the_site_group_name() ?></a></div>
     50                                                        <div class="item-meta"><span class="activity"><?php bp_the_site_group_member_count() ?></span></div>
     51                                                </div>
     52                                        </li>
     53
     54                                <?php endwhile; ?>
     55                        </ul>           
     56                        <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?>
     57                        <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo attribute_escape( $instance['max_groups'] ); ?>" />
     58                       
     59                <?php else: ?>
     60
     61                        <div class="widget-error">
     62                                <?php _e('There are no groups to display.', 'buddypress') ?>
     63                        </div>
     64
     65                <?php endif; ?>
     66                       
     67                <?php echo $after_widget; ?>
     68        <?php
     69        }
     70
     71        function update( $new_instance, $old_instance ) {
     72                $instance = $old_instance;
     73                $instance['max_groups'] = strip_tags( $new_instance['max_groups'] );
     74
     75                return $instance;
     76        }
     77
     78        function form( $instance ) {
     79                $instance = wp_parse_args( (array) $instance, array( 'max_groups' => 5 ) );
     80                $max_groups = strip_tags( $instance['max_groups'] );
     81                ?>
     82
     83                <p><label for="bp-groups-widget-groups-max"><?php _e('Max groups to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_groups' ); ?>" name="<?php echo $this->get_field_name( 'max_groups' ); ?>" type="text" value="<?php echo attribute_escape( $max_groups ); ?>" style="width: 30%" /></label></p>
     84        <?php
     85        }
     86}
    11187?>
    112                 <p><label for="groups-widget-groups-list-max"><?php _e('Maximum number of groups to show:', 'buddypress'); ?><br /> <input class="widefat" id="groups-widget-groups-list-max" name="groups-widget-groups-list-max" type="text" value="<?php echo attribute_escape( $options['max_groups'] ); ?>" style="width: 30%" /></label></p>
    113                 <input type="hidden" id="groups-widget-groups-list-submit" name="groups-widget-groups-list-submit" value="1" />
    114 <?php
    115 }
Note: See TracChangeset for help on using the changeset viewer.