BP_Groups_Widget

Groups widget.

Defined (1)

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

/bp-groups/classes/class-bp-groups-widget.php  
  1. class BP_Groups_Widget extends WP_Widget { 
  2.  
  3. /** 
  4. * Working as a group, we get things done better. 
  5. * @since 1.0.3 
  6. */ 
  7. public function __construct() { 
  8. $widget_ops = array( 
  9. 'description' => __( 'A dynamic list of recently active, popular, newest, or alphabetical groups', 'buddypress' ),  
  10. 'classname' => 'widget_bp_groups_widget buddypress widget',  
  11. 'customize_selective_refresh' => true,  
  12. ); 
  13. parent::__construct( false, _x( '(BuddyPress) Groups', '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( 'groups_widget_groups_list-js', buddypress()->plugin_url . "bp-groups/js/widget-groups{$min}.js", array( 'jquery' ), bp_get_version() ); 
  25.  
  26. /** 
  27. * Extends our front-end output method. 
  28. * @since 1.0.3 
  29. * @param array $args Array of arguments for the widget. 
  30. * @param array $instance Widget instance data. 
  31. */ 
  32. public function widget( $args, $instance ) { 
  33. global $groups_template; 
  34.  
  35. /** 
  36. * Filters the user ID to use with the widget instance. 
  37. * @since 1.5.0 
  38. * @param string $value Empty user ID. 
  39. */ 
  40. $user_id = apply_filters( 'bp_group_widget_user_id', '0' ); 
  41.  
  42. extract( $args ); 
  43.  
  44. if ( empty( $instance['group_default'] ) ) { 
  45. $instance['group_default'] = 'popular'; 
  46.  
  47. if ( empty( $instance['title'] ) ) { 
  48. $instance['title'] = __( 'Groups', 'buddypress' ); 
  49.  
  50. /** 
  51. * Filters the title of the Groups widget. 
  52. * @since 1.8.0 
  53. * @since 2.3.0 Added 'instance' and 'id_base' to arguments passed to filter. 
  54. * @param string $title The widget title. 
  55. * @param array $instance The settings for the particular instance of the widget. 
  56. * @param string $id_base Root ID for all widgets of this type. 
  57. */ 
  58. $title = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base ); 
  59.  
  60. /** 
  61. * Filters the separator of the group widget links. 
  62. * @since 2.4.0 
  63. * @param string $separator Separator string. Default '|'. 
  64. */ 
  65. $separator = apply_filters( 'bp_groups_widget_separator', '|' ); 
  66.  
  67. echo $before_widget; 
  68.  
  69. $title = ! empty( $instance['link_title'] ) ? '<a href="' . bp_get_groups_directory_permalink() . '">' . $title . '</a>' : $title; 
  70.  
  71. echo $before_title . $title . $after_title; 
  72.  
  73. $max_groups = ! empty( $instance['max_groups'] ) ? (int) $instance['max_groups'] : 5; 
  74.  
  75. $group_args = array( 
  76. 'user_id' => $user_id,  
  77. 'type' => $instance['group_default'],  
  78. 'per_page' => $max_groups,  
  79. 'max' => $max_groups,  
  80. ); 
  81.  
  82. // Back up the global. 
  83. $old_groups_template = $groups_template; 
  84.  
  85. ?> 
  86.  
  87. <?php if ( bp_has_groups( $group_args ) ) : ?> 
  88. <div class="item-options" id="groups-list-options"> 
  89. <a href="<?php bp_groups_directory_permalink(); ?>" id="newest-groups"<?php if ( $instance['group_default'] == 'newest' ) : ?> class="selected"<?php endif; ?>><?php _e("Newest", 'buddypress') ?></a> 
  90. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  91. <a href="<?php bp_groups_directory_permalink(); ?>" id="recently-active-groups"<?php if ( $instance['group_default'] == 'active' ) : ?> class="selected"<?php endif; ?>><?php _e("Active", 'buddypress') ?></a> 
  92. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  93. <a href="<?php bp_groups_directory_permalink(); ?>" id="popular-groups" <?php if ( $instance['group_default'] == 'popular' ) : ?> class="selected"<?php endif; ?>><?php _e("Popular", 'buddypress') ?></a> 
  94. <span class="bp-separator" role="separator"><?php echo esc_html( $separator ); ?></span> 
  95. <a href="<?php bp_groups_directory_permalink(); ?>" id="alphabetical-groups" <?php if ( $instance['group_default'] == 'alphabetical' ) : ?> class="selected"<?php endif; ?>><?php _e("Alphabetical", 'buddypress') ?></a> 
  96. </div> 
  97.  
  98. <ul id="groups-list" class="item-list" aria-live="polite" aria-relevant="all" aria-atomic="true"> 
  99. <?php while ( bp_groups() ) : bp_the_group(); ?> 
  100. <li <?php bp_group_class(); ?>> 
  101. <div class="item-avatar"> 
  102. <a href="<?php bp_group_permalink() ?>" title="<?php bp_group_name() ?>"><?php bp_group_avatar_thumb() ?></a> 
  103. </div> 
  104.  
  105. <div class="item"> 
  106. <div class="item-title"><a href="<?php bp_group_permalink() ?>"><?php bp_group_name() ?></a></div> 
  107. <div class="item-meta"> 
  108. <span class="activity"> 
  109. <?php 
  110. if ( 'newest' == $instance['group_default'] ) { 
  111. printf( __( 'created %s', 'buddypress' ), bp_get_group_date_created() ); 
  112. } elseif ( 'popular' == $instance['group_default'] ) { 
  113. bp_group_member_count(); 
  114. } else { 
  115. printf( __( 'active %s', 'buddypress' ), bp_get_group_last_active() ); 
  116. ?> 
  117. </span> 
  118. </div> 
  119. </div> 
  120. </li> 
  121.  
  122. <?php endwhile; ?> 
  123. </ul> 
  124. <?php wp_nonce_field( 'groups_widget_groups_list', '_wpnonce-groups' ); ?> 
  125. <input type="hidden" name="groups_widget_max" id="groups_widget_max" value="<?php echo esc_attr( $max_groups ); ?>" /> 
  126.  
  127. <?php else: ?> 
  128.  
  129. <div class="widget-error"> 
  130. <?php _e('There are no groups to display.', 'buddypress') ?> 
  131. </div> 
  132.  
  133. <?php endif; ?> 
  134.  
  135. <?php echo $after_widget; 
  136.  
  137. // Restore the global. 
  138. $groups_template = $old_groups_template; 
  139.  
  140. /** 
  141. * Extends our update method. 
  142. * @since 1.0.3 
  143. * @param array $new_instance New instance data. 
  144. * @param array $old_instance Original instance data. 
  145. * @return array 
  146. */ 
  147. public function update( $new_instance, $old_instance ) { 
  148. $instance = $old_instance; 
  149.  
  150. $instance['title'] = strip_tags( $new_instance['title'] ); 
  151. $instance['max_groups'] = strip_tags( $new_instance['max_groups'] ); 
  152. $instance['group_default'] = strip_tags( $new_instance['group_default'] ); 
  153. $instance['link_title'] = (bool) $new_instance['link_title']; 
  154.  
  155. return $instance; 
  156.  
  157. /** 
  158. * Extends our form method. 
  159. * @since 1.0.3 
  160. * @param array $instance Current instance. 
  161. * @return mixed 
  162. */ 
  163. public function form( $instance ) { 
  164. $defaults = array( 
  165. 'title' => __( 'Groups', 'buddypress' ),  
  166. 'max_groups' => 5,  
  167. 'group_default' => 'active',  
  168. 'link_title' => false 
  169. ); 
  170. $instance = wp_parse_args( (array) $instance, $defaults ); 
  171.  
  172. $title = strip_tags( $instance['title'] ); 
  173. $max_groups = strip_tags( $instance['max_groups'] ); 
  174. $group_default = strip_tags( $instance['group_default'] ); 
  175. $link_title = (bool) $instance['link_title']; 
  176. ?> 
  177.  
  178. <p><label for="<?php echo $this->get_field_id( '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 esc_attr( $title ); ?>" style="width: 100%" /></label></p> 
  179.  
  180. <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 Groups directory', 'buddypress' ) ?></label></p> 
  181.  
  182. <p><label for="<?php echo $this->get_field_id( 'max_groups' ); ?>"><?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 esc_attr( $max_groups ); ?>" style="width: 30%" /></label></p> 
  183.  
  184. <p> 
  185. <label for="<?php echo $this->get_field_id( 'group_default' ); ?>"><?php _e('Default groups to show:', 'buddypress'); ?></label> 
  186. <select name="<?php echo $this->get_field_name( 'group_default' ); ?>" id="<?php echo $this->get_field_id( 'group_default' ); ?>"> 
  187. <option value="newest" <?php selected( $group_default, 'newest' ); ?>><?php _e( 'Newest', 'buddypress' ) ?></option> 
  188. <option value="active" <?php selected( $group_default, 'active' ); ?>><?php _e( 'Active', 'buddypress' ) ?></option> 
  189. <option value="popular" <?php selected( $group_default, 'popular' ); ?>><?php _e( 'Popular', 'buddypress' ) ?></option> 
  190. <option value="alphabetical" <?php selected( $group_default, 'alphabetical' ); ?>><?php _e( 'Alphabetical', 'buddypress' ) ?></option> 
  191. </select> 
  192. </p> 
  193. <?php