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-core/bp-core-widgets.php

    r1520 r1602  
    33/* Register widgets for the core component */
    44function bp_core_register_widgets() {
    5         global $current_blog;
    6        
    7         /* Site welcome widget */
    8         wp_register_sidebar_widget( 'buddypress-welcome', __( 'Welcome', 'buddypress' ), 'bp_core_widget_welcome' );
    9         wp_register_widget_control( 'buddypress-welcome', __( 'Welcome', 'buddypress' ), 'bp_core_widget_welcome_control' );
    10        
    11         /* Site members widget */
    12         wp_register_sidebar_widget( 'buddypress-members', __( 'Members', 'buddypress' ), 'bp_core_widget_members' );
    13         wp_register_widget_control( 'buddypress-members', __( 'Members', 'buddypress' ), 'bp_core_widget_members_control' );
    14        
    15         /* Include the javascript needed for activated widgets only */
    16         if ( is_active_widget( 'bp_core_widget_members' ) ) {
     5        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Welcome_Widget");') );
     6        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Members_Widget");') );
     7        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Whos_Online_Widget");') );
     8        add_action('widgets_init', create_function('', 'return register_widget("BP_Core_Recently_Active_Widget");') );
     9}
     10add_action( 'plugins_loaded', 'bp_core_register_widgets' );
     11
     12/*** WELCOME WIDGET *****************/
     13
     14class BP_Core_Welcome_Widget extends WP_Widget {
     15        function bp_core_welcome_widget() {
     16                parent::WP_Widget( false, $name = 'Welcome' );
     17        }
     18
     19        function widget($args, $instance) {
     20                extract( $args );
     21        ?>
     22                <?php echo $before_widget; ?>
     23                <?php echo $before_title
     24                        . $instance['title']
     25                        . $after_title; ?>
     26
     27                <?php if ( $instance['title'] ) : ?><h3><?php echo attribute_escape( $instance['title'] ) ?></h3><?php endif; ?>
     28                <?php if ( $instance['text'] ) : ?><p><?php echo attribute_escape( $instance['text'] ) ?></p><?php endif; ?>
     29
     30                <?php if ( !is_user_logged_in() ) { ?>
     31                <div class="create-account"><div class="visit generic-button"><a href="<?php bp_signup_page() ?>" title="<?php _e('Create Account', 'buddypress') ?>"><?php _e('Create Account', 'buddypress') ?></a></div></div>
     32                <?php } ?>
     33       
     34                <?php echo $after_widget; ?>
     35        <?php
     36        }
     37
     38        function update( $new_instance, $old_instance ) {
     39                $instance = $old_instance;
     40                $instance['title'] = strip_tags( $new_instance['title'] );
     41                $instance['text'] = strip_tags( wp_filter_post_kses( $new_instance['text'] ) );
     42
     43                return $instance;
     44        }
     45
     46        function form( $instance ) {
     47                $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) );
     48                $title = strip_tags( $instance['title'] );
     49                $text = strip_tags( $instance['text'] );
     50                ?>
     51                        <p><label for="bp-widget-welcome-title"><?php _e('Title:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo attribute_escape( $title ); ?>" /></label></p>
     52                        <p>
     53                                <label for="bp-widget-welcome-text"><?php _e( 'Welcome Text:' , 'buddypress'); ?>
     54                                        <textarea id="<?php echo $this->get_field_id( 'text' ); ?>" name="<?php echo $this->get_field_name( 'text' ); ?>" class="widefat" style="height: 100px"><?php echo attribute_escape( $text ); ?></textarea>
     55                                </label>
     56                        </p>
     57        <?php
     58        }
     59}
     60
     61/*** MEMBERS WIDGET *****************/
     62
     63class BP_Core_Members_Widget extends WP_Widget {
     64        function bp_core_members_widget() {
     65                parent::WP_Widget( false, $name = 'Members' );
    1766                wp_enqueue_script( 'bp_core_widget_members-js', BP_PLUGIN_URL . '/bp-core/js/widget-members.js', array('jquery', 'jquery-livequery-pack') );           
    1867                wp_enqueue_style( 'bp_core_widget_members-css', BP_PLUGIN_URL . '/bp-core/css/widget-members.css' );
    1968        }
    20        
    21         wp_register_sidebar_widget( 'buddypress-whosonline', __( "Who's Online", 'buddypress' ), 'bp_core_widget_whos_online' );
    22         wp_register_widget_control( 'buddypress-whosonline', __( "Who's Online", 'buddypress' ), 'bp_core_widget_whos_online_control' );       
    23 
    24 }
    25 add_action( 'plugins_loaded', 'bp_core_register_widgets' );
    26 
    27 
    28 /*** WELCOME WIDGET *****************/
    29 
    30 function bp_core_widget_welcome($args) {
    31         global $current_blog;
    32        
    33     extract($args);
    34         $options = get_blog_option( $current_blog->blog_id, 'bp_core_widget_welcome' );
     69
     70        function widget($args, $instance) {
     71                global $bp;
     72               
     73            extract( $args );
     74               
     75                echo $before_widget;
     76                echo $before_title
     77                   . $widget_name
     78                   . $after_title; ?>
     79       
     80                <?php
     81                if ( empty( $instance['max_members'] ) || !$instance['max_members'] )
     82                        $instance['max_members'] = 5; ?>
     83               
     84                <?php if ( bp_has_site_members( 'type=newest&max=' . $instance['max_members'] ) ) : ?>
     85                        <div class="item-options" id="members-list-options">
     86                                <img id="ajax-loader-members" src="<?php echo $bp->core->image_base ?>/ajax-loader.gif" height="7" alt="<?php _e( 'Loading', 'buddypress' ) ?>" style="display: none;" />
     87                                <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="newest-members" class="selected"><?php _e( 'Newest', 'buddypress' ) ?></a> |
     88                                <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="recently-active-members"><?php _e( 'Active', 'buddypress' ) ?></a> |
     89                                <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="popular-members"><?php _e( 'Popular', 'buddypress' ) ?></a>
     90                        </div>
     91                       
     92                        <ul id="members-list" class="item-list">
     93                                <?php while ( bp_site_members() ) : bp_the_site_member(); ?>
     94                                        <li class="vcard">
     95                                                <div class="item-avatar">
     96                                                        <a href="<?php bp_the_site_member_link() ?>"><?php bp_the_site_member_avatar() ?></a>
     97                                                </div>
     98
     99                                                <div class="item">
     100                                                        <div class="item-title fn"><a href="<?php bp_the_site_member_name() ?>" title="<?php bp_the_site_member_name() ?>"><?php bp_the_site_member_name() ?></a></div>
     101                                                        <div class="item-meta"><span class="activity"><?php echo bp_core_get_last_activity( bp_get_the_site_member_registered(), __( 'registered %s ago', 'buddypress' ) ) ?></span></div>
     102                                                </div>
     103                                        </li>
     104
     105                                <?php endwhile; ?>
     106                        </ul>           
     107                        <?php wp_nonce_field( 'bp_core_widget_members', '_wpnonce-members' ); ?>
     108                        <input type="hidden" name="members_widget_max" id="members_widget_max" value="<?php echo attribute_escape( $instance['max_members'] ); ?>" />
     109                       
     110                <?php else: ?>
     111
     112                        <div class="widget-error">
     113                                <?php _e('No one has signed up yet!', 'buddypress') ?>
     114                        </div>
     115
     116                <?php endif; ?>
     117                       
     118                <?php echo $after_widget; ?>
     119        <?php
     120        }
     121
     122        function update( $new_instance, $old_instance ) {
     123                $instance = $old_instance;
     124                $instance['max_members'] = strip_tags( $new_instance['max_members'] );
     125
     126                return $instance;
     127        }
     128
     129        function form( $instance ) {
     130                $instance = wp_parse_args( (array) $instance, array( 'max_members' => 5 ) );
     131                $max_members = strip_tags( $instance['max_members'] );
     132                ?>
     133
     134                <p><label for="bp-core-widget-members-max"><?php _e('Max Members to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="text" value="<?php echo attribute_escape( $max_members ); ?>" style="width: 30%" /></label></p>
     135        <?php
     136        }
     137}
     138
     139/*** WHO'S ONLINE WIDGET *****************/
     140
     141class BP_Core_Whos_Online_Widget extends WP_Widget {
     142        function bp_core_whos_online_widget() {
     143                parent::WP_Widget( false, $name = 'Who\'s Online Avatars' );
     144        }
     145
     146        function widget($args, $instance) {
     147                global $bp;
     148               
     149            extract( $args );
     150               
     151                echo $before_widget;
     152                echo $before_title
     153                   . $widget_name
     154                   . $after_title; ?>
     155       
     156                <?php
     157                if ( empty( $instance['max_members'] ) || !$instance['max_members'] )
     158                        $instance['max_members'] = 5; ?>
     159               
     160                <?php if ( bp_has_site_members( 'type=online&max=' . $instance['max_members'] ) ) : ?>                 
     161                        <div class="avatar-block">
     162                                <?php while ( bp_site_members() ) : bp_the_site_member(); ?>
     163                                        <div class="item-avatar">
     164                                                <a href="<?php bp_the_site_member_link() ?>" title="<?php bp_the_site_member_name() ?>"><?php bp_the_site_member_avatar() ?></a>
     165                                        </div>
     166                                <?php endwhile; ?>
     167                        </div>                                 
     168                <?php else: ?>
     169
     170                        <div class="widget-error">
     171                                <?php _e( 'There are no users currently online', 'buddypress' ) ?>
     172                        </div>
     173
     174                <?php endif; ?>
     175                       
     176                <?php echo $after_widget; ?>
     177        <?php
     178        }
     179
     180        function update( $new_instance, $old_instance ) {
     181                $instance = $old_instance;
     182                $instance['max_members'] = strip_tags( $new_instance['max_members'] );
     183
     184                return $instance;
     185        }
     186
     187        function form( $instance ) {
     188                $instance = wp_parse_args( (array) $instance, array( 'max_members' => 5 ) );
     189                $max_members = strip_tags( $instance['max_members'] );
     190                ?>
     191
     192                <p><label for="bp-core-widget-members-max"><?php _e('Max Members to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="text" value="<?php echo attribute_escape( $max_members ); ?>" style="width: 30%" /></label></p>
     193        <?php
     194        }
     195}
     196
     197/*** RECENTLY ACTIVE WIDGET *****************/
     198
     199class BP_Core_Recently_Active_Widget extends WP_Widget {
     200        function bp_core_recently_active_widget() {
     201                parent::WP_Widget( false, $name = 'Recently Active Member Avatars' );
     202        }
     203
     204        function widget($args, $instance) {
     205                global $bp;
     206               
     207            extract( $args );
     208               
     209                echo $before_widget;
     210                echo $before_title
     211                   . $widget_name
     212                   . $after_title; ?>
     213       
     214                <?php
     215                if ( empty( $instance['max_members'] ) || !$instance['max_members'] )
     216                        $instance['max_members'] = 5; ?>
     217               
     218                <?php if ( bp_has_site_members( 'type=active&max=' . $instance['max_members'] ) ) : ?>                 
     219                        <div class="avatar-block">
     220                                <?php while ( bp_site_members() ) : bp_the_site_member(); ?>
     221                                        <div class="item-avatar">
     222                                                <a href="<?php bp_the_site_member_link() ?>" title="<?php bp_the_site_member_name() ?>"><?php bp_the_site_member_avatar() ?></a>
     223                                        </div>
     224                                <?php endwhile; ?>
     225                        </div>                                 
     226                <?php else: ?>
     227
     228                        <div class="widget-error">
     229                                <?php _e( 'There are no recently active members', 'buddypress' ) ?>
     230                        </div>
     231
     232                <?php endif; ?>
     233                       
     234                <?php echo $after_widget; ?>
     235        <?php
     236        }
     237
     238        function update( $new_instance, $old_instance ) {
     239                $instance = $old_instance;
     240                $instance['max_members'] = strip_tags( $new_instance['max_members'] );
     241
     242                return $instance;
     243        }
     244
     245        function form( $instance ) {
     246                $instance = wp_parse_args( (array) $instance, array( 'max_members' => 5 ) );
     247                $max_members = strip_tags( $instance['max_members'] );
     248                ?>
     249
     250                <p><label for="bp-core-widget-members-max"><?php _e('Max Members to show:', 'buddypress'); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'max_members' ); ?>" name="<?php echo $this->get_field_name( 'max_members' ); ?>" type="text" value="<?php echo attribute_escape( $max_members ); ?>" style="width: 30%" /></label></p>
     251        <?php
     252        }
     253}
     254
    35255?>
    36         <?php echo $before_widget; ?>
    37         <?php echo $before_title
    38                 . $widget_name
    39                 . $after_title; ?>
    40 
    41         <?php if ( $options['title'] ) : ?><h3><?php echo attribute_escape( $options['title'] ) ?></h3><?php endif; ?>
    42         <?php if ( $options['text'] ) : ?><p><?php echo attribute_escape( $options['text'] ) ?></p><?php endif; ?>
    43 
    44         <?php if ( !is_user_logged_in() ) { ?>
    45         <div class="create-account"><div class="visit generic-button"><a href="<?php bp_signup_page() ?>" title="<?php _e('Create Account', 'buddypress') ?>"><?php _e('Create Account', 'buddypress') ?></a></div></div>
    46         <?php } ?>
    47        
    48         <?php echo $after_widget; ?>
    49 <?php
    50 }
    51 
    52 function bp_core_widget_welcome_control() {
    53         global $current_blog;
    54        
    55         $options = $newoptions = get_blog_option( $current_blog->blog_id, 'bp_core_widget_welcome' );
    56 
    57         if ( $_POST['bp-widget-welcome-submit'] ) {
    58                 $newoptions['title'] = strip_tags( stripslashes( $_POST['bp-widget-welcome-title'] ) );
    59                 $newoptions['text'] = stripslashes( wp_filter_post_kses( $_POST['bp-widget-welcome-text'] ) );
    60         }
    61        
    62         if ( $options != $newoptions ) {
    63                 $options = $newoptions;
    64                 update_blog_option( $current_blog->blog_id, 'bp_core_widget_welcome', $options );
    65         }
    66 
    67 ?>
    68                 <p><label for="bp-widget-welcome-title"><?php _e('Title:', 'buddypress'); ?> <input class="widefat" id="bp-widget-welcome-title" name="bp-widget-welcome-title" type="text" value="<?php echo attribute_escape( $options['title'] ); ?>" /></label></p>
    69                 <p>
    70                         <label for="bp-widget-welcome-text"><?php _e( 'Welcome Text:' , 'buddypress'); ?>
    71                                 <textarea id="bp-widget-welcome-text" name="bp-widget-welcome-text" class="widefat" style="height: 100px"><?php echo htmlspecialchars( $options['text'] ); ?></textarea>
    72                         </label>
    73                 </p>
    74                 <input type="hidden" id="bp-widget-welcome-submit" name="bp-widget-welcome-submit" value="1" />
    75 <?php
    76 }
    77 
    78 /*** MEMBERS WIDGET *****************/
    79 
    80 function bp_core_widget_members($args) {
    81         global $current_blog, $bp;
    82        
    83     extract($args);
    84         $options = get_blog_option( $current_blog->blog_id, 'bp_core_widget_members' );
    85 ?>
    86         <?php echo $before_widget; ?>
    87         <?php echo $before_title
    88                 . $widget_name
    89                 . $after_title; ?>
    90        
    91         <?php
    92         if ( empty( $options['max_members'] ) || !$options['max_members'] )
    93                 $options['max_members'] = 5;
    94                
    95         if ( !$users = wp_cache_get( 'newest_users', 'bp' ) ) {
    96                 $users = BP_Core_User::get_newest_users( $options['max_members'] );
    97                 wp_cache_set( 'newest_users', $users, 'bp' );
    98         }
    99         ?>
    100        
    101         <?php if ( $users['users'] ) : ?>
    102                 <div class="item-options" id="members-list-options">
    103                         <img id="ajax-loader-members" src="<?php echo $bp->core->image_base ?>/ajax-loader.gif" height="7" alt="<?php _e( 'Loading', 'buddypress' ) ?>" style="display: none;" />
    104                         <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="newest-members" class="selected"><?php _e( 'Newest', 'buddypress' ) ?></a> |
    105                         <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="recently-active-members"><?php _e( 'Active', 'buddypress' ) ?></a> |
    106                         <a href="<?php echo site_url() . '/' . BP_MEMBERS_SLUG ?>" id="popular-members"><?php _e( 'Popular', 'buddypress' ) ?></a>
    107                 </div>
    108                 <ul id="members-list" class="item-list">
    109                         <?php foreach ( (array) $users['users'] as $user ) : ?>
    110                                 <li class="vcard">
    111                                         <div class="item-avatar">
    112                                                 <a href="<?php echo bp_core_get_userlink( $user->user_id, false, true ) ?>"><?php echo bp_core_get_avatar( $user->user_id, 1 ) ?></a>
    113                                         </div>
    114 
    115                                         <div class="item">
    116                                                 <div class="item-title fn"><?php echo bp_core_get_userlink( $user->user_id ) ?></div>
    117                                                 <div class="item-meta"><span class="activity"><?php echo bp_core_get_last_activity( $user->user_registered, __( 'registered %s ago', 'buddypress' ) ) ?></span></div>
    118                                         </div>
    119                                 </li>
    120                                 <?php $counter++; ?>   
    121                         <?php endforeach; ?>
    122                 </ul>
    123                
    124                 <?php
    125                 if ( function_exists('wp_nonce_field') )
    126                         wp_nonce_field( 'bp_core_widget_members', '_wpnonce-members' );
    127                 ?>
    128                
    129                 <input type="hidden" name="members_widget_max" id="members_widget_max" value="<?php echo attribute_escape( $options['max_members'] ); ?>" />
    130                
    131         <?php else: ?>
    132                 <div class="widget-error">
    133                         <?php _e('No one has signed up yet!', 'buddypress') ?>
    134                 </div>
    135         <?php endif; ?>
    136        
    137         <?php echo $after_widget; ?>
    138 <?php
    139 }
    140 
    141 function bp_core_widget_members_control() {
    142         global $current_blog;
    143        
    144         $options = $newoptions = get_blog_option( $current_blog->blog_id, 'bp_core_widget_members');
    145 
    146         if ( $_POST['bp-core-widget-members-submit'] ) {
    147                 $newoptions['max_members'] = strip_tags( stripslashes( $_POST['bp-core-widget-members-max'] ) );
    148         }
    149        
    150         if ( $options != $newoptions ) {
    151                 $options = $newoptions;
    152                 update_blog_option( $current_blog->blog_id, 'bp_core_widget_members', $options );
    153         }
    154 
    155         $max_members = attribute_escape( $options['max_members'] );
    156 ?>
    157                 <p><label for="bp-core-widget-members-max"><?php _e('Max Members to show:', 'buddypress'); ?> <input class="widefat" id="bp-core-widget-members-max" name="bp-core-widget-members-max" type="text" value="<?php echo attribute_escape( $options['max_members'] ); ?>" style="width: 30%" /></label></p>
    158                 <input type="hidden" id="bp-core-widget-members-submit" name="bp-core-widget-members-submit" value="1" />
    159 <?php
    160 }
    161 
    162 /*** WHO'S ONLINE WIDGET *****************/
    163 
    164 function bp_core_widget_whos_online($args) {
    165         global $current_blog;
    166        
    167     extract($args);
    168         $options = get_blog_option( $current_blog->blog_id, 'bp_core_widget_whos_online' );
    169 ?>
    170         <?php echo $before_widget; ?>
    171         <?php echo $before_title
    172                 . $widget_name
    173                 . $after_title; ?>
    174 
    175         <?php
    176         if ( empty( $options['max_members'] ) || !$options['max_members'] )
    177                 $options['max_members'] = 5;
    178                
    179         if ( !$users = wp_cache_get( 'online_users', 'bp' ) ) {
    180                 $users = BP_Core_User::get_online_users( $options['max_members'] );
    181                 wp_cache_set( 'online_users', $users, 'bp' );
    182         }
    183         ?>
    184 
    185         <?php $users = BP_Core_User::get_online_users($options['max_members']) ?>
    186 
    187         <?php if ( $users['users'] ) : ?>
    188                 <div class="avatar-block">
    189                 <?php foreach ( (array) $users['users'] as $user ) : ?>
    190                         <div class="item-avatar">
    191                                 <a href="<?php echo bp_core_get_userurl($user->user_id) ?>" title="<?php echo bp_core_get_user_displayname( $user->user_id ) ?>"><?php echo bp_core_get_avatar( $user->user_id, 1 ) ?></a>
    192                         </div>
    193                 <?php endforeach; ?>
    194                 </div>
    195                        
    196                 <?php
    197                 if ( function_exists('wp_nonce_field') )
    198                         wp_nonce_field( 'bp_core_widget_members', '_wpnonce-members' );
    199                 ?>
    200 
    201                 <input type="hidden" name="bp_core_widget_members_max" id="bp_core_widget_members_max" value="<?php echo attribute_escape( $options['max_members'] ); ?>" />
    202 
    203         <?php else: ?>
    204                 <div class="widget-error">
    205                         <?php _e('There are no users currently online.', 'buddypress') ?>
    206                 </div>
    207         <?php endif; ?>
    208 
    209         <?php echo $after_widget; ?>
    210        
    211         <div class="clear" style="margin-bottom: 25px"></div>
    212        
    213 <?php
    214 }
    215 
    216 function bp_core_widget_whos_online_control() {
    217         global $current_blog;
    218        
    219         $options = $newoptions = get_blog_option( $current_blog->blog_id, 'bp_core_widget_whos_online' );
    220 
    221         if ( $_POST['bp-widget-whos-online-submit'] ) {
    222                 $newoptions['max_members'] = strip_tags( stripslashes( $_POST['bp-widget-whos-online-max-members'] ) );
    223         }
    224        
    225         if ( $options != $newoptions ) {
    226                 $options = $newoptions;
    227                 update_blog_option( $current_blog->blog_id, 'bp_core_widget_whos_online', $options );
    228         }
    229 
    230 ?>
    231                 <p><label for="bp-widget-whos-online-max-members"><?php _e('Maximum number of members to show:', 'buddypress'); ?><br /><input class="widefat" id="bp-widget-whos-online-max-members" name="bp-widget-whos-online-max-members" type="text" value="<?php echo attribute_escape( $options['max_members'] ); ?>" style="width: 30%" /></label></p>
    232                 <input type="hidden" id="bp-widget-whos-online-submit" name="bp-widget-whos-online-submit" value="1" />
    233 <?php
    234 }
Note: See TracChangeset for help on using the changeset viewer.