BP_Core_Friends_Widget

The User Friends widget class.

Defined (1)

The class is defined in the following location(s).

/bp-friends/classes/class-bp-core-friends-widget.php  
  1. class BP_Core_Friends_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Class constructor. 
  5. * @since 1.9.0 
  6. */ 
  7. function __construct() { 
  8. $widget_ops = array( 
  9. 'description' => __( 'A dynamic list of recently active, popular, and newest Friends of the displayed member. Widget is only shown when viewing a member profile.', 'buddypress' ),  
  10. 'classname' => 'widget_bp_core_friends_widget buddypress widget',  
  11. 'customize_selective_refresh' => true,  
  12. ); 
  13. parent::__construct( false, $name = _x( '(BuddyPress) Friends', 'widget name', 'buddypress' ), $widget_ops ); 
  14.  
  15. if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) { 
  16. add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  17.  
  18. /** 
  19. * Enqueue scripts. 
  20. * @since 2.6.0 
  21. */ 
  22. public function enqueue_scripts() { 
  23. $min = bp_core_get_minified_asset_suffix(); 
  24. wp_enqueue_script( 'bp_core_widget_friends-js', buddypress()->plugin_url . "bp-friends/js/widget-friends{$min}.js", array( 'jquery' ), bp_get_version() ); 
  25.  
  26. /** 
  27. * Display the widget. 
  28. * @since 1.9.0 
  29. * @param array $args Widget arguments. 
  30. * @param array $instance The widget settings, as saved by the user. 
  31. */ 
  32. function widget( $args, $instance ) { 
  33. global $members_template; 
  34.  
  35. extract( $args ); 
  36.  
  37. if ( ! bp_displayed_user_id() ) { 
  38. return; 
  39.  
  40. $user_id = bp_displayed_user_id(); 
  41. $link = trailingslashit( bp_displayed_user_domain() . bp_get_friends_slug() ); 
  42. $instance['title'] = sprintf( __( "%s's Friends", 'buddypress' ), bp_get_displayed_user_fullname() ); 
  43.  
  44. if ( empty( $instance['friend_default'] ) ) { 
  45. $instance['friend_default'] = 'active'; 
  46.  
  47. /** 
  48. * Filters the Friends widget title. 
  49. * @since 1.8.0 
  50. * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter. 
  51. * @param string $title The widget title. 
  52. * @param array $instance The settings for the particular instance of the widget. 
  53. * @param string $id_base Root ID for all widgets of this type. 
  54. */ 
  55. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 
  56.  
  57. echo $before_widget; 
  58.  
  59. $title = $instance['link_title'] ? '<a href="' . esc_url( $link ) . '">' . esc_html( $title ) . '</a>' : esc_html( $title ); 
  60.  
  61. echo $before_title . $title . $after_title; 
  62.  
  63. $members_args = array( 
  64. 'user_id' => absint( $user_id ),  
  65. 'type' => sanitize_text_field( $instance['friend_default'] ),  
  66. 'max' => absint( $instance['max_friends'] ),  
  67. 'populate_extras' => 1,  
  68. ); 
  69.  
  70. // Back up the global. 
  71. $old_members_template = $members_template; 
  72.  
  73. ?> 
  74.  
  75. <?php if ( bp_has_members( $members_args ) ) : ?> 
  76. <div class="item-options" id="friends-list-options"> 
  77. <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> 
  78. | <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> 
  79. | <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> 
  80. </div> 
  81.  
  82. <ul id="friends-list" class="item-list"> 
  83. <?php while ( bp_members() ) : bp_the_member(); ?> 
  84. <li class="vcard"> 
  85. <div class="item-avatar"> 
  86. <a href="<?php bp_member_permalink(); ?>" title="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a> 
  87. </div> 
  88.  
  89. <div class="item"> 
  90. <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a></div> 
  91. <div class="item-meta"> 
  92. <?php if ( 'newest' == $instance['friend_default'] ) : ?> 
  93. <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_registered( array( 'relative' => false ) ) ); ?>"><?php bp_member_registered(); ?></span> 
  94. <?php elseif ( 'active' == $instance['friend_default'] ) : ?> 
  95. <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_last_active( array( 'relative' => false ) ) ); ?>"><?php bp_member_last_active(); ?></span> 
  96. <?php else : ?> 
  97. <span class="activity"><?php bp_member_total_friend_count(); ?></span> 
  98. <?php endif; ?> 
  99. </div> 
  100. </div> 
  101. </li> 
  102.  
  103. <?php endwhile; ?> 
  104. </ul> 
  105. <?php wp_nonce_field( 'bp_core_widget_friends', '_wpnonce-friends' ); ?> 
  106. <input type="hidden" name="friends_widget_max" id="friends_widget_max" value="<?php echo absint( $instance['max_friends'] ); ?>" /> 
  107.  
  108. <?php else: ?> 
  109.  
  110. <div class="widget-error"> 
  111. <?php _e( 'Sorry, no members were found.', 'buddypress' ); ?> 
  112. </div> 
  113.  
  114. <?php endif; ?> 
  115.  
  116. <?php echo $after_widget; 
  117.  
  118. // Restore the global. 
  119. $members_template = $old_members_template; 
  120.  
  121. /** 
  122. * Process a widget save. 
  123. * @since 1.9.0 
  124. * @param array $new_instance The parameters saved by the user. 
  125. * @param array $old_instance The parameters as previously saved to the database. 
  126. * @return array $instance The processed settings to save. 
  127. */ 
  128. function update( $new_instance, $old_instance ) { 
  129. $instance = $old_instance; 
  130.  
  131. $instance['max_friends'] = absint( $new_instance['max_friends'] ); 
  132. $instance['friend_default'] = sanitize_text_field( $new_instance['friend_default'] ); 
  133. $instance['link_title'] = (bool) $new_instance['link_title']; 
  134.  
  135. return $instance; 
  136.  
  137. /** 
  138. * Render the widget edit form. 
  139. * @since 1.9.0 
  140. * @param array $instance The saved widget settings. 
  141. * @return void 
  142. */ 
  143. function form( $instance ) { 
  144. $defaults = array( 
  145. 'max_friends' => 5,  
  146. 'friend_default' => 'active',  
  147. 'link_title' => false 
  148. ); 
  149. $instance = wp_parse_args( (array) $instance, $defaults ); 
  150.  
  151. $max_friends = $instance['max_friends']; 
  152. $friend_default = $instance['friend_default']; 
  153. $link_title = (bool) $instance['link_title']; 
  154. ?> 
  155.  
  156. <p><label for="<?php echo $this->get_field_id( 'link_title' ); ?>"><input type="checkbox" name="<?php echo $this->get_field_name('link_title'); ?>" id="<?php echo $this->get_field_id( 'link_title' ); ?>" value="1" <?php checked( $link_title ); ?> /> <?php _e( 'Link widget title to Members directory', 'buddypress' ); ?></label></p> 
  157.  
  158. <p><label for="<?php echo $this->get_field_id( 'max_friends' ); ?>"><?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 absint( $max_friends ); ?>" style="width: 30%" /></label></p> 
  159.  
  160. <p> 
  161. <label for="<?php echo $this->get_field_id( 'friend_default' ) ?>"><?php _e( 'Default friends to show:', 'buddypress' ); ?></label> 
  162. <select name="<?php echo $this->get_field_name( 'friend_default' ); ?>" id="<?php echo $this->get_field_id( 'friend_default' ); ?>"> 
  163. <option value="newest" <?php selected( $friend_default, 'newest' ); ?>><?php _e( 'Newest', 'buddypress' ); ?></option> 
  164. <option value="active" <?php selected( $friend_default, 'active' );?>><?php _e( 'Active', 'buddypress' ); ?></option> 
  165. <option value="popular" <?php selected( $friend_default, 'popular' ); ?>><?php _e( 'Popular', 'buddypress' ); ?></option> 
  166. </select> 
  167. </p> 
  168.  
  169. <?php