BP_Core_Members_Widget

Members Widget.

Defined (1)

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

/bp-members/classes/class-bp-core-members-widget.php  
  1. class BP_Core_Members_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Constructor method. 
  5. * @since 1.5.0 
  6. */ 
  7. public function __construct() { 
  8.  
  9. // Setup widget name & description. 
  10. $name = _x( '(BuddyPress) Members', 'widget name', 'buddypress' ); 
  11. $description = __( 'A dynamic list of recently active, popular, and newest members', 'buddypress' ); 
  12.  
  13. // Call WP_Widget constructor. 
  14. parent::__construct( false, $name, array( 
  15. 'description' => $description,  
  16. 'classname' => 'widget_bp_core_members_widget buddypress widget',  
  17. 'customize_selective_refresh' => true,  
  18. ) ); 
  19.  
  20. if ( is_customize_preview() || is_active_widget( false, false, $this->id_base ) ) { 
  21. add_action( 'bp_enqueue_scripts', array( $this, 'enqueue_scripts' ) ); 
  22.  
  23. /** 
  24. * Enqueue scripts. 
  25. * @since 2.6.0 
  26. */ 
  27. public function enqueue_scripts() { 
  28. wp_enqueue_script( 'bp-widget-members' ); 
  29.  
  30. /** 
  31. * Display the Members widget. 
  32. * @since 1.0.3 
  33. * @see WP_Widget::widget() for description of parameters. 
  34. * @param array $args Widget arguments. 
  35. * @param array $instance Widget settings, as saved by the user. 
  36. */ 
  37. public function widget( $args, $instance ) { 
  38. global $members_template; 
  39.  
  40. // Get widget settings. 
  41. $settings = $this->parse_settings( $instance ); 
  42.  
  43. /** 
  44. * Filters the title of the Members widget. 
  45. * @since 1.8.0 
  46. * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter. 
  47. * @param string $title The widget title. 
  48. * @param array $settings The settings for the particular instance of the widget. 
  49. * @param string $id_base Root ID for all widgets of this type. 
  50. */ 
  51. $title = apply_filters( 'widget_title', $settings['title'], $settings, $this->id_base ); 
  52. $title = $settings['link_title'] ? '<a href="' . bp_get_members_directory_permalink() . '">' . $title . '</a>' : $title; 
  53.  
  54. /** 
  55. * Filters the separator of the member widget links. 
  56. * @since 2.4.0 
  57. * @param string $separator Separator string. Default '|'. 
  58. */ 
  59. $separator = apply_filters( 'bp_members_widget_separator', '|' ); 
  60.  
  61. // Output before widget HTMl, title (and maybe content before & after it). 
  62. echo $args['before_widget'] . $args['before_title'] . $title . $args['after_title']; 
  63.  
  64. // Setup args for querying members. 
  65. $members_args = array( 
  66. 'user_id' => 0,  
  67. 'type' => $settings['member_default'],  
  68. 'per_page' => $settings['max_members'],  
  69. 'max' => $settings['max_members'],  
  70. 'populate_extras' => true,  
  71. 'search_terms' => false,  
  72. ); 
  73.  
  74. // Back up the global. 
  75. $old_members_template = $members_template; 
  76.  
  77. ?> 
  78.  
  79. <?php if ( bp_has_members( $members_args ) ) : ?> 
  80.  
  81. <div class="item-options" id="members-list-options"> 
  82. <a href="<?php bp_members_directory_permalink(); ?>" id="newest-members" <?php if ( 'newest' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Newest', 'buddypress' ); ?></a> 
  83. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  84. <a href="<?php bp_members_directory_permalink(); ?>" id="recently-active-members" <?php if ( 'active' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Active', 'buddypress' ); ?></a> 
  85.  
  86. <?php if ( bp_is_active( 'friends' ) ) : ?> 
  87. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  88. <a href="<?php bp_members_directory_permalink(); ?>" id="popular-members" <?php if ( 'popular' === $settings['member_default'] ) : ?>class="selected"<?php endif; ?>><?php esc_html_e( 'Popular', 'buddypress' ); ?></a> 
  89.  
  90. <?php endif; ?> 
  91.  
  92. </div> 
  93.  
  94. <ul id="members-list" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true"> 
  95.  
  96. <?php while ( bp_members() ) : bp_the_member(); ?> 
  97.  
  98. <li class="vcard"> 
  99. <div class="item-avatar"> 
  100. <a href="<?php bp_member_permalink() ?>" title="<?php bp_member_name(); ?>"><?php bp_member_avatar(); ?></a> 
  101. </div> 
  102.  
  103. <div class="item"> 
  104. <div class="item-title fn"><a href="<?php bp_member_permalink(); ?>"><?php bp_member_name(); ?></a></div> 
  105. <div class="item-meta"> 
  106. <?php if ( 'newest' == $settings['member_default'] ) : ?> 
  107. <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_registered( array( 'relative' => false ) ) ); ?>"><?php bp_member_registered(); ?></span> 
  108. <?php elseif ( 'active' == $settings['member_default'] ) : ?> 
  109. <span class="activity" data-livestamp="<?php bp_core_iso8601_date( bp_get_member_last_active( array( 'relative' => false ) ) ); ?>"><?php bp_member_last_active(); ?></span> 
  110. <?php else : ?> 
  111. <span class="activity"><?php bp_member_total_friend_count(); ?></span> 
  112. <?php endif; ?> 
  113. </div> 
  114. </div> 
  115. </li> 
  116.  
  117. <?php endwhile; ?> 
  118.  
  119. </ul> 
  120.  
  121. <?php wp_nonce_field( 'bp_core_widget_members', '_wpnonce-members', false ); ?> 
  122.  
  123. <input type="hidden" name="members_widget_max" id="members_widget_max" value="<?php echo esc_attr( $settings['max_members'] ); ?>" /> 
  124.  
  125. <?php else: ?> 
  126.  
  127. <div class="widget-error"> 
  128. <?php esc_html_e( 'No one has signed up yet!', 'buddypress' ); ?> 
  129. </div> 
  130.  
  131. <?php endif; ?> 
  132.  
  133. <?php echo $args['after_widget']; 
  134.  
  135. // Restore the global. 
  136. $members_template = $old_members_template; 
  137.  
  138. /** 
  139. * Update the Members widget options. 
  140. * @since 1.0.3 
  141. * @param array $new_instance The new instance options. 
  142. * @param array $old_instance The old instance options. 
  143. * @return array $instance The parsed options to be saved. 
  144. */ 
  145. public function update( $new_instance, $old_instance ) { 
  146. $instance = $old_instance; 
  147.  
  148. $instance['title'] = strip_tags( $new_instance['title'] ); 
  149. $instance['max_members'] = strip_tags( $new_instance['max_members'] ); 
  150. $instance['member_default'] = strip_tags( $new_instance['member_default'] ); 
  151. $instance['link_title'] = (bool) $new_instance['link_title']; 
  152.  
  153. return $instance; 
  154.  
  155. /** 
  156. * Output the Members widget options form. 
  157. * @since 1.0.3 
  158. * @param array $instance Widget instance settings. 
  159. * @return void 
  160. */ 
  161. public function form( $instance ) { 
  162.  
  163. // Get widget settings. 
  164. $settings = $this->parse_settings( $instance ); 
  165. $title = strip_tags( $settings['title'] ); 
  166. $max_members = strip_tags( $settings['max_members'] ); 
  167. $member_default = strip_tags( $settings['member_default'] ); 
  168. $link_title = (bool) $settings['link_title']; ?> 
  169.  
  170. <p> 
  171. <label for="<?php echo $this->get_field_id( 'title' ); ?>"> 
  172. <?php esc_html_e( 'Title:', 'buddypress' ); ?> 
  173. <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" style="width: 100%" /> 
  174. </label> 
  175. </p> 
  176.  
  177. <p> 
  178. <label for="<?php echo $this->get_field_id( 'link_title' ) ?>"> 
  179. <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 ) ?> /> 
  180. <?php esc_html_e( 'Link widget title to Members directory', 'buddypress' ); ?> 
  181. </label> 
  182. </p> 
  183.  
  184. <p> 
  185. <label for="<?php echo $this->get_field_id( 'max_members' ); ?>"> 
  186. <?php esc_html_e( 'Max members to show:', 'buddypress' ); ?> 
  187. <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 esc_attr( $max_members ); ?>" style="width: 30%" /> 
  188. </label> 
  189. </p> 
  190.  
  191. <p> 
  192. <label for="<?php echo $this->get_field_id( 'member_default' ) ?>"><?php esc_html_e( 'Default members to show:', 'buddypress' ); ?></label> 
  193. <select name="<?php echo $this->get_field_name( 'member_default' ) ?>" id="<?php echo $this->get_field_id( 'member_default' ) ?>"> 
  194. <option value="newest" <?php if ( 'newest' === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Newest', 'buddypress' ); ?></option> 
  195. <option value="active" <?php if ( 'active' === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Active', 'buddypress' ); ?></option> 
  196. <option value="popular" <?php if ( 'popular' === $member_default ) : ?>selected="selected"<?php endif; ?>><?php esc_html_e( 'Popular', 'buddypress' ); ?></option> 
  197. </select> 
  198. </p> 
  199.  
  200. <?php 
  201.  
  202. /** 
  203. * Merge the widget settings into defaults array. 
  204. * @since 2.3.0 
  205. * @param array $instance Widget instance settings. 
  206. * @return array 
  207. */ 
  208. public function parse_settings( $instance = array() ) { 
  209. return bp_parse_args( $instance, array( 
  210. 'title' => __( 'Members', 'buddypress' ),  
  211. 'max_members' => 5,  
  212. 'member_default' => 'active',  
  213. 'link_title' => false 
  214. ), 'members_widget_settings' );