Guest User

Untitled

a guest
Feb 3rd, 2011
165
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2. /*
  3. Plugin Name: BP Show Friends
  4. Plugin URI: http://imath.owni.fr/2010/12/11/bp-show-friends/
  5. Description: Widget to diplay the friends of the user displayed in BuddyPress Member area
  6. Version: 1.0.1
  7. Requires at least: WordPress 3.0 / BuddyPress 1.2.5.x
  8. Tested up to: WordPress 3.0.4 / BuddyPress 1.2.7
  9. License: GNU/GPL 2
  10. Author: imath
  11. Author URI: http://imath.owni.fr/
  12. Network: true
  13. */
  14.  
  15. define ( 'BP_SF_PLUGIN_NAME', 'bp-show-friends' );
  16. define ( 'BP_SF_PLUGIN_URL', WP_PLUGIN_URL . '/' . BP_SF_PLUGIN_NAME );
  17. define ( 'BP_SF_PLUGIN_DIR', WP_PLUGIN_DIR . '/' . BP_SF_PLUGIN_NAME );
  18. define ( 'BP_SF_VERSION', '1.0.1' );
  19.  
  20. /**
  21. * bp_show_friends_register_widgets
  22. * register widget.
  23. */
  24.  
  25. function bp_show_friends_register_widgets() {
  26. add_action('widgets_init', create_function('', 'return register_widget("BP_Show_Friends_Widget");') );
  27. }
  28. add_action( 'plugins_loaded', 'bp_show_friends_register_widgets' );
  29.  
  30. class BP_Show_Friends_Widget extends WP_Widget {
  31.  
  32. function bp_show_friends_widget() {
  33. $widget_ops = array('classname' => 'widget_show_friends', 'description' => __( "Show the friends of the displayed member", "bp-show-friends") );
  34. parent::WP_Widget( false, $name = __('Friends','bp-show-friends'), $widget_ops);
  35. }
  36.  
  37. function widget( $args, $instance ) {
  38. global $bp;
  39. $user_id = $bp->loggedin_user->id;
  40. $user_all_friends_url = $bp->loggedin_user->domain . BP_FRIENDS_SLUG;
  41. ?>
  42. <?php if(is_user_logged_in()): ?>
  43. <?php
  44. extract( $args );
  45.  
  46. echo $before_widget;
  47. echo $before_title;
  48. printf( __( "Friends - <a href='%s'>All</a>", 'bp-show-friends' ), $user_all_friends_url);
  49. echo $after_title; ?>
  50.  
  51. <div class="item-options" id="bpsf-list-options">
  52. <a href="javascript:imathSwitchToOnlineFriends('offline',<?php echo $instance['per_page'];?>)"><?php _e('Recently Actives','bp-show-friends');?></a>&nbsp;|&nbsp;
  53. <a href="javascript:imathSwitchToOnlineFriends('online',<?php echo $instance['per_page'];?>)"><?php _e('Online Friends','bp-show-friends');?></a>
  54. </div>
  55. <?php bp_show_friends_widget_list($instance['per_page']);?>
  56.  
  57. <?php echo $after_widget; ?>
  58. <?php endif;?>
  59. <?php
  60. }
  61.  
  62. function update( $new_instance, $old_instance ) {
  63. $instance = $old_instance;
  64. $instance['per_page'] = strip_tags( $new_instance['per_page'] );
  65.  
  66. return $instance;
  67. }
  68.  
  69. function form( $instance ) {
  70. $instance = wp_parse_args( (array) $instance, array( 'per_page' => 5 ) );
  71. $per_page = strip_tags( $instance['per_page'] );
  72. ?>
  73.  
  74. <p><label for="bp-show-friends-widget-per-page"><?php _e( 'Max Number of Avatars:', 'bp-show-friends' ); ?> <input class="widefat" id="<?php echo $this->get_field_id( 'per_page' ); ?>" name="<?php echo $this->get_field_name( 'per_page' ); ?>" type="text" value="<?php echo attribute_escape( $per_page ); ?>" style="width: 30%" /></label></p>
  75.  
  76. <?php
  77. }
  78. }
  79.  
  80. /**
  81. * bp_show_friends_widget_list
  82. * send the html for the widget (by default send recently active friends)
  83. */
  84.  
  85. function bp_show_friends_widget_list($limit=5){
  86. global $bp;
  87. $user_id = $bp->loggedin_user->id;
  88. ?>
  89. <div id="friends-container" style="padding-top:5px">
  90. <?php if ( bp_has_members( 'user_id='.$user_id.'&type=active&per_page='.$limit.'&max='.$limit.'&populate_extras=0' ) ) : ?>
  91. <?php while ( bp_members() ) : bp_the_member(); ?>
  92. <div class="item-avatar">
  93. <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
  94. </div>
  95. <?php endwhile; ?>
  96. <?php else:?>
  97. <p><?php _e('No friends!','bp-show-friends')?></p>
  98. <?php endif;?>
  99. </div>
  100. <br style="clear:both"/>
  101. <?php
  102. }
  103.  
  104. /**
  105. * bp_show_friends_get_type_to_display
  106. * ajax to show online or recently active friends
  107. */
  108.  
  109. function bp_show_friends_get_type_to_display(){
  110. global $bp;
  111. $user_id = $bp->loggedin_user->id;
  112. if(isset($_POST['offoronline'])) $type = $_POST['offoronline'];
  113. else $type = 'offline';
  114. if(isset($_POST['number'])) $max = $_POST['number'];
  115. else $max = 5;
  116. if($type=='offline') $args = 'user_id='.$user_id.'&type=active&per_page='.$max.'&max='.$max.'&populate_extras=0';
  117. else $args = 'user_id='.$user_id.'&type=online&per_page='.$max.'&max='.$max.'&populate_extras=0';
  118. if ( bp_has_members( $args ) ){
  119. ?>
  120. <?php while ( bp_members() ) : bp_the_member(); ?>
  121. <div class="item-avatar">
  122. <a href="<?php bp_member_permalink() ?>"><?php bp_member_avatar() ?></a>
  123. </div>
  124. <?php endwhile; ?>
  125. <?php
  126. }
  127. else{
  128. if($type=='online') echo '<p>'.__('No online friends!','bp-show-friends').'</p>';
  129. else echo '<p>'.__('No friends!','bp-show-friends').'</p>';
  130. }
  131. die();
  132. }
  133.  
  134. function bp_show_friends_custom_js(){
  135. if(is_user_logged_in()){
  136. ?>
  137. <script type="text/javascript">
  138. function imathSwitchToOnlineFriends(type, max){
  139. jQuery("#friends-container").html('<p style="margin-top:10px;text-align:center"><img src="<?php echo BP_SF_PLUGIN_URL;?>/images/ajax-loader.gif" alt="loader"/></p>');
  140. var data = {
  141. action: 'bpsf_online_friends',
  142. offoronline: type,
  143. number:max
  144. };
  145. jQuery.post(ajaxurl, data, function(response) {
  146. jQuery("#friends-container").html(response);
  147. });
  148. }
  149. </script>
  150. <?php
  151. }
  152. }
  153. add_action('wp_footer','bp_show_friends_custom_js');
  154. add_action( 'wp_ajax_bpsf_online_friends', 'bp_show_friends_get_type_to_display');
  155.  
  156. /**
  157. * Function for Translation
  158. */
  159. /**
  160. * bp_code_snippets_load_textdomain
  161. * translation!
  162. *
  163. */
  164. function bp_show_friends_load_textdomain() {
  165.  
  166. // try to get locale
  167. $locale = apply_filters( 'bp_show_friends_load_textdomain_get_locale', get_locale() );
  168.  
  169. // if we found a locale, try to load .mo file
  170. if ( !empty( $locale ) ) {
  171. // default .mo file path
  172. $mofile_default = sprintf( '%s/languages/%s-%s.mo', BP_SF_PLUGIN_DIR, BP_SF_PLUGIN_NAME, $locale );
  173. // final filtered file path
  174. $mofile = apply_filters( 'bp_show_friends_load_textdomain_mofile', $mofile_default );
  175. // make sure file exists, and load it
  176. if ( file_exists( $mofile ) ) {
  177. load_textdomain( BP_SF_PLUGIN_NAME, $mofile );
  178. }
  179. }
  180. }
  181. add_action ( 'bp_init', 'bp_show_friends_load_textdomain', 2 );
  182.  
  183. /**
  184. * bp_show_friends_activate
  185. * store widget's version
  186. *
  187. */
  188. function bp_show_friends_activate() {
  189. //if first install
  190. if(!get_option('bp-show-friends-version')){
  191. update_option( 'bp-show-friends-version', BP_SF_VERSION );
  192. }
  193. elseif(get_option('bp-show-friends-version')!=BP_SF_VERSION){
  194. update_option( 'bp-show-friends-version', BP_SF_VERSION );
  195. }
  196. }
  197. register_activation_hook( __FILE__, 'bp_show_friends_activate' );
  198. ?>
RAW Paste Data